WebBrowser/CookieJar/CookiesDialog.py

changeset 5030
b728bb00886e
parent 5029
1ce5e98ebc43
child 5037
b2b37d7c0791
equal deleted inserted replaced
5029:1ce5e98ebc43 5030:b728bb00886e
7 Module implementing a dialog to show all cookies. 7 Module implementing a dialog to show all cookies.
8 """ 8 """
9 9
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, QByteArray
13 QSortFilterProxyModel
14 from PyQt5.QtGui import QFont, QFontMetrics
15 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem 13 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem
16 14
17 ##from .CookieModel import CookieModel 15 from E5Gui import E5MessageBox
18 ## 16
19 from .Ui_CookiesDialog import Ui_CookiesDialog 17 from .Ui_CookiesDialog import Ui_CookiesDialog
20 18
21 19
22 # TODO: Change dialog to use a QTreeWidget and show cookie data on bottom of dialog
23 # TODO: Remove CookieModel, CookieDetailsDialog and related files
24 class CookiesDialog(QDialog, Ui_CookiesDialog): 20 class CookiesDialog(QDialog, Ui_CookiesDialog):
25 """ 21 """
26 Class implementing a dialog to show all cookies. 22 Class implementing a dialog to show all cookies.
27 """ 23 """
28 DomainRole = Qt.UserRole + 1 24 DomainRole = Qt.UserRole + 1
41 self.addButton.setEnabled(False) 37 self.addButton.setEnabled(False)
42 38
43 self.__cookieJar = cookieJar 39 self.__cookieJar = cookieJar
44 40
45 self.__domainDict = {} 41 self.__domainDict = {}
46 ## self.__itemDict = {} # TODO: Maybe get rid of this
47 42
48 for cookie in self.__cookieJar.cookies(): 43 for cookie in self.__cookieJar.cookies():
49 self.__addCookie(cookie) 44 self.__addCookie(cookie)
50 45
51 def __cookieDomain(self, cookie): 46 def __cookieDomain(self, cookie):
81 itm = QTreeWidgetItem(newParent) 76 itm = QTreeWidgetItem(newParent)
82 77
83 itm.setText(0, cookie.domain()) 78 itm.setText(0, cookie.domain())
84 itm.setText(1, bytes(cookie.name()).decode()) 79 itm.setText(1, bytes(cookie.name()).decode())
85 itm.setData(0, self.CookieRole, cookie) 80 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()
161 81
162 @pyqtSlot() 82 @pyqtSlot()
163 def on_addButton_clicked(self): 83 def on_addButton_clicked(self):
164 """ 84 """
165 Private slot to add a new exception. 85 Private slot to add a new exception.
166 """ 86 """
167 # TODO: change this 87 current = self.cookiesTree.currentItem()
168 ## selection = self.cookiesTable.selectionModel().selectedRows() 88 if current is None:
169 ## if len(selection) == 0: 89 return
170 ## return 90
171 ## 91 from .CookiesExceptionsDialog import CookiesExceptionsDialog
172 ## from .CookiesExceptionsDialog import CookiesExceptionsDialog 92
173 ## 93 domain = current.text(0)
174 ## firstSelected = selection[0] 94 dlg = CookiesExceptionsDialog(self.__cookieJar, self)
175 ## domainSelection = firstSelected.sibling(firstSelected.row(), 0) 95 dlg.setDomainName(domain)
176 ## domain = self.__proxyModel.data(domainSelection, Qt.DisplayRole) 96 dlg.exec_()
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 97
196 @pyqtSlot() 98 @pyqtSlot()
197 def on_removeButton_clicked(self): 99 def on_removeButton_clicked(self):
198 """ 100 """
199 Slot documentation goes here. 101 Private slot to remove the selected cookie(s).
200 """ 102 """
201 # TODO: not implemented yet 103 current = self.cookiesTree.currentItem()
202 raise NotImplementedError 104 if current is None:
105 return
106
107 if current.childCount() == 0:
108 # single cookie
109 cookie = current.data(0, self.CookieRole)
110 self.__cookieJar.removeCookie(cookie)
111 current.parent().removeChild(current)
112 del current
113 else:
114 cookies = []
115 for row in range(current.childCount() - 1, -1, -1):
116 child = current.child(row)
117 cookies.append(child.data(0, self.CookieRole))
118 current.removeChild(child)
119 del child
120 self.__cookieJar.removeCookies(cookies)
121 index = self.cookiesTree.indexOfTopLevelItem(current)
122 self.cookiesTree.takeTopLevelItem(index)
123 del current
203 124
204 @pyqtSlot() 125 @pyqtSlot()
205 def on_removeAllButton_clicked(self): 126 def on_removeAllButton_clicked(self):
206 """ 127 """
207 Slot documentation goes here. 128 Private slot to remove all cookies.
208 """ 129 """
209 # TODO: not implemented yet 130 res = E5MessageBox.yesNo(
210 raise NotImplementedError 131 self,
132 self.tr("Remove All Cookies"),
133 self.tr("""Do you really want to remove all stored cookies?"""))
134 if res:
135 self.__cookieJar.clear()
136 self.__domainDict = {}
137 self.cookiesTree.clear()
211 138
212 @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem) 139 @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem)
213 def on_cookiesTree_currentItemChanged(self, current, previous): 140 def on_cookiesTree_currentItemChanged(self, current, previous):
214 """ 141 """
215 Private slot to handle a change of the current item. 142 Private slot to handle a change of the current item.

eric ide

mercurial