src/eric7/WebBrowser/CookieJar/CookiesDialog.py

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

eric ide

mercurial