|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a model for password management. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 from PyQt5.QtCore import Qt, QModelIndex, QAbstractTableModel |
|
13 |
|
14 |
|
15 class PasswordModel(QAbstractTableModel): |
|
16 """ |
|
17 Class implementing a model for password management. |
|
18 """ |
|
19 def __init__(self, manager, parent=None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param manager reference to the password manager (PasswordManager) |
|
24 @param parent reference to the parent object (QObject) |
|
25 """ |
|
26 super(PasswordModel, self).__init__(parent) |
|
27 |
|
28 self.__manager = manager |
|
29 manager.changed.connect(self.__passwordsChanged) |
|
30 |
|
31 self.__headers = [ |
|
32 self.tr("Website"), |
|
33 self.tr("Username"), |
|
34 self.tr("Password") |
|
35 ] |
|
36 |
|
37 self.__showPasswords = False |
|
38 |
|
39 def setShowPasswords(self, on): |
|
40 """ |
|
41 Public methods to show passwords. |
|
42 |
|
43 @param on flag indicating if passwords shall be shown (boolean) |
|
44 """ |
|
45 self.__showPasswords = on |
|
46 self.beginResetModel() |
|
47 self.endResetModel() |
|
48 |
|
49 def showPasswords(self): |
|
50 """ |
|
51 Public method to indicate, if passwords shall be shown. |
|
52 |
|
53 @return flag indicating if passwords shall be shown (boolean) |
|
54 """ |
|
55 return self.__showPasswords |
|
56 |
|
57 def __passwordsChanged(self): |
|
58 """ |
|
59 Private slot handling a change of the registered passwords. |
|
60 """ |
|
61 self.beginResetModel() |
|
62 self.endResetModel() |
|
63 |
|
64 def removeRows(self, row, count, parent=None): |
|
65 """ |
|
66 Public method to remove entries from the model. |
|
67 |
|
68 @param row start row (integer) |
|
69 @param count number of rows to remove (integer) |
|
70 @param parent parent index (QModelIndex) |
|
71 @return flag indicating success (boolean) |
|
72 """ |
|
73 if parent is None: |
|
74 parent = QModelIndex() |
|
75 |
|
76 if parent.isValid(): |
|
77 return False |
|
78 |
|
79 if count <= 0: |
|
80 return False |
|
81 |
|
82 lastRow = row + count - 1 |
|
83 |
|
84 self.beginRemoveRows(parent, row, lastRow) |
|
85 |
|
86 siteList = self.__manager.allSiteNames() |
|
87 for index in range(row, lastRow + 1): |
|
88 self.__manager.removePassword(siteList[index]) |
|
89 |
|
90 # removeEngine emits changed() |
|
91 #self.endRemoveRows() |
|
92 |
|
93 return True |
|
94 |
|
95 def rowCount(self, parent=None): |
|
96 """ |
|
97 Public method to get the number of rows of the model. |
|
98 |
|
99 @param parent parent index (QModelIndex) |
|
100 @return number of rows (integer) |
|
101 """ |
|
102 if parent is None: |
|
103 parent = QModelIndex() |
|
104 |
|
105 if parent.isValid(): |
|
106 return 0 |
|
107 else: |
|
108 return self.__manager.sitesCount() |
|
109 |
|
110 def columnCount(self, parent=None): |
|
111 """ |
|
112 Public method to get the number of columns of the model. |
|
113 |
|
114 @param parent parent index (QModelIndex) (Unused) |
|
115 @return number of columns (integer) |
|
116 """ |
|
117 if self.__showPasswords: |
|
118 return 3 |
|
119 else: |
|
120 return 2 |
|
121 |
|
122 def data(self, index, role): |
|
123 """ |
|
124 Public method to get data from the model. |
|
125 |
|
126 @param index index to get data for (QModelIndex) |
|
127 @param role role of the data to retrieve (integer) |
|
128 @return requested data |
|
129 """ |
|
130 if index.row() >= self.__manager.sitesCount() or index.row() < 0: |
|
131 return None |
|
132 |
|
133 site = self.__manager.allSiteNames()[index.row()] |
|
134 siteInfo = self.__manager.siteInfo(site) |
|
135 |
|
136 if siteInfo is None: |
|
137 return None |
|
138 |
|
139 if role == Qt.DisplayRole: |
|
140 if index.column() == 0: |
|
141 return site |
|
142 elif index.column() in [1, 2]: |
|
143 return siteInfo[index.column() - 1] |
|
144 |
|
145 return None |
|
146 |
|
147 def headerData(self, section, orientation, role=Qt.DisplayRole): |
|
148 """ |
|
149 Public method to get the header data. |
|
150 |
|
151 @param section section number (integer) |
|
152 @param orientation header orientation (Qt.Orientation) |
|
153 @param role data role (integer) |
|
154 @return header data |
|
155 """ |
|
156 if orientation == Qt.Horizontal and role == Qt.DisplayRole: |
|
157 try: |
|
158 return self.__headers[section] |
|
159 except IndexError: |
|
160 pass |
|
161 |
|
162 return None |