Sat, 31 Dec 2022 16:23:21 +0100
Updated copyright for 2023.
6695 | 1 | # -*- coding: utf-8 -*- |
2 | ||
9653
e67609152c5e
Updated copyright for 2023.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9624
diff
changeset
|
3 | # Copyright (c) 2019 - 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
6695 | 4 | # |
5 | ||
6 | """ | |
7 | Module implementing the protocol handler manager. | |
8 | """ | |
9 | ||
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
10 | import contextlib |
6695 | 11 | import json |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
12 | import os |
6695 | 13 | |
8318
962bce857696
Replaced all imports of PyQt5 to PyQt6 and started to replace code using obsoleted methods and adapt to the PyQt6 enum usage.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8312
diff
changeset
|
14 | from PyQt6.QtCore import QObject, QUrl |
8553
10d31e5ce9e5
First batch of changes for QtWebEngine as of Qt 6.2.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8318
diff
changeset
|
15 | from PyQt6.QtWebEngineCore import QWebEnginePage |
6695 | 16 | |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
17 | from eric7 import Globals |
6695 | 18 | |
19 | ||
20 | class ProtocolHandlerManager(QObject): | |
21 | """ | |
22 | Class implementing the protocol handler manager. | |
23 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
24 | |
6695 | 25 | def __init__(self, parent=None): |
26 | """ | |
27 | Constructor | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
28 | |
6695 | 29 | @param parent reference to the parent object |
30 | @type QObject | |
31 | """ | |
8218
7c09585bd960
Applied some more code simplifications suggested by the new Simplify checker (super(Foo, self) => super()).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7923
diff
changeset
|
32 | super().__init__(parent) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
33 | |
6695 | 34 | self.__protocolHandlers = {} |
35 | # dictionary of handlers with scheme as key | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
36 | |
6695 | 37 | self.__load() |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
38 | |
6695 | 39 | def protocolHandler(self, scheme): |
40 | """ | |
41 | Public method to get the protocol handler URL for a given scheme. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
42 | |
6695 | 43 | @param scheme scheme to look for |
44 | @type str | |
45 | @return protocol handler URL | |
46 | @rtype QUrl | |
47 | """ | |
48 | try: | |
49 | return QUrl(self.__protocolHandlers[scheme]) | |
50 | except KeyError: | |
51 | return QUrl() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
52 | |
6695 | 53 | def protocolHandlers(self): |
54 | """ | |
55 | Public method to get the registered protocol handlers. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
56 | |
6695 | 57 | @return dictionary containing the registered protocol handlers |
58 | @rtype dict | |
59 | """ | |
60 | return {s: QUrl(u) for s, u in self.__protocolHandlers.items()} | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
61 | |
6695 | 62 | def addProtocolHandler(self, scheme, url): |
63 | """ | |
64 | Public method to add a protocol handler for a scheme. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
65 | |
6695 | 66 | @param scheme scheme of the protocol handler |
67 | @type str | |
68 | @param url URL of the protocol handler | |
69 | @type QUrl | |
70 | """ | |
71 | if bool(scheme) and not url.isEmpty(): | |
72 | self.__protocolHandlers[scheme] = url | |
73 | self.__registerHandler(scheme, url) | |
74 | self.__save() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
75 | |
6695 | 76 | def removeProtocolHandler(self, scheme): |
77 | """ | |
78 | Public method to remove the protocol handler for a given scheme. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
79 | |
6695 | 80 | @param scheme scheme to remove |
81 | @type str | |
82 | """ | |
83 | if scheme in self.__protocolHandlers: | |
84 | self.__unregisterHandler(scheme, self.__protocolHandlers[scheme]) | |
85 | del self.__protocolHandlers[scheme] | |
86 | self.__save() | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
87 | |
6695 | 88 | def __protocolHandlersFileName(self): |
89 | """ | |
90 | Private method to determine the protocol handlers file name. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
91 | |
6695 | 92 | @return name of the protocol handlers file |
93 | @rtype str | |
94 | """ | |
95 | return os.path.join( | |
9624
b47dfa7a137d
Refactored the Utilities and Globals modules in order to enhance the maintainability.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9473
diff
changeset
|
96 | Globals.getConfigDir(), "web_browser", "protocol_handlers.json" |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
97 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
98 | |
6695 | 99 | def __load(self): |
100 | """ | |
101 | Private method to load the registered protocol handlers. | |
102 | """ | |
8243
cc717c2ae956
Applied some more code simplifications suggested by the new Simplify checker (Y105: use contextlib.suppress) (batch 2).
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
8218
diff
changeset
|
103 | with contextlib.suppress(OSError): |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
104 | with open(self.__protocolHandlersFileName(), "r") as protocolHandlersFile: |
7785
9978016560ec
Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7781
diff
changeset
|
105 | protocolHandlersData = json.load(protocolHandlersFile) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
106 | |
6695 | 107 | if protocolHandlersData: |
108 | self.__protocolHandlers = {} | |
109 | for scheme, urlStr in protocolHandlersData.items(): | |
110 | url = QUrl(urlStr) | |
111 | self.__protocolHandlers[scheme] = url | |
112 | self.__registerHandler(scheme, url) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
113 | |
6695 | 114 | def __save(self): |
115 | """ | |
116 | Private method to save the protocol handlers. | |
117 | """ | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
118 | protocolHandlers = { |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
119 | scheme: url.toString() for scheme, url in self.__protocolHandlers.items() |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
120 | } |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
121 | |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
122 | with open(self.__protocolHandlersFileName(), "w") as protocolHandlersFile: |
7785
9978016560ec
Changed code to use context manager 'open()' for file operations.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
7781
diff
changeset
|
123 | json.dump(protocolHandlers, protocolHandlersFile, indent=2) |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
124 | |
6695 | 125 | def __registerHandler(self, scheme, url): |
126 | """ | |
127 | Private method to register a protocol handler for a scheme. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
128 | |
6695 | 129 | @param scheme scheme of the protocol handler |
130 | @type str | |
131 | @param url URL of the protocol handler | |
132 | @type QUrl | |
133 | """ | |
134 | urlStr = url.toString().replace("%25s", "%s") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
135 | |
6695 | 136 | page = QWebEnginePage(self) |
137 | page.loadFinished.connect(page.deleteLater) | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
138 | page.registerProtocolHandlerRequested.connect(lambda r: r.accept()) |
6695 | 139 | page.setHtml( |
140 | "<script>navigator.registerProtocolHandler('{0}', '{1}', '')" | |
141 | "</script>".format(scheme, urlStr), | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
142 | url, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
143 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
144 | |
6695 | 145 | def __unregisterHandler(self, scheme, url): |
146 | """ | |
147 | Private method to unregister a protocol handler for a scheme. | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
148 | |
6695 | 149 | @param scheme scheme of the protocol handler |
150 | @type str | |
151 | @param url URL of the protocol handler | |
152 | @type QUrl | |
153 | """ | |
154 | urlStr = url.toString().replace("%25s", "%s") | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
155 | |
6695 | 156 | page = QWebEnginePage(self) |
157 | page.loadFinished.connect(page.deleteLater) | |
158 | page.setHtml( | |
159 | "<script>navigator.unregisterProtocolHandler('{0}', '{1}', '')" | |
160 | "</script>".format(scheme, urlStr), | |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
161 | url, |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
162 | ) |
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
163 | |
6695 | 164 | def showProtocolHandlerManagerDialog(self): |
165 | """ | |
166 | Public method to show the protocol handler manager dialog. | |
167 | """ | |
9413
80c06d472826
Changed the eric7 import statements to include the package name (i.e. eric7) in order to not fiddle with sys.path.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9221
diff
changeset
|
168 | from eric7.WebBrowser.WebBrowserWindow import WebBrowserWindow |
9473
3f23dbf37dbe
Resorted the import statements using isort.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9413
diff
changeset
|
169 | |
6695 | 170 | from .ProtocolHandlerManagerDialog import ProtocolHandlerManagerDialog |
9221
bf71ee032bb4
Reformatted the source code using the 'Black' utility.
Detlev Offenbach <detlev@die-offenbachs.de>
parents:
9209
diff
changeset
|
171 | |
6695 | 172 | dlg = ProtocolHandlerManagerDialog(self, WebBrowserWindow.getWindow()) |
173 | dlg.open() |