Helpviewer/FlashCookieManager/FlashCookieManagerDialog.py

changeset 4362
1a171c85b275
parent 4361
9eec3a532d59
child 4363
c00061f670c7
--- a/Helpviewer/FlashCookieManager/FlashCookieManagerDialog.py	Tue Aug 11 19:49:05 2015 +0200
+++ b/Helpviewer/FlashCookieManager/FlashCookieManagerDialog.py	Wed Aug 12 20:13:45 2015 +0200
@@ -8,7 +8,9 @@
 """
 
 from PyQt5.QtCore import pyqtSlot, Qt, QPoint, QTimer
-from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QApplication
+from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QApplication, QMenu
+
+from E5Gui import E5MessageBox
 
 from .Ui_FlashCookieManagerDialog import Ui_FlashCookieManagerDialog
 
@@ -42,18 +44,18 @@
     @pyqtSlot()
     def on_whiteList_itemSelectionChanged(self):
         """
-        Slot documentation goes here.
+        Private slot handling the selection of items in the whitelist.
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        enable = len(self.whiteList.selectedItems()) > 0
+        self.removeWhiteButton.setEnabled(enable)
     
     @pyqtSlot()
     def on_blackList_itemSelectionChanged(self):
         """
-        Slot documentation goes here.
+        Private slot handling the selection of items in the blacklist.
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        enable = len(self.blackList.selectedItems()) > 0
+        self.removeBlackButton.setEnabled(enable)
     
     @pyqtSlot()
     def on_removeWhiteButton_clicked(self):
@@ -92,56 +94,139 @@
         """
         Slot documentation goes here.
         """
+        return
         # TODO: not implemented yet
         raise NotImplementedError
     
     @pyqtSlot(QTreeWidgetItem, QTreeWidgetItem)
     def on_cookiesList_currentItemChanged(self, current, previous):
         """
-        Slot documentation goes here.
+        Private slot handling a change of the current cookie item.
+        
+        @param current reference to the current item
+        @type QTreeWidgetItem
+        @param previous reference to the previous item
+        @type QTreeWidgetItem
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
-    
-    @pyqtSlot()
-    def on_cookiesList_itemSelectionChanged(self):
-        """
-        Slot documentation goes here.
-        """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        if current is None:
+            self.removeButton.setEnabled(False)
+            return
+        
+        cookie = current.data(0, Qt.UserRole)
+        if cookie is None:
+            self.nameLabel.setText(self.tr("<no flash cookie selected>"))
+            self.sizeLabel.setText(self.tr("<no flash cookie selected>"))
+            self.originLabel.setText(self.tr("<no flash cookie selected>"))
+            self.modifiedLabel.setText(self.tr("<no flash cookie selected>"))
+            self.contentsEdit.clear()
+            self.pathEdit.clear()
+            self.removeButton.setText(self.tr("Remove Cookie Group"))
+        else:
+            suffix = ""
+            if cookie.path.startswith(
+                self.__manager.flashPlayerDataPath() +
+                    "/macromedia.com/support/flashplayer/sys"):
+                suffix = self.tr(" (settings)")
+            self.nameLabel.setText(
+                self.tr("{0}{1}", "name and suffix")
+                .format(cookie.name, suffix))
+            self.sizeLabel.setText(self.tr("{0} Byte").format(cookie.size))
+            self.originLabel.setText(cookie.origin)
+            self.modifiedLabel.setText(
+                cookie.lastModified.toString("yyyy-MM-dd hh:mm:ss"))
+            self.contentsEdit.setPlainText(cookie.contents)
+            self.pathEdit.setText(cookie.path)
+            self.removeButton.setText(self.tr("Remove Cookie"))
+        self.removeButton.setEnabled(True)
     
     @pyqtSlot(QPoint)
-    def __cookiesListContextMenuRequested(self, point):
+    def __cookiesListContextMenuRequested(self, pos):
+        """
+        Private slot handling the cookies list context menu.
+        
+        @param pos position to show the menu at
+        @type QPoint
         """
-        Slot documentation goes here.
-        """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        itm = self.cookiesList.itemAt(pos)
+        if itm is None:
+            return
+        
+        menu = QMenu()
+        addBlacklistAct = menu.addAction(self.tr("Add to blacklist"))
+        addWhitelistAct = menu.addAction(self.tr("Add to whitelist"))
+        
+        self.cookiesList.setCurrentItem(itm)
+        
+        activatedAction = menu.exec_(
+            self.cookiesList.viewport().mapToGlobal(pos))
+        if itm.childCount() == 0:
+            origin = itm.data(0, Qt.UserRole).origin
+        else:
+            origin = itm.text(0)
+        
+        if activatedAction == addBlacklistAct:
+            self.__addBlacklist(origin)
+        elif activatedAction == addWhitelistAct:
+            self.__addWhitelist(origin)
     
     @pyqtSlot()
     def on_reloadButton_clicked(self):
         """
