src/eric7/WebBrowser/FeaturePermissions/FeaturePermissionBar.py

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

eric ide

mercurial