Helpviewer/FeaturePermissions/FeaturePermissionBar.py

changeset 4355
40ec6bef4c22
child 4356
975993ebd7fb
equal deleted inserted replaced
4354:bc2b247a75df 4355:40ec6bef4c22
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2015 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the feature permission bar widget.
8 """
9
10 from __future__ import unicode_literals
11
12 from PyQt5.QtCore import pyqtSlot
13 from PyQt5.QtWidgets import QLabel, QHBoxLayout, QPushButton, QCheckBox
14 from PyQt5.QtWebKitWidgets import QWebPage
15
16 from E5Gui.E5AnimatedWidget import E5AnimatedWidget
17
18 import Helpviewer
19
20 import UI.PixmapCache
21
22
23 class FeaturePermissionBar(E5AnimatedWidget):
24 """
25 Class implementing the feature permission bar widget.
26 """
27 DefaultHeight = 30
28
29 def __init__(self, view, frame, feature):
30 """
31 Constructor
32
33 @param view reference to the web view
34 @type QWebView
35 @param frame frame sending the request
36 @type QWebFrame
37 @param feature requested feature
38 @type QWebPage.Feature
39 """
40 super(FeaturePermissionBar, self).__init__(parent=view)
41
42 self.__messageLabel = QLabel(self)
43
44 self.__frame = frame
45 self.__feature = feature
46 self.__view = view
47
48 self.__permissionFeatureTexts = {
49 QWebPage.Notifications:
50 self.tr("{0} wants to use desktop notifications."),
51 QWebPage.Geolocation:
52 self.tr("{0} wants to use your position.")
53 }
54
55 self.setAutoFillBackground(True)
56 self.__layout = QHBoxLayout()
57 self.setLayout(self.__layout)
58 self.__layout.setContentsMargins(self.DefaultHeight, 0, 0, 0)
59 self.__layout.addWidget(self.__messageLabel)
60 self.__layout.addStretch()
61 self.__rememberCheckBox = QCheckBox(self.tr("Remember"), self)
62 self.__layout.addWidget(self.__rememberCheckBox)
63 self.__allowButton = QPushButton(self.tr("Allow"), self)
64 self.__denyButton = QPushButton(self.tr("Deny"), self)
65 self.__discardButton = QPushButton(UI.PixmapCache.getIcon("close.png"),
66 "", self)
67 self.__allowButton.clicked.connect(self.__permissionGranted)
68 self.__denyButton.clicked.connect(self.__permissionDenied)
69 self.__discardButton.clicked.connect(self.__permissionUnknown)
70 self.__layout.addWidget(self.__allowButton)
71 self.__layout.addWidget(self.__denyButton)
72 self.__layout.addWidget(self.__discardButton)
73
74 try:
75 self.__messageLabel.setText(
76 self.__permissionFeatureTexts[self.__feature].format(
77 self.__frame.securityOrigin().host()))
78 except KeyError:
79 self.__messageLabel.setText(
80 self.tr("{0} wants to use an unknown feature.").format(
81 self.__frame.securityOrigin().host()))
82
83 self.__view.page().loadStarted.connect(self.hide)
84
85 self.resize(view.width(), self.height())
86 self.startAnimation()
87
88 @pyqtSlot()
89 def hide(self):
90 """
91 Public slot to hide the animated widget.
92 """
93 self.__view.page().loadStarted.disconnect(self.hide)
94 super(FeaturePermissionBar, self).hide()
95
96 def __permissionDenied(self):
97 """
98 Private slot handling the user pressing the deny button.
99 """
100 if self.__frame is None or self.__frame.page() is None:
101 return
102
103 page = self.__frame.page()
104 page.setFeaturePermission(self.__frame, self.__feature,
105 QWebPage.PermissionDeniedByUser)
106
107 if self.__rememberCheckBox.isChecked():
108 Helpviewer.HelpWindow.HelpWindow.featurePermissionManager()\
109 .rememberFeaturePermission(page.url().host(), self.__feature,
110 QWebPage.PermissionDeniedByUser)
111
112 self.hide()
113
114 def __permissionGranted(self):
115 """
116 Private slot handling the user pressing the allow button.
117 """
118 if self.__frame is None or self.__frame.page() is None:
119 return
120
121 page = self.__frame.page()
122 page.setFeaturePermission(self.__frame, self.__feature,
123 QWebPage.PermissionGrantedByUser)
124
125 if self.__rememberCheckBox.isChecked():
126 Helpviewer.HelpWindow.HelpWindow.featurePermissionManager()\
127 .rememberFeaturePermission(page.url().host(), self.__feature,
128 QWebPage.PermissionGrantedByUser)
129
130 self.hide()
131
132 def __permissionUnknown(self):
133 """
134 Private slot handling the user closing the dialog without.
135 """
136 if self.__frame is None or self.__frame.page() is None:
137 return
138
139 page = self.__frame.page()
140 page.setFeaturePermission(self.__frame, self.__feature,
141 QWebPage.PermissionUnknown)
142 self.hide()

eric ide

mercurial