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