Helpviewer/CookieJar/CookieExceptionsModel.py

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

eric ide

mercurial