comparison cagou/core/simple_xhtml.py @ 335:597cc207c8e7

core (simple_xhtml): handle `aesgcm` schemes: when an `aesgcm` scheme is found, the file is download/decrypted using fileDownload, then opened.
author Goffi <goffi@goffi.org>
date Fri, 20 Dec 2019 12:29:37 +0100
parents 5868a5575e01
children 83697218b9b2
comparison
equal deleted inserted replaced
334:36d6763af547 335:597cc207c8e7
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 21 import webbrowser
22 from xml.etree import ElementTree as ET 22 from xml.etree import ElementTree as ET
23 from urllib.parse import urlparse
24 from pathlib import Path
23 from kivy.uix.stacklayout import StackLayout 25 from kivy.uix.stacklayout import StackLayout
24 from kivy.uix.label import Label 26 from kivy.uix.label import Label
25 from kivy.utils import escape_markup 27 from kivy.utils import escape_markup
26 from kivy.metrics import sp, dp 28 from kivy.metrics import sp, dp
27 from kivy import properties 29 from kivy import properties
28 from sat.core import log as logging 30 from sat.core import log as logging
31 from sat.tools.common import files_utils
29 from sat_frontends.tools import css_color, strings as sat_strings 32 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
30 from cagou.core.image import AsyncImage 35 from cagou.core.image import AsyncImage
31 from cagou.core.constants import Const as C 36 from cagou.core.constants import Const as C
32 37
33 38
34 log = logging.getLogger(__name__) 39 log = logging.getLogger(__name__)
77 if text and not self.markup: 82 if text and not self.markup:
78 self._addUrlMarkup(text) 83 self._addUrlMarkup(text)
79 84
80 def on_ref_press(self, ref): 85 def on_ref_press(self, ref):
81 url = self.ref_urls[ref] 86 url = self.ref_urls[ref]
82 webbrowser.open(url) 87 parsed_url = urlparse(url)
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)
83 109
84 110
85 class SimpleXHTMLWidgetText(Label): 111 class SimpleXHTMLWidgetText(Label):
86 112
87 def on_parent(self, instance, parent): 113 def on_parent(self, instance, parent):