WebBrowser/CookieJar/CookiesDialog.py

changeset 5030
b728bb00886e
parent 5029
1ce5e98ebc43
child 5037
b2b37d7c0791
--- a/WebBrowser/CookieJar/CookiesDialog.py	Sun Jul 10 19:41:24 2016 +0200
+++ b/WebBrowser/CookieJar/CookiesDialog.py	Tue Jul 12 12:48:11 2016 +0200
@@ -9,18 +9,14 @@
 
 from __future__ import unicode_literals
 
-from PyQt5.QtCore import pyqtSlot, Qt, QDateTime, QByteArray, \
-    QSortFilterProxyModel
-from PyQt5.QtGui import QFont, QFontMetrics
+from PyQt5.QtCore import pyqtSlot, Qt, QByteArray
 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem
 
-##from .CookieModel import CookieModel
-##
+from E5Gui import E5MessageBox
+
 from .Ui_CookiesDialog import Ui_CookiesDialog
 
 
-# TODO: Change dialog to use a QTreeWidget and show cookie data on bottom of dialog
-# TODO: Remove CookieModel, CookieDetailsDialog and related files
 class CookiesDialog(QDialog, Ui_CookiesDialog):
     """
     Class implementing a dialog to show all cookies.
@@ -43,7 +39,6 @@
         self.__cookieJar = cookieJar
         
         self.__domainDict = {}
-##        self.__itemDict = {}        # TODO: Maybe get rid of this
         
         for cookie in self.__cookieJar.cookies():
             self.__addCookie(cookie)
@@ -83,131 +78,63 @@
         itm.setText(0, cookie.domain())
         itm.setText(1, bytes(cookie.name()).decode())
         itm.setData(0, self.CookieRole, cookie)
-        
-##        self.__itemDict[itm] = cookie
-        
-##        self.removeButton.clicked.connect(self.cookiesTable.removeSelected)
-##        self.removeAllButton.clicked.connect(self.cookiesTable.removeAll)
-##        
-##        self.cookiesTable.verticalHeader().hide()
-##        model = CookieModel(cookieJar, self)
-##        self.__proxyModel = QSortFilterProxyModel(self)
-##        self.__proxyModel.setSourceModel(model)
-##        self.searchEdit.textChanged.connect(
-##            self.__proxyModel.setFilterFixedString)
-##        self.cookiesTable.setModel(self.__proxyModel)
-##        self.cookiesTable.doubleClicked.connect(self.__showCookieDetails)
-##        self.cookiesTable.selectionModel().selectionChanged.connect(
-##            self.__tableSelectionChanged)
-##        self.cookiesTable.model().modelReset.connect(self.__tableModelReset)
-##        
-##        fm = QFontMetrics(QFont())
-##        height = fm.height() + fm.height() // 3
-##        self.cookiesTable.verticalHeader().setDefaultSectionSize(height)
-##        self.cookiesTable.verticalHeader().setMinimumSectionSize(-1)
-##        for section in range(model.columnCount()):
-##            header = self.cookiesTable.horizontalHeader()\
-##                .sectionSizeHint(section)
-##            if section == 0:
-##                header = fm.width("averagebiglonghost.averagedomain.info")
-##            elif section == 1:
-##                header = fm.width("_session_id")
-##            elif section == 4:
-##                header = fm.width(
-##                    QDateTime.currentDateTime().toString(Qt.LocalDate))
-##            buffer = fm.width("mm")
-##            header += buffer
-##            self.cookiesTable.horizontalHeader().resizeSection(section, header)
-##        self.cookiesTable.horizontalHeader().setStretchLastSection(True)
-##        self.cookiesTable.model().sort(
-##            self.cookiesTable.horizontalHeader().sortIndicatorSection(),
-##            Qt.AscendingOrder)
-##        
-##        self.__detailsDialog = None
-##    
-##    def __showCookieDetails(self, index):
-##        """
-##        Private slot to show a dialog with the cookie details.
-##        
-##        @param index index of the entry to show (QModelIndex)
-##        """
-##        if not index.isValid():
-##            return
-##        
-##        cookiesTable = self.sender()
-##        if cookiesTable is None:
-##            return
-##        
-##        model = cookiesTable.model()
-##        row = index.row()
-##        
-##        domain = model.data(model.index(row, 0))
-##        name = model.data(model.index(row, 1))
-##        path = model.data(model.index(row, 2))
-##        secure = model.data(model.index(row, 3))
-##        expires = model.data(model.index(row, 4)).toString("yyyy-MM-dd hh:mm")
-##        data = model.data(model.index(row, 5))
-##        if data is None:
-##            value = ""
-##        else:
-##            value = bytes(QByteArray.fromPercentEncoding(data)).decode()
-##        
-##        if self.__detailsDialog is None:
-##            from .CookieDetailsDialog import CookieDetailsDialog
-##            self.__detailsDialog = CookieDetailsDialog(self)
-##        self.__detailsDialog.setData(domain, name, path, secure, expires,
-##                                     value)
-##        self.__detailsDialog.show()
     
     @pyqtSlot()
     def on_addButton_clicked(self):
         """
         Private slot to add a new exception.
         """
