|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2019 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the protocol handler manager. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 import json |
|
14 |
|
15 from PyQt5.QtCore import QObject, QUrl |
|
16 from PyQt5.QtWebEngineWidgets import QWebEnginePage |
|
17 |
|
18 import Utilities |
|
19 |
|
20 |
|
21 class ProtocolHandlerManager(QObject): |
|
22 """ |
|
23 Class implementing the protocol handler manager. |
|
24 """ |
|
25 def __init__(self, parent=None): |
|
26 """ |
|
27 Constructor |
|
28 |
|
29 @param parent reference to the parent object |
|
30 @type QObject |
|
31 """ |
|
32 super(ProtocolHandlerManager, self).__init__(parent) |
|
33 |
|
34 self.__protocolHandlers = {} |
|
35 # dictionary of handlers with scheme as key |
|
36 |
|
37 self.__load() |
|
38 |
|
39 def protocolHandler(self, scheme): |
|
40 """ |
|
41 Public method to get the protocol handler URL for a given scheme. |
|
42 |
|
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() |
|
52 |
|
53 def protocolHandlers(self): |
|
54 """ |
|
55 Public method to get the registered protocol handlers. |
|
56 |
|
57 @return dictionary containing the registered protocol handlers |
|
58 @rtype dict |
|
59 """ |
|
60 return {s: QUrl(u) for s, u in self.__protocolHandlers.items()} |
|
61 |
|
62 def addProtocolHandler(self, scheme, url): |
|
63 """ |
|
64 Public method to add a protocol handler for a scheme. |
|
65 |
|
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() |
|
75 |
|
76 def removeProtocolHandler(self, scheme): |
|
77 """ |
|
78 Public method to remove the protocol handler for a given scheme. |
|
79 |
|
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() |
|
87 |
|
88 def __protocolHandlersFileName(self): |
|
89 """ |
|
90 Private method to determine the protocol handlers file name. |
|
91 |
|
92 @return name of the protocol handlers file |
|
93 @rtype str |
|
94 """ |
|
95 return os.path.join( |
|
96 Utilities.getConfigDir(), "web_browser", "protocol_handlers.json") |
|
97 |
|
98 def __load(self): |
|
99 """ |
|
100 Private method to load the registered protocol handlers. |
|
101 """ |
|
102 try: |
|
103 protocolHandlersFile = open(self.__protocolHandlersFileName(), "r") |
|
104 protocolHandlersData = json.load(protocolHandlersFile) |
|
105 protocolHandlersFile.close() |
|
106 |
|
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) |
|
113 except (IOError, OSError): |
|
114 # ignore issues silently |
|
115 pass |
|
116 |
|
117 def __save(self): |
|
118 """ |
|
119 Private method to save the protocol handlers. |
|
120 """ |
|
121 protocolHandlers = {scheme: url.toString() |
|
122 for scheme, url in self.__protocolHandlers.items()} |
|
123 |
|
124 protocolHandlersFile = open(self.__protocolHandlersFileName(), "w") |
|
125 json.dump(protocolHandlers, protocolHandlersFile, indent=2) |
|
126 protocolHandlersFile.close() |
|
127 |
|
128 def __registerHandler(self, scheme, url): |
|
129 """ |
|
130 Private method to register a protocol handler for a scheme. |
|
131 |
|
132 @param scheme scheme of the protocol handler |
|
133 @type str |
|
134 @param url URL of the protocol handler |
|
135 @type QUrl |
|
136 """ |
|
137 urlStr = url.toString().replace("%25s", "%s") |
|
138 |
|
139 page = QWebEnginePage(self) |
|
140 page.loadFinished.connect(page.deleteLater) |
|
141 try: |
|
142 # Qt >= 5.11 |
|
143 page.registerProtocolHandlerRequested.connect( |
|
144 lambda r: r.accept()) |
|
145 except AttributeError: |
|
146 pass |
|
147 page.setHtml( |
|
148 "<script>navigator.registerProtocolHandler('{0}', '{1}', '')" |
|
149 "</script>".format(scheme, urlStr), |
|
150 url) |
|
151 |
|
152 def __unregisterHandler(self, scheme, url): |
|
153 """ |
|
154 Private method to unregister a protocol handler for a scheme. |
|
155 |
|
156 @param scheme scheme of the protocol handler |
|
157 @type str |
|
158 @param url URL of the protocol handler |
|
159 @type QUrl |
|
160 """ |
|
161 urlStr = url.toString().replace("%25s", "%s") |
|
162 |
|
163 page = QWebEnginePage(self) |
|
164 page.loadFinished.connect(page.deleteLater) |
|
165 page.setHtml( |
|
166 "<script>navigator.unregisterProtocolHandler('{0}', '{1}', '')" |
|
167 "</script>".format(scheme, urlStr), |
|
168 url) |
|
169 |
|
170 def showProtocolHandlerManagerDialog(self): |
|
171 """ |
|
172 Public method to show the protocol handler manager dialog. |
|
173 """ |
|
174 from WebBrowser.WebBrowserWindow import WebBrowserWindow |
|
175 from .ProtocolHandlerManagerDialog import ProtocolHandlerManagerDialog |
|
176 |
|
177 dlg = ProtocolHandlerManagerDialog(self, WebBrowserWindow.getWindow()) |
|
178 dlg.open() |