-        Slot documentation goes here.
+        Private slot handling a press of the reload button.
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        self.refreshView(True)
     
     @pyqtSlot()
     def on_removeAllButton_clicked(self):
         """
-        Slot documentation goes here.
+        Private slot to remove all cookies.
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        ok = E5MessageBox.yesNo(
+            self,
+            self.tr("Remove All"),
+            self.tr("""Do you really want to delete all flash cookies on"""
+                    """ your computer?"""))
+        if ok:
+            cookies = self.__manager.flashCookies()
+            for cookie in cookies:
+                self.__manager.removeCookie(cookie)
+            
+            self.cookiesList.clear()
+            self.__manager.clearNewOrigins()
+            self.__manager.clearCache()
     
     @pyqtSlot()
     def on_removeButton_clicked(self):
         """
-        Slot documentation goes here.
+        Private slot to remove one cookie or a cookie group.
         """
-        # TODO: not implemented yet
-        raise NotImplementedError
+        itm = self.cookiesList.currentItem()
+        if itm is None:
+            return
+        
+        cookie = itm.data(0, Qt.UserRole)
+        if cookie is None:
+            # remove a whole cookie group
+            origin = itm.text(0)
+            cookieList = self.__manager.flashCookies()
+            for fcookie in cookieList:
+                if fcookie.origin == origin:
+                    self.__manager.removeCookie(fcookie)
+            
+            index = self.cookiesList.indexOfTopLevelItem(itm)
+            self.cookiesList.takeTopLevelItem(index)
+        else:
+            self.__manager.removeCookie(cookie)
+            parent = itm.parent()
+            index = parent.indexOfChild(itm)
+            parent.takeChild(index)
+            
+            if parent.childCount() == 0:
+                # remove origin item as well
+                index = self.cookiesList.indexOfTopLevelItem(parent)
+                self.cookiesList.takeTopLevelItem(index)
+                del parent
+        del itm
     
     def refreshView(self, forceReload=False):
         """
@@ -193,7 +278,7 @@
             else:
                 newParent = QTreeWidgetItem(self.cookiesList)
                 newParent.setText(0, cookieOrigin)
-                newParent.setIcon(UI.PixmapCache.getIcon("dirOpen.png"))
+                newParent.setIcon(0, UI.PixmapCache.getIcon("dirOpen.png"))
                 self.cookiesList.addTopLevelItem(newParent)
                 originDict[cookieOrigin] = newParent
                 
@@ -213,7 +298,8 @@
                 itm.setFont(font)
                 itm.parent().setExpanded(True)
             
-            itm.setText(0, cookie.name + suffix)
+            itm.setText(0, self.tr("{0}{1}", "name and suffix").format(
+                cookie.name, suffix))
             itm.setData(0, Qt.UserRole, cookie)
             
             counter += 1
@@ -221,6 +307,10 @@
                 QApplication.processEvents()
                 counter = 0
         
+        self.removeAllButton.setEnabled(
+            self.cookiesList.topLevelItemCount() > 0)
+        self.removeButton.setEnabled(False)
+        
         QApplication.restoreOverrideCursor()
     
     @pyqtSlot()
@@ -233,3 +323,28 @@
         
         self.whiteList.addItems(Preferences.getHelp("FlashCookiesWhitelist"))
         self.blackList.addItems(Preferences.getHelp("FlashCookiesBlacklist"))
+        
+        self.on_whiteList_itemSelectionChanged()
+        self.on_blackList_itemSelectionChanged()
+    
+    def closeEvent(self, evt):
+        """
+        Protected method to handle the close event.
+        
+        @param evt reference to the close event
+        @type QCloseEvent
+        """
+        self.__manager.clearNewOrigins()
+        
+        whiteList = []
+        for row in range(self.whiteList.count()):
+            whiteList.append(self.whiteList.item(row).text())
+        
+        blackList = []
+        for row in range(self.blackList.count()):
+            blackList.append(self.blackList.item(row).text())
+        
+        Preferences.setHelp("FlashCookiesWhitelist", whiteList)
+        Preferences.setHelp("FlashCookiesBlacklist", blackList)
+        
+        evt.accept()

eric ide

mercurial