WebBrowser/FeaturePermissions/FeaturePermissionsDialog.py

branch
QtWebEngine
changeset 4742
f9d1090f6ab9
parent 4631
5c1a96925da4
child 4904
ea8f13f76d26
equal deleted inserted replaced
4741:f9e1adc69076 4742:f9d1090f6ab9
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2016 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 self.notifList = QTreeWidget()
41 self.notifList.setAlternatingRowColors(True)
42 self.notifList.setSelectionMode(QAbstractItemView.ExtendedSelection)
43 self.notifList.setRootIsDecorated(False)
44 self.notifList.setItemsExpandable(False)
45 self.notifList.setAllColumnsShowFocus(True)
46 self.notifList.setObjectName("notifList")
47 self.notifList.setSortingEnabled(True)
48 self.notifList.headerItem().setText(0, self.tr("Host"))
49 self.notifList.headerItem().setText(1, self.tr("Permission"))
50 self.tabWidget.addTab(
51 self.notifList,
52 UI.PixmapCache.getIcon("notification.png"),
53 self.tr("Notifications"))
54
55 self.geoList = QTreeWidget()
56 self.geoList.setAlternatingRowColors(True)
57 self.geoList.setSelectionMode(QAbstractItemView.ExtendedSelection)
58 self.geoList.setRootIsDecorated(False)
59 self.geoList.setItemsExpandable(False)
60 self.geoList.setAllColumnsShowFocus(True)
61 self.geoList.setObjectName("geoList")
62 self.geoList.setSortingEnabled(True)
63 self.geoList.headerItem().setText(0, self.tr("Host"))
64 self.geoList.headerItem().setText(1, self.tr("Permission"))
65 self.tabWidget.addTab(
66 self.geoList,
67 UI.PixmapCache.getIcon("geolocation.png"),
68 self.tr("Geolocation"))
69
70 self.micList = QTreeWidget()
71 self.micList.setAlternatingRowColors(True)
72 self.micList.setSelectionMode(QAbstractItemView.ExtendedSelection)
73 self.micList.setRootIsDecorated(False)
74 self.micList.setItemsExpandable(False)
75 self.micList.setAllColumnsShowFocus(True)
76 self.micList.setObjectName("micList")
77 self.micList.setSortingEnabled(True)
78 self.micList.headerItem().setText(0, self.tr("Host"))
79 self.micList.headerItem().setText(1, self.tr("Permission"))
80 self.tabWidget.addTab(
81 self.micList,
82 UI.PixmapCache.getIcon("audiocapture.png"),
83 self.tr("Microphone"))
84
85 self.camList = QTreeWidget()
86 self.camList.setAlternatingRowColors(True)
87 self.camList.setSelectionMode(QAbstractItemView.ExtendedSelection)
88 self.camList.setRootIsDecorated(False)
89 self.camList.setItemsExpandable(False)
90 self.camList.setAllColumnsShowFocus(True)
91 self.camList.setObjectName("camList")
92 self.camList.setSortingEnabled(True)
93 self.camList.headerItem().setText(0, self.tr("Host"))
94 self.camList.headerItem().setText(1, self.tr("Permission"))
95 self.tabWidget.addTab(
96 self.camList,
97 UI.PixmapCache.getIcon("camera.png"),
98 self.tr("Camera"))
99
100 self.micCamList = QTreeWidget()
101 self.micCamList.setAlternatingRowColors(True)
102 self.micCamList.setSelectionMode(QAbstractItemView.ExtendedSelection)
103 self.micCamList.setRootIsDecorated(False)
104 self.micCamList.setItemsExpandable(False)
105 self.micCamList.setAllColumnsShowFocus(True)
106 self.micCamList.setObjectName("micCamList")
107 self.micCamList.setSortingEnabled(True)
108 self.micCamList.headerItem().setText(0, self.tr("Host"))
109 self.micCamList.headerItem().setText(1, self.tr("Permission"))
110 self.tabWidget.addTab(
111 self.micCamList,
112 UI.PixmapCache.getIcon("audio-video.png"),
113 self.tr("Microphone && Camera"))
114
115 self.setTabOrder(self.tabWidget, self.notifList)
116 self.setTabOrder(self.notifList, self.geoList)
117 self.setTabOrder(self.geoList, self.micList)
118 self.setTabOrder(self.micList, self.camList)
119 self.setTabOrder(self.camList, self.micCamList)
120 self.setTabOrder(self.micCamList, self.removeButton)
121 self.setTabOrder(self.removeButton, self.removeAllButton)
122
123 self.__permissionStrings = {
124 QWebEnginePage.PermissionGrantedByUser: self.tr("Allow"),
125 QWebEnginePage.PermissionDeniedByUser: self.tr("Deny"),
126 }
127
128 self.__permissionsLists = {
129 # TODO: Qt 5.6
130 ## QWebEnginePage.Notifications: self.notifList,
131 QWebEnginePage.Geolocation: self.geoList,
132 QWebEnginePage.MediaAudioCapture: self.micList,
133 QWebEnginePage.MediaVideoCapture: self.camList,
134 QWebEnginePage.MediaAudioVideoCapture: self.micCamList,
135 }
136
137 for feature, permissionsList in self.__permissionsLists.items():
138 for permission in featurePermissions[feature]:
139 for host in featurePermissions[feature][permission]:
140 itm = QTreeWidgetItem(
141 permissionsList,
142 [host, self.__permissionStrings[permission]])
143 itm.setData(0, Qt.UserRole, permission)
144
145 self.__previousCurrent = -1
146 self.tabWidget.currentChanged.connect(self.__currentTabChanged)
147 self.tabWidget.setCurrentIndex(0)
148
149 @pyqtSlot(int)
150 def __currentTabChanged(self, index):
151 """
152 Private slot handling changes of the selected tab.
153
154 @param index index of the current tab
155 @type int
156 """
157 if self.__previousCurrent >= 0:
158 previousList = self.tabWidget.widget(self.__previousCurrent)
159 previousList.itemSelectionChanged.disconnect(
160 self.__itemSelectionChanged)
161
162 self.__updateButtons()
163
164 currentList = self.tabWidget.currentWidget()
165 currentList.itemSelectionChanged.connect(self.__itemSelectionChanged)
166 self.__previousCurrent = index
167
168 def __updateButtons(self):
169 """
170 Private method to update the buttons.
171 """
172 currentList = self.tabWidget.currentWidget()
173 self.removeAllButton.setEnabled(
174 currentList.topLevelItemCount() > 0)
175 self.removeButton.setEnabled(
176 len(currentList.selectedItems()) > 0)
177
178 @pyqtSlot()
179 def __itemSelectionChanged(self):
180 """
181 Private slot handling changes in the current list of selected items.
182 """
183 self.__updateButtons()
184
185 @pyqtSlot()
186 def on_removeButton_clicked(self):
187 """
188 Private slot to remove selected entries.
189 """
190 currentList = self.tabWidget.currentWidget()
191 for itm in currentList.selectedItems():
192 row = currentList.indexOfTopLevelItem(itm)
193 itm = currentList.takeTopLevelItem(row)
194 del itm
195 self.__updateButtons()
196
197 @pyqtSlot()
198 def on_removeAllButton_clicked(self):
199 """
200 Private slot to remove all entries.
201 """
202 currentList = self.tabWidget.currentWidget()
203 while currentList.topLevelItemCount() > 0:
204 itm = currentList.takeTopLevelItem(0) # __IGNORE_WARNING__
205 del itm
206 self.__updateGeoButtons()
207
208 def getData(self):
209 """
210 Public method to retrieve the dialog contents.
211
212 @return new feature permission settings
213 @rtype dict of dict of list
214 """
215 featurePermissions = {}
216 for feature, permissionsList in self.__permissionsLists.items():
217 featurePermissions[feature] = {
218 QWebEnginePage.PermissionGrantedByUser: [],
219 QWebEnginePage.PermissionDeniedByUser: [],
220 }
221 for row in range(permissionsList.topLevelItemCount()):
222 itm = permissionsList.topLevelItem(row)
223 host = itm.text(0)
224 permission = itm.data(0, Qt.UserRole)
225 featurePermissions[feature][permission].append(host)
226
227 return featurePermissions

eric ide

mercurial