comparison cagou/core/simple_xhtml.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 597cc207c8e7
children 4d660b252487
comparison
equal deleted inserted replaced
343:7a171d11eab6 344:83697218b9b2
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 20
21 import webbrowser
22 from xml.etree import ElementTree as ET 21 from xml.etree import ElementTree as ET
23 from urllib.parse import urlparse
24 from pathlib import Path
25 from kivy.uix.stacklayout import StackLayout 22 from kivy.uix.stacklayout import StackLayout
26 from kivy.uix.label import Label 23 from kivy.uix.label import Label
27 from kivy.utils import escape_markup 24 from kivy.utils import escape_markup
28 from kivy.metrics import sp, dp 25 from kivy.metrics import sp, dp
29 from kivy import properties 26 from kivy import properties
30 from sat.core import log as logging 27 from sat.core import log as logging
31 from sat.tools.common import files_utils
32 from sat_frontends.tools import css_color, strings as sat_strings 28 from sat_frontends.tools import css_color, strings as sat_strings
33 from sat_frontends.quick_frontend.quick_widgets import QuickWidget
34 from cagou import G 29 from cagou import G
35 from cagou.core.image import AsyncImage 30 from cagou.core.image import AsyncImage
36 from cagou.core.constants import Const as C 31 from cagou.core.constants import Const as C
37 32
38 33
82 if text and not self.markup: 77 if text and not self.markup:
83 self._addUrlMarkup(text) 78 self._addUrlMarkup(text)
84 79
85 def on_ref_press(self, ref): 80 def on_ref_press(self, ref):
86 url = self.ref_urls[ref] 81 url = self.ref_urls[ref]
87 parsed_url = urlparse(url) 82 G.local_platform.open_url(url, self)
88 if parsed_url.scheme == "aesgcm":
89 # aesgcm files need to be decrypted first
90 # so we download them before opening
91 name = Path(parsed_url.path).name.strip() or "unnamed_file"
92 log.info(f"downloading/decrypting file {name!r}")
93 dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name)
94 quick_widget = G.host.getAncestorWidget(self, QuickWidget)
95 if quick_widget is None:
96 log.error("Can't find ancestor QuickWidget")
97 return
98 profile = quick_widget.profile
99 G.host.bridge.fileDownloadComplete(
100 url,
101 str(dest_path),
102 '',
103 profile,
104 callback=webbrowser.open,
105 errback=G.host.errback
106 )
107 else:
108 webbrowser.open(url)
109 83
110 84
111 class SimpleXHTMLWidgetText(Label): 85 class SimpleXHTMLWidgetText(Label):
112 86
113 def on_parent(self, instance, parent): 87 def on_parent(self, instance, parent):