This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
/
PasswdDialog.py
557 lines (438 loc) · 19.3 KB
/
PasswdDialog.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
MIT License
Copyright (c) 2013-2016 Frantisek Uhrecky
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
import logging
from PyQt4 import QtGui, QtCore
from TransController import tr
from GroupController import GroupController
import os
import AppSettings
from SaveDialog import SaveDialog
import InfoMsgBoxes
import qrcode
import random
import string
class Image(qrcode.image.base.BaseImage):
def __init__(self, border, width, box_size):
self.border = border
self.width = width
self.box_size = box_size
size = (width + border * 2) * box_size
self._image = QtGui.QImage(
size, size, QtGui.QImage.Format_RGB16)
self._image.fill(QtCore.Qt.white)
def pixmap(self):
return QtGui.QPixmap.fromImage(self._image)
def drawrect(self, row, col):
painter = QtGui.QPainter(self._image)
painter.fillRect(
(col + self.border) * self.box_size,
(row + self.border) * self.box_size,
self.box_size, self.box_size,
QtCore.Qt.black)
def save(self, stream, kind=None):
pass
class QrDialog(QtGui.QDialog):
def __init__(self, text):
super(QrDialog, self).__init__()
self.setWindowFlags(QtCore.Qt.Tool)
self.label = QtGui.QLabel(self)
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.label)
text = unicode(text)
self.label.setPixmap(qrcode.make(text, image_factory=Image).pixmap())
def center(self):
"""
Center window.
"""
# get frame geometry
wg = self.frameGeometry()
# get screen center
cs = QtGui.QDesktopWidget().availableGeometry().center()
wg.moveCenter(cs)
self.move(wg.topLeft())
class PasswdDialog(SaveDialog):
# emiting after saving passowrd
# param: p_id
signalPasswdSaved = QtCore.pyqtSignal(int)
def __init__(self, db_ctrl, show_pass = False, edit = True):
"""
COnstructor for password dialog, displys all necessary inputs.
@param db_ctrl: database controller
@param edit: if it will we edit dialog, show creation and modification date, else do not
@param show_pass: show password in visible form
"""
self.__db_ctrl = db_ctrl
self.__edit = edit
self.__show_pass = show_pass
super(PasswdDialog, self).__init__()
self.initUi()
self.initConections()
self.center()
# dafult never expire password
self._e_date_never.setChecked(True)
# intialize variables
self._attachment_data = ""
def initUi(self):
"""
Initilize UI components.
"""
SaveDialog.initUi(self)
title_label = QtGui.QLabel("<b>" + tr("Title:") + "</b>")
username_label = QtGui.QLabel("<b>" + tr("Username:") + "</b>")
passwd_label = QtGui.QLabel("<b>" + tr("Password:") + "</b>")
url_label = QtGui.QLabel("<b>" + tr("URL:") + "</b>")
if (self.__edit):
# if it is edit dialog display
layout_offset = 0
c_date_label = QtGui.QLabel("<b>" + tr("Creation date:") + "</b>")
m_date_label = QtGui.QLabel("<b>" + tr("Modification date:") + "</b>")
self._layout_gl.addWidget(c_date_label, 4, 0)
self._layout_gl.addWidget(m_date_label, 5, 0)
self._c_date = QtGui.QLabel()
self._m_date = QtGui.QLabel()
self._layout_gl.addWidget(self._c_date, 4, 1)
self._layout_gl.addWidget(self._m_date, 5, 1)
else:
layout_offset = -2
e_date_label = QtGui.QLabel("<b>" + tr("Expiration date:") + "</b>")
comment_label = QtGui.QLabel("<b>" + tr("Comment:") + "</b>")
attachment_label = QtGui.QLabel("<b>" + tr("Attachment:") + "</b>")
group_label = QtGui.QLabel("<b>" + tr("Groups:") + "</b>")
self._layout_gl.addWidget(title_label, 0, 0)
self._layout_gl.addWidget(username_label, 1, 0)
self._layout_gl.addWidget(passwd_label, 2, 0, QtCore.Qt.AlignTop)
self._layout_gl.addWidget(url_label, 3, 0)
self._layout_gl.addWidget(e_date_label, 6 + layout_offset, 0)
self._layout_gl.addWidget(attachment_label, 7 + layout_offset, 0)
self._layout_gl.addWidget(comment_label, 9 + layout_offset, 0)
self._layout_gl.addWidget(group_label, 10 + layout_offset, 0)
self._title = QtGui.QLineEdit()
self._username = QtGui.QLineEdit()
self._username.setFont(QtGui.QFont("Monospace"))
self._passwd = QtGui.QLineEdit()
self._passwd.setFont(QtGui.QFont("Monospace"))
self._pw_size = QtGui.QSpinBox()
self._pw_size.setMinimum(16)
self._pw_size.setValue(32)
self._pw_size.setToolTip("Minimum is 16 characters")
if (not self.__show_pass):
self._passwd.setEchoMode(QtGui.QLineEdit.Password)
# password layout
passwd_vl = QtGui.QVBoxLayout()
passwd_hl = QtGui.QHBoxLayout()
# password visibility check box
self._show_passwd_check = QtGui.QCheckBox(tr("Show passwd"))
self._show_passwd_check.setChecked(self.__show_pass)
# password QR code
self._qr_button = QtGui.QPushButton(tr("QR"))
self._pwgen_btn = QtGui.QPushButton(tr("RND"))
passwd_vl.addWidget(self._passwd)
passwd_vl.addLayout(passwd_hl)
passwd_hl.addWidget(self._show_passwd_check)
passwd_hl.addWidget(self._qr_button)
passwd_hl.addWidget(self._pwgen_btn)
passwd_hl.addWidget(self._pw_size)
self._url = QtGui.QLineEdit()
self._e_date = QtGui.QLineEdit()
self._comment = QtGui.QTextEdit()
self._comment.setLineWrapMode(QtGui.QTextEdit.NoWrap)
self._comment.setMaximumHeight(200)
self._group = QtGui.QComboBox()
self._att_name = QtGui.QLineEdit()
self._att_name.setEnabled(False)
self._layout_gl.addWidget(self._title, 0, 1)
self._layout_gl.addWidget(self._username, 1, 1)
self._layout_gl.addLayout(passwd_vl, 2, 1)
self._layout_gl.addWidget(self._url, 3, 1)
# attachment vertical layout
att_vl = QtGui.QVBoxLayout()
# attachment layout
att_hl_1 = QtGui.QHBoxLayout()
att_hl_2 = QtGui.QHBoxLayout()
att_vl.addLayout(att_hl_1)
att_vl.addLayout(att_hl_2)
# open file button
self._att_button = QtGui.QPushButton(tr("Load"))
self._att_del_button = QtGui.QPushButton(tr("Delete"))
self._att_save_button = QtGui.QPushButton(tr("Download"))
self._att_open_button = QtGui.QPushButton(tr("Open"))
self._att_del_button.setEnabled(False)
self._att_save_button.setEnabled(False)
self._att_open_button.setEnabled(False)
att_hl_1.addWidget(self._att_button)
att_hl_1.addWidget(self._att_del_button)
att_hl_2.addWidget(self._att_save_button)
att_hl_2.addWidget(self._att_open_button)
self._layout_gl.addWidget(self._att_name, 7 + layout_offset, 1)
self._layout_gl.addLayout(att_vl, 8 + layout_offset, 1)
self._layout_gl.addWidget(self._comment, 9 + layout_offset, 1)
self._layout_gl.addWidget(self._group, 10 + layout_offset, 1)
# date time edit
self._e_date_edit = QtGui.QDateTimeEdit()
self._e_date_edit.setCalendarPopup(True)
# expiration date can't be lower than current date
self._e_date_edit.setMinimumDateTime(QtCore.QDateTime.currentDateTime())
# create never check box
self._e_date_never = QtGui.QCheckBox(tr("Never"))
# create horizontal layout for date selector and never check box
e_date_hl = QtGui.QHBoxLayout()
e_date_hl.addWidget(self._e_date_edit)
e_date_hl.addWidget(self._e_date_never)
# add to main layout
self._layout_gl.addLayout(e_date_hl, 6 + layout_offset, 1)
def setVisibilityPass(self, state):
"""
Set no visible password and username.
"""
if (state == QtCore.Qt.Checked):
self._passwd.setEchoMode(QtGui.QLineEdit.Normal)
else:
self._passwd.setEchoMode(QtGui.QLineEdit.Password)
def initConections(self):
"""
Initialize all connections, handling events.
@requires: initUI(), setPassword() first
"""
SaveDialog.initConections(self)
# when something changed, enable save button
self._title.textChanged.connect(self.enableSaveButton)
self._username.textChanged.connect(self.enableSaveButton)
self._passwd.textChanged.connect(self.enableSaveButton)
self._url.textChanged.connect(self.enableSaveButton)
self._comment.textChanged.connect(self.enableSaveButton)
self._att_name.textChanged.connect(self.enableSaveButton)
self._e_date_edit.dateChanged.connect(self.enableSaveButton)
self._group.currentIndexChanged.connect(self.enableSaveButton)
# never checked
self._e_date_never.stateChanged.connect(self.enDisExpDate)
self._e_date_never.stateChanged.connect(self.enableSaveButton)
# show QR
self._qr_button.clicked.connect(self.showQrCode)
# open attachment
self._att_button.clicked.connect(self.loadAttachment)
# delete attachment
self._att_del_button.clicked.connect(self.delAttachment)
# save attachment to disk
self._att_save_button.clicked.connect(self.saveAttachment)
# open attachment file
self._att_open_button.clicked.connect(self.openAttachment)
# attachment input label
self._att_name.textChanged.connect(self.enableAttEditAndButton)
# show/hide password
self._show_passwd_check.stateChanged.connect(self.setVisibilityPass)
# pw generator
self._pwgen_btn.clicked.connect(self.pwGen)
def pwGen(self):
self._passwd.setText(''.join(random.SystemRandom().choice(string.letters + string.digits) for _ in range(self._pw_size.value())))
def delAttachment(self):
"""
Delete actual attachment.
"""
logging.info("deleting attachment")
# empty attachment name and disable input
self._att_name.clear()
self._att_name.setDisabled(True)
# empty binary data
self._attachment_data = ""
# diable del button
self._att_del_button.setDisabled(True)
self._att_save_button.setDisabled(True)
self._att_open_button.setDisabled(True)
def enableAttEditAndButton(self):
"""
Enable attachment name input.
"""
self._att_name.setEnabled(True)
self._att_del_button.setEnabled(True)
self._att_save_button.setEnabled(True)
self._att_open_button.setEnabled(True)
def loadGroups(self, g_id = False):
"""
Load available groups to combobox
"""
# set groups combobox
group_ctrl = GroupController(self.__db_ctrl)
groups = group_ctrl.selectAll()
# tmp index
tmp = 0
# have to increment tmp
inc_tmp = True
# fill combobox
for group in groups:
logging.info("adding group ID: %d", group._id)
# load icon
pix = QtGui.QPixmap()
pix.loadFromData(group._icon._icon)
# add item with icon, name and group ID
self._group.addItem(QtGui.QIcon(pix), tr(group._name), group._id)
if (g_id):
# if a dont have curent group
if (group._id != g_id and inc_tmp):
tmp += 1
logging.info("temp group index: %d, group._id: %d, g_id: %d", tmp, group._id, g_id)
else:
if inc_tmp:
logging.info("group found")
inc_tmp = False
# set current group
if (g_id):
self._group.setCurrentIndex(tmp)
def enDisExpDate(self, state):
"""
Enable or disable expiration date selector. Depends on checkbox state.
@param state: check box state
"""
logging.debug("never checkbox state changed")
if (state == QtCore.Qt.Checked):
self._e_date_edit.setEnabled(False)
else:
self._e_date_edit.setEnabled(True)
def getGroupId(self):
"""
Get group ID from combobox item.
@return: group ID
"""
index = self._group.currentIndex()
# return a touple
group_id = self._group.itemData(index).toInt()[0]
logging.info("current item index: %d group: %d", index, group_id)
return group_id
def keyReleaseEvent(self, event):
"""
Handle release event.
"""
logging.info("key release event")
def showQrCode(self):
print "dsadsa"
qr_w = QrDialog(self._passwd.text())
qr_w.resize(200, 200)
qr_w.setGeometry(self.geometry().x() + self.geometry().width(), self.geometry().y(), 200, 200)
# qr_w.center()
qr_w.exec_()
def loadAttachment(self):
"""
Exec filedialog, open file and get absolute file path and name.
"""
try:
home_loc = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.HomeLocation)
file_path = QtGui.QFileDialog.getOpenFileName(self, tr("Open attachment"), home_loc)
if (not file_path.isEmpty()):
file_path = str(file_path.toUtf8())
file_name = os.path.basename(file_path)
logging.info("attachment file path: %s", file_path)
logging.info("attachment file name: %s", file_name)
# set attachment name
self._att_name.setText(QtCore.QString.fromUtf8(file_name))
# read binary data
data = self.readFile(file_path)
if (data):
self._attachment_data = data
self.enableSaveButton()
else:
logging.debug("file not selected")
except Exception as e:
logging.exception(e)
InfoMsgBoxes.showErrorMsg(e)
def saveAttachment(self):
"""
Save attachment to disk.
"""
try:
home_loc = QtGui.QDesktopServices.storageLocation(QtGui.QDesktopServices.HomeLocation)
home_loc = QtCore.QString.fromUtf8(home_loc + os.path.sep)
home_loc.append(self._att_name.text())
file_path = QtGui.QFileDialog.getSaveFileName(self, tr("Open attachment"), home_loc)
logging.info("save attachment to file: %s", file_path)
if (not file_path.isEmpty()):
file_path = str(file_path.toUtf8())
logging.info("attachment file path: %s", file_path)
# write data to disk
self.writeFile(file_path)
else:
logging.info("file not selected")
except Exception as e:
logging.exception(e)
InfoMsgBoxes.showErrorMsg(e)
def openAttachment(self):
"""
Open attachment using desktop services.
"""
try:
tmp_file = AppSettings.TMP_PATH + str(self._att_name.text().toUtf8())
logging.info("saving attachment to tmp file: '%s'", tmp_file)
self.writeFile(tmp_file)
if (not QtGui.QDesktopServices.openUrl(QtCore.QUrl.fromLocalFile(QtCore.QString.fromUtf8(tmp_file)))):
# not succesfully opened
QtGui.QMessageBox(QtGui.QMessageBox.Information, tr("Something wrong!"), tr("Can't open file '") + QtCore.QString.fromUtf8(tmp_file) + "\n" +
tr("Save it to disk and open with selected program.")).exec_()
except Exception as e:
logging.exception(e)
InfoMsgBoxes.showErrorMsg(e)
def writeFile(self, file_path):
"""
Write file to disk.
@param file_path: file to write
"""
f = None
try:
f = open(AppSettings.decodePath(file_path), "wb")
f.write(self._attachment_data)
except IOError as e:
logging.exception(e)
raise e
except:
logging.exception("exception writting file: %s", file_path)
raise e
finally:
if (f):
f.close()
def readFile(self, file_path):
"""
Read file binary. Return read data.
@param file_path: path to file
@return: on succes binary data, else None
"""
data = None
f = None
try:
logging.info("reading file: %s", file_path)
f = open(AppSettings.decodePath(file_path), "rb")
data = f.read()
logging.info("file size: %i", len(data))
except IOError as e:
logging.exception(e)
raise e
except:
# all other exceptions
logging.exception("exception, file: %s", file_path)
raise "exception, file: " + file_path
finally:
if (f):
f.close()
return data
def saveChanges(self):
"""
Save changes to database, read all inputs and save DB entry.
@todo: implement saving password, emiting signal singalPasswdSaved, and close dialog
"""
# TODO: implement saving password, emiting signal singalPasswdSaved, and close dialog
pass