This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.py
463 lines (357 loc) · 17.6 KB
/
MainWindow.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
#!/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.
"""
from PyQt4 import QtGui, QtCore
from TransController import tr
from GroupsWidget import GroupsWidget
from PasswordsWidget import PasswordsWidget
from DetailWidget import DetailWidget
from EditPasswdDialog import EditPasswdDialog
from NewPasswdDialog import NewPasswdDialog
from NewGroupDialog import NewGroupDialog
import logging
from UserController import UserController
import AppSettings
from EditGroupDialog import EditGroupDialog
import shutil
import InfoMsgBoxes
class MainWindow(QtGui.QMainWindow):
"""
MainWindow class represents main window.
"""
# public attr:
_db_ctrl = None
def __init__(self, db_ctrl, user = None):
self._db_ctrl = db_ctrl
self._user = user
super(MainWindow, self).__init__()
self._close_act = None
self.initUI()
self.createActions()
self.createMenu()
self.initConections()
def closeEvent(self, event):
"""
Do matters on close event. In example delete clipboard.
"""
logging.debug("deleting clipboard")
QtGui.QApplication.clipboard().clear()
try:
logging.info("removing tmp dir: '%s'", AppSettings.TMP_PATH)
# remove tmp files
shutil.rmtree(AppSettings.decodePath(AppSettings.TMP_PATH))
except Exception as e:
logging.exception(e)
InfoMsgBoxes.showErrorMsg(e)
def initUI(self):
"""
Initialize gui components. Create dock widgets.
"""
# self.resize(300, 300)
self.setWindowTitle("UserPass Manager " + AppSettings.APP_VERSION)
self.resize(1000, 600)
self.center()
# create main splitter, splits passwords table and gorups
self._main_splitter = QtGui.QSplitter(QtCore.Qt.Horizontal)
self.setCentralWidget(self._main_splitter)
# create groups widget with label, and groups
groups_mw = QtGui.QWidget()
groups_vl = QtGui.QVBoxLayout()
groups_mw.setLayout(groups_vl)
# create label
groups_label = QtGui.QLabel("<b>" + tr("Groups") + "</b>")
groups_vl.addWidget(groups_label)
# create groups tree widget
self._groups_tw = GroupsWidget(self)
groups_vl.addWidget(self._groups_tw)
self._main_splitter.addWidget(groups_mw)
# create password central widget
self._passwords_cw = QtGui.QWidget()
self._main_splitter.addWidget(self._passwords_cw)
self._main_splitter.setStretchFactor(1, 1)
# create passwords layout, will contain passwords table and detail widget with spliter
self._passwords_vl = QtGui.QVBoxLayout()
self._passwords_cw.setLayout(self._passwords_vl)
# add table widget
self._passwords_table = PasswordsWidget(self)
# create password and detail splitter
self._passwd_splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
# create label
passwdords_label = QtGui.QLabel("<b>" + tr("Passwords") + "</b>")
self._passwords_vl.addWidget(passwdords_label)
# add splitter to layout
self._passwords_vl.addWidget(self._passwd_splitter)
# create detail widget
self._detail_w = DetailWidget(self, self._passwords_table._show_pass)
# add widgets to splitter
self._passwd_splitter.addWidget(self._passwords_table)
self._passwd_splitter.addWidget(self._detail_w)
# set stretch factor for password table
self._passwd_splitter.setStretchFactor(0, 1)
def setUserReloadShow(self, username, master):
"""
Load user from database and reload items.
"""
user_ctrl = UserController(self._db_ctrl)
username = str(username.toUtf8())
master = str(master.toUtf8())
self._user = user_ctrl.selectByNameMaster(username, master)
if (self._user):
self.reloadItems()
else:
logging.error("something wrong, can't log in user.")
self.show()
def initConections(self):
"""
Initialize all connections, handling events.
@requires: initUI() first
"""
# create connection to update table view
self._groups_tw.signalGroupSelChanged.connect(self._passwords_table.showPasswords)
self._groups_tw.signalGroupSelChanged.connect(self._detail_w.handleTypePassword)
self._passwords_table.signalShowDetailPasswd.connect(self._detail_w.setPassword)
# show edit passwd dialog
self._passwords_table.signalEditPasswd.connect(self.showEditPasswdDialog)
self._groups_tw.signalEditPasswd.connect(self.showEditPasswdDialog)
# enable/disable delete action, depends on selection type in tree widget
self._groups_tw.signalGroupSelChanged.connect(self.enDisPassGrpActions)
# enable/disable delete action with selection password talbe
self._passwords_table.signalSelChangedTypeId.connect(self.enDisPassGrpActions)
def createActions(self):
"""
Initialize all actions, i.e. Close, Save etc.
"""
# init close
self._close_act = QtGui.QAction(tr("&Close"), self)
self._close_act.setShortcuts(QtGui.QKeySequence.Close)
self._close_act.setToolTip(tr("Close application"))
# connect to slot
self._close_act.triggered.connect(QtCore.QCoreApplication.instance().quit)
# init about action
self._about_act = QtGui.QAction(tr("About"), self)
self._about_act.setToolTip(tr("About UserPass Manager"))
self._about_act.triggered.connect(self.aboutDialog)
# new password action
self._new_passwd = QtGui.QAction(tr("New"), self)
self._new_passwd.setShortcuts(QtGui.QKeySequence.New)
self._new_passwd.setToolTip(tr("Add new password to DB"))
self._new_passwd.triggered.connect(self.showNewPasswdDialog)
# displayed in groups tree
self._new_passwd_g = QtGui.QAction(tr("New password"), self)
self._new_passwd_g.setToolTip(tr("Add new password to DB"))
self._new_passwd_g.triggered.connect(self.showNewPasswdDialog)
# delete password action
self._del_passwd = QtGui.QAction(tr("Delete"), self)
self._del_passwd.setShortcuts(QtGui.QKeySequence.Delete)
self._del_passwd.setToolTip(tr("Delete password from DB"))
self._del_passwd.setDisabled(True)
self._del_passwd.triggered.connect(self.deletePassword)
# displayed in groups tree
self._del_passwd_g = QtGui.QAction(tr("Delete password"), self)
self._del_passwd_g.setToolTip(tr("Delete password from DB"))
self._del_passwd_g.setDisabled(True)
self._del_passwd_g.triggered.connect(self.deletePassword)
# new group action
self._new_group = QtGui.QAction(tr("New"), self)
self._new_group.setToolTip(tr("Add new group to DB"))
self._new_group.triggered.connect(self.showNewGroupDialog)
# new group action in groups tree right click menu
self._new_group_g = QtGui.QAction(tr("New group"), self)
self._new_group_g.setToolTip(tr("Add new group to DB"))
self._new_group_g.triggered.connect(self.showNewGroupDialog)
# edit group action
self._edit_group = QtGui.QAction(tr("Edit"), self)
self._edit_group.setToolTip(tr("Edit selected group"))
self._edit_group.setDisabled(True)
self._edit_group.triggered.connect(self.showEditGroupDialog)
# edit group action in groups tree right click menu
self._edit_group_g = QtGui.QAction(tr("Edit group"), self)
self._edit_group_g.setToolTip(tr("Edit selected group"))
self._edit_group_g.setDisabled(True)
self._edit_group_g.triggered.connect(self.showEditGroupDialog)
# delete group in groups tree
self._del_group = QtGui.QAction(tr("Delete"), self)
self._del_group.setToolTip(tr("Delete group from DB"))
self._del_group.setDisabled(True)
self._del_group.triggered.connect(self.deleteGroup)
# delete group displayed in groups tree
self._del_group_g = QtGui.QAction(tr("Delete group"), self)
self._del_group_g.setToolTip(tr("Delete group from DB"))
self._del_group_g.setDisabled(True)
self._del_group_g.triggered.connect(self.deleteGroup)
# add to table actions
self._passwords_table.addAction(self._new_passwd)
self._passwords_table.addAction(self._del_passwd)
# add to groups tree actions
self._groups_tw.addAction(self._new_passwd_g)
self._groups_tw.addAction(self._del_passwd_g)
self._groups_tw.addAction(self._new_group_g)
self._groups_tw.addAction(self._edit_group_g)
self._groups_tw.addAction(self._del_group_g)
def enDisPassGrpActions(self, item_type, item_id):
"""
Disable delete password action.
"""
if (item_type == self._groups_tw._TYPE_PASS):
self._del_passwd.setEnabled(True)
self._del_passwd_g.setEnabled(True)
else:
self._del_passwd.setEnabled(False)
self._del_passwd_g.setEnabled(False)
if (item_type == self._groups_tw._TYPE_GROUP):
self._del_group.setEnabled(True)
self._del_group_g.setEnabled(True)
self._edit_group.setEnabled(True)
self._edit_group_g.setEnabled(True)
else:
self._del_group.setEnabled(False)
self._del_group_g.setEnabled(False)
self._edit_group.setEnabled(False)
self._edit_group_g.setEnabled(False)
def createMenu(self):
"""
Initialize menu, add actions to menu.
"""
# create menu bar
menubar = QtGui.QMenuBar()
self.setMenuBar(menubar)
# create menu options and add actions
file_menu = self.menuBar().addMenu(tr("&File"))
file_menu.addAction(self._close_act)
password_menu = self.menuBar().addMenu(tr("Password"))
password_menu.addAction(self._new_passwd)
password_menu.addAction(self._del_passwd)
group_menu = self.menuBar().addMenu(tr("Group"))
group_menu.addAction(self._new_group)
group_menu.addAction(self._edit_group)
group_menu.addAction(self._del_group)
settings_menu = self.menuBar().addMenu(tr("Settings"))
about_menu = self.menuBar().addMenu(tr("About"))
about_menu.addAction(self._about_act)
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())
def aboutDialog(self):
QtGui.QMessageBox(QtGui.QMessageBox.Information, tr("About"), tr("ABOUT_TEXT_1") + " " + AppSettings.APP_VERSION + "\n" + tr("ABOUT_TEXT_2") + """
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.""").exec_()
def showEditPasswdDialog(self, p_id):
"""
Show edit password dialog.
@param p_id: password id to edit
"""
edit_dialog = EditPasswdDialog(self, p_id, self._passwords_table._show_pass)
edit_dialog.signalPasswdSaved.connect(self.reloadItems)
edit_dialog.exec_()
def showNewPasswdDialog(self):
"""
Password dialog to add new password.
"""
new_pass_dialog = NewPasswdDialog(self, self._groups_tw.currentItemGroupID(), self._passwords_table._show_pass)
if (new_pass_dialog.exec_() == QtGui.QDialog.Accepted):
# all done
self.reloadItems()
def showNewGroupDialog(self):
"""
Group dialog to add new password.
"""
new_group_dialog = NewGroupDialog(self)
if (new_group_dialog.exec_() == QtGui.QDialog.Accepted):
# all done
self.reloadItems()
def deletePassword(self):
"""
Delete password from database.
"""
# frist check tree widget
title = self._groups_tw.currentPasswordTitle()
p_id = self._groups_tw.currentPasswordId()
# also chck in table widget
if (not title):
title = self._passwords_table.currentItemTitle()
p_id = self._passwords_table.currentItemID()
logging.debug("delete password title: %s, ID: %i", title, p_id)
if (title != False):
msg = QtGui.QMessageBox(QtGui.QMessageBox.Question, title ,tr("Do you want delete password '")
+ title + "'?")
msg.addButton(QtGui.QMessageBox.Yes)
msg.addButton(QtGui.QMessageBox.No)
ret = msg.exec_()
if (ret == QtGui.QMessageBox.Yes):
# delete password
self._passwords_table.deletePassword(p_id)
self.reloadItems()
logging.debug("Not password selected title: %s", title)
def deleteGroup(self):
"""
Delete group from database and also all passwords in this group, if foreign key are enabled.
"""
# frist check tree widget
title = self._groups_tw.currentItemGroupName()
g_id = self._groups_tw.currentItemGroupID()
logging.debug("delete group title: %s, ID: %i", title, g_id)
if (title != False):
msg = QtGui.QMessageBox(QtGui.QMessageBox.Question, title ,tr("Do you want delete group '")
+ title + "' and also containing passwords?")
msg.addButton(QtGui.QMessageBox.Yes)
msg.addButton(QtGui.QMessageBox.No)
ret = msg.exec_()
if (ret == QtGui.QMessageBox.Yes):
# delete password
self._groups_tw.deleteGroup(g_id)
self.reloadItems()
logging.debug("Not group selected title: %s", title)
def showEditGroupDialog(self):
"""
Edit selected group.
"""
g_id = self._groups_tw.currentItemGroupID()
if (g_id):
# is group selected
edit_group_dialog = EditGroupDialog(self, g_id)
if (edit_group_dialog.exec_() == QtGui.QDialog.Accepted):
# all done
self.reloadItems()
def reloadItems(self, p_id = -1):
"""
Reload groups, passwords.
@param p_id: password id to display, if is < 0, doesn't display
"""
try:
self._groups_tw.reloadItems()
self._passwords_table.reloadItems()
if (p_id >= 0):
self._detail_w.setPassword(p_id)
else:
self._detail_w.setHidden(True)
except Exception as e:
logging.exception(e)
InfoMsgBoxes.showErrorMsg(e)