-        # TODO: change this
-##        selection = self.cookiesTable.selectionModel().selectedRows()
-##        if len(selection) == 0:
-##            return
-##        
-##        from .CookiesExceptionsDialog import CookiesExceptionsDialog
-##        
-##        firstSelected = selection[0]
-##        domainSelection = firstSelected.sibling(firstSelected.row(), 0)
-##        domain = self.__proxyModel.data(domainSelection, Qt.DisplayRole)
-##        dlg = CookiesExceptionsDialog(self.__cookieJar, self)
-##        dlg.setDomainName(domain)
-##        dlg.exec_()
-##    
-##    def __tableSelectionChanged(self, selected, deselected):
-##        """
-##        Private slot to handle a change of selected items.
-##        
-##        @param selected selected indexes (QItemSelection)
-##        @param deselected deselected indexes (QItemSelection)
-##        """
-##        self.addButton.setEnabled(len(selected.indexes()) > 0)
-##    
-##    def __tableModelReset(self):
-##        """
-##        Private slot to handle a reset of the cookies table.
-##        """
-##        self.addButton.setEnabled(False)
+        current = self.cookiesTree.currentItem()
+        if current is None:
+            return
+        
+        from .CookiesExceptionsDialog import CookiesExceptionsDialog
+        
+        domain = current.text(0)
+        dlg = CookiesExceptionsDialog(self.__cookieJar, self)
+        dlg.setDomainName(domain)
+        dlg.exec_()
     
     @pyqtSlot()
     def on_removeButton_clicked(self):
         """
-        Slot documentation goes here.
+        Private slot to remove the selected cookie(s).
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        current = self.cookiesTree.currentItem()
+        if current is None:
+            return
+        
+        if current.childCount() == 0:
+            # single cookie
+            cookie = current.data(0, self.CookieRole)
+            self.__cookieJar.removeCookie(cookie)
+            current.parent().removeChild(current)
+            del current
+        else:
+            cookies = []
+            for row in range(current.childCount() - 1, -1, -1):
+                child = current.child(row)
+                cookies.append(child.data(0, self.CookieRole))
+                current.removeChild(child)
+                del child
+            self.__cookieJar.removeCookies(cookies)
+            index = self.cookiesTree.indexOfTopLevelItem(current)
+            self.cookiesTree.takeTopLevelItem(index)
+            del current
     
     @pyqtSlot()
     def on_removeAllButton_clicked(self):
         """
-        Slot documentation goes here.
+        Private slot to remove all cookies.
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        res = E5MessageBox.yesNo(
+            self,
+            self.tr("Remove All Cookies"),
+            self.tr("""Do you really want to remove all stored cookies?"""))
+        if res:
+            self.__cookieJar.clear()
+            self.__domainDict = {}
+            self.cookiesTree.clear()
     
     @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem)
     def on_cookiesTree_currentItemChanged(self, current, previous):

eric ide

mercurial