src/eric7/WebBrowser/FeaturePermissions/FeaturePermissionsDialog.py

branch
eric7
changeset 11102
ceabb30155b8
parent 11090
f5f5f5803935
child 11105
ec86fc991d28
equal deleted inserted replaced
11101:33079db9e5ba 11102:ceabb30155b8
5 5
6 """ 6 """
7 Module implementing the feature permission dialog. 7 Module implementing the feature permission dialog.
8 """ 8 """
9 9
10 import contextlib
11
10 from PyQt6.QtCore import Qt, pyqtSlot 12 from PyQt6.QtCore import Qt, pyqtSlot
11 from PyQt6.QtWebEngineCore import QWebEnginePage
12 from PyQt6.QtWidgets import QAbstractItemView, QDialog, QTreeWidget, QTreeWidgetItem 13 from PyQt6.QtWidgets import QAbstractItemView, QDialog, QTreeWidget, QTreeWidgetItem
14 try:
15 # Qt 6.8+
16 from PyQt6.QtWebEngineCore import QWebEnginePermission
17 except ImportError:
18 # Qt <6.8
19 from PyQt6.QtWebEngineCore import QWebEnginePage
13 20
14 from eric7.EricGui import EricPixmapCache 21 from eric7.EricGui import EricPixmapCache
22 from eric7.SystemUtilities import QtUtilities
15 23
16 from .Ui_FeaturePermissionsDialog import Ui_FeaturePermissionsDialog 24 from .Ui_FeaturePermissionsDialog import Ui_FeaturePermissionsDialog
17 25
18 26
19 class FeaturePermissionsDialog(QDialog, Ui_FeaturePermissionsDialog): 27 class FeaturePermissionsDialog(QDialog, Ui_FeaturePermissionsDialog):
24 def __init__(self, featurePermissions, parent=None): 32 def __init__(self, featurePermissions, parent=None):
25 """ 33 """
26 Constructor 34 Constructor
27 35
28 @param featurePermissions dictionary with remembered feature 36 @param featurePermissions dictionary with remembered feature
29 permissions 37 permissions (Qt <6.8) or a list of permission objects (Qt 6.8+)
30 @type dict of dict of list 38 @type dict of dict of list or list of QWebEnginePermission
31 @param parent reference to the parent widget 39 @param parent reference to the parent widget
32 @type QWidget 40 @type QWidget
33 """ 41 """
34 super().__init__(parent) 42 super().__init__(parent)
35 self.setupUi(self) 43 self.setupUi(self)
164 self.deskAudVidList, 172 self.deskAudVidList,
165 EricPixmapCache.getIcon("desktopAudioVideoCapture"), 173 EricPixmapCache.getIcon("desktopAudioVideoCapture"),
166 self.tr("Desktop Audio && Video"), 174 self.tr("Desktop Audio && Video"),
167 ) 175 )
168 176
177 if QtUtilities.qVersionTuple() >= (6, 8, 0):
178 # Qt 6.8+
179 self.clipboardList = QTreeWidget()
180 self.clipboardList.setAlternatingRowColors(True)
181 self.clipboardList.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
182 self.clipboardList.setRootIsDecorated(False)
183 self.clipboardList.setItemsExpandable(False)
184 self.clipboardList.setAllColumnsShowFocus(True)
185 self.clipboardList.setObjectName("camList")
186 self.clipboardList.setSortingEnabled(True)
187 self.clipboardList.headerItem().setText(0, self.tr("Host"))
188 self.clipboardList.headerItem().setText(1, self.tr("Permission"))
189 self.tabWidget.addTab(
190 self.clipboardList, EricPixmapCache.getIcon("clipboard"), self.tr("Clipboard")
191 )
192
193 self.localFontsList = QTreeWidget()
194 self.localFontsList.setAlternatingRowColors(True)
195 self.localFontsList.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
196 self.localFontsList.setRootIsDecorated(False)
197 self.localFontsList.setItemsExpandable(False)
198 self.localFontsList.setAllColumnsShowFocus(True)
199 self.localFontsList.setObjectName("camList")
200 self.localFontsList.setSortingEnabled(True)
201 self.localFontsList.headerItem().setText(0, self.tr("Host"))
202 self.localFontsList.headerItem().setText(1, self.tr("Permission"))
203 self.tabWidget.addTab(
204 self.localFontsList, EricPixmapCache.getIcon("font"), self.tr("Local Fonts")
205 )
206
169 self.setTabOrder(self.tabWidget, self.notifList) 207 self.setTabOrder(self.tabWidget, self.notifList)
170 self.setTabOrder(self.notifList, self.geoList) 208 self.setTabOrder(self.notifList, self.geoList)
171 self.setTabOrder(self.geoList, self.micList) 209 self.setTabOrder(self.geoList, self.micList)
172 self.setTabOrder(self.micList, self.camList) 210 self.setTabOrder(self.micList, self.camList)
173 self.setTabOrder(self.camList, self.micCamList) 211 self.setTabOrder(self.camList, self.micCamList)
174 self.setTabOrder(self.micCamList, self.mouseLockList) 212 self.setTabOrder(self.micCamList, self.mouseLockList)
175 self.setTabOrder(self.mouseLockList, self.deskVidList) 213 self.setTabOrder(self.mouseLockList, self.deskVidList)
176 self.setTabOrder(self.deskVidList, self.deskAudVidList) 214 self.setTabOrder(self.deskVidList, self.deskAudVidList)
177 self.setTabOrder(self.deskAudVidList, self.removeButton) 215 if QtUtilities.qVersionTuple() >= (6, 8, 0):
216 # Qt 6.8+
217 self.setTabOrder(self.deskAudVidList, self.clipboardList)
218 self.setTabOrder(self.clipboardList, self.localFontsList)
219 self.setTabOrder(self.localFontsList, self.removeButton)
220 else:
221 self.setTabOrder(self.deskAudVidList, self.removeButton)
178 self.setTabOrder(self.removeButton, self.removeAllButton) 222 self.setTabOrder(self.removeButton, self.removeAllButton)
179 223
180 self.__permissionStrings = { 224 if QtUtilities.qVersionTuple() >= (6, 8, 0):
181 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser: self.tr("Allow"), 225 # Qt 6.8+
182 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser: self.tr("Deny"), 226 self.__permissionStrings = {
183 } 227 QWebEnginePermission.State.Granted: self.tr("Allow"),
184 228 QWebEnginePermission.State.Denied: self.tr("Deny"),
185 self.__permissionsLists = { 229 QWebEnginePermission.State.Ask: self.tr("Always Ask"),
186 QWebEnginePage.Feature.Geolocation: self.geoList, 230 QWebEnginePermission.State.Invalid: self.tr("Invalid"),
187 QWebEnginePage.Feature.MediaAudioCapture: self.micList, 231 }
188 QWebEnginePage.Feature.MediaVideoCapture: self.camList, 232
189 QWebEnginePage.Feature.MediaAudioVideoCapture: self.micCamList, 233 self.__permissionsLists = {
190 QWebEnginePage.Feature.MouseLock: self.mouseLockList, 234 QWebEnginePermission.PermissionType.Geolocation: self.geoList,
191 QWebEnginePage.Feature.DesktopVideoCapture: self.deskVidList, 235 QWebEnginePermission.PermissionType.MediaAudioCapture: self.micList,
192 QWebEnginePage.Feature.DesktopAudioVideoCapture: self.deskAudVidList, 236 QWebEnginePermission.PermissionType.MediaVideoCapture: self.camList,
193 QWebEnginePage.Feature.Notifications: self.notifList, 237 QWebEnginePermission.PermissionType.MediaAudioVideoCapture: self.micCamList,
194 } 238 QWebEnginePermission.PermissionType.MouseLock: self.mouseLockList,
195 239 QWebEnginePermission.PermissionType.DesktopVideoCapture: self.deskVidList,
196 for feature, permissionsList in self.__permissionsLists.items(): 240 QWebEnginePermission.PermissionType.DesktopAudioVideoCapture: self.deskAudVidList,
197 for permission in featurePermissions[feature]: 241 QWebEnginePermission.PermissionType.Notifications: self.notifList,
198 for host in featurePermissions[feature][permission]: 242 QWebEnginePermission.PermissionType.ClipboardReadWrite: self.clipboardList,
243 QWebEnginePermission.PermissionType.LocalFontsAccess: self.localFontsList,
244 }
245
246 for permission in featurePermissions:
247 with contextlib.suppress(KeyError):
248 permissionsList = self.__permissionsLists[permission.permissionType()]
199 itm = QTreeWidgetItem( 249 itm = QTreeWidgetItem(
200 permissionsList, [host, self.__permissionStrings[permission]] 250 permissionsList, [
251 permission.origin().toString(),
252 self.__permissionStrings[permission.state()],
253 ]
201 ) 254 )
202 itm.setData(0, Qt.ItemDataRole.UserRole, permission) 255 itm.setData(0, Qt.ItemDataRole.UserRole, permission)
203 256 else:
257 # Qt <6.8
258 self.__permissionStrings = {
259 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser: self.tr("Allow"),
260 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser: self.tr("Deny"),
261 }
262
263 self.__permissionsLists = {
264 QWebEnginePage.Feature.Geolocation: self.geoList,
265 QWebEnginePage.Feature.MediaAudioCapture: self.micList,
266 QWebEnginePage.Feature.MediaVideoCapture: self.camList,
267 QWebEnginePage.Feature.MediaAudioVideoCapture: self.micCamList,
268 QWebEnginePage.Feature.MouseLock: self.mouseLockList,
269 QWebEnginePage.Feature.DesktopVideoCapture: self.deskVidList,
270 QWebEnginePage.Feature.DesktopAudioVideoCapture: self.deskAudVidList,
271 QWebEnginePage.Feature.Notifications: self.notifList,
272 }
273
274 for feature, permissionsList in self.__permissionsLists.items():
275 for permission in featurePermissions[feature]:
276 for host in featurePermissions[feature][permission]:
277 itm = QTreeWidgetItem(
278 permissionsList, [host, self.__permissionStrings[permission]]
279 )
280 itm.setData(0, Qt.ItemDataRole.UserRole, permission)
281
282 self.__removedPermissions = []
204 self.__previousCurrent = -1 283 self.__previousCurrent = -1
205 self.tabWidget.currentChanged.connect(self.__currentTabChanged) 284 self.tabWidget.currentChanged.connect(self.__currentTabChanged)
206 self.tabWidget.setCurrentIndex(0) 285 self.tabWidget.setCurrentIndex(0)
207 286
208 @pyqtSlot(int) 287 @pyqtSlot(int)
245 """ 324 """
246 currentList = self.tabWidget.currentWidget() 325 currentList = self.tabWidget.currentWidget()
247 for itm in currentList.selectedItems(): 326 for itm in currentList.selectedItems():
248 row = currentList.indexOfTopLevelItem(itm) 327 row = currentList.indexOfTopLevelItem(itm)
249 itm = currentList.takeTopLevelItem(row) 328 itm = currentList.takeTopLevelItem(row)
329 self.__removedPermissions.append(itm.data(0, Qt.ItemDataRole.UserRole))
250 del itm 330 del itm
251 self.__updateButtons() 331 self.__updateButtons()
252 332
253 @pyqtSlot() 333 @pyqtSlot()
254 def on_removeAllButton_clicked(self): 334 def on_removeAllButton_clicked(self):
256 Private slot to remove all entries. 336 Private slot to remove all entries.
257 """ 337 """
258 currentList = self.tabWidget.currentWidget() 338 currentList = self.tabWidget.currentWidget()
259 while currentList.topLevelItemCount() > 0: 339 while currentList.topLevelItemCount() > 0:
260 itm = currentList.takeTopLevelItem(0) # __IGNORE_WARNING__ 340 itm = currentList.takeTopLevelItem(0) # __IGNORE_WARNING__
341 self.__removedPermissions.append(itm.data(0, Qt.ItemDataRole.UserRole))
261 del itm 342 del itm
262 self.__updateButtons() 343 self.__updateButtons()
263 344
264 def getData(self): 345 def getData(self):
265 """ 346 """
266 Public method to retrieve the dialog contents. 347 Public method to retrieve the dialog contents.
267 348
268 @return new feature permission settings 349 @return new feature permission settings
269 @rtype dict of dict of list 350 @rtype dict of dict of list
270 """ 351 """
352 # Qt <6.8
271 featurePermissions = {} 353 featurePermissions = {}
272 for feature, permissionsList in self.__permissionsLists.items(): 354 for feature, permissionsList in self.__permissionsLists.items():
273 featurePermissions[feature] = { 355 featurePermissions[feature] = {
274 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser: [], 356 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser: [],
275 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser: [], 357 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser: [],
279 host = itm.text(0) 361 host = itm.text(0)
280 permission = itm.data(0, Qt.ItemDataRole.UserRole) 362 permission = itm.data(0, Qt.ItemDataRole.UserRole)
281 featurePermissions[feature][permission].append(host) 363 featurePermissions[feature][permission].append(host)
282 364
283 return featurePermissions 365 return featurePermissions
366
367 def persistChanges(self):
368 """
369 Public method to persist the removed permissions.
370 """
371 # Qt 6.8+
372 for permission in self.__removedPermissions:
373 permission.reset()

eric ide

mercurial