eric6/WebBrowser/CookieJar/CookiesDialog.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 a dialog to show all cookies.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot, Qt, QByteArray
13 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QHeaderView
14
15 from E5Gui import E5MessageBox
16
17 from .Ui_CookiesDialog import Ui_CookiesDialog
18
19
20 class CookiesDialog(QDialog, Ui_CookiesDialog):
21 """
22 Class implementing a dialog to show all cookies.
23 """
24 DomainRole = Qt.UserRole + 1
25 CookieRole = Qt.UserRole + 2
26
27 def __init__(self, cookieJar, parent=None):
28 """
29 Constructor
30
31 @param cookieJar reference to the cookie jar (CookieJar)
32 @param parent reference to the parent widget (QWidget)
33 """
34 super(CookiesDialog, self).__init__(parent)
35 self.setupUi(self)
36
37 self.addButton.setEnabled(False)
38
39 self.__cookieJar = cookieJar
40
41 self.__domainDict = {}
42
43 self.cookiesTree.headerItem().setText(
44 self.cookiesTree.columnCount(), "")
45
46 for cookie in self.__cookieJar.cookies():
47 self.__addCookie(cookie)
48 self.__resizeColumns()
49
50 self.cookiesTree.itemExpanded.connect(self.__resizeColumns)
51 self.cookiesTree.itemCollapsed.connect(self.__resizeColumns)
52
53 @pyqtSlot()
54 def __resizeColumns(self):
55 """
56 Private slot to resize the columns.
57 """
58 self.cookiesTree.header().resizeSections(QHeaderView.ResizeToContents)
59 self.cookiesTree.header().setStretchLastSection(True)
60
61 def __cookieDomain(self, cookie):
62 """
63 Private method to extract the cookie domain.
64
65 @param cookie cookie to get the domain from
66 @type QNetworkCookie
67 @return domain of the cookie
68 @rtype str
69 """
70 domain = cookie.domain()
71 if domain.startswith("."):
72 domain = domain[1:]
73 return domain
74
75 def __addCookie(self, cookie):
76 """
77 Private method to add a cookie to the tree.
78
79 @param cookie reference to the cookie
80 @type QNetworkCookie
81 """
82 domain = self.__cookieDomain(cookie)
83 if domain in self.__domainDict:
84 itm = QTreeWidgetItem(self.__domainDict[domain])
85 else:
86 newParent = QTreeWidgetItem(self.cookiesTree)
87 newParent.setText(0, domain)
88 newParent.setData(0, self.DomainRole, cookie.domain())
89 self.__domainDict[domain] = newParent
90
91 itm = QTreeWidgetItem(newParent)
92
93 itm.setText(0, cookie.domain())
94 itm.setText(1, bytes(cookie.name()).decode())
95 itm.setData(0, self.CookieRole, cookie)
96
97 @pyqtSlot()
98 def on_addButton_clicked(self):
99 """
100 Private slot to add a new exception.
101 """
102 current = self.cookiesTree.currentItem()
103 if current is None:
104 return
105
106 from .CookiesExceptionsDialog import CookiesExceptionsDialog
107
108 domain = current.text(0)
109 dlg = CookiesExceptionsDialog(self.__cookieJar, self)
110 dlg.setDomainName(domain)
111 dlg.exec_()
112
113 @pyqtSlot()
114 def on_removeButton_clicked(self):
115 """
116 Private slot to remove the selected cookie(s).
117 """
118 current = self.cookiesTree.currentItem()
119 if current is None:
120 return
121
122 if current.childCount() == 0:
123 # single cookie
124 cookie = current.data(0, self.CookieRole)
125 self.__cookieJar.removeCookie(cookie)
126 current.parent().removeChild(current)
127 del current
128 else:
129 cookies = []
130 for row in range(current.childCount() - 1, -1, -1):
131 child = current.child(row)
132 cookies.append(child.data(0, self.CookieRole))
133 current.removeChild(child)
134 del child
135 self.__cookieJar.removeCookies(cookies)
136 index = self.cookiesTree.indexOfTopLevelItem(current)
137 self.cookiesTree.takeTopLevelItem(index)
138 del current
139
140 @pyqtSlot()
141 def on_removeAllButton_clicked(self):
142 """
143 Private slot to remove all cookies.
144 """
145 res = E5MessageBox.yesNo(
146 self,
147 self.tr("Remove All Cookies"),
148 self.tr("""Do you really want to remove all stored cookies?"""))
149 if res:
150 self.__cookieJar.clear()
151 self.__domainDict = {}
152 self.cookiesTree.clear()
153
154 @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem)
155 def on_cookiesTree_currentItemChanged(self, current, previous):
156 """
157 Private slot to handle a change of the current item.
158
159 @param current reference to the current item
160 @type QTreeWidgetItem
161 @param previous reference to the previous current item
162 @type QTreeWidgetItem
163 """
164 self.addButton.setEnabled(current is not None)
165 self.removeButton.setEnabled(current is not None)
166
167 if current is None:
168 return
169
170 if not current.text(1):
171 # it is a cookie domain entry
172 self.domain.setText(self.tr("<no cookie selected>"))
173 self.name.setText(self.tr("<no cookie selected>"))
174 self.path.setText(self.tr("<no cookie selected>"))
175 self.secure.setText(self.tr("<no cookie selected>"))
176 self.expiration.setText(self.tr("<no cookie selected>"))
177 self.value.setText(self.tr("<no cookie selected>"))
178
179 self.removeButton.setText(self.tr("Remove Cookies"))
180 else:
181 # it is a cookie entry
182 cookie = current.data(0, self.CookieRole)
183
184 self.domain.setText(cookie.domain())
185 self.name.setText(bytes(cookie.name()).decode())
186 self.path.setText(cookie.path())
187 if cookie.isSecure():
188 self.secure.setText(self.tr("Secure connections only"))
189 else:
190 self.secure.setText(self.tr("All connections"))
191 if cookie.isSessionCookie():
192 self.expiration.setText(self.tr("Session Cookie"))
193 else:
194 self.expiration.setText(
195 cookie.expirationDate().toString("yyyy-MM-dd HH:mm:ss"))
196 self.value.setText(
197 bytes(QByteArray.fromPercentEncoding(cookie.value())).decode())
198
199 self.removeButton.setText(self.tr("Remove Cookie"))
200
201 @pyqtSlot(str)
202 def on_searchEdit_textChanged(self, txt):
203 """
204 Private slot to search and filter the cookie tree.
205
206 @param txt text to search for
207 @type str
208 """
209 if not txt:
210 for row in range(self.cookiesTree.topLevelItemCount()):
211 self.cookiesTree.topLevelItem(row).setHidden(False)
212 else:
213 for row in range(self.cookiesTree.topLevelItemCount()):
214 text = self.cookiesTree.topLevelItem(row).text(0)
215 self.cookiesTree.topLevelItem(row).setHidden(txt not in text)

eric ide

mercurial