"Fossies" - the Fresh Open Source Software Archive

Member "eric6-20.8/eric/eric6/WebBrowser/AdBlock/AdBlockDialog.py" (2 May 2020, 13442 Bytes) of package /linux/misc/eric6-20.8.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "AdBlockDialog.py" see the Fossies "Dox" file reference documentation.

    1 # -*- coding: utf-8 -*-
    2 
    3 # Copyright (c) 2009 - 2020 Detlev Offenbach <detlev@die-offenbachs.de>
    4 #
    5 
    6 """
    7 Module implementing the AdBlock configuration dialog.
    8 """
    9 
   10 
   11 from PyQt5.QtCore import pyqtSlot, Qt, QTimer, QCoreApplication
   12 from PyQt5.QtWidgets import QDialog, QMenu, QToolButton
   13 
   14 from E5Gui import E5MessageBox
   15 
   16 from .Ui_AdBlockDialog import Ui_AdBlockDialog
   17 
   18 import UI.PixmapCache
   19 import Preferences
   20 
   21 
   22 class AdBlockDialog(QDialog, Ui_AdBlockDialog):
   23     """
   24     Class implementing the AdBlock configuration dialog.
   25     """
   26     def __init__(self, manager, parent=None):
   27         """
   28         Constructor
   29         
   30         @param manager reference to the AdBlock manager
   31         @type AdBlockManager
   32         @param parent reference to the parent object
   33         @type QWidget
   34         """
   35         super(AdBlockDialog, self).__init__(parent)
   36         self.setupUi(self)
   37         self.setWindowFlags(Qt.Window)
   38         
   39         self.__manager = manager
   40         
   41         self.iconLabel.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus48"))
   42         
   43         self.updateSpinBox.setValue(
   44             Preferences.getWebBrowser("AdBlockUpdatePeriod"))
   45         
   46         self.useLimitedEasyListCheckBox.setChecked(Preferences.getWebBrowser(
   47             "AdBlockUseLimitedEasyList"))
   48         
   49         self.searchEdit.setInactiveText(self.tr("Search..."))
   50         
   51         self.adBlockGroup.setChecked(self.__manager.isEnabled())
   52         self.__manager.requiredSubscriptionLoaded.connect(self.addSubscription)
   53         self.__manager.enabledChanged.connect(self.__managerEnabledChanged)
   54         
   55         self.__currentTreeWidget = None
   56         self.__currentSubscription = None
   57         self.__loaded = False
   58         
   59         menu = QMenu(self)
   60         menu.aboutToShow.connect(self.__aboutToShowActionMenu)
   61         self.actionButton.setMenu(menu)
   62         self.actionButton.setIcon(UI.PixmapCache.getIcon("adBlockAction"))
   63         self.actionButton.setPopupMode(QToolButton.InstantPopup)
   64         
   65         self.__load()
   66         
   67         self.buttonBox.setFocus()
   68     
   69     def __loadSubscriptions(self):
   70         """
   71         Private slot to load the AdBlock subscription rules.
   72         """
   73         for index in range(self.subscriptionsTabWidget.count()):
   74             tree = self.subscriptionsTabWidget.widget(index)
   75             tree.refresh()
   76     
   77     def __load(self):
   78         """
   79         Private slot to populate the tab widget with subscriptions.
   80         """
   81         if self.__loaded or not self.adBlockGroup.isChecked():
   82             return
   83         
   84         from .AdBlockTreeWidget import AdBlockTreeWidget
   85         for subscription in self.__manager.subscriptions():
   86             tree = AdBlockTreeWidget(subscription, self.subscriptionsTabWidget)
   87             if subscription.isEnabled():
   88                 icon = UI.PixmapCache.getIcon("adBlockPlus")
   89             else:
   90                 icon = UI.PixmapCache.getIcon("adBlockPlusDisabled")
   91             self.subscriptionsTabWidget.addTab(
   92                 tree, icon, subscription.title())
   93         
   94         self.__loaded = True
   95         QCoreApplication.processEvents()
   96         
   97         QTimer.singleShot(50, self.__loadSubscriptions)
   98     
   99     def addSubscription(self, subscription, refresh=True):
  100         """
  101         Public slot adding a subscription to the list.
  102         
  103         @param subscription reference to the subscription to be
  104             added
  105         @type AdBlockSubscription
  106         @param refresh flag indicating to refresh the tree
  107         @type bool
  108         """
  109         from .AdBlockTreeWidget import AdBlockTreeWidget
  110         tree = AdBlockTreeWidget(subscription, self.subscriptionsTabWidget)
  111         index = self.subscriptionsTabWidget.insertTab(
  112             self.subscriptionsTabWidget.count() - 1, tree,
  113             subscription.title())
  114         self.subscriptionsTabWidget.setCurrentIndex(index)
  115         QCoreApplication.processEvents()
  116         if refresh:
  117             tree.refresh()
  118         self.__setSubscriptionEnabled(subscription, True)
  119     
  120     def __aboutToShowActionMenu(self):
  121         """
  122         Private slot to show the actions menu.
  123         """
  124         subscriptionEditable = (
  125             self.__currentSubscription and
  126             self.__currentSubscription.canEditRules()
  127         )
  128         subscriptionRemovable = (
  129             self.__currentSubscription and
  130             self.__currentSubscription.canBeRemoved()
  131         )
  132         subscriptionEnabled = (
  133             self.__currentSubscription and
  134             self.__currentSubscription.isEnabled()
  135         )
  136         
  137         menu = self.actionButton.menu()
  138         menu.clear()
  139         
  140         menu.addAction(
  141             self.tr("Add Rule"), self.__addCustomRule
  142         ).setEnabled(subscriptionEditable)
  143         menu.addAction(
  144             self.tr("Remove Rule"), self.__removeCustomRule
  145         ).setEnabled(subscriptionEditable)
  146         menu.addSeparator()
  147         menu.addAction(
  148             self.tr("Browse Subscriptions..."), self.__browseSubscriptions)
  149         menu.addAction(
  150             self.tr("Remove Subscription"), self.__removeSubscription
  151         ).setEnabled(subscriptionRemovable)
  152         if self.__currentSubscription:
  153             menu.addSeparator()
  154             if subscriptionEnabled:
  155                 txt = self.tr("Disable Subscription")
  156             else:
  157                 txt = self.tr("Enable Subscription")
  158             menu.addAction(txt, self.__switchSubscriptionEnabled)
  159         menu.addSeparator()
  160         menu.addAction(
  161             self.tr("Update Subscription"), self.__updateSubscription
  162         ).setEnabled(not subscriptionEditable)
  163         menu.addAction(
  164             self.tr("Update All Subscriptions"),
  165             self.__updateAllSubscriptions)
  166         menu.addSeparator()
  167         menu.addAction(self.tr("Learn more about writing rules..."),
  168                        self.__learnAboutWritingFilters)
  169     
  170     def addCustomRule(self, filterRule):
  171         """
  172         Public slot to add a custom AdBlock rule.
  173         
  174         @param filterRule filter to be added
  175         @type string
  176         """
  177         self.subscriptionsTabWidget.setCurrentIndex(
  178             self.subscriptionsTabWidget.count() - 1)
  179         self.__currentTreeWidget.addRule(filterRule)
  180     
  181     def __addCustomRule(self):
  182         """
  183         Private slot to add a custom AdBlock rule.
  184         """
  185         self.__currentTreeWidget.addRule()
  186     
  187     def __removeCustomRule(self):
  188         """
  189         Private slot to remove a custom AdBlock rule.
  190         """
  191         self.__currentTreeWidget.removeRule()
  192     
  193     def __updateSubscription(self):
  194         """
  195         Private slot to update the selected subscription.
  196         """
  197         self.__currentSubscription.updateNow()
  198     
  199     def __updateAllSubscriptions(self):
  200         """
  201         Private slot to update all subscriptions.
  202         """
  203         self.__manager.updateAllSubscriptions()
  204     
  205     def __browseSubscriptions(self):
  206         """
  207         Private slot to browse the list of available AdBlock subscriptions.
  208         """
  209         from WebBrowser.WebBrowserWindow import WebBrowserWindow
  210         mw = WebBrowserWindow.mainWindow()
  211         mw.newTab("http://adblockplus.org/en/subscriptions")
  212         mw.raise_()
  213     
  214     def __learnAboutWritingFilters(self):
  215         """
  216         Private slot to show the web page about how to write filters.
  217         """
  218         from WebBrowser.WebBrowserWindow import WebBrowserWindow
  219         mw = WebBrowserWindow.mainWindow()
  220         mw.newTab("http://adblockplus.org/en/filters")
  221         mw.raise_()
  222     
  223     def __removeSubscription(self):
  224         """
  225         Private slot to remove the selected subscription.
  226         """
  227         requiresTitles = []
  228         requiresSubscriptions = (
  229             self.__manager.getRequiresSubscriptions(self.__currentSubscription)
  230         )
  231         for subscription in requiresSubscriptions:
  232             requiresTitles.append(subscription.title())
  233         if requiresTitles:
  234             message = self.tr(
  235                 "<p>Do you really want to remove subscription"
  236                 " <b>{0}</b> and all subscriptions requiring it?</p>"
  237                 "<ul><li>{1}</li></ul>").format(
  238                 self.__currentSubscription.title(),
  239                 "</li><li>".join(requiresTitles))
  240         else:
  241             message = self.tr(
  242                 "<p>Do you really want to remove subscription"
  243                 " <b>{0}</b>?</p>").format(self.__currentSubscription.title())
  244         res = E5MessageBox.yesNo(
  245             self,
  246             self.tr("Remove Subscription"),
  247             message)
  248         
  249         if res:
  250             removeSubscription = self.__currentSubscription
  251             removeTrees = [self.__currentTreeWidget]
  252             for index in range(self.subscriptionsTabWidget.count()):
  253                 tree = self.subscriptionsTabWidget.widget(index)
  254                 if tree.subscription() in requiresSubscriptions:
  255                     removeTrees.append(tree)
  256             for tree in removeTrees:
  257                 self.subscriptionsTabWidget.removeTab(
  258                     self.subscriptionsTabWidget.indexOf(tree))
  259             self.__manager.removeSubscription(removeSubscription)
  260     
  261     def __switchSubscriptionEnabled(self):
  262         """
  263         Private slot to switch the enabled state of the selected subscription.
  264         """
  265         newState = not self.__currentSubscription.isEnabled()
  266         self.__setSubscriptionEnabled(self.__currentSubscription, newState)
  267     
  268     def __setSubscriptionEnabled(self, subscription, enable):
  269         """
  270         Private slot to set the enabled state of a subscription.
  271         
  272         @param subscription subscription to set the state for
  273         @type AdBlockSubscription
  274         @param enable state to set to
  275         @type bool
  276         """
  277         if enable:
  278             # enable required one as well
  279             sub = self.__manager.subscription(subscription.requiresLocation())
  280             requiresSubscriptions = [] if sub is None else [sub]
  281             icon = UI.PixmapCache.getIcon("adBlockPlus")
  282         else:
  283             # disable dependent ones as well
  284             requiresSubscriptions = (
  285                 self.__manager.getRequiresSubscriptions(subscription)
  286             )
  287             icon = UI.PixmapCache.getIcon("adBlockPlusDisabled")
  288         requiresSubscriptions.append(subscription)
  289         for sub in requiresSubscriptions:
  290             sub.setEnabled(enable)
  291         
  292         for index in range(self.subscriptionsTabWidget.count()):
  293             tree = self.subscriptionsTabWidget.widget(index)
  294             if tree.subscription() in requiresSubscriptions:
  295                 self.subscriptionsTabWidget.setTabIcon(
  296                     self.subscriptionsTabWidget.indexOf(tree), icon)
  297     
  298     @pyqtSlot(int)
  299     def on_updateSpinBox_valueChanged(self, value):
  300         """
  301         Private slot to handle changes of the update period.
  302         
  303         @param value update period
  304         @type int
  305         """
  306         if value != Preferences.getWebBrowser("AdBlockUpdatePeriod"):
  307             Preferences.setWebBrowser("AdBlockUpdatePeriod", value)
  308             
  309             from WebBrowser.WebBrowserWindow import WebBrowserWindow
  310             manager = WebBrowserWindow.adBlockManager()
  311             for subscription in manager.subscriptions():
  312                 subscription.checkForUpdate()
  313     
  314     @pyqtSlot(int)
  315     def on_subscriptionsTabWidget_currentChanged(self, index):
  316         """
  317         Private slot handling the selection of another tab.
  318         
  319         @param index index of the new current tab
  320         @type int
  321         """
  322         if index != -1:
  323             self.__currentTreeWidget = (
  324                 self.subscriptionsTabWidget.widget(index)
  325             )
  326             self.__currentSubscription = (
  327                 self.__currentTreeWidget.subscription()
  328             )
  329             
  330             isEasyList = (
  331                 self.__currentSubscription.url().toString().startswith(
  332                     self.__manager.getDefaultSubscriptionUrl())
  333             )
  334             self.useLimitedEasyListCheckBox.setVisible(isEasyList)
  335     
  336     @pyqtSlot(str)
  337     def on_searchEdit_textChanged(self, filterRule):
  338         """
  339         Private slot to set a new filter on the current widget.
  340         
  341         @param filterRule filter to be set
  342         @type str
  343         """
  344         if self.__currentTreeWidget and self.adBlockGroup.isChecked():
  345             self.__currentTreeWidget.filterString(filterRule)
  346     
  347     @pyqtSlot(bool)
  348     def on_adBlockGroup_toggled(self, state):
  349         """
  350         Private slot handling the enabling/disabling of AdBlock.
  351         
  352         @param state state of the toggle
  353         @type bool
  354         """
  355         self.__manager.setEnabled(state)
  356         
  357         if state:
  358             self.__load()
  359     
  360     @pyqtSlot(bool)
  361     def on_useLimitedEasyListCheckBox_clicked(self, checked):
  362         """
  363         Private slot handling the selection of the limited EasyList.
  364         
  365         @param checked flag indicating the state of the check box
  366         @type bool
  367         """
  368         self.__manager.setUseLimitedEasyList(
  369             self.useLimitedEasyListCheckBox.isChecked())
  370     
  371     @pyqtSlot(bool)
  372     def __managerEnabledChanged(self, enabled):
  373         """
  374         Private slot handling a change of the AdBlock manager enabled state.
  375         
  376         @param enabled flag indicating the enabled state
  377         @type bool
  378         """
  379         self.adBlockGroup.setChecked(enabled)