changeset 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 83d184393fe1
children efee0e0afb78
files cagou/core/platform_/base.py
diffstat 1 files changed, 28 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/core/platform_/base.py	Wed Feb 26 16:38:17 2020 +0100
+++ b/cagou/core/platform_/base.py	Wed Feb 26 16:39:17 2020 +0100
@@ -18,13 +18,13 @@
 
 import sys
 import webbrowser
-from pathlib import Path
 import subprocess
-from urllib.parse import urlparse
+import shutil
+from urllib import parse
 from kivy.config import Config as KivyConfig
 from sat.core.i18n import _
 from sat.core.log import getLogger
-from sat.tools.common import files_utils
+from sat.core import exceptions
 from sat_frontends.quick_frontend.quick_widgets import QuickWidget
 from cagou import G
 
@@ -44,7 +44,7 @@
         # activate it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
         KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch')
 
-    def on_app_build(self, wid):
+    def on_app_build(self, Wid):
         pass
 
     def on_host_init(self, host):
@@ -88,6 +88,23 @@
         """Check that plugin permissions for this platform are granted"""
         callback()
 
+    def _open(self, path):
+        """Open url or path with appropriate application if possible"""
+        try:
+            opener = self._opener
+        except AttributeError:
+            xdg_open_path = shutil.which("xdg-open")
+            if xdg_open_path is not None:
+                log.debug("xdg-open found, it will be used to open files")
+                opener = lambda path: subprocess.Popen([xdg_open_path, path])
+            else:
+                log.debug("files will be opened with webbrower.open")
+                opener = webbrowser.open
+            self._opener = opener
+
+        opener(path)
+
+
     def open_url(self, url, wid=None):
         """Open an URL in the way appropriate for the platform
 
@@ -95,25 +112,18 @@
         @param wid(CagouWidget, None): widget requesting the opening
             it may influence the way the URL is opened
         """
-        parsed_url = urlparse(url)
+        parsed_url = parse.urlparse(url)
         if parsed_url.scheme == "aesgcm" and wid is not None:
             # aesgcm files need to be decrypted first
             # so we download them before opening
-            name = Path(parsed_url.path).name.strip() or "unnamed_file"
-            log.info(f"downloading/decrypting file {name!r}")
-            dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name)
             quick_widget = G.host.getAncestorWidget(wid, QuickWidget)
             if quick_widget is None:
-                log.error("Can't find ancestor QuickWidget")
+                msg = f"Can't find ancestor QuickWidget of {wid}"
+                log.error(msg)
+                G.host.errback(exceptions.InternalError(msg))
                 return
-            profile = quick_widget.profile
-            G.host.bridge.fileDownloadComplete(
-                url,
-                str(dest_path),
-                '',
-                profile,
-                callback=webbrowser.open,
-                errback=G.host.errback
+            G.host.downloadURL(
+                parsed_url, self._open, G.host.errback, profile=quick_widget.profile
             )
         else:
-            webbrowser.open(url)
+            self._open(url)