|
@@ -64,32 +64,32 @@ from PySide.QtUiTools import QUiLoader
|
64
|
64
|
|
65
|
65
|
ROOT_DIR = abspath(dirname(__file__))
|
66
|
66
|
|
|
67
|
+
|
67
|
68
|
class ChecksumWorker(QObject):
|
68
|
|
-
|
|
69
|
+
|
69
|
70
|
fractionCalculated = QtCore.Signal(int)
|
70
|
71
|
calculationFinished = QtCore.Signal(str)
|
71
|
72
|
calculationCancelled = QtCore.Signal()
|
72
|
73
|
calculationException = QtCore.Signal(object)
|
73
|
|
-
|
|
74
|
+
|
74
|
75
|
def __init__(self, file_name, method):
|
75
|
76
|
QObject.__init__(self)
|
76
|
|
-
|
|
77
|
+
|
77
|
78
|
self._fileName = file_name
|
78
|
79
|
self._method = method
|
79
|
80
|
self._mutex = QMutex()
|
80
|
81
|
self._stopped = False
|
81
|
82
|
|
82
|
|
-
|
83
|
83
|
@QtCore.Slot()
|
84
|
84
|
def run(self):
|
85
|
85
|
try:
|
86
|
86
|
with open(self._fileName, 'rb') as file:
|
87
|
87
|
fileSize = path.getsize(self._fileName)
|
88
|
88
|
chunkSize = fileSize // 99
|
89
|
|
-
|
|
89
|
+
|
90
|
90
|
bytesRead = file.read(chunkSize)
|
91
|
91
|
totalBytesRead = len(bytesRead)
|
92
|
|
-
|
|
92
|
+
|
93
|
93
|
while bytesRead:
|
94
|
94
|
self._mutex.lock()
|
95
|
95
|
if self._stopped:
|
|
@@ -97,19 +97,18 @@ class ChecksumWorker(QObject):
|
97
|
97
|
self.calculationCancelled.emit()
|
98
|
98
|
return
|
99
|
99
|
self._mutex.unlock()
|
100
|
|
-
|
|
100
|
+
|
101
|
101
|
self._method.update(bytesRead)
|
102
|
102
|
bytesRead = file.read(chunkSize)
|
103
|
103
|
totalBytesRead += len(bytesRead)
|
104
|
|
-
|
|
104
|
+
|
105
|
105
|
self.fractionCalculated.emit(totalBytesRead / fileSize * 100)
|
106
|
|
-
|
|
106
|
+
|
107
|
107
|
return self.calculationFinished.emit(self._method.hexdigest())
|
108
|
|
-
|
|
108
|
+
|
109
|
109
|
except Exception as e:
|
110
|
110
|
self.calculationException.emit(e)
|
111
|
111
|
|
112
|
|
-
|
113
|
112
|
def stop(self):
|
114
|
113
|
self._mutex.lock()
|
115
|
114
|
self._stopped = True
|
|
@@ -120,52 +119,48 @@ class MainController(QObject):
|
120
|
119
|
|
121
|
120
|
def __init__(self, fileName=''):
|
122
|
121
|
QObject.__init__(self)
|
123
|
|
-
|
|
122
|
+
|
124
|
123
|
self._thread = QThread()
|
125
|
124
|
self._worker = None
|
126
|
125
|
self._fileName = fileName
|
127
|
|
-
|
|
126
|
+
|
128
|
127
|
loader = QUiLoader()
|
129
|
|
-
|
|
128
|
+
|
130
|
129
|
self.loadMainWindow(loader)
|
131
|
130
|
self.loadAboutDialog(loader)
|
132
|
131
|
self.loadCreditsDialog(loader)
|
133
|
|
-
|
|
132
|
+
|
134
|
133
|
self.setupUi()
|
135
|
134
|
self.setupConnections()
|
136
|
135
|
|
137
|
|
-
|
138
|
136
|
def loadMainWindow(self, loader):
|
139
|
137
|
self._window = loader.load(join(ROOT_DIR, 'ui/main_window.ui'))
|
140
|
138
|
self._window.setWindowFlags(Qt.WindowCloseButtonHint | Qt.WindowMinimizeButtonHint)
|
141
|
|
-
|
|
139
|
+
|
142
|
140
|
self._fileChooser = self._window.findChild(QPushButton, 'fileChooser')
|
143
|
141
|
self._comboBox = self._window.findChild(QComboBox, 'comboBox')
|
144
|
142
|
self._lineEdit = self._window.findChild(QLineEdit, 'lineEdit')
|
145
|
143
|
self._progressBar = self._window.findChild(QProgressBar, 'progressBar')
|
146
|
144
|
self._verifyCancelButton = self._window.findChild(QPushButton, 'verifyCancelButton')
|
147
|
|
-
|
|
145
|
+
|
148
|
146
|
self._statusBar = QLabel()
|
149
|
147
|
self._window.findChild(QStatusBar, 'statusBar').addPermanentWidget(self._statusBar, 1)
|
150
|
148
|
self._statusBar.setAlignment(Qt.AlignHCenter)
|
151
|
|
-
|
|
149
|
+
|
152
|
150
|
self._newAction = self._window.findChild(QAction, 'actionNew')
|
153
|
151
|
self._quitAction = self._window.findChild(QAction, 'actionQuit')
|
154
|
152
|
self._aboutAction = self._window.findChild(QAction, 'actionAbout')
|
155
|
|
-
|
|
153
|
+
|
156
|
154
|
if len(self._fileName):
|
157
|
155
|
self._fileChooser.setText(basename(self._fileName))
|
158
|
156
|
self._verifyCancelButton.setEnabled(True)
|
159
|
157
|
|
160
|
|
-
|
161
|
158
|
def loadAboutDialog(self, loader):
|
162
|
159
|
self._aboutDialog = loader.load(join(ROOT_DIR, 'ui/about_dialog.ui'))
|
163
|
160
|
|
164
|
|
-
|
165
|
161
|
def loadCreditsDialog(self, loader):
|
166
|
162
|
self._creditsDialog = loader.load(join(ROOT_DIR, 'ui/credits_dialog.ui'))
|
167
|
163
|
|
168
|
|
-
|
169
|
164
|
def setupUi(self):
|
170
|
165
|
self._comboBox.addItem('MD5')
|
171
|
166
|
self._comboBox.addItem('SHA-1')
|
|
@@ -175,7 +170,6 @@ class MainController(QObject):
|
175
|
170
|
self._comboBox.addItem('SHA-512')
|
176
|
171
|
self._comboBox.setCurrentIndex(3)
|
177
|
172
|
|
178
|
|
-
|
179
|
173
|
def setupConnections(self):
|
180
|
174
|
self._newAction.activated.connect(self.reset)
|
181
|
175
|
self._fileChooser.clicked.connect(self.showFileChooserDialog)
|
|
@@ -184,11 +178,9 @@ class MainController(QObject):
|
184
|
178
|
button = self._aboutDialog.findChild(QPushButton, 'creditsButton')
|
185
|
179
|
button.clicked.connect(self.showCreditsDialog)
|
186
|
180
|
|
187
|
|
-
|
188
|
181
|
def show(self):
|
189
|
182
|
self._window.show()
|
190
|
183
|
|
191
|
|
-
|
192
|
184
|
def reset(self):
|
193
|
185
|
self._verifyCancelButton.setEnabled(False)
|
194
|
186
|
self._fileChooser.setText(self.tr('(None)'))
|
|
@@ -196,61 +188,54 @@ class MainController(QObject):
|
196
|
188
|
self._progressBar.setValue(0)
|
197
|
189
|
self._statusBar.setText('')
|
198
|
190
|
|
199
|
|
-
|
200
|
191
|
def showFileChooserDialog(self):
|
201
|
192
|
self._fileName, _ = QtGui.QFileDialog.getOpenFileName()
|
202
|
193
|
if len(self._fileName):
|
203
|
194
|
self._fileChooser.setText(basename(self._fileName))
|
204
|
195
|
self._verifyCancelButton.setEnabled(True)
|
205
|
196
|
|
206
|
|
-
|
207
|
197
|
def showAboutDialog(self):
|
208
|
198
|
self._aboutDialog.show()
|
209
|
199
|
|
210
|
|
-
|
211
|
200
|
def showCreditsDialog(self):
|
212
|
201
|
self._creditsDialog.show()
|
213
|
202
|
|
214
|
|
-
|
215
|
203
|
def startVerification(self):
|
216
|
204
|
self.setCancelAction()
|
217
|
|
-
|
|
205
|
+
|
218
|
206
|
self._progressBar.setValue(0)
|
219
|
207
|
self._fileChooser.setEnabled(False)
|
220
|
208
|
self._comboBox.setEnabled(False)
|
221
|
209
|
self._lineEdit.setEnabled(False)
|
222
|
210
|
self._newAction.setEnabled(False)
|
223
|
211
|
self._statusBar.setText('')
|
224
|
|
-
|
225
|
|
- self._startThread()
|
226
|
212
|
|
|
213
|
+ self._startThread()
|
227
|
214
|
|
228
|
215
|
def _startThread(self):
|
229
|
216
|
self._worker = ChecksumWorker(self._fileName, self.getAlgorithm())
|
230
|
217
|
self._worker.moveToThread(self._thread)
|
231
|
|
-
|
|
218
|
+
|
232
|
219
|
self._thread.started.connect(self._worker.run)
|
233
|
220
|
self._worker.calculationFinished.connect(self.onCalculationFinished)
|
234
|
221
|
self._worker.fractionCalculated.connect(self.onFractionCalculated)
|
235
|
222
|
self._worker.calculationCancelled.connect(self.onCalculationCancelled)
|
236
|
223
|
self._worker.calculationException.connect(self.onCalculationException)
|
237
|
|
-
|
238
|
|
- self._thread.start()
|
239
|
224
|
|
|
225
|
+ self._thread.start()
|
240
|
226
|
|
241
|
227
|
def cancelVerification(self):
|
242
|
228
|
self.setVerifyAction()
|
243
|
|
-
|
|
229
|
+
|
244
|
230
|
self._worker.stop()
|
245
|
231
|
self._thread.quit()
|
246
|
232
|
self._worker = None
|
247
|
|
-
|
|
233
|
+
|
248
|
234
|
self._fileChooser.setEnabled(True)
|
249
|
235
|
self._comboBox.setEnabled(True)
|
250
|
236
|
self._lineEdit.setEnabled(True)
|
251
|
237
|
self._newAction.setEnabled(True)
|
252
|
238
|
|
253
|
|
-
|
254
|
239
|
def getAlgorithm(self):
|
255
|
240
|
options = {
|
256
|
241
|
0: md5(),
|
|
@@ -262,71 +247,64 @@ class MainController(QObject):
|
262
|
247
|
}
|
263
|
248
|
return options[self._comboBox.currentIndex()]
|
264
|
249
|
|
265
|
|
-
|
266
|
250
|
@QtCore.Slot(int)
|
267
|
251
|
def onFractionCalculated(self, fraction):
|
268
|
252
|
self._progressBar.setValue(fraction)
|
269
|
253
|
|
270
|
|
-
|
271
|
254
|
@QtCore.Slot(str)
|
272
|
255
|
def onCalculationFinished(self, checksum):
|
273
|
256
|
self._thread.quit()
|
274
|
257
|
self._worker = None
|
275
|
|
-
|
|
258
|
+
|
276
|
259
|
self.setVerifyAction()
|
277
|
|
-
|
|
260
|
+
|
278
|
261
|
if checksum == self._lineEdit.text():
|
279
|
262
|
self._statusBar.setStyleSheet('QLabel { color: darkgreen; }')
|
280
|
263
|
self._statusBar.setText(self.tr('Checksums are equal'))
|
281
|
264
|
else:
|
282
|
265
|
self._statusBar.setStyleSheet('QLabel { color: red; }')
|
283
|
266
|
self._statusBar.setText(self.tr('Checksums ARE NOT equal'))
|
284
|
|
-
|
|
267
|
+
|
285
|
268
|
self._progressBar.setValue(100)
|
286
|
269
|
self._fileChooser.setEnabled(True)
|
287
|
270
|
self._comboBox.setEnabled(True)
|
288
|
271
|
self._lineEdit.setEnabled(True)
|
289
|
272
|
self._newAction.setEnabled(True)
|
290
|
273
|
|
291
|
|
-
|
292
|
274
|
@QtCore.Slot()
|
293
|
275
|
def onCalculationCancelled(self):
|
294
|
276
|
self._progressBar.setValue(0)
|
295
|
277
|
|
296
|
|
-
|
297
|
278
|
@QtCore.Slot(object)
|
298
|
279
|
def onCalculationException(self, exception):
|
299
|
280
|
self._thread.quit()
|
300
|
281
|
self._worker = None
|
301
|
|
-
|
|
282
|
+
|
302
|
283
|
self._statusBar.setStyleSheet('QLabel { color: red; }')
|
303
|
284
|
self._statusBar.setText(str(exception))
|
304
|
|
-
|
|
285
|
+
|
305
|
286
|
self.setVerifyAction()
|
306
|
|
-
|
|
287
|
+
|
307
|
288
|
self._progressBar.setValue(100)
|
308
|
289
|
self._fileChooser.setEnabled(True)
|
309
|
290
|
self._comboBox.setEnabled(True)
|
310
|
291
|
self._lineEdit.setEnabled(True)
|
311
|
292
|
self._newAction.setEnabled(True)
|
312
|
293
|
|
313
|
|
-
|
314
|
294
|
def setVerifyAction(self):
|
315
|
295
|
self._verifyCancelButton.clicked.disconnect(self.cancelVerification)
|
316
|
296
|
self._verifyCancelButton.clicked.connect(self.startVerification)
|
317
|
297
|
self._verifyCancelButton.setText(self.tr('&Verify'))
|
318
|
298
|
self._verifyCancelButton.setIcon(QIcon.fromTheme('media-playback-start'))
|
319
|
299
|
|
320
|
|
-
|
321
|
300
|
def setCancelAction(self):
|
322
|
301
|
self._verifyCancelButton.clicked.disconnect(self.startVerification)
|
323
|
302
|
self._verifyCancelButton.clicked.connect(self.cancelVerification)
|
324
|
303
|
self._verifyCancelButton.setText(self.tr('&Cancel'))
|
325
|
|
- self._verifyCancelButton.setIcon(QIcon.fromTheme('process-stop'))
|
326
|
|
-
|
|
304
|
+ self._verifyCancelButton.setIcon(QIcon.fromTheme('process-stop'))
|
327
|
305
|
|
328
|
306
|
def quit(self, *args):
|
329
|
|
- if self._worker != None:
|
|
307
|
+ if self._worker is not None:
|
330
|
308
|
self._worker.stop()
|
331
|
309
|
self._thread.quit()
|
332
|
310
|
self._thread.wait()
|
|
@@ -341,12 +319,12 @@ if __name__ == '__main__':
|
341
|
319
|
|
342
|
320
|
translator = QtCore.QTranslator()
|
343
|
321
|
translator.load(join(ROOT_DIR, 'l10n/' + locale.getlocale()[0] + '.qm'))
|
344
|
|
-
|
|
322
|
+
|
345
|
323
|
app = QApplication(sys.argv)
|
346
|
324
|
app.installTranslator(translator)
|
347
|
|
-
|
|
325
|
+
|
348
|
326
|
controller = MainController(args.file)
|
349
|
327
|
app.aboutToQuit.connect(controller.quit)
|
350
|
328
|
controller.show()
|
351
|
|
-
|
|
329
|
+
|
352
|
330
|
sys.exit(app.exec_())
|