|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2012 - 2022 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the AdBlock icon for the main window status bar. |
|
8 """ |
|
9 |
|
10 from PyQt6.QtCore import Qt |
|
11 from PyQt6.QtWidgets import QMenu |
|
12 |
|
13 from EricWidgets.EricClickableLabel import EricClickableLabel |
|
14 |
|
15 import UI.PixmapCache |
|
16 |
|
17 |
|
18 class AdBlockIcon(EricClickableLabel): |
|
19 """ |
|
20 Class implementing the AdBlock icon for the main window status bar. |
|
21 """ |
|
22 def __init__(self, parent): |
|
23 """ |
|
24 Constructor |
|
25 |
|
26 @param parent reference to the parent widget |
|
27 @type WebBrowserWindow |
|
28 """ |
|
29 super().__init__(parent) |
|
30 |
|
31 self.__mw = parent |
|
32 self.__menu = QMenu(self.tr("AdBlock")) |
|
33 self.__enabled = False |
|
34 |
|
35 self.setMaximumHeight(16) |
|
36 self.setCursor(Qt.CursorShape.PointingHandCursor) |
|
37 self.setToolTip(self.tr( |
|
38 "AdBlock lets you block unwanted content on web pages.")) |
|
39 |
|
40 self.__menu.aboutToShow.connect(self.__aboutToShowMenu) |
|
41 self.clicked.connect(self.__showMenu) |
|
42 |
|
43 def setEnabled(self, enabled): |
|
44 """ |
|
45 Public slot to set the enabled state. |
|
46 |
|
47 @param enabled enabled state |
|
48 @type bool |
|
49 """ |
|
50 self.__enabled = enabled |
|
51 if enabled: |
|
52 self.currentChanged() |
|
53 else: |
|
54 self.setPixmap( |
|
55 UI.PixmapCache.getPixmap("adBlockPlusDisabled16")) |
|
56 |
|
57 def __aboutToShowMenu(self): |
|
58 """ |
|
59 Private slot to show the context menu. |
|
60 """ |
|
61 self.__menu.clear() |
|
62 |
|
63 manager = self.__mw.adBlockManager() |
|
64 |
|
65 if manager.isEnabled(): |
|
66 act = self.__menu.addAction( |
|
67 UI.PixmapCache.getIcon("adBlockPlusDisabled"), |
|
68 self.tr("Disable AdBlock")) |
|
69 act.triggered.connect(lambda: self.__enableAdBlock(False)) |
|
70 else: |
|
71 act = self.__menu.addAction( |
|
72 UI.PixmapCache.getIcon("adBlockPlus"), |
|
73 self.tr("Enable AdBlock")) |
|
74 act.triggered.connect(lambda: self.__enableAdBlock(True)) |
|
75 self.__menu.addSeparator() |
|
76 if manager.isEnabled() and self.__mw.currentBrowser().url().host(): |
|
77 if self.__isCurrentHostExcepted(): |
|
78 act = self.__menu.addAction( |
|
79 UI.PixmapCache.getIcon("adBlockPlus"), |
|
80 self.tr("Remove AdBlock Exception")) |
|
81 act.triggered.connect(lambda: self.__setException(False)) |
|
82 else: |
|
83 act = self.__menu.addAction( |
|
84 UI.PixmapCache.getIcon("adBlockPlusGreen"), |
|
85 self.tr("Add AdBlock Exception")) |
|
86 act.triggered.connect(lambda: self.__setException(True)) |
|
87 self.__menu.addAction( |
|
88 UI.PixmapCache.getIcon("adBlockPlusGreen"), |
|
89 self.tr("AdBlock Exceptions..."), manager.showExceptionsDialog) |
|
90 self.__menu.addSeparator() |
|
91 self.__menu.addAction( |
|
92 UI.PixmapCache.getIcon("adBlockPlus"), |
|
93 self.tr("AdBlock Configuration..."), manager.showDialog) |
|
94 |
|
95 def menu(self): |
|
96 """ |
|
97 Public method to get a reference to the menu. |
|
98 |
|
99 @return reference to the menu |
|
100 @rtype QMenu |
|
101 """ |
|
102 if self.__enabled: |
|
103 self.__menu.setIcon( |
|
104 UI.PixmapCache.getIcon("adBlockPlus")) |
|
105 else: |
|
106 self.__menu.setIcon( |
|
107 UI.PixmapCache.getIcon("adBlockPlusDisabled")) |
|
108 |
|
109 return self.__menu |
|
110 |
|
111 def __showMenu(self, pos): |
|
112 """ |
|
113 Private slot to show the context menu. |
|
114 |
|
115 @param pos position the context menu should be shown |
|
116 @type QPoint |
|
117 """ |
|
118 self.__menu.exec(pos) |
|
119 |
|
120 def __enableAdBlock(self, enable): |
|
121 """ |
|
122 Private slot to enable or disable AdBlock. |
|
123 |
|
124 @param enable flag indicating the desired enable state |
|
125 @type bool |
|
126 """ |
|
127 self.__mw.adBlockManager().setEnabled(enable) |
|
128 |
|
129 def __isCurrentHostExcepted(self): |
|
130 """ |
|
131 Private method to check, if the host of the current browser is |
|
132 excepted. |
|
133 |
|
134 @return flag indicating an exception |
|
135 @rtype bool |
|
136 """ |
|
137 browser = self.__mw.currentBrowser() |
|
138 if browser is None: |
|
139 return False |
|
140 |
|
141 urlHost = browser.page().url().host() |
|
142 |
|
143 return ( |
|
144 urlHost and |
|
145 self.__mw.adBlockManager().isHostExcepted(urlHost) |
|
146 ) |
|
147 |
|
148 def currentChanged(self): |
|
149 """ |
|
150 Public slot to handle a change of the current browser tab. |
|
151 """ |
|
152 if self.__enabled: |
|
153 if self.__isCurrentHostExcepted(): |
|
154 self.setPixmap( |
|
155 UI.PixmapCache.getPixmap("adBlockPlusGreen16")) |
|
156 else: |
|
157 self.setPixmap(UI.PixmapCache.getPixmap("adBlockPlus16")) |
|
158 |
|
159 def __setException(self, enable): |
|
160 """ |
|
161 Private slot to add or remove the current host from the list of |
|
162 exceptions. |
|
163 |
|
164 @param enable flag indicating to set or remove an exception |
|
165 @type bool |
|
166 """ |
|
167 urlHost = self.__mw.currentBrowser().url().host() |
|
168 if enable: |
|
169 self.__mw.adBlockManager().addException(urlHost) |
|
170 else: |
|
171 self.__mw.adBlockManager().removeException(urlHost) |
|
172 self.currentChanged() |
|
173 |
|
174 def sourceChanged(self, browser, url): |
|
175 """ |
|
176 Public slot to handle URL changes. |
|
177 |
|
178 @param browser reference to the browser |
|
179 @type WebBrowserView |
|
180 @param url new URL |
|
181 @type QUrl |
|
182 """ |
|
183 if browser == self.__mw.currentBrowser(): |
|
184 self.currentChanged() |