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