-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialogs.py
More file actions
182 lines (148 loc) · 6.07 KB
/
dialogs.py
File metadata and controls
182 lines (148 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import logging
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QDialogButtonBox, QShortcut, QMessageBox
from PyQt5.QtGui import QPixmap, QKeySequence
from PyQt5.QtCore import QT_VERSION_STR
from PyQt5.Qt import PYQT_VERSION_STR
from ui_dialog import Ui_Dialog
import basics
import encoding
from processing import processAll
"""
This code should cover the GUI of the business logic of the application.
Code should work on both Python 2.7 as well as 3.4.
Requires PyQt5.
(Old version supported PyQt4.)
"""
class Dialog(QDialog, Ui_Dialog):
DESCRHEADER = """<!DOCTYPE html><html><head>
<body style="font-size:11pt; font-weight:400; font-style:normal;">
<b>Welcome to """ + basics.NAME + """</b>, version """ + basics.VERSION_STR + """ from
""" + basics.VERSION_DATE_STR + """<br>The output, the encrypted hash, is
placed on the clipboard. The only way to get it is by pasting it to
your destination.
"""
DESCRTRAILER = "</p></body></html>"
def __init__(self, trezor, settings):
super(Dialog, self).__init__()
# Set up the user interface from Designer.
self.setupUi(self)
# Make some local modifications.
self.trezor = trezor
self.settings = settings
self.teh = None
self.inputField.textChanged.connect(self.validate)
self.validate()
self.version = u''
self.description1 = self.DESCRHEADER
self.description2 = u''
self.description3 = self.DESCRTRAILER
# Apply is not automatically set up, only OK is automatically set up
button = self.buttonBox.button(QDialogButtonBox.Apply) # QDialogButtonBox.Ok
button.clicked.connect(self.accept)
# Abort is automatically set up as Reject, like Cancel
# self.buttonBox.clicked.connect(self.handleButtonClick) # connects ALL buttons
# Created the action in GUI with designer
self.actionApply.triggered.connect(self.accept) # Save
self.actionDone.triggered.connect(self.reject) # Quit
QShortcut(QKeySequence(u"Ctrl+Q"), self, self.reject) # Quit
QShortcut(QKeySequence(u"Ctrl+S"), self, self.accept) # Save
QShortcut(QKeySequence(u"Ctrl+A"), self, self.accept) # Apply
QShortcut(QKeySequence(u"Ctrl+C"), self, self.copy2Clipboard)
QShortcut(QKeySequence(u"Ctrl+T"), self, self.printAbout) # Version/About
self.clipboard = QApplication.clipboard()
self.textBrowser.selectionChanged.connect(self.selectionChanged)
def descrHeader(self):
return self.DESCRHEADER
def descrContent(self):
return self.description2
def descrTrailer(self):
return self.DESCRTRAILER
def printAbout(self):
"""
Show window with about and version information.
"""
msgBox = QMessageBox(QMessageBox.Information, "About",
u"About <b>" + basics.NAME + "</b>: <br><br>" + basics.NAME + " " +
"computes the encrypted hash (digest) of an input string "
"(message) using a Trezor hardware "
"device for safety and security.<br><br>" +
"<b>" + basics.NAME + " Version: </b>" + basics.VERSION_STR +
" from " + basics.VERSION_DATE_STR +
"<br><br><b>Python Version: </b>" + sys.version.replace(" \n", "; ") +
"<br><br><b>Qt Version: </b>" + QT_VERSION_STR +
"<br><br><b>PyQt Version: </b>" + PYQT_VERSION_STR)
msgBox.setIconPixmap(QPixmap(basics.LOGO_IMAGE))
msgBox.exec_()
def selectionChanged(self):
"""
called whenever selected text in textarea is changed
"""
# self.textBrowser.copy() # copy selected to clipboard
# self.settings.mlogger.log("Copied text to clipboard: %s" % self.clipboard.text(),
# logging.DEBUG, "Clipboard")
pass
def copy2Clipboard(self):
self.textBrowser.copy() # copy selected to clipboard
# This is content from the Status textarea, so no secrets here, we can log it
self.settings.mlogger.log("Copied text to clipboard: %s" % self.clipboard.text(),
logging.DEBUG, "Clipboard")
def setTeh(self, teh):
self.teh = teh
def setVersion(self, version):
self.version = version
def setDescription(self, extradescription):
self.textBrowser.setHtml(self.description1 + extradescription + self.description3)
def appendDescription(self, extradescription):
"""
@param extradescription: text in HTML format, use </br> for linebreaks
"""
self.description2 += extradescription
self.textBrowser.setHtml(self.description1 + self.description2 + self.description3)
def input(self):
return encoding.normalize_nfc(self.inputField.text())
def setInput(self, arg):
self.inputField.setText(encoding.normalize_nfc(arg))
def output(self):
return encoding.normalize_nfc(self.outputField.text())
def setOutput(self, arg):
self.outputField.setText(encoding.normalize_nfc(arg))
def validate(self):
"""
Enable OK/Apply buttons only if both master and backup are repeated
without typo and some file is selected and
exactly a single decrypt/encrypt option from the radio buttons is set.
And only when Encrypt is selected.
On decrypt passphrase does not need to be specified twice.
"""
# QDialogButtonBox.Ok
button = self.buttonBox.button(QDialogButtonBox.Apply)
button.setEnabled((self.input() != ""))
return (self.input() != "")
# def handleButtonClick(self, button):
# sb = self.buttonBox.standardButton(button)
# if sb == QDialogButtonBox.Apply:
# processAll(self.trezor, self.settings, self)
# # elif sb == QDialogButtonBox.Reset:
# # self.settings.mlogger.log("Reset Clicked, quitting now...", logging.DEBUG, "UI")
def accept(self):
"""
Apply button was pressed
"""
if self.validate():
self.settings.mlogger.log("Apply was called by user request. Start processing now.",
logging.DEBUG, "GUI IO")
# the main business logic is in this function processAll()
# processAll() should be shared between GUI mode and Terminal mode
processAll(self.teh, self.settings, self)
else:
self.settings.mlogger.log("Apply was called by user request. Apply is denied. "
"User input is not valid for processing. Did you enter an input string?",
logging.DEBUG, "GUI IO")
# Don't set up a reject() method, it is automatically created.
# If created here again it would overwrite the default one
# def reject(self):
# self.close()