Sat, 02 May 2020 19:10:08 +0200
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
6695 | 1 | # -*- coding: utf-8 -*- |
2 | ||
7360
9190402e4505
Updated copyright for 2020.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7269
diff
changeset
|
3 | # Copyright (c) 2019 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
6695 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing a widget to show some site information. | |
8 | """ | |
9 | ||
10 | ||
11 | from PyQt5.QtCore import pyqtSlot, Qt, QPoint | |
7269
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
12 | from PyQt5.QtWidgets import ( |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
13 | QMenu, QGridLayout, QHBoxLayout, QLabel, QFrame, QSizePolicy, QPushButton, |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
14 | QSpacerItem |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
15 | ) |
6695 | 16 | |
17 | import UI.PixmapCache | |
18 | ||
19 | from WebBrowser.WebBrowserWindow import WebBrowserWindow | |
20 | ||
21 | ||
22 | class SiteInfoWidget(QMenu): | |
23 | """ | |
24 | Class implementing a widget to show SSL certificate infos. | |
25 | """ | |
26 | def __init__(self, browser, parent=None): | |
27 | """ | |
28 | Constructor | |
29 | ||
30 | @param browser reference to the browser view | |
31 | @type WebBrowserView | |
32 | @param parent reference to the parent object | |
33 | @type QWidget | |
34 | """ | |
35 | super(SiteInfoWidget, self).__init__(parent) | |
36 | ||
37 | self.__browser = browser | |
38 | url = browser.url() | |
39 | ||
40 | self.setMinimumWidth(400) | |
41 | ||
42 | layout = QGridLayout(self) | |
43 | rows = 0 | |
44 | ||
45 | titleLabel = QLabel(self) | |
46 | titleLabel.setText(self.tr("<b>Site {0}</b>").format(url.host())) | |
47 | layout.addWidget(titleLabel, rows, 0, 1, -1, Qt.AlignCenter) | |
48 | rows += 1 | |
49 | ||
50 | line = QFrame(self) | |
51 | line.setLineWidth(1) | |
52 | line.setFrameStyle(QFrame.HLine | QFrame.Sunken) | |
53 | layout.addWidget(line, rows, 0, 1, -1) | |
54 | rows += 1 | |
55 | ||
56 | secureIcon = QLabel() | |
57 | layout.addWidget(secureIcon, rows, 0, Qt.AlignCenter) | |
58 | secureLabel = QLabel() | |
59 | secureLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) | |
60 | layout.addWidget(secureLabel, rows, 1) | |
61 | if url.scheme() in ["https"]: | |
7565
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
62 | if WebBrowserWindow.networkManager().isInsecureHost(url.host()): |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
63 | secureLabel.setText( |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
64 | self.tr("Your connection to this site " |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
65 | "<b>may not be secure</b>.")) |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
66 | secureIcon.setPixmap( |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
67 | UI.PixmapCache.getPixmap("securityMedium")) |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
68 | else: |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
69 | secureLabel.setText( |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
70 | self.tr("Your connection to this site is <b>secure</b>.")) |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
71 | secureIcon.setPixmap( |
928373562e36
Implemented a preliminary fix for issue 328 showing the user an indication, if he made an exception for a certificate issue of a host.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7533
diff
changeset
|
72 | UI.PixmapCache.getPixmap("securityHigh")) |
6695 | 73 | else: |
74 | secureLabel.setText( | |
75 | self.tr("Your connection to this site is <b>not secure</b>.")) | |
76 | secureIcon.setPixmap( | |
7533
88261c96484b
Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
77 | UI.PixmapCache.getPixmap("securityLow")) |
6695 | 78 | rows += 1 |
79 | ||
80 | visits = WebBrowserWindow.historyManager().siteVisitsCount( | |
81 | url.scheme(), url.host()) | |
82 | historyIcon = QLabel() | |
83 | layout.addWidget(historyIcon, rows, 0, Qt.AlignCenter) | |
84 | historyLabel = QLabel() | |
85 | historyLabel.setSizePolicy(QSizePolicy.Expanding, | |
86 | QSizePolicy.Preferred) | |
87 | layout.addWidget(historyLabel, rows, 1) | |
88 | if visits > 3: | |
89 | historyLabel.setText( | |
90 | self.tr("This is your <b>{0}.</b> visit of this site.") | |
91 | .format(visits)) | |
92 | historyIcon.setPixmap( | |
7533
88261c96484b
Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
93 | UI.PixmapCache.getPixmap("flagGreen")) |
6695 | 94 | elif visits == 0: |
95 | historyLabel.setText( | |
96 | self.tr("You have <b>never</b> visited this site before.") | |
97 | .format(visits)) | |
98 | historyIcon.setPixmap( | |
7533
88261c96484b
Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
99 | UI.PixmapCache.getPixmap("flagBlack")) |
6695 | 100 | else: |
101 | historyIcon.setPixmap( | |
7533
88261c96484b
Removed the '.png' extension from all call to get an icon or a pixmap from the PixmapCache because this is not needed anymore.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7360
diff
changeset
|
102 | UI.PixmapCache.getPixmap("flagYellow")) |
6695 | 103 | if visits == 1: |
104 | visitStr = self.tr("first") | |
105 | elif visits == 2: | |
106 | visitStr = self.tr("second") | |
107 | else: | |
108 | visitStr = self.tr("third") | |
109 | historyLabel.setText( | |
110 | self.tr("This is your <b>{0}</b> visit of this site.") | |
111 | .format(visitStr)) | |
112 | rows += 1 | |
113 | ||
114 | line = QFrame(self) | |
115 | line.setLineWidth(1) | |
116 | line.setFrameStyle(QFrame.HLine | QFrame.Sunken) | |
117 | layout.addWidget(line, rows, 0, 1, -1) | |
118 | rows += 1 | |
119 | ||
120 | page = self.__browser.page() | |
121 | scheme = page.registerProtocolHandlerRequestScheme() | |
7269
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
122 | registeredUrl = ( |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
123 | WebBrowserWindow.protocolHandlerManager().protocolHandler(scheme) |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
124 | ) |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
125 | if ( |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
126 | bool(scheme) and |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
127 | registeredUrl != page.registerProtocolHandlerRequestUrl() |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
128 | ): |
6695 | 129 | horizontalLayout = QHBoxLayout() |
130 | protocolHandlerLabel = QLabel( | |
131 | self.tr("Register as <b>{0}</b> links handler.") | |
132 | .format(scheme), self) | |
133 | protocolHandlerLabel.setSizePolicy(QSizePolicy.Expanding, | |
134 | QSizePolicy.Preferred) | |
135 | ||
136 | horizontalLayout.addWidget(protocolHandlerLabel) | |
137 | protocolHandlerButton = QPushButton(self.tr("Register"), self) | |
138 | horizontalLayout.addWidget(protocolHandlerButton) | |
139 | protocolHandlerButton.clicked.connect( | |
140 | self.__registerProtocolHandler) | |
141 | layout.addLayout(horizontalLayout, rows, 0, 1, -1) | |
142 | rows += 1 | |
143 | ||
144 | protocolHandlerLine = QFrame(self) | |
145 | protocolHandlerLine.setLineWidth(1) | |
146 | protocolHandlerLine.setFrameStyle(QFrame.HLine | QFrame.Sunken) | |
147 | layout.addWidget(protocolHandlerLine, rows, 0, 1, -1) | |
148 | rows += 1 | |
149 | ||
150 | horizontalLayout = QHBoxLayout() | |
151 | spacerItem = QSpacerItem( | |
152 | 40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) | |
153 | horizontalLayout.addItem(spacerItem) | |
154 | moreButton = QPushButton(self.tr("More..."), self) | |
155 | horizontalLayout.addWidget(moreButton) | |
156 | moreButton.clicked.connect(self.__showSiteInfo) | |
157 | layout.addLayout(horizontalLayout, rows, 0, 1, -1) | |
158 | ||
159 | def showAt(self, pos): | |
160 | """ | |
161 | Public method to show the widget. | |
162 | ||
163 | @param pos position to show at | |
164 | @type QPoint | |
165 | """ | |
166 | self.adjustSize() | |
167 | xpos = pos.x() - self.width() // 2 | |
168 | if xpos < 0: | |
169 | xpos = 10 | |
170 | p = QPoint(xpos, pos.y() + 10) | |
171 | self.move(p) | |
172 | self.show() | |
173 | ||
174 | def accept(self): | |
175 | """ | |
176 | Public method to accept the widget. | |
177 | """ | |
178 | self.close() | |
179 | ||
180 | @pyqtSlot() | |
181 | def __showSiteInfo(self): | |
182 | """ | |
183 | Private slot to show the site info dialog. | |
184 | """ | |
185 | from .SiteInfoDialog import SiteInfoDialog | |
186 | siteinfoDialog = SiteInfoDialog( | |
187 | self.__browser, self.__browser.mainWindow()) | |
188 | siteinfoDialog.show() | |
189 | ||
190 | @pyqtSlot() | |
191 | def __registerProtocolHandler(self): | |
192 | """ | |
193 | Private slot to register a protocol handler. | |
194 | """ | |
195 | self.close() | |
196 | page = self.__browser.page() | |
197 | WebBrowserWindow.protocolHandlerManager().addProtocolHandler( | |
198 | page.registerProtocolHandlerRequestScheme(), | |
199 | page.registerProtocolHandlerRequestUrl()) |