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