WebBrowser/CookieJar/CookiesDialog.py

changeset 5029
1ce5e98ebc43
parent 5028
f7ea46dd1b7e
child 5030
b728bb00886e
equal deleted inserted replaced
5028:f7ea46dd1b7e 5029:1ce5e98ebc43
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 from PyQt5.QtCore import pyqtSlot, Qt, QDateTime, QByteArray, \ 12 from PyQt5.QtCore import pyqtSlot, Qt, QDateTime, QByteArray, \
13 QSortFilterProxyModel 13 QSortFilterProxyModel
14 from PyQt5.QtGui import QFont, QFontMetrics 14 from PyQt5.QtGui import QFont, QFontMetrics
15 from PyQt5.QtWidgets import QDialog 15 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem
16 16
17 from .CookieModel import CookieModel 17 ##from .CookieModel import CookieModel
18 18 ##
19 from .Ui_CookiesDialog import Ui_CookiesDialog 19 from .Ui_CookiesDialog import Ui_CookiesDialog
20 20
21 21
22 # TODO: Change dialog to use a QTreeWidget and show cookie data on bottom of dialog
23 # TODO: Remove CookieModel, CookieDetailsDialog and related files
22 class CookiesDialog(QDialog, Ui_CookiesDialog): 24 class CookiesDialog(QDialog, Ui_CookiesDialog):
23 """ 25 """
24 Class implementing a dialog to show all cookies. 26 Class implementing a dialog to show all cookies.
25 """ 27 """
28 DomainRole = Qt.UserRole + 1
29 CookieRole = Qt.UserRole + 2
30
26 def __init__(self, cookieJar, parent=None): 31 def __init__(self, cookieJar, parent=None):
27 """ 32 """
28 Constructor 33 Constructor
29 34
30 @param cookieJar reference to the cookie jar (CookieJar) 35 @param cookieJar reference to the cookie jar (CookieJar)
35 40
36 self.addButton.setEnabled(False) 41 self.addButton.setEnabled(False)
37 42
38 self.__cookieJar = cookieJar 43 self.__cookieJar = cookieJar
39 44
40 self.removeButton.clicked.connect(self.cookiesTable.removeSelected) 45 self.__domainDict = {}
41 self.removeAllButton.clicked.connect(self.cookiesTable.removeAll) 46 ## self.__itemDict = {} # TODO: Maybe get rid of this
42 47
43 self.cookiesTable.verticalHeader().hide() 48 for cookie in self.__cookieJar.cookies():
44 model = CookieModel(cookieJar, self) 49 self.__addCookie(cookie)
45 self.__proxyModel = QSortFilterProxyModel(self) 50
46 self.__proxyModel.setSourceModel(model) 51 def __cookieDomain(self, cookie):
47 self.searchEdit.textChanged.connect( 52 """
48 self.__proxyModel.setFilterFixedString) 53 Private method to extract the cookie domain.
49 self.cookiesTable.setModel(self.__proxyModel) 54
50 self.cookiesTable.doubleClicked.connect(self.__showCookieDetails) 55 @param cookie cookie to get the domain from
51 self.cookiesTable.selectionModel().selectionChanged.connect( 56 @type QNetworkCookie
52 self.__tableSelectionChanged) 57 @return domain of the cookie
53 self.cookiesTable.model().modelReset.connect(self.__tableModelReset) 58 @rtype str
54 59 """
55 fm = QFontMetrics(QFont()) 60 domain = cookie.domain()
56 height = fm.height() + fm.height() // 3 61 if domain.startswith("."):
57 self.cookiesTable.verticalHeader().setDefaultSectionSize(height) 62 domain = domain[1:]
58 self.cookiesTable.verticalHeader().setMinimumSectionSize(-1) 63 return domain
59 for section in range(model.columnCount()): 64
60 header = self.cookiesTable.horizontalHeader()\ 65 def __addCookie(self, cookie):
61 .sectionSizeHint(section) 66 """
62 if section == 0: 67 Private method to add a cookie to the tree.
63 header = fm.width("averagebiglonghost.averagedomain.info") 68
64 elif section == 1: 69 @param cookie reference to the cookie
65 header = fm.width("_session_id") 70 @type QNetworkCookie
66 elif section == 4: 71 """
67 header = fm.width( 72 domain = self.__cookieDomain(cookie)
68 QDateTime.currentDateTime().toString(Qt.LocalDate)) 73 if domain in self.__domainDict:
69 buffer = fm.width("mm") 74 itm = QTreeWidgetItem(self.__domainDict[domain])
70 header += buffer
71 self.cookiesTable.horizontalHeader().resizeSection(section, header)
72 self.cookiesTable.horizontalHeader().setStretchLastSection(True)
73 self.cookiesTable.model().sort(
74 self.cookiesTable.horizontalHeader().sortIndicatorSection(),
75 Qt.AscendingOrder)
76
77 self.__detailsDialog = None
78
79 def __showCookieDetails(self, index):
80 """
81 Private slot to show a dialog with the cookie details.
82
83 @param index index of the entry to show (QModelIndex)
84 """
85 if not index.isValid():
86 return
87
88 cookiesTable = self.sender()
89 if cookiesTable is None:
90 return
91
92 model = cookiesTable.model()
93 row = index.row()
94
95 domain = model.data(model.index(row, 0))
96 name = model.data(model.index(row, 1))
97 path = model.data(model.index(row, 2))
98 secure = model.data(model.index(row, 3))
99 expires = model.data(model.index(row, 4)).toString("yyyy-MM-dd hh:mm")
100 data = model.data(model.index(row, 5))
101 if data is None:
102 value = ""
103 else: 75 else:
104 value = bytes(QByteArray.fromPercentEncoding(data)).decode() 76 newParent = QTreeWidgetItem(self.cookiesTree)
105 77 newParent.setText(0, domain)
106 if self.__detailsDialog is None: 78 newParent.setData(0, self.DomainRole, cookie.domain())
107 from .CookieDetailsDialog import CookieDetailsDialog 79 self.__domainDict[domain] = newParent
108 self.__detailsDialog = CookieDetailsDialog(self) 80
109 self.__detailsDialog.setData(domain, name, path, secure, expires, 81 itm = QTreeWidgetItem(newParent)
110 value) 82
111 self.__detailsDialog.show() 83 itm.setText(0, cookie.domain())
84 itm.setText(1, bytes(cookie.name()).decode())
85 itm.setData(0, self.CookieRole, cookie)
86
87 ## self.__itemDict[itm] = cookie
88
89 ## self.removeButton.clicked.connect(self.cookiesTable.removeSelected)
90 ## self.removeAllButton.clicked.connect(self.cookiesTable.removeAll)
91 ##
92 ## self.cookiesTable.verticalHeader().hide()
93 ## model = CookieModel(cookieJar, self)
94 ## self.__proxyModel = QSortFilterProxyModel(self)
95 ## self.__proxyModel.setSourceModel(model)
96 ## self.searchEdit.textChanged.connect(
97 ## self.__proxyModel.setFilterFixedString)
98 ## self.cookiesTable.setModel(self.__proxyModel)
99 ## self.cookiesTable.doubleClicked.connect(self.__showCookieDetails)
100 ## self.cookiesTable.selectionModel().selectionChanged.connect(
101 ## self.__tableSelectionChanged)
102 ## self.cookiesTable.model().modelReset.connect(self.__tableModelReset)
103 ##
104 ## fm = QFontMetrics(QFont())
105 ## height = fm.height() + fm.height() // 3
106 ## self.cookiesTable.verticalHeader().setDefaultSectionSize(height)
107 ## self.cookiesTable.verticalHeader().setMinimumSectionSize(-1)
108 ## for section in range(model.columnCount()):
109 ## header = self.cookiesTable.horizontalHeader()\
110 ## .sectionSizeHint(section)
111 ## if section == 0:
112 ## header = fm.width("averagebiglonghost.averagedomain.info")
113 ## elif section == 1:
114 ## header = fm.width("_session_id")
115 ## elif section == 4:
116 ## header = fm.width(
117 ## QDateTime.currentDateTime().toString(Qt.LocalDate))
118 ## buffer = fm.width("mm")
119 ## header += buffer
120 ## self.cookiesTable.horizontalHeader().resizeSection(section, header)
121 ## self.cookiesTable.horizontalHeader().setStretchLastSection(True)
122 ## self.cookiesTable.model().sort(
123 ## self.cookiesTable.horizontalHeader().sortIndicatorSection(),
124 ## Qt.AscendingOrder)
125 ##
126 ## self.__detailsDialog = None
127 ##
128 ## def __showCookieDetails(self, index):
129 ## """
130 ## Private slot to show a dialog with the cookie details.
131 ##
132 ## @param index index of the entry to show (QModelIndex)
133 ## """
134 ## if not index.isValid():
135 ## return
136 ##
137 ## cookiesTable = self.sender()
138 ## if cookiesTable is None:
139 ## return
140 ##
141 ## model = cookiesTable.model()
142 ## row = index.row()
143 ##
144 ## domain = model.data(model.index(row, 0))
145 ## name = model.data(model.index(row, 1))
146 ## path = model.data(model.index(row, 2))
147 ## secure = model.data(model.index(row, 3))
148 ## expires = model.data(model.index(row, 4)).toString("yyyy-MM-dd hh:mm")
149 ## data = model.data(model.index(row, 5))
150 ## if data is None:
151 ## value = ""
152 ## else:
153 ## value = bytes(QByteArray.fromPercentEncoding(data)).decode()
154 ##
155 ## if self.__detailsDialog is None:
156 ## from .CookieDetailsDialog import CookieDetailsDialog
157 ## self.__detailsDialog = CookieDetailsDialog(self)
158 ## self.__detailsDialog.setData(domain, name, path, secure, expires,
159 ## value)
160 ## self.__detailsDialog.show()
112 161
113 @pyqtSlot() 162 @pyqtSlot()
114 def on_addButton_clicked(self): 163 def on_addButton_clicked(self):
115 """ 164 """
116 Private slot to add a new exception. 165 Private slot to add a new exception.
117 """ 166 """
118 selection = self.cookiesTable.selectionModel().selectedRows() 167 # TODO: change this
119 if len(selection) == 0: 168 ## selection = self.cookiesTable.selectionModel().selectedRows()
169 ## if len(selection) == 0:
170 ## return
171 ##
172 ## from .CookiesExceptionsDialog import CookiesExceptionsDialog
173 ##
174 ## firstSelected = selection[0]
175 ## domainSelection = firstSelected.sibling(firstSelected.row(), 0)
176 ## domain = self.__proxyModel.data(domainSelection, Qt.DisplayRole)
177 ## dlg = CookiesExceptionsDialog(self.__cookieJar, self)
178 ## dlg.setDomainName(domain)
179 ## dlg.exec_()
180 ##
181 ## def __tableSelectionChanged(self, selected, deselected):
182 ## """
183 ## Private slot to handle a change of selected items.
184 ##
185 ## @param selected selected indexes (QItemSelection)
186 ## @param deselected deselected indexes (QItemSelection)
187 ## """
188 ## self.addButton.setEnabled(len(selected.indexes()) > 0)
189 ##
190 ## def __tableModelReset(self):
191 ## """
192 ## Private slot to handle a reset of the cookies table.
193 ## """
194 ## self.addButton.setEnabled(False)
195
196 @pyqtSlot()
197 def on_removeButton_clicked(self):
198 """
199 Slot documentation goes here.
200 """
201 # TODO: not implemented yet
202 raise NotImplementedError
203
204 @pyqtSlot()
205 def on_removeAllButton_clicked(self):
206 """
207 Slot documentation goes here.
208 """
209 # TODO: not implemented yet
210 raise NotImplementedError
211
212 @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem)
213 def on_cookiesTree_currentItemChanged(self, current, previous):
214 """
215 Private slot to handle a change of the current item.
216
217 @param current reference to the current item
218 @type QTreeWidgetItem
219 @param previous reference to the previous current item
220 @type QTreeWidgetItem
221 """
222 self.addButton.setEnabled(current is not None)
223 self.removeButton.setEnabled(current is not None)
224
225 if current is None:
120 return 226 return
121 227
122 from .CookiesExceptionsDialog import CookiesExceptionsDialog 228 if not current.text(1):
123 229 # it is a cookie domain entry
124 firstSelected = selection[0] 230 self.domain.setText(self.tr("<no cookie selected>"))
125 domainSelection = firstSelected.sibling(firstSelected.row(), 0) 231 self.name.setText(self.tr("<no cookie selected>"))
126 domain = self.__proxyModel.data(domainSelection, Qt.DisplayRole) 232 self.path.setText(self.tr("<no cookie selected>"))
127 dlg = CookiesExceptionsDialog(self.__cookieJar, self) 233 self.secure.setText(self.tr("<no cookie selected>"))
128 dlg.setDomainName(domain) 234 self.expiration.setText(self.tr("<no cookie selected>"))
129 dlg.exec_() 235 self.value.setText(self.tr("<no cookie selected>"))
130 236
131 def __tableSelectionChanged(self, selected, deselected): 237 self.removeButton.setText(self.tr("Remove Cookies"))
132 """ 238 else:
133 Private slot to handle a change of selected items. 239 # it is a cookie entry
134 240 cookie = current.data(0, self.CookieRole)
135 @param selected selected indexes (QItemSelection) 241
136 @param deselected deselected indexes (QItemSelection) 242 self.domain.setText(cookie.domain())
137 """ 243 self.name.setText(bytes(cookie.name()).decode())
138 self.addButton.setEnabled(len(selected.indexes()) > 0) 244 self.path.setText(cookie.path())
139 245 if cookie.isSecure():
140 def __tableModelReset(self): 246 self.secure.setText(self.tr("Secure connections only"))
141 """ 247 else:
142 Private slot to handle a reset of the cookies table. 248 self.secure.setText(self.tr("All connections"))
143 """ 249 if cookie.isSessionCookie():
144 self.addButton.setEnabled(False) 250 self.expiration.setText(self.tr("Session Cookie"))
251 else:
252 self.expiration.setText(
253 cookie.expirationDate().toString("yyyy-MM-dd HH:mm:ss"))
254 self.value.setText(
255 bytes(QByteArray.fromPercentEncoding(cookie.value())).decode())
256
257 self.removeButton.setText(self.tr("Remove Cookie"))
258
259 @pyqtSlot(str)
260 def on_searchEdit_textChanged(self, txt):
261 """
262 Private slot to search and filter the cookie tree.
263
264 @param txt text to search for
265 @type str
266 """
267 if not txt:
268 for row in range(self.cookiesTree.topLevelItemCount()):
269 self.cookiesTree.topLevelItem(row).setHidden(False)
270 else:
271 for row in range(self.cookiesTree.topLevelItemCount()):
272 text = self.cookiesTree.topLevelItem(row).text(0)
273 self.cookiesTree.topLevelItem(row).setHidden(txt not in text)

eric ide

mercurial