src/eric7/WebBrowser/Network/ProtocolHandlerManager.py

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

eric ide

mercurial