eric7/WebBrowser/FeaturePermissions/FeaturePermissionBar.py

branch
eric7
changeset 8312
800c432b34c8
parent 8243
cc717c2ae956
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the feature permission bar widget.
8 """
9
10 import contextlib
11
12 from PyQt5.QtCore import pyqtSlot, QUrl
13 from PyQt5.QtWidgets import QLabel, QHBoxLayout, QPushButton
14 from PyQt5.QtWebEngineWidgets import QWebEnginePage
15
16 from E5Gui.E5AnimatedWidget import E5AnimatedWidget
17
18 import UI.PixmapCache
19
20
21 class FeaturePermissionBar(E5AnimatedWidget):
22 """
23 Class implementing the feature permission bar widget.
24 """
25 DefaultHeight = 30
26
27 def __init__(self, page, origin, feature, manager):
28 """
29 Constructor
30
31 @param page reference to the web page
32 @type QWebView
33 @param origin security origin requesting the feature
34 @type QUrl
35 @param feature requested feature
36 @type QWebPage.Feature
37 @param manager reference to the feature permissions manager
38 @type FeaturePermissionManager
39 """
40 super().__init__(parent=page.view())
41
42 self.__origin = QUrl(origin)
43 self.__feature = feature
44 self.__page = page
45 self.__manager = manager
46
47 self.__permissionFeatureTexts = {
48 QWebEnginePage.Feature.Geolocation:
49 self.tr("{0} wants to use your position."),
50 QWebEnginePage.Feature.MediaAudioCapture:
51 self.tr("{0} wants to use your microphone."),
52 QWebEnginePage.Feature.MediaVideoCapture:
53 self.tr("{0} wants to use your camera."),
54 QWebEnginePage.Feature.MediaAudioVideoCapture:
55 self.tr("{0} wants to use your microphone and camera."),
56 QWebEnginePage.Feature.MouseLock:
57 self.tr("{0} wants to lock your mouse."),
58 QWebEnginePage.Feature.DesktopVideoCapture:
59 self.tr("{0} wants to capture video of your screen."),
60 QWebEnginePage.Feature.DesktopAudioVideoCapture:
61 self.tr("{0} wants to capture audio and video of your"
62 " screen."),
63 }
64 with contextlib.suppress(AttributeError):
65 # this was re-added in Qt 5.13.0
66 self.__permissionFeatureTexts[
67 QWebEnginePage.Feature.Notifications] = self.tr(
68 "{0} wants to use desktop notifications.")
69
70 self.__permissionFeatureIconNames = {
71 QWebEnginePage.Feature.Geolocation: "geolocation",
72 QWebEnginePage.Feature.MediaAudioCapture: "audiocapture",
73 QWebEnginePage.Feature.MediaVideoCapture: "camera",
74 QWebEnginePage.Feature.MediaAudioVideoCapture: "audio-video",
75 QWebEnginePage.Feature.MouseLock: "mouse",
76 QWebEnginePage.Feature.DesktopVideoCapture:
77 "desktopVideoCapture",
78 QWebEnginePage.Feature.DesktopAudioVideoCapture:
79 "desktopAudioVideoCapture",
80 }
81 with contextlib.suppress(AttributeError):
82 # this was re-added in Qt 5.13.0
83 self.__permissionFeatureIconNames[
84 QWebEnginePage.Feature.Notifications] = "notification"
85
86 self.setAutoFillBackground(True)
87 self.__layout = QHBoxLayout()
88 self.setLayout(self.__layout)
89 self.__layout.setContentsMargins(9, 0, 0, 0)
90 self.__iconLabel = QLabel(self)
91 self.__layout.addWidget(self.__iconLabel)
92 self.__messageLabel = QLabel(self)
93 self.__layout.addWidget(self.__messageLabel)
94 self.__layout.addStretch()
95 self.__rememberButton = QPushButton(self.tr("Remember"), self)
96 self.__rememberButton.setCheckable(True)
97 self.__allowButton = QPushButton(self.tr("Allow"), self)
98 self.__denyButton = QPushButton(self.tr("Deny"), self)
99 self.__discardButton = QPushButton(UI.PixmapCache.getIcon("close"),
100 "", self)
101 self.__allowButton.clicked.connect(self.__permissionGranted)
102 self.__denyButton.clicked.connect(self.__permissionDenied)
103 self.__discardButton.clicked.connect(self.__permissionUnknown)
104 self.__layout.addWidget(self.__rememberButton)
105 self.__layout.addWidget(self.__allowButton)
106 self.__layout.addWidget(self.__denyButton)
107 self.__layout.addWidget(self.__discardButton)
108
109 with contextlib.suppress(KeyError):
110 self.__iconLabel.setPixmap(UI.PixmapCache.getPixmap(
111 self.__permissionFeatureIconNames[self.__feature]))
112
113 try:
114 self.__messageLabel.setText(
115 self.__permissionFeatureTexts[self.__feature].format(
116 self.__origin.host()))
117 except KeyError:
118 self.__messageLabel.setText(
119 self.tr("{0} wants to use an unknown feature.").format(
120 self.__origin.host()))
121
122 self.__page.loadStarted.connect(self.hide)
123
124 self.resize(self.__page.view().width(), self.height())
125 self.startAnimation()
126
127 @pyqtSlot()
128 def hide(self):
129 """
130 Public slot to hide the animated widget.
131 """
132 self.__page.loadStarted.disconnect(self.hide)
133 super().hide()
134
135 def __permissionDenied(self):
136 """
137 Private slot handling the user pressing the deny button.
138 """
139 if self.__page is None or self.__manager is None:
140 return
141
142 self.__page.setFeaturePermission(
143 self.__origin, self.__feature,
144 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser)
145
146 if self.__rememberButton.isChecked():
147 self.__manager.rememberFeaturePermission(
148 self.__page.url().host(), self.__feature,
149 QWebEnginePage.PermissionPolicy.PermissionDeniedByUser)
150
151 self.hide()
152
153 def __permissionGranted(self):
154 """
155 Private slot handling the user pressing the allow button.
156 """
157 if self.__page is None or self.__manager is None:
158 return
159
160 self.__page.setFeaturePermission(
161 self.__origin, self.__feature,
162 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser)
163
164 if self.__rememberButton.isChecked():
165 self.__manager.rememberFeaturePermission(
166 self.__page.url().host(), self.__feature,
167 QWebEnginePage.PermissionPolicy.PermissionGrantedByUser)
168
169 self.hide()
170
171 def __permissionUnknown(self):
172 """
173 Private slot handling the user closing the dialog without.
174 """
175 if self.__page is None or self.__manager is None:
176 return
177
178 self.__page.setFeaturePermission(
179 self.__origin, self.__feature,
180 QWebEnginePage.PermissionPolicy.PermissionUnknown)
181
182 self.hide()

eric ide

mercurial