Mon, 12 Oct 2020 19:28:42 +0200
Performed some more code cleanup.
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 | 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())) | |
46 | layout.addWidget(titleLabel, rows, 0, 1, -1, Qt.AlignCenter) | |
47 | rows += 1 | |
48 | ||
49 | line = QFrame(self) | |
50 | line.setLineWidth(1) | |
51 | line.setFrameStyle(QFrame.HLine | QFrame.Sunken) | |
52 | layout.addWidget(line, rows, 0, 1, -1) | |
53 | rows += 1 | |
54 | ||
55 | secureIcon = QLabel() | |
56 | layout.addWidget(secureIcon, rows, 0, Qt.AlignCenter) | |
57 | secureLabel = QLabel() | |
58 | secureLabel.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred) | |
59 | layout.addWidget(secureLabel, rows, 1) | |
60 | 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
|
61 | 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
|
62 | 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
|
63 | 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
|
64 | "<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
|
65 | 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
|
66 | 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
|
67 | 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
|
68 | 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
|
69 | 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
|
70 | 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
|
71 | UI.PixmapCache.getPixmap("securityHigh")) |
6695 | 72 | else: |
73 | secureLabel.setText( | |
74 | self.tr("Your connection to this site is <b>not secure</b>.")) | |
75 | 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
|
76 | UI.PixmapCache.getPixmap("securityLow")) |
6695 | 77 | rows += 1 |
78 | ||
79 | visits = WebBrowserWindow.historyManager().siteVisitsCount( | |
80 | url.scheme(), url.host()) | |
81 | historyIcon = QLabel() | |
82 | layout.addWidget(historyIcon, rows, 0, Qt.AlignCenter) | |
83 | historyLabel = QLabel() | |
84 | historyLabel.setSizePolicy(QSizePolicy.Expanding, | |
85 | QSizePolicy.Preferred) | |
86 | layout.addWidget(historyLabel, rows, 1) | |
87 | if visits > 3: | |
88 | historyLabel.setText( | |
89 | self.tr("This is your <b>{0}.</b> visit of this site.") | |
90 | .format(visits)) | |
91 | 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
|
92 | UI.PixmapCache.getPixmap("flagGreen")) |
6695 | 93 | elif visits == 0: |
94 | historyLabel.setText( | |
95 | self.tr("You have <b>never</b> visited this site before.") | |
96 | .format(visits)) | |
97 | 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
|
98 | UI.PixmapCache.getPixmap("flagBlack")) |
6695 | 99 | else: |
100 | 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
|
101 | UI.PixmapCache.getPixmap("flagYellow")) |
6695 | 102 | if visits == 1: |
103 | visitStr = self.tr("first") | |
104 | elif visits == 2: | |
105 | visitStr = self.tr("second") | |
106 | else: | |
107 | visitStr = self.tr("third") | |
108 | historyLabel.setText( | |
109 | self.tr("This is your <b>{0}</b> visit of this site.") | |
110 | .format(visitStr)) | |
111 | rows += 1 | |
112 | ||
113 | line = QFrame(self) | |
114 | line.setLineWidth(1) | |
115 | line.setFrameStyle(QFrame.HLine | QFrame.Sunken) | |
116 | layout.addWidget(line, rows, 0, 1, -1) | |
117 | rows += 1 | |
118 | ||
119 | page = self.__browser.page() | |
120 | scheme = page.registerProtocolHandlerRequestScheme() | |
7269
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
121 | registeredUrl = ( |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
122 | WebBrowserWindow.protocolHandlerManager().protocolHandler(scheme) |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
123 | ) |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
124 | if ( |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
125 | bool(scheme) and |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
126 | registeredUrl != page.registerProtocolHandlerRequestUrl() |
0c63ea7f94bd
Continued to resolve code style issue M841.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7229
diff
changeset
|
127 | ): |
6695 | 128 | horizontalLayout = QHBoxLayout() |
129 | protocolHandlerLabel = QLabel( | |
130 | self.tr("Register as <b>{0}</b> links handler.") | |
131 | .format(scheme), self) | |
132 | protocolHandlerLabel.setSizePolicy(QSizePolicy.Expanding, | |
133 | QSizePolicy.Preferred) | |
134 | ||
135 | horizontalLayout.addWidget(protocolHandlerLabel) | |
136 | protocolHandlerButton = QPushButton(self.tr("Register"), self) | |
137 | horizontalLayout.addWidget(protocolHandlerButton) | |
138 | protocolHandlerButton.clicked.connect( | |
139 | self.__registerProtocolHandler) | |
140 | layout.addLayout(horizontalLayout, rows, 0, 1, -1) | |
141 | rows += 1 | |
142 | ||
143 | protocolHandlerLine = QFrame(self) | |
144 | protocolHandlerLine.setLineWidth(1) | |
145 | protocolHandlerLine.setFrameStyle(QFrame.HLine | QFrame.Sunken) | |
146 | layout.addWidget(protocolHandlerLine, rows, 0, 1, -1) | |
147 | rows += 1 | |
148 | ||
149 | horizontalLayout = QHBoxLayout() | |
150 | spacerItem = QSpacerItem( | |
151 | 40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum) | |
152 | horizontalLayout.addItem(spacerItem) | |
153 | moreButton = QPushButton(self.tr("More..."), self) | |
154 | horizontalLayout.addWidget(moreButton) | |
155 | moreButton.clicked.connect(self.__showSiteInfo) | |
156 | layout.addLayout(horizontalLayout, rows, 0, 1, -1) | |
157 | ||
158 | def showAt(self, pos): | |
159 | """ | |
160 | Public method to show the widget. | |
161 | ||
162 | @param pos position to show at | |
163 | @type QPoint | |
164 | """ | |
165 | self.adjustSize() | |
166 | xpos = pos.x() - self.width() // 2 | |
167 | if xpos < 0: | |
168 | xpos = 10 | |
169 | p = QPoint(xpos, pos.y() + 10) | |
170 | self.move(p) | |
171 | self.show() | |
172 | ||
173 | def accept(self): | |
174 | """ | |
175 | Public method to accept the widget. | |
176 | """ | |
177 | self.close() | |
178 | ||
179 | @pyqtSlot() | |
180 | def __showSiteInfo(self): | |
181 | """ | |
182 | Private slot to show the site info dialog. | |
183 | """ | |
184 | from .SiteInfoDialog import SiteInfoDialog | |
185 | siteinfoDialog = SiteInfoDialog( | |
186 | self.__browser, self.__browser.mainWindow()) | |
187 | siteinfoDialog.show() | |
188 | ||
189 | @pyqtSlot() | |
190 | def __registerProtocolHandler(self): | |
191 | """ | |
192 | Private slot to register a protocol handler. | |
193 | """ | |
194 | self.close() | |
195 | page = self.__browser.page() | |
196 | WebBrowserWindow.protocolHandlerManager().addProtocolHandler( | |
197 | page.registerProtocolHandlerRequestScheme(), | |
198 | page.registerProtocolHandlerRequestUrl()) |