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