|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 - 2016 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=QModelIndex()): |
|
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.isValid(): |
|
74 return False |
|
75 |
|
76 if count <= 0: |
|
77 return False |
|
78 |
|
79 lastRow = row + count - 1 |
|
80 |
|
81 self.beginRemoveRows(parent, row, lastRow) |
|
82 |
|
83 siteList = self.__manager.allSiteNames() |
|
84 for index in range(row, lastRow + 1): |
|
85 self.__manager.removePassword(siteList[index]) |
|
86 |
|
87 # removeEngine emits changed() |
|
88 #self.endRemoveRows() |
|
89 |
|
90 return True |
|
91 |
|
92 def rowCount(self, parent=QModelIndex()): |
|
93 """ |
|
94 Public method to get the number of rows of the model. |
|
95 |
|
96 @param parent parent index (QModelIndex) |
|
97 @return number of rows (integer) |
|
98 """ |
|
99 if parent.isValid(): |
|
100 return 0 |
|
101 else: |
|
102 return self.__manager.sitesCount() |
|
103 |
|
104 def columnCount(self, parent=QModelIndex()): |
|
105 """ |
|
106 Public method to get the number of columns of the model. |
|
107 |
|
108 @param parent parent index (QModelIndex) |
|
109 @return number of columns (integer) |
|
110 """ |
|
111 if self.__showPasswords: |
|
112 return 3 |
|
113 else: |
|
114 return 2 |
|
115 |
|
116 def data(self, index, role): |
|
117 """ |
|
118 Public method to get data from the model. |
|
119 |
|
120 @param index index to get data for (QModelIndex) |
|
121 @param role role of the data to retrieve (integer) |
|
122 @return requested data |
|
123 """ |
|
124 if index.row() >= self.__manager.sitesCount() or index.row() < 0: |
|
125 return None |
|
126 |
|
127 site = self.__manager.allSiteNames()[index.row()] |
|
128 siteInfo = self.__manager.siteInfo(site) |
|
129 |
|
130 if siteInfo is None: |
|
131 return None |
|
132 |
|
133 if role == Qt.DisplayRole: |
|
134 if index.column() == 0: |
|
135 return site |
|
136 elif index.column() in [1, 2]: |
|
137 return siteInfo[index.column() - 1] |
|
138 |
|
139 return None |
|
140 |
|
141 def headerData(self, section, orientation, role=Qt.DisplayRole): |
|
142 """ |
|
143 Public method to get the header data. |
|
144 |
|
145 @param section section number (integer) |
|
146 @param orientation header orientation (Qt.Orientation) |
|
147 @param role data role (integer) |
|
148 @return header data |
|
149 """ |
|
150 if orientation == Qt.Horizontal and role == Qt.DisplayRole: |
|
151 try: |
|
152 return self.__headers[section] |
|
153 except IndexError: |
|
154 pass |
|
155 |
|
156 return None |