From 41eb59c3f0004338221fe0b6f27a5d4ff7d42d62 Mon Sep 17 00:00:00 2001 From: jj-so Date: Fri, 13 Oct 2023 14:14:25 +0200 Subject: [PATCH] add Copy to Clipboard --- nitrokeyapp/secrets_tab/__init__.py | 19 +++++++++++++++++-- nitrokeyapp/ui/secrets_tab.ui | 26 +++++++++----------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/nitrokeyapp/secrets_tab/__init__.py b/nitrokeyapp/secrets_tab/__init__.py index 0f56ea41..c88ac149 100644 --- a/nitrokeyapp/secrets_tab/__init__.py +++ b/nitrokeyapp/secrets_tab/__init__.py @@ -1,8 +1,8 @@ from datetime import datetime from typing import Optional -from PyQt5.QtCore import Qt, QThread, QTimer, pyqtSignal, pyqtSlot -from PyQt5.QtGui import QIcon +from PyQt5.QtCore import Qt, QThread, QTimer, pyqtSignal, pyqtSlot, QObject +from PyQt5.QtGui import QIcon, QClipboard, QGuiApplication from PyQt5.QtWidgets import QDialog, QListWidgetItem, QWidget from nitrokeyapp.add_secret_dialog import AddSecretDialog @@ -65,6 +65,9 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: self.otp_timer.timeout.connect(self.update_otp_timeout) self.otp_timer.setInterval(1000) + self.clipboard = QGuiApplication.clipboard() + self.originalText = self.clipboard.text() + self.ui = Ui_SecretsTab() self.ui.setupUi(self) @@ -81,6 +84,7 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: self.ui.buttonDelete.pressed.connect(self.delete_credential) self.ui.buttonRefresh.pressed.connect(self.refresh_credential_list) self.ui.checkBoxProtected.stateChanged.connect(self.refresh_credential_list) + self.ui.pushButtonOtpCopyToClipbord.pressed.connect(self.copy_to_clipboard) self.ui.pushButtonOtpGenerate.pressed.connect(self.generate_otp) self.ui.secretsList.currentItemChanged.connect(self.credential_changed) @@ -169,6 +173,7 @@ def credentials_listed(self, credentials: list[Credential]) -> None: @pyqtSlot(OtpData) def otp_generated(self, data: OtpData) -> None: self.ui.lineEditOtp.setText(data.otp) + self.data_otp = data.otp if data.validity: start, end = data.validity @@ -184,6 +189,13 @@ def otp_generated(self, data: OtpData) -> None: self.ui.labelOtp.show() self.ui.lineEditOtp.show() + def copy_to_clipboard(self) -> None: + self.clipboard.setText(self.data_otp) + + def clear_clipboard(self) -> None: + self.clipboard.clear() + #QProcess.startDetached("/usr/bin/wl-copy", {"--clear"}) + def add_credential(self, credential: Credential) -> QListWidgetItem: icon = ( "lock_FILL0_wght500_GRAD0_opsz40" @@ -234,6 +246,8 @@ def hide_otp(self) -> None: self.ui.progressBarOtpTimeout.hide() self.ui.labelOtp.hide() self.ui.lineEditOtp.hide() + self.ui.pushButtonOtpCopyToClipbord.hide() + self.clear_clipboard() credential = self.get_current_credential() self.update_otp_generation(credential) @@ -249,6 +263,7 @@ def update_otp_timeout(self) -> None: timeout = int((self.otp_timeout - datetime.now()).total_seconds()) if timeout >= 0: self.ui.progressBarOtpTimeout.setValue(timeout) + self.ui.pushButtonOtpCopyToClipbord.show() else: self.hide_otp() diff --git a/nitrokeyapp/ui/secrets_tab.ui b/nitrokeyapp/ui/secrets_tab.ui index c5a7687c..ae732155 100644 --- a/nitrokeyapp/ui/secrets_tab.ui +++ b/nitrokeyapp/ui/secrets_tab.ui @@ -163,19 +163,7 @@ 457 - - - 9 - - - 9 - - - 9 - - - 0 - + @@ -224,9 +212,6 @@ One-Time Password - - 20 - @@ -280,13 +265,20 @@ - + Generate + + + + Copy to Clipbord + + +