comparison libervia/desktop_kivy/core/platform_/base.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/core/platform_/base.py@203755bbe0fe
children
comparison
equal deleted inserted replaced
492:5114bbb5daa3 493:b3cedbee561d
1 #!/usr/bin/env python3
2
3 #Libervia Desktop-Kivy
4 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
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/>.
18
19 import sys
20 import webbrowser
21 import subprocess
22 import shutil
23 from urllib import parse
24 from kivy.config import Config as KivyConfig
25 from libervia.backend.core.i18n import _
26 from libervia.backend.core.log import getLogger
27 from libervia.backend.core import exceptions
28 from libervia.frontends.quick_frontend.quick_widgets import QuickWidget
29 from libervia.desktop_kivy import G
30
31
32 log = getLogger(__name__)
33
34
35 class Platform:
36 """Base class to handle platform specific behaviours"""
37 # set to True to always show the send button in chat
38 send_button_visible = False
39
40 def init_platform(self):
41 # we don't want multi-touch emulation with mouse
42
43 # this option doesn't make sense on Android and cause troubles, so we only
44 # activate it for other platforms (cf. https://github.com/kivy/kivy/issues/6229)
45 KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch')
46
47 def on_app_build(self, Wid):
48 pass
49
50 def on_host_init(self, host):
51 pass
52
53 def on_init_frontend_state(self):
54 pass
55
56 def do_post_init(self):
57 return True
58
59 def on_pause(self):
60 pass
61
62 def on_resume(self):
63 pass
64
65 def on_stop(self):
66 pass
67
68 def on_key_back_root(self):
69 """Back key is called while being on root widget"""
70 return True
71
72 def on_key_back_share(self, share_widget):
73 """Back key is called while being on share widget"""
74 share_widget.close()
75 return True
76
77 def _on_new_window(self):
78 """Launch a new instance of LiberviaDesktopKivy to have an extra window"""
79 subprocess.Popen(sys.argv)
80
81 def on_extra_menu_init(self, extra_menu):
82 extra_menu.add_item(_('new window'), self._on_new_window)
83
84 def update_params_extra(self, extra):
85 pass
86
87 def check_plugin_permissions(self, plug_info, callback, errback):
88 """Check that plugin permissions for this platform are granted"""
89 callback()
90
91 def _open(self, path):
92 """Open url or path with appropriate application if possible"""
93 try:
94 opener = self._opener
95 except AttributeError:
96 xdg_open_path = shutil.which("xdg-open")
97 if xdg_open_path is not None:
98 log.debug("xdg-open found, it will be used to open files")
99 opener = lambda path: subprocess.Popen([xdg_open_path, path])
100 else:
101 log.debug("files will be opened with webbrower.open")
102 opener = webbrowser.open
103 self._opener = opener
104
105 opener(path)
106
107
108 def open_url(self, url, wid=None):
109 """Open an URL in the way appropriate for the platform
110
111 @param url(str): URL to open
112 @param wid(LiberviaDesktopKivyWidget, None): widget requesting the opening
113 it may influence the way the URL is opened
114 """
115 parsed_url = parse.urlparse(url)
116 if parsed_url.scheme == "aesgcm" and wid is not None:
117 # aesgcm files need to be decrypted first
118 # so we download them before opening
119 quick_widget = G.host.get_ancestor_widget(wid, QuickWidget)
120 if quick_widget is None:
121 msg = f"Can't find ancestor QuickWidget of {wid}"
122 log.error(msg)
123 G.host.errback(exceptions.InternalError(msg))
124 return
125 G.host.download_url(
126 parsed_url, self.open_url, G.host.errback, profile=quick_widget.profile
127 )
128 else:
129 self._open(url)