-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_dialog.py
269 lines (224 loc) · 8.61 KB
/
scanner_dialog.py
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# FlipQR - transfer data over an air gap
# Copyright GPLv2 2015 Huang Hongqing (hhqyn@hotmail.com)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
from PyQt4 import QtGui, QtCore
from os.path import isfile
import zbar
import time
import hashlib
import zlib
import base64
import traceback
import constants as const
class ScannerDialog(QtGui.QDialog):
def __init__(self, parent):
QtGui.QDialog.__init__(self, parent)
self.setModal(True)
self.setupUI()
def reset(self):
self.source_id = ''
self.todo_list = []
self.done_list = []
self.is_done = False
self.is_success = False
self.total_frames = 0
self.source_md5 = ''
self.source_type = 0
self.source_filename = ''
self.progress_bar.setMaximum(100)
self.progress_bar.setValue(0)
self.lbl_status.setText('')
self.btn_scan_finish.setText('Scan')
self.show()
QtGui.qApp.processEvents()
def setupUI(self):
vbox = QtGui.QVBoxLayout()
self.progress_bar = QtGui.QProgressBar(self)
vbox.addWidget(self.progress_bar)
self.lbl_status = QtGui.QLabel()
vbox.addWidget(self.lbl_status)
vbox.addStretch(1)
self.btn_scan_finish = QtGui.QPushButton("Scan")
self.btn_scan_finish.clicked.connect(self.onScanFinish)
# self.btn_scan_finish.setEnabled(False)
self.btn_cancel = QtGui.QPushButton("Cancel")
self.btn_cancel.clicked.connect(self.onCancel)
# self.btn_cancel.setEnabled(False)
hbox = QtGui.QHBoxLayout()
hbox.addStretch(1)
hbox.addWidget(self.btn_scan_finish)
hbox.addWidget(self.btn_cancel)
vbox.addLayout(hbox)
self.setLayout(vbox)
self.setWindowTitle('Scan')
self.setFixedSize(300, 100)
def isFlipQR(self, text):
return text.startswith('FLIPQR')
def dataHandler(self, proc, image, closure):
# only use first symbol
for symbol in image.symbols:
break
if not self.source_id and not self.isFlipQR(symbol.data):
self.result = str(symbol.data)
self.progress_bar.setValue(100)
self.is_done = True
self.is_success = True
self.lbl_status.setText('Done')
self.btn_scan_finish.setText('Finish')
QtGui.qApp.processEvents()
return
# meta format: FLIPQR:source_id:frame number:total
# frames:md5:source_type:file name
try:
frame_data = str(symbol.data).split("\n", 1)
meta = frame_data[0].split(":")
if len(meta) < 4:
return
if self.source_id and self.source_id != meta[1]:
return
content = frame_data[1]
frame_number = int(meta[2])
if frame_number == 1:
self.source_md5 = meta[4]
self.source_type = int(meta[5])
self.source_filename = meta[6] if int(
meta[5]) != const.TYPE_RAW_TEXT else ""
if not self.source_id:
self.source_id = meta[1]
self.total_frames = int(meta[3])
self.todo_list = range(1, self.total_frames + 1)
self.done_list = ['' for i in range(1, self.total_frames + 1)]
self.progress_bar.setMaximum(self.total_frames)
if self.done_list[frame_number - 1] != '':
return
self.todo_list.remove(frame_number)
self.done_list[frame_number - 1] = content
self.progress_bar.setValue(self.total_frames - len(self.todo_list))
status_text = "Required Frames:" if len(
self.todo_list) > 1 else "Required Frame:"
status_text += " %s ..." if len(self.todo_list) > 3 else " %s "
requires = ",".join(str(i) for i in self.todo_list[0:3])
self.lbl_status.setText(status_text % requires)
QtGui.qApp.processEvents()
# all frames were scanned
if not self.todo_list and self.source_id:
self.is_done = True
self.lbl_status.setText('Done')
self.btn_scan_finish.setText('Finish')
QtGui.qApp.processEvents()
if self.source_type == const.TYPE_RAW_TEXT:
self.result = "".join(self.done_list)
if hashlib.md5(self.result).hexdigest() == self.source_md5:
self.is_success = True
self.stopScan()
return
else:
QtGui.QMessageBox.information(
self, 'FlipQR', 'md5 verify failed,please rescan')
self.stopScan()
return
else:
result = "".join(self.done_list)
try:
result = base64.b64decode(result)
if self.source_type == const.TYPE_ZIP_BASE64_FILE:
result = zlib.decompress(result)
self.result = result
except:
QtGui.QMessageBox.information(
self, 'FlipQR', 'Decode failed')
self.stopScan()
return
if hashlib.md5(self.result).hexdigest() == self.source_md5:
self.is_success = True
self.stopScan()
return
else:
QtGui.QMessageBox.information(
self, 'FlipQR', 'md5 verify failed,please rescan')
self.stopScan()
return
except Exception, e:
pass
# print traceback.print_exc()
def saveFile(self):
fd = QtGui.QFileDialog(self)
savefile = unicode(
fd.getSaveFileName(self, '', unicode(self.source_filename, "utf-8")))
if savefile:
fh = open(savefile, 'wb')
fh.write(self.result)
fh.close()
self.accept()
def initScan(self):
if not hasattr(self, 'proc'):
self.proc = zbar.Processor()
self.proc.parse_config('enable')
self.device = ''
try:
self.proc.init(self.device)
self.proc.set_data_handler(self.dataHandler)
self.proc.active = True
except:
QtGui.QMessageBox.information(self, 'FlipQR',
"Scan process initialize failed")
else:
pass
def startScan(self):
self.proc.visible = True
self.setEnabled(False)
QtGui.qApp.processEvents()
try:
ret = self.proc.user_wait()
except:
pass
self.proc.visible = False
self.setEnabled(True)
if self.is_done and not self.is_success:
self.reject()
def stopScan(self):
if hasattr(self.proc, 'cancel_process'):
self.proc.cancel_process()
else:
pass
def onScanFinish(self):
if self.is_done:
if self.source_type == const.TYPE_RAW_TEXT:
self.accept()
else:
self.saveFile()
else:
self.scan()
def scan(self):
self.setEnabled(False)
self.initScan()
self.startScan()
def onCancel(self):
if self.source_id or self.is_done:
reply = QtGui.QMessageBox.question(self, "FlipQR",
"Are you sure to cancel?\n discarge unsaved data?",
QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
self.reject()
else:
pass
else:
self.reject()
def closeEvent(self, event):
event.ignore()