Mercurial > libervia-desktop-kivy
changeset 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 | 7a171d11eab6 |
children | a3cefa7158dc |
files | cagou/core/platform_/base.py cagou/core/simple_xhtml.py |
diffstat | 2 files changed, 41 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/cagou/core/platform_/base.py Sat Jan 04 16:24:57 2020 +0100 +++ b/cagou/core/platform_/base.py Sat Jan 04 16:24:57 2020 +0100 @@ -16,7 +16,17 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. +import webbrowser +from pathlib import Path +from urllib.parse import urlparse from kivy.config import Config as KivyConfig +from sat.core.log import getLogger +from sat.tools.common import files_utils +from sat_frontends.quick_frontend.quick_widgets import QuickWidget +from cagou import G + + +log = getLogger(__name__) class Platform: @@ -46,3 +56,33 @@ def on_stop(self): pass + + def open_url(self, url, wid=None): + """Open an URL in the way appropriate for the platform + + @param url(str): URL to open + @param wid(CagouWidget, None): widget requesting the opening + it may influence the way the URL is opened + """ + parsed_url = 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") + return + profile = quick_widget.profile + G.host.bridge.fileDownloadComplete( + url, + str(dest_path), + '', + profile, + callback=webbrowser.open, + errback=G.host.errback + ) + else: + webbrowser.open(url)
--- a/cagou/core/simple_xhtml.py Sat Jan 04 16:24:57 2020 +0100 +++ b/cagou/core/simple_xhtml.py Sat Jan 04 16:24:57 2020 +0100 @@ -18,19 +18,14 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. -import webbrowser from xml.etree import ElementTree as ET -from urllib.parse import urlparse -from pathlib import Path from kivy.uix.stacklayout import StackLayout from kivy.uix.label import Label from kivy.utils import escape_markup from kivy.metrics import sp, dp from kivy import properties from sat.core import log as logging -from sat.tools.common import files_utils from sat_frontends.tools import css_color, strings as sat_strings -from sat_frontends.quick_frontend.quick_widgets import QuickWidget from cagou import G from cagou.core.image import AsyncImage from cagou.core.constants import Const as C @@ -84,28 +79,7 @@ def on_ref_press(self, ref): url = self.ref_urls[ref] - parsed_url = urlparse(url) - if parsed_url.scheme == "aesgcm": - # 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(self, QuickWidget) - if quick_widget is None: - log.error("Can't find ancestor QuickWidget") - return - profile = quick_widget.profile - G.host.bridge.fileDownloadComplete( - url, - str(dest_path), - '', - profile, - callback=webbrowser.open, - errback=G.host.errback - ) - else: - webbrowser.open(url) + G.local_platform.open_url(url, self) class SimpleXHTMLWidgetText(Label):