eric6/WebBrowser/Network/ProtocolHandlerManager.py

branch
maintenance
changeset 8273
698ae46f40a4
parent 8243
cc717c2ae956
equal deleted inserted replaced
8190:fb0ef164f536 8273:698ae46f40a4
7 Module implementing the protocol handler manager. 7 Module implementing the protocol handler manager.
8 """ 8 """
9 9
10 import os 10 import os
11 import json 11 import json
12 import contextlib
12 13
13 from PyQt5.QtCore import QObject, QUrl 14 from PyQt5.QtCore import QObject, QUrl
14 from PyQt5.QtWebEngineWidgets import QWebEnginePage 15 from PyQt5.QtWebEngineWidgets import QWebEnginePage
15 16
16 import Utilities 17 import Utilities
25 Constructor 26 Constructor
26 27
27 @param parent reference to the parent object 28 @param parent reference to the parent object
28 @type QObject 29 @type QObject
29 """ 30 """
30 super(ProtocolHandlerManager, self).__init__(parent) 31 super().__init__(parent)
31 32
32 self.__protocolHandlers = {} 33 self.__protocolHandlers = {}
33 # dictionary of handlers with scheme as key 34 # dictionary of handlers with scheme as key
34 35
35 self.__load() 36 self.__load()
95 96
96 def __load(self): 97 def __load(self):
97 """ 98 """
98 Private method to load the registered protocol handlers. 99 Private method to load the registered protocol handlers.
99 """ 100 """
100 try: 101 with contextlib.suppress(OSError):
101 with open(self.__protocolHandlersFileName(), 102 with open(self.__protocolHandlersFileName(),
102 "r") as protocolHandlersFile: 103 "r") as protocolHandlersFile:
103 protocolHandlersData = json.load(protocolHandlersFile) 104 protocolHandlersData = json.load(protocolHandlersFile)
104 105
105 if protocolHandlersData: 106 if protocolHandlersData:
106 self.__protocolHandlers = {} 107 self.__protocolHandlers = {}
107 for scheme, urlStr in protocolHandlersData.items(): 108 for scheme, urlStr in protocolHandlersData.items():
108 url = QUrl(urlStr) 109 url = QUrl(urlStr)
109 self.__protocolHandlers[scheme] = url 110 self.__protocolHandlers[scheme] = url
110 self.__registerHandler(scheme, url) 111 self.__registerHandler(scheme, url)
111 except OSError:
112 # ignore issues silently
113 pass
114 112
115 def __save(self): 113 def __save(self):
116 """ 114 """
117 Private method to save the protocol handlers. 115 Private method to save the protocol handlers.
118 """ 116 """
134 """ 132 """
135 urlStr = url.toString().replace("%25s", "%s") 133 urlStr = url.toString().replace("%25s", "%s")
136 134
137 page = QWebEnginePage(self) 135 page = QWebEnginePage(self)
138 page.loadFinished.connect(page.deleteLater) 136 page.loadFinished.connect(page.deleteLater)
139 try: 137 page.registerProtocolHandlerRequested.connect(
140 # for Qt >= 5.11 138 lambda r: r.accept())
141 page.registerProtocolHandlerRequested.connect(
142 lambda r: r.accept())
143 except AttributeError:
144 pass
145 page.setHtml( 139 page.setHtml(
146 "<script>navigator.registerProtocolHandler('{0}', '{1}', '')" 140 "<script>navigator.registerProtocolHandler('{0}', '{1}', '')"
147 "</script>".format(scheme, urlStr), 141 "</script>".format(scheme, urlStr),
148 url) 142 url)
149 143

eric ide

mercurial