eric6/WebBrowser/CookieJar/CookieExceptionsModel.py

changeset 6942
2602857055c5
parent 6645
ad476851d7e0
child 7229
53054eb5b15a
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2009 - 2019 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=None):
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 is None:
114 parent = QModelIndex()
115
116 if parent.isValid():
117 return 0
118 else:
119 return len(self.__headers)
120
121 def rowCount(self, parent=None):
122 """
123 Public method to get the number of rows of the model.
124
125 @param parent parent index (QModelIndex)
126 @return number of rows (integer)
127 """
128 if parent is None:
129 parent = QModelIndex()
130
131 if parent.isValid() or self.__cookieJar is None:
132 return 0
133 else:
134 return len(self.__allowedCookies) + \
135 len(self.__blockedCookies) + \
136 len(self.__sessionCookies)
137
138 def removeRows(self, row, count, parent=None):
139 """
140 Public method to remove entries from the model.
141
142 @param row start row (integer)
143 @param count number of rows to remove (integer)
144 @param parent parent index (QModelIndex)
145 @return flag indicating success (boolean)
146 """
147 if parent is None:
148 parent = QModelIndex()
149
150 if parent.isValid() or self.__cookieJar is None:
151 return False
152
153 lastRow = row + count - 1
154 self.beginRemoveRows(parent, row, lastRow)
155 for i in range(lastRow, row - 1, -1):
156 rowToRemove = i
157
158 if rowToRemove < len(self.__allowedCookies):
159 del self.__allowedCookies[rowToRemove]
160 continue
161
162 rowToRemove -= len(self.__allowedCookies)
163 if rowToRemove < len(self.__blockedCookies):
164 del self.__blockedCookies[rowToRemove]
165 continue
166
167 rowToRemove -= len(self.__blockedCookies)
168 if rowToRemove < len(self.__sessionCookies):
169 del self.__sessionCookies[rowToRemove]
170 continue
171
172 self.__cookieJar.setAllowedCookies(self.__allowedCookies)
173 self.__cookieJar.setBlockedCookies(self.__blockedCookies)
174 self.__cookieJar.setAllowForSessionCookies(self.__sessionCookies)
175 self.endRemoveRows()
176
177 return True
178
179 def addRule(self, host, rule):
180 """
181 Public method to add an exception rule.
182
183 @param host name of the host to add a rule for (string)
184 @param rule type of rule to add (CookieJar.Allow, CookieJar.Block or
185 CookieJar.AllowForSession)
186 """
187 if not host:
188 return
189
190 from .CookieJar import CookieJar
191
192 if rule == CookieJar.Allow:
193 self.__addHost(
194 host, self.__allowedCookies, self.__blockedCookies,
195 self.__sessionCookies)
196 return
197 elif rule == CookieJar.Block:
198 self.__addHost(
199 host, self.__blockedCookies, self.__allowedCookies,
200 self.__sessionCookies)
201 return
202 elif rule == CookieJar.AllowForSession:
203 self.__addHost(
204 host, self.__sessionCookies, self.__allowedCookies,
205 self.__blockedCookies)
206 return
207
208 def __addHost(self, host, addList, removeList1, removeList2):
209 """
210 Private method to add a host to an exception list.
211
212 @param host name of the host to add (string)
213 @param addList reference to the list to add it to (list of strings)
214 @param removeList1 reference to first list to remove it from
215 (list of strings)
216 @param removeList2 reference to second list to remove it from
217 (list of strings)
218 """
219 if host not in addList:
220 addList.append(host)
221 if host in removeList1:
222 removeList1.remove(host)
223 if host in removeList2:
224 removeList2.remove(host)
225
226 # Avoid to have similar rules (with or without leading dot)
227 # e.g. python-projects.org and .python-projects.org
228 if host.startswith("."):
229 otherRule = host[1:]
230 else:
231 otherRule = '.' + host
232 if otherRule in addList:
233 addList.remove(otherRule)
234 if otherRule in removeList1:
235 removeList1.remove(otherRule)
236 if otherRule in removeList2:
237 removeList2.remove(otherRule)
238
239 self.__cookieJar.setAllowedCookies(self.__allowedCookies)
240 self.__cookieJar.setBlockedCookies(self.__blockedCookies)
241 self.__cookieJar.setAllowForSessionCookies(self.__sessionCookies)
242
243 self.beginResetModel()
244 self.endResetModel()

eric ide

mercurial