13 |
13 |
14 class CookieExceptionsModel(QAbstractTableModel): |
14 class CookieExceptionsModel(QAbstractTableModel): |
15 """ |
15 """ |
16 Class implementing the cookie exceptions model. |
16 Class implementing the cookie exceptions model. |
17 """ |
17 """ |
|
18 |
18 def __init__(self, cookieJar, parent=None): |
19 def __init__(self, cookieJar, parent=None): |
19 """ |
20 """ |
20 Constructor |
21 Constructor |
21 |
22 |
22 @param cookieJar reference to the cookie jar (CookieJar) |
23 @param cookieJar reference to the cookie jar (CookieJar) |
23 @param parent reference to the parent object (QObject) |
24 @param parent reference to the parent object (QObject) |
24 """ |
25 """ |
25 super().__init__(parent) |
26 super().__init__(parent) |
26 |
27 |
27 self.__cookieJar = cookieJar |
28 self.__cookieJar = cookieJar |
28 self.__allowedCookies = self.__cookieJar.allowedCookies() |
29 self.__allowedCookies = self.__cookieJar.allowedCookies() |
29 self.__blockedCookies = self.__cookieJar.blockedCookies() |
30 self.__blockedCookies = self.__cookieJar.blockedCookies() |
30 self.__sessionCookies = self.__cookieJar.allowForSessionCookies() |
31 self.__sessionCookies = self.__cookieJar.allowForSessionCookies() |
31 |
32 |
32 self.__headers = [ |
33 self.__headers = [ |
33 self.tr("Website"), |
34 self.tr("Website"), |
34 self.tr("Status"), |
35 self.tr("Status"), |
35 ] |
36 ] |
36 |
37 |
37 def headerData(self, section, orientation, role): |
38 def headerData(self, section, orientation, role): |
38 """ |
39 """ |
39 Public method to get header data from the model. |
40 Public method to get header data from the model. |
40 |
41 |
41 @param section section number (integer) |
42 @param section section number (integer) |
42 @param orientation orientation (Qt.Orientation) |
43 @param orientation orientation (Qt.Orientation) |
43 @param role role of the data to retrieve (Qt.ItemDataRole) |
44 @param role role of the data to retrieve (Qt.ItemDataRole) |
44 @return requested data |
45 @return requested data |
45 """ |
46 """ |
46 if role == Qt.ItemDataRole.SizeHintRole: |
47 if role == Qt.ItemDataRole.SizeHintRole: |
47 fm = QFontMetrics(QFont()) |
48 fm = QFontMetrics(QFont()) |
48 height = fm.height() + fm.height() // 3 |
49 height = fm.height() + fm.height() // 3 |
49 try: |
50 try: |
50 width = fm.horizontalAdvance( |
51 width = fm.horizontalAdvance( |
51 self.headerData(section, orientation, |
52 self.headerData(section, orientation, Qt.ItemDataRole.DisplayRole) |
52 Qt.ItemDataRole.DisplayRole)) |
53 ) |
53 except AttributeError: |
54 except AttributeError: |
54 width = fm.width( |
55 width = fm.width( |
55 self.headerData(section, orientation, |
56 self.headerData(section, orientation, Qt.ItemDataRole.DisplayRole) |
56 Qt.ItemDataRole.DisplayRole)) |
57 ) |
57 return QSize(width, height) |
58 return QSize(width, height) |
58 |
59 |
59 if ( |
60 if ( |
60 orientation == Qt.Orientation.Horizontal and |
61 orientation == Qt.Orientation.Horizontal |
61 role == Qt.ItemDataRole.DisplayRole |
62 and role == Qt.ItemDataRole.DisplayRole |
62 ): |
63 ): |
63 try: |
64 try: |
64 return self.__headers[section] |
65 return self.__headers[section] |
65 except IndexError: |
66 except IndexError: |
66 return None |
67 return None |
67 |
68 |
68 return QAbstractTableModel.headerData(self, section, orientation, role) |
69 return QAbstractTableModel.headerData(self, section, orientation, role) |
69 |
70 |
70 def data(self, index, role): |
71 def data(self, index, role): |
71 """ |
72 """ |
72 Public method to get data from the model. |
73 Public method to get data from the model. |
73 |
74 |
74 @param index index to get data for (QModelIndex) |
75 @param index index to get data for (QModelIndex) |
75 @param role role of the data to retrieve (integer) |
76 @param role role of the data to retrieve (integer) |
76 @return requested data |
77 @return requested data |
77 """ |
78 """ |
78 if index.row() < 0 or index.row() >= self.rowCount(): |
79 if index.row() < 0 or index.row() >= self.rowCount(): |
79 return None |
80 return None |
80 |
81 |
81 if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole): |
82 if role in (Qt.ItemDataRole.DisplayRole, Qt.ItemDataRole.EditRole): |
82 row = index.row() |
83 row = index.row() |
83 if row < len(self.__allowedCookies): |
84 if row < len(self.__allowedCookies): |
84 if index.column() == 0: |
85 if index.column() == 0: |
85 return self.__allowedCookies[row] |
86 return self.__allowedCookies[row] |
86 elif index.column() == 1: |
87 elif index.column() == 1: |
87 return self.tr("Allow") |
88 return self.tr("Allow") |
88 else: |
89 else: |
89 return None |
90 return None |
90 |
91 |
91 row -= len(self.__allowedCookies) |
92 row -= len(self.__allowedCookies) |
92 if row < len(self.__blockedCookies): |
93 if row < len(self.__blockedCookies): |
93 if index.column() == 0: |
94 if index.column() == 0: |
94 return self.__blockedCookies[row] |
95 return self.__blockedCookies[row] |
95 elif index.column() == 1: |
96 elif index.column() == 1: |
96 return self.tr("Block") |
97 return self.tr("Block") |
97 else: |
98 else: |
98 return None |
99 return None |
99 |
100 |
100 row -= len(self.__blockedCookies) |
101 row -= len(self.__blockedCookies) |
101 if row < len(self.__sessionCookies): |
102 if row < len(self.__sessionCookies): |
102 if index.column() == 0: |
103 if index.column() == 0: |
103 return self.__sessionCookies[row] |
104 return self.__sessionCookies[row] |
104 elif index.column() == 1: |
105 elif index.column() == 1: |
105 return self.tr("Allow For Session") |
106 return self.tr("Allow For Session") |
106 else: |
107 else: |
107 return None |
108 return None |
108 |
109 |
109 return None |
110 return None |
110 |
111 |
111 return None |
112 return None |
112 |
113 |
113 def columnCount(self, parent=None): |
114 def columnCount(self, parent=None): |
114 """ |
115 """ |
115 Public method to get the number of columns of the model. |
116 Public method to get the number of columns of the model. |
116 |
117 |
117 @param parent parent index (QModelIndex) |
118 @param parent parent index (QModelIndex) |
118 @return number of columns (integer) |
119 @return number of columns (integer) |
119 """ |
120 """ |
120 if parent is None: |
121 if parent is None: |
121 parent = QModelIndex() |
122 parent = QModelIndex() |
122 |
123 |
123 if parent.isValid(): |
124 if parent.isValid(): |
124 return 0 |
125 return 0 |
125 else: |
126 else: |
126 return len(self.__headers) |
127 return len(self.__headers) |
127 |
128 |
128 def rowCount(self, parent=None): |
129 def rowCount(self, parent=None): |
129 """ |
130 """ |
130 Public method to get the number of rows of the model. |
131 Public method to get the number of rows of the model. |
131 |
132 |
132 @param parent parent index (QModelIndex) |
133 @param parent parent index (QModelIndex) |
133 @return number of rows (integer) |
134 @return number of rows (integer) |
134 """ |
135 """ |
135 if parent is None: |
136 if parent is None: |
136 parent = QModelIndex() |
137 parent = QModelIndex() |
137 |
138 |
138 if parent.isValid() or self.__cookieJar is None: |
139 if parent.isValid() or self.__cookieJar is None: |
139 return 0 |
140 return 0 |
140 else: |
141 else: |
141 return ( |
142 return ( |
142 len(self.__allowedCookies) + |
143 len(self.__allowedCookies) |
143 len(self.__blockedCookies) + |
144 + len(self.__blockedCookies) |
144 len(self.__sessionCookies) |
145 + len(self.__sessionCookies) |
145 ) |
146 ) |
146 |
147 |
147 def removeRows(self, row, count, parent=None): |
148 def removeRows(self, row, count, parent=None): |
148 """ |
149 """ |
149 Public method to remove entries from the model. |
150 Public method to remove entries from the model. |
150 |
151 |
151 @param row start row (integer) |
152 @param row start row (integer) |
152 @param count number of rows to remove (integer) |
153 @param count number of rows to remove (integer) |
153 @param parent parent index (QModelIndex) |
154 @param parent parent index (QModelIndex) |
154 @return flag indicating success (boolean) |
155 @return flag indicating success (boolean) |
155 """ |
156 """ |
156 if parent is None: |
157 if parent is None: |
157 parent = QModelIndex() |
158 parent = QModelIndex() |
158 |
159 |
159 if parent.isValid() or self.__cookieJar is None: |
160 if parent.isValid() or self.__cookieJar is None: |
160 return False |
161 return False |
161 |
162 |
162 lastRow = row + count - 1 |
163 lastRow = row + count - 1 |
163 self.beginRemoveRows(parent, row, lastRow) |
164 self.beginRemoveRows(parent, row, lastRow) |
164 for i in range(lastRow, row - 1, -1): |
165 for i in range(lastRow, row - 1, -1): |
165 rowToRemove = i |
166 rowToRemove = i |
166 |
167 |
167 if rowToRemove < len(self.__allowedCookies): |
168 if rowToRemove < len(self.__allowedCookies): |
168 del self.__allowedCookies[rowToRemove] |
169 del self.__allowedCookies[rowToRemove] |
169 continue |
170 continue |
170 |
171 |
171 rowToRemove -= len(self.__allowedCookies) |
172 rowToRemove -= len(self.__allowedCookies) |
172 if rowToRemove < len(self.__blockedCookies): |
173 if rowToRemove < len(self.__blockedCookies): |
173 del self.__blockedCookies[rowToRemove] |
174 del self.__blockedCookies[rowToRemove] |
174 continue |
175 continue |
175 |
176 |
176 rowToRemove -= len(self.__blockedCookies) |
177 rowToRemove -= len(self.__blockedCookies) |
177 if rowToRemove < len(self.__sessionCookies): |
178 if rowToRemove < len(self.__sessionCookies): |
178 del self.__sessionCookies[rowToRemove] |
179 del self.__sessionCookies[rowToRemove] |
179 continue |
180 continue |
180 |
181 |
181 self.__cookieJar.setAllowedCookies(self.__allowedCookies) |
182 self.__cookieJar.setAllowedCookies(self.__allowedCookies) |
182 self.__cookieJar.setBlockedCookies(self.__blockedCookies) |
183 self.__cookieJar.setBlockedCookies(self.__blockedCookies) |
183 self.__cookieJar.setAllowForSessionCookies(self.__sessionCookies) |
184 self.__cookieJar.setAllowForSessionCookies(self.__sessionCookies) |
184 self.endRemoveRows() |
185 self.endRemoveRows() |
185 |
186 |
186 return True |
187 return True |
187 |
188 |
188 def addRule(self, host, rule): |
189 def addRule(self, host, rule): |
189 """ |
190 """ |
190 Public method to add an exception rule. |
191 Public method to add an exception rule. |
191 |
192 |
192 @param host name of the host to add a rule for (string) |
193 @param host name of the host to add a rule for (string) |
193 @param rule type of rule to add (CookieJar.Allow, CookieJar.Block or |
194 @param rule type of rule to add (CookieJar.Allow, CookieJar.Block or |
194 CookieJar.AllowForSession) |
195 CookieJar.AllowForSession) |
195 """ |
196 """ |
196 if not host: |
197 if not host: |
197 return |
198 return |
198 |
199 |
199 from .CookieJar import CookieJar |
200 from .CookieJar import CookieJar |
200 |
201 |
201 if rule == CookieJar.Allow: |
202 if rule == CookieJar.Allow: |
202 self.__addHost( |
203 self.__addHost( |
203 host, self.__allowedCookies, self.__blockedCookies, |
204 host, |
204 self.__sessionCookies) |
205 self.__allowedCookies, |
|
206 self.__blockedCookies, |
|
207 self.__sessionCookies, |
|
208 ) |
205 return |
209 return |
206 elif rule == CookieJar.Block: |
210 elif rule == CookieJar.Block: |
207 self.__addHost( |
211 self.__addHost( |
208 host, self.__blockedCookies, self.__allowedCookies, |
212 host, |
209 self.__sessionCookies) |
213 self.__blockedCookies, |
|
214 self.__allowedCookies, |
|
215 self.__sessionCookies, |
|
216 ) |
210 return |
217 return |
211 elif rule == CookieJar.AllowForSession: |
218 elif rule == CookieJar.AllowForSession: |
212 self.__addHost( |
219 self.__addHost( |
213 host, self.__sessionCookies, self.__allowedCookies, |
220 host, |
214 self.__blockedCookies) |
221 self.__sessionCookies, |
215 return |
222 self.__allowedCookies, |
216 |
223 self.__blockedCookies, |
|
224 ) |
|
225 return |
|
226 |
217 def __addHost(self, host, addList, removeList1, removeList2): |
227 def __addHost(self, host, addList, removeList1, removeList2): |
218 """ |
228 """ |
219 Private method to add a host to an exception list. |
229 Private method to add a host to an exception list. |
220 |
230 |
221 @param host name of the host to add (string) |
231 @param host name of the host to add (string) |
222 @param addList reference to the list to add it to (list of strings) |
232 @param addList reference to the list to add it to (list of strings) |
223 @param removeList1 reference to first list to remove it from |
233 @param removeList1 reference to first list to remove it from |
224 (list of strings) |
234 (list of strings) |
225 @param removeList2 reference to second list to remove it from |
235 @param removeList2 reference to second list to remove it from |