eric7/WebBrowser/Network/ProtocolHandlerManager.py

branch
eric7
changeset 8312
800c432b34c8
parent 8243
cc717c2ae956
child 8318
962bce857696
equal deleted inserted replaced
8311:4e8b98454baa 8312:800c432b34c8
1 # -*- coding: utf-8 -*-
2
3 # Copyright (c) 2019 - 2021 Detlev Offenbach <detlev@die-offenbachs.de>
4 #
5
6 """
7 Module implementing the protocol handler manager.
8 """
9
10 import os
11 import json
12 import contextlib
13
14 from PyQt5.QtCore import QObject, QUrl
15 from PyQt5.QtWebEngineWidgets import QWebEnginePage
16
17 import Utilities
18
19
20 class ProtocolHandlerManager(QObject):
21 """
22 Class implementing the protocol handler manager.
23 """
24 def __init__(self, parent=None):
25 """
26 Constructor
27
28 @param parent reference to the parent object
29 @type QObject
30 """
31 super().__init__(parent)
32
33 self.__protocolHandlers = {}
34 # dictionary of handlers with scheme as key
35
36 self.__load()
37
38 def protocolHandler(self, scheme):
39 """
40 Public method to get the protocol handler URL for a given scheme.
41
42 @param scheme scheme to look for
43 @type str
44 @return protocol handler URL
45 @rtype QUrl
46 """
47 try:
48 return QUrl(self.__protocolHandlers[scheme])
49 except KeyError:
50 return QUrl()
51
52 def protocolHandlers(self):
53 """
54 Public method to get the registered protocol handlers.
55
56 @return dictionary containing the registered protocol handlers
57 @rtype dict
58 """
59 return {s: QUrl(u) for s, u in self.__protocolHandlers.items()}
60
61 def addProtocolHandler(self, scheme, url):
62 """
63 Public method to add a protocol handler for a scheme.
64
65 @param scheme scheme of the protocol handler
66 @type str
67 @param url URL of the protocol handler
68 @type QUrl
69 """
70 if bool(scheme) and not url.isEmpty():
71 self.__protocolHandlers[scheme] = url
72 self.__registerHandler(scheme, url)
73 self.__save()
74
75 def removeProtocolHandler(self, scheme):
76 """
77 Public method to remove the protocol handler for a given scheme.
78
79 @param scheme scheme to remove
80 @type str
81 """
82 if scheme in self.__protocolHandlers:
83 self.__unregisterHandler(scheme, self.__protocolHandlers[scheme])
84 del self.__protocolHandlers[scheme]
85 self.__save()
86
87 def __protocolHandlersFileName(self):
88 """
89 Private method to determine the protocol handlers file name.
90
91 @return name of the protocol handlers file
92 @rtype str
93 """
94 return os.path.join(
95 Utilities.getConfigDir(), "web_browser", "protocol_handlers.json")
96
97 def __load(self):
98 """
99 Private method to load the registered protocol handlers.
100 """
101 with contextlib.suppress(OSError):
102 with open(self.__protocolHandlersFileName(),
103 "r") as protocolHandlersFile:
104 protocolHandlersData = json.load(protocolHandlersFile)
105
106 if protocolHandlersData:
107 self.__protocolHandlers = {}
108 for scheme, urlStr in protocolHandlersData.items():
109 url = QUrl(urlStr)
110 self.__protocolHandlers[scheme] = url
111 self.__registerHandler(scheme, url)
112
113 def __save(self):
114 """
115 Private method to save the protocol handlers.
116 """
117 protocolHandlers = {scheme: url.toString()
118 for scheme, url in self.__protocolHandlers.items()}
119
120 with open(self.__protocolHandlersFileName(),
121 "w") as protocolHandlersFile:
122 json.dump(protocolHandlers, protocolHandlersFile, indent=2)
123
124 def __registerHandler(self, scheme, url):
125 """
126 Private method to register a protocol handler for a scheme.
127
128 @param scheme scheme of the protocol handler
129 @type str
130 @param url URL of the protocol handler
131 @type QUrl
132 """
133 urlStr = url.toString().replace("%25s", "%s")
134
135 page = QWebEnginePage(self)
136 page.loadFinished.connect(page.deleteLater)
137 page.registerProtocolHandlerRequested.connect(
138 lambda r: r.accept())
139 page.setHtml(
140 "<script>navigator.registerProtocolHandler('{0}', '{1}', '')"
141 "</script>".format(scheme, urlStr),
142 url)
143
144 def __unregisterHandler(self, scheme, url):
145 """
146 Private method to unregister a protocol handler for a scheme.
147
148 @param scheme scheme of the protocol handler
149 @type str
150 @param url URL of the protocol handler
151 @type QUrl
152 """
153 urlStr = url.toString().replace("%25s", "%s")
154
155 page = QWebEnginePage(self)
156 page.loadFinished.connect(page.deleteLater)
157 page.setHtml(
158 "<script>navigator.unregisterProtocolHandler('{0}', '{1}', '')"
159 "</script>".format(scheme, urlStr),
160 url)
161
162 def showProtocolHandlerManagerDialog(self):
163 """
164 Public method to show the protocol handler manager dialog.
165 """
166 from WebBrowser.WebBrowserWindow import WebBrowserWindow
167 from .ProtocolHandlerManagerDialog import ProtocolHandlerManagerDialog
168
169 dlg = ProtocolHandlerManagerDialog(self, WebBrowserWindow.getWindow())
170 dlg.open()

eric ide

mercurial