Skip to content

Commit

Permalink
add Copy to Clipboard
Browse files Browse the repository at this point in the history
  • Loading branch information
jj-so committed Oct 13, 2023
1 parent 473afa7 commit 41eb59c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
19 changes: 17 additions & 2 deletions nitrokeyapp/secrets_tab/__init__.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand Down
26 changes: 9 additions & 17 deletions nitrokeyapp/ui/secrets_tab.ui
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,7 @@
<height>457</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<property name="leftMargin">
<number>9</number>
</property>
<property name="topMargin">
<number>9</number>
</property>
<property name="rightMargin">
<number>9</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QGroupBox" name="groupBoxMetadata">
<property name="title">
Expand Down Expand Up @@ -224,9 +212,6 @@
<string>One-Time Password</string>
</property>
<layout class="QFormLayout" name="formLayout_2">
<property name="horizontalSpacing">
<number>20</number>
</property>
<item row="0" column="0">
<widget class="QLabel" name="labelAlgorithm">
<property name="text">
Expand Down Expand Up @@ -280,13 +265,20 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="3" column="0">
<widget class="QPushButton" name="pushButtonOtpGenerate">
<property name="text">
<string>Generate</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QPushButton" name="pushButtonOtpCopyToClipbord">
<property name="text">
<string notr="true">Copy to Clipbord</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
Expand Down

0 comments on commit 41eb59c

Please sign in to comment.