comparison cagou/core/platform_/base.py @ 344:83697218b9b2

core: handle URLs opening in a per-platform way: URLs opening may have to be done in a different way according to platforms, so an `open_url` method has been created in base Platform which can be overriden.
author Goffi <goffi@goffi.org>
date Sat, 04 Jan 2020 16:24:57 +0100
parents 89799148f894
children 4d3a0c4f2430
comparison
equal deleted inserted replaced
343:7a171d11eab6 344:83697218b9b2
14 # GNU Affero General Public License for more details. 14 # GNU Affero General Public License for more details.
15 15
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 webbrowser
20 from pathlib import Path
21 from urllib.parse import urlparse
19 from kivy.config import Config as KivyConfig 22 from kivy.config import Config as KivyConfig
23 from sat.core.log import getLogger
24 from sat.tools.common import files_utils
25 from sat_frontends.quick_frontend.quick_widgets import QuickWidget
26 from cagou import G
27
28
29 log = getLogger(__name__)
20 30
21 31
22 class Platform: 32 class Platform:
23 """Base class to handle platform specific behaviours""" 33 """Base class to handle platform specific behaviours"""
24 34
44 def on_resume(self): 54 def on_resume(self):
45 pass 55 pass
46 56
47 def on_stop(self): 57 def on_stop(self):
48 pass 58 pass
59
60 def open_url(self, url, wid=None):
61 """Open an URL in the way appropriate for the platform
62
63 @param url(str): URL to open
64 @param wid(CagouWidget, None): widget requesting the opening
65 it may influence the way the URL is opened
66 """
67 parsed_url = urlparse(url)
68 if parsed_url.scheme == "aesgcm" and wid is not None:
69 # aesgcm files need to be decrypted first
70 # so we download them before opening
71 name = Path(parsed_url.path).name.strip() or "unnamed_file"
72 log.info(f"downloading/decrypting file {name!r}")
73 dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name)
74 quick_widget = G.host.getAncestorWidget(wid, QuickWidget)
75 if quick_widget is None:
76 log.error("Can't find ancestor QuickWidget")
77 return
78 profile = quick_widget.profile
79 G.host.bridge.fileDownloadComplete(
80 url,
81 str(dest_path),
82 '',
83 profile,
84 callback=webbrowser.open,
85 errback=G.host.errback
86 )
87 else:
88 webbrowser.open(url)