comparison cagou/core/platform_/base.py @ 421:ddca5a837029

platform (base): use "xdg-open" when available + use new host.downloadURL
author Goffi <goffi@goffi.org>
date Wed, 26 Feb 2020 16:39:17 +0100
parents c466678c57b2
children 2cfb9f7a6b62
comparison
equal deleted inserted replaced
420:83d184393fe1 421:ddca5a837029
16 # You should have received a copy of the GNU Affero General Public License 16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. 17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
19 import sys 19 import sys
20 import webbrowser 20 import webbrowser
21 from pathlib import Path
22 import subprocess 21 import subprocess
23 from urllib.parse import urlparse 22 import shutil
23 from urllib import parse
24 from kivy.config import Config as KivyConfig 24 from kivy.config import Config as KivyConfig
25 from sat.core.i18n import _ 25 from sat.core.i18n import _
26 from sat.core.log import getLogger 26 from sat.core.log import getLogger
27 from sat.tools.common import files_utils 27 from sat.core import exceptions
28 from sat_frontends.quick_frontend.quick_widgets import QuickWidget 28 from sat_frontends.quick_frontend.quick_widgets import QuickWidget
29 from cagou import G 29 from cagou import G
30 30
31 31
32 log = getLogger(__name__) 32 log = getLogger(__name__)
42 42
43 # this option doesn't make sense on Android and cause troubles, so we only 43 # this option doesn't make sense on Android and cause troubles, so we only
44 # activate it for other platforms (cf. https://github.com/kivy/kivy/issues/6229) 44 # activate it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
45 KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch') 45 KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch')
46 46
47 def on_app_build(self, wid): 47 def on_app_build(self, Wid):
48 pass 48 pass
49 49
50 def on_host_init(self, host): 50 def on_host_init(self, host):
51 pass 51 pass
52 52
86 86
87 def check_plugin_permissions(self, plug_info, callback, errback): 87 def check_plugin_permissions(self, plug_info, callback, errback):
88 """Check that plugin permissions for this platform are granted""" 88 """Check that plugin permissions for this platform are granted"""
89 callback() 89 callback()
90 90
91 def _open(self, path):
92 """Open url or path with appropriate application if possible"""
93 try:
94 opener = self._opener
95 except AttributeError:
96 xdg_open_path = shutil.which("xdg-open")
97 if xdg_open_path is not None:
98 log.debug("xdg-open found, it will be used to open files")
99 opener = lambda path: subprocess.Popen([xdg_open_path, path])
100 else:
101 log.debug("files will be opened with webbrower.open")
102 opener = webbrowser.open
103 self._opener = opener
104
105 opener(path)
106
107
91 def open_url(self, url, wid=None): 108 def open_url(self, url, wid=None):
92 """Open an URL in the way appropriate for the platform 109 """Open an URL in the way appropriate for the platform
93 110
94 @param url(str): URL to open 111 @param url(str): URL to open
95 @param wid(CagouWidget, None): widget requesting the opening 112 @param wid(CagouWidget, None): widget requesting the opening
96 it may influence the way the URL is opened 113 it may influence the way the URL is opened
97 """ 114 """
98 parsed_url = urlparse(url) 115 parsed_url = parse.urlparse(url)
99 if parsed_url.scheme == "aesgcm" and wid is not None: 116 if parsed_url.scheme == "aesgcm" and wid is not None:
100 # aesgcm files need to be decrypted first 117 # aesgcm files need to be decrypted first
101 # so we download them before opening 118 # so we download them before opening
102 name = Path(parsed_url.path).name.strip() or "unnamed_file"
103 log.info(f"downloading/decrypting file {name!r}")
104 dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name)
105 quick_widget = G.host.getAncestorWidget(wid, QuickWidget) 119 quick_widget = G.host.getAncestorWidget(wid, QuickWidget)
106 if quick_widget is None: 120 if quick_widget is None:
107 log.error("Can't find ancestor QuickWidget") 121 msg = f"Can't find ancestor QuickWidget of {wid}"
122 log.error(msg)
123 G.host.errback(exceptions.InternalError(msg))
108 return 124 return
109 profile = quick_widget.profile 125 G.host.downloadURL(
110 G.host.bridge.fileDownloadComplete( 126 parsed_url, self._open, G.host.errback, profile=quick_widget.profile
111 url,
112 str(dest_path),
113 '',
114 profile,
115 callback=webbrowser.open,
116 errback=G.host.errback
117 ) 127 )
118 else: 128 else:
119 webbrowser.open(url) 129 self._open(url)