src/eric7/WebBrowser/FeaturePermissions/FeaturePermissionsDialog.py

branch
eric7
changeset 9209
b99e7fd55fd3
parent 8881
54e42bc2437a
child 9221
bf71ee032bb4
equal deleted inserted replaced
9208:3fc8dfeb6ebe 9209:b99e7fd55fd3
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2022 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the feature permission dialog.
8 """
9
10 from PyQt6.QtCore import pyqtSlot, Qt
11 from PyQt6.QtWidgets import (
12 QDialog, QTreeWidgetItem, QTreeWidget, QAbstractItemView
13 )
14 from PyQt6.QtWebEngineCore import QWebEnginePage
15
16 import UI.PixmapCache
17
18 from .Ui_FeaturePermissionsDialog import Ui_FeaturePermissionsDialog
19
20
21 class FeaturePermissionsDialog(QDialog, Ui_FeaturePermissionsDialog):
22 """
23 Class implementing the feature permission dialog.
24 """
25 def __init__(self, featurePermissions, parent=None):
26 """
27 Constructor
28
29 @param featurePermissions dictionary with remembered feature
30 permissions
31 @type dict of dict of list
32 @param parent reference to the parent widget
33 @type QWidget
34 """
35 super().__init__(parent)
36 self.setupUi(self)
37
38 # add the various lists
39
40 self.notifList = QTreeWidget()
41 self.notifList.setAlternatingRowColors(True)
42 self.notifList.setSelectionMode(
43 QAbstractItemView.SelectionMode.ExtendedSelection)
44 self.notifList.setRootIsDecorated(False)
45 self.notifList.setItemsExpandable(False)
46 self.notifList.setAllColumnsShowFocus(True)
47 self.notifList.setObjectName("notifList")
48 self.notifList.setSortingEnabled(True)
49 self.notifList.headerItem().setText(0, self.tr("Host"))
50 self.notifList.headerItem().setText(1, self.tr("Permission"))
51 self.tabWidget.addTab(
52 self.notifList,
53 UI.PixmapCache.getIcon("notification"),
54 self.tr("Notifications"))
55
56 self.geoList = QTreeWidget()
57 self.geoList.setAlternatingRowColors(True)
58 self.geoList.setSelectionMode(
59 QAbstractItemView.SelectionMode.ExtendedSelection)
60 self.geoList.setRootIsDecorated(False)
61 self.geoList.setItemsExpandable(False)
62 self.geoList.setAllColumnsShowFocus(True)
63 self.geoList.setObjectName("geoList")
64 self.geoList.setSortingEnabled(True)
65 self.geoList.headerItem().setText(0, self.tr("Host"))
66 self.geoList.headerItem().setText(1, self.tr("Permission"))
67 self.tabWidget.addTab(
68 self.geoList,
69 UI.PixmapCache.getIcon("geolocation"),
70 self.tr("Geolocation"))
71
72 self.micList = QTreeWidget()
73 self.micList.setAlternatingRowColors(True)
74 self.micList.setSelectionMode(
75 QAbstractItemView.SelectionMode.ExtendedSelection)
76 self.micList.setRootIsDecorated(False)
77 self.micList.setItemsExpandable(False)
78 self.micList.setAllColumnsShowFocus(True)
79 self.micList.setObjectName("micList")
80 self.micList.setSortingEnabled(True)
81 self.micList.headerItem().setText(0, self.tr("Host"))
82 self.micList.headerItem().setText(1, self.tr("Permission"))
83 self.tabWidget.addTab(
84 self.micList,
85 UI.PixmapCache.getIcon("audiocapture"),
86 self.tr("Microphone"))
87
88 self.camList = QTreeWidget()
89 self.camList.setAlternatingRowColors(True)
90 self.camList.setSelectionMode(
91 QAbstractItemView.SelectionMode.ExtendedSelection)
92 self.camList.setRootIsDecorated(False)
93 self.camList.setItemsExpandable(False)
94 self.camList.setAllColumnsShowFocus(True)
95 self.camList.setObjectName("camList")
96 self.camList.setSortingEnabled(True)
97 self.camList.headerItem().setText(0, self.tr("Host"))
98 self.camList.headerItem().setText(1, self.tr("Permission"))
99 self.tabWidget.addTab(
100 self.camList,
101 UI.PixmapCache.getIcon("camera"),
102 self.tr("Camera"))
103
104 self.micCamList = QTreeWidget()
105 self.micCamList.setAlternatingRowColors(True)
106 self.micCamList.setSelectionMode(
107 QAbstractItemView.SelectionMode.ExtendedSelection)
108 self.micCamList.setRootIsDecorated(False)
109 self.micCamList.setItemsExpandable(False)
110 self.micCamList.setAllColumnsShowFocus(True)
111 self.micCamList.setObjectName("micCamList")
112 self.micCamList.setSortingEnabled(True)
113 self.micCamList.headerItem().setText(0, self.tr("Host"))
114 self.micCamList.headerItem().setText(1, self.tr("Permission"))
115 self.tabWidget.addTab(
116 self.micCamList,
117 UI.PixmapCache.getIcon("audio-video"),
118 self.tr("Microphone && Camera"))
119
120 self.mouseLockList = QTreeWidget()
121 self.mouseLockList.setAlternatingRowColors(True)
122 self.mouseLockList.setSelectionMode(
123 QAbstractItemView.SelectionMode.ExtendedSelection)
124 self.mouseLockList.setRootIsDecorated(False)
125 self.mouseLockList.setItemsExpandable(False)
126 self.mouseLockList.setAllColumnsShowFocus(True)
127 self.mouseLockList.setObjectName("mouseLockList")
128 self.mouseLockList.setSortingEnabled(True)
129 self.mouseLockList.headerItem().setText(0, self.tr("Host"))
130 self.mouseLockList.headerItem().setText(1, self.tr("Permission"))
131 self.tabWidget.addTab(
132 self.mouseLockList,
133 UI.PixmapCache.getIcon("mouse"),
134 self.tr("Mouse Lock"))
135
136 self.deskVidList = QTreeWidget()
137 self.deskVidList.setAlternatingRowColors(True)
138 self.deskVidList.setSelectionMode(
139 QAbstractItemView.SelectionMode.ExtendedSelection)
140 self.deskVidList.setRootIsDecorated(False)
141 self.deskVidList.setItemsExpandable(False)
142 self.deskVidList.setAllColumnsShowFocus(True)
143 self.deskVidList.setObjectName("deskVidList")
144 self.deskVidList.setSortingEnabled(True)
145 self.deskVidList.headerItem().setText(0, self.tr("Host"))
146 self.deskVidList.headerItem().setText(1, self.tr("Permission"))
147 self.tabWidget.addTab(
148 self.deskVidList,
149 UI.PixmapCache.getIcon("desktopVideoCapture"),
150 self.tr("Desktop Video"))
151
152 self.deskAudVidList = QTreeWidget()
153 self.deskAudVidList.setAlternatingRowColors(True)
154 self.deskAudVidList.setSelectionMode(
155 QAbstractItemView.SelectionMode.ExtendedSelection)
156 self.deskAudVidList.setRootIsDecorated(False)
157 self.deskAudVidList.setItemsExpandable(False)
158 self.deskAudVidList.setAllColumnsShowFocus(True)
159 self.deskAudVidList.setObjectName("deskAudVidList")
160 self.deskAudVidList.setSortingEnabled(True)
161 self.deskAudVidList.headerItem().setText(0, self.tr("Host"))
162 self.deskAudVidList.headerItem().setText(1, self.tr("Permission"))
163 self.tabWidget.addTab(
164 self.deskAudVidList,
165 UI.PixmapCache.getIcon("desktopAudioVideoCapture"),
166 self.tr("Desktop Audio && Video"))
167
168 self.setTabOrder(self.tabWidget, self.notifList)
169 self.setTabOrder(self.notifList, self.geoList)
170 self.setTabOrder(self.geoList, self.micList)
171 self.setTabOrder(self.micList, self.camList)
172 self.setTabOrder(self.camList, self.micCamList)
173 self.setTabOrder(self.micCamList, self.mouseLockList)
174 self.setTabOrder(self.mouseLockList, self.deskVidList)
175 self.setTabOrder(self.deskVidList, self.deskAudVidList)
176 self.setTabOrder(self.deskAudVidList, self.removeButton)
177 self.setTabOrder(self.removeButton, self.removeAllButton)
178
179 self.__permissionStrings = {
180 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser:
181 self.tr("Allow"),
182 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser:
183 self.tr("Deny"),
184 }
185
186 self.__permissionsLists = {
187 QWebEnginePage.Feature.Geolocation: self.geoList,
188 QWebEnginePage.Feature.MediaAudioCapture: self.micList,
189 QWebEnginePage.Feature.MediaVideoCapture: self.camList,
190 QWebEnginePage.Feature.MediaAudioVideoCapture: self.micCamList,
191 QWebEnginePage.Feature.MouseLock: self.mouseLockList,
192 QWebEnginePage.Feature.DesktopVideoCapture: self.deskVidList,
193 QWebEnginePage.Feature.DesktopAudioVideoCapture:
194 self.deskAudVidList,
195 QWebEnginePage.Feature.Notifications: self.notifList,
196 }
197
198 for feature, permissionsList in self.__permissionsLists.items():
199 for permission in featurePermissions[feature]:
200 for host in featurePermissions[feature][permission]:
201 itm = QTreeWidgetItem(
202 permissionsList,
203 [host, self.__permissionStrings[permission]])
204 itm.setData(0, Qt.ItemDataRole.UserRole, permission)
205
206 self.__previousCurrent = -1
207 self.tabWidget.currentChanged.connect(self.__currentTabChanged)
208 self.tabWidget.setCurrentIndex(0)
209
210 @pyqtSlot(int)
211 def __currentTabChanged(self, index):
212 """
213 Private slot handling changes of the selected tab.
214
215 @param index index of the current tab
216 @type int
217 """
218 if self.__previousCurrent >= 0:
219 previousList = self.tabWidget.widget(self.__previousCurrent)
220 previousList.itemSelectionChanged.disconnect(
221 self.__itemSelectionChanged)
222
223 self.__updateButtons()
224
225 currentList = self.tabWidget.currentWidget()
226 currentList.itemSelectionChanged.connect(self.__itemSelectionChanged)
227 self.__previousCurrent = index
228
229 def __updateButtons(self):
230 """
231 Private method to update the buttons.
232 """
233 currentList = self.tabWidget.currentWidget()
234 self.removeAllButton.setEnabled(
235 currentList.topLevelItemCount() > 0)
236 self.removeButton.setEnabled(
237 len(currentList.selectedItems()) > 0)
238
239 @pyqtSlot()
240 def __itemSelectionChanged(self):
241 """
242 Private slot handling changes in the current list of selected items.
243 """
244 self.__updateButtons()
245
246 @pyqtSlot()
247 def on_removeButton_clicked(self):
248 """
249 Private slot to remove selected entries.
250 """
251 currentList = self.tabWidget.currentWidget()
252 for itm in currentList.selectedItems():
253 row = currentList.indexOfTopLevelItem(itm)
254 itm = currentList.takeTopLevelItem(row)
255 del itm
256 self.__updateButtons()
257
258 @pyqtSlot()
259 def on_removeAllButton_clicked(self):
260 """
261 Private slot to remove all entries.
262 """
263 currentList = self.tabWidget.currentWidget()
264 while currentList.topLevelItemCount() > 0:
265 itm = currentList.takeTopLevelItem(0) # __IGNORE_WARNING__
266 del itm
267 self.__updateButtons()
268
269 def getData(self):
270 """
271 Public method to retrieve the dialog contents.
272
273 @return new feature permission settings
274 @rtype dict of dict of list
275 """
276 featurePermissions = {}
277 for feature, permissionsList in self.__permissionsLists.items():
278 featurePermissions[feature] = {
279 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser: [],
280 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser: [],
281 }
282 for row in range(permissionsList.topLevelItemCount()):
283 itm = permissionsList.topLevelItem(row)
284 host = itm.text(0)
285 permission = itm.data(0, Qt.ItemDataRole.UserRole)
286 featurePermissions[feature][permission].append(host)
287
288 return featurePermissions

eric ide

mercurial