eric6/WebBrowser/FeaturePermissions/FeaturePermissionsDialog.py

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

eric ide

mercurial