Mercurial > libervia-desktop-kivy
annotate cagou/core/platform_/base.py @ 400:71f51198478c
android: handle runtime permissions:
- some mandatory permissions are requested on Cagou start, Cagou won't start at all and
display a warning message if they are not granted (we request 5 times before showing the
warning)
- transfer plugin can now use "android_permissions" in plugin_info, to indicate what is
necessary. The permissions will then be requested, and the plugin widget won't be shown
if they are not granted (and a warning not will then be displayed)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 09 Feb 2020 23:47:29 +0100 |
parents | 4d660b252487 |
children | 355326a3501c |
rev | line source |
---|---|
342
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client |
378 | 4 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org) |
342
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
344
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
19 import webbrowser |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
20 from pathlib import Path |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
21 from urllib.parse import urlparse |
342
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from kivy.config import Config as KivyConfig |
344
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
23 from sat.core.log import getLogger |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
24 from sat.tools.common import files_utils |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
25 from sat_frontends.quick_frontend.quick_widgets import QuickWidget |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
26 from cagou import G |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
27 |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
28 |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
29 log = getLogger(__name__) |
342
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 class Platform: |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 """Base class to handle platform specific behaviours""" |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 def init_platform(self): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 # we don't want multi-touch emulation with mouse |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
357
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
38 # this option doesn't make sense on Android and cause troubles, so we only |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
39 # activate it for other platforms (cf. https://github.com/kivy/kivy/issues/6229) |
342
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 KivyConfig.set('input', 'mouse', 'mouse,disable_multitouch') |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 def on_app_build(self, wid): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 pass |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 def on_host_init(self, host): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 pass |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 def on_initFrontendState(self): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 pass |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
370
1a12bbd80943
android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents:
357
diff
changeset
|
51 def do_postInit(self): |
1a12bbd80943
android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents:
357
diff
changeset
|
52 return True |
1a12bbd80943
android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents:
357
diff
changeset
|
53 |
342
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 def on_pause(self): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 pass |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def on_resume(self): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 pass |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def on_stop(self): |
89799148f894
core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 pass |
344
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
62 |
357
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
63 def on_key_back_root(self): |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
64 """Back key is called while being on root widget""" |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
65 return True |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
66 |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
67 def on_key_back_share(self, share_widget): |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
68 """Back key is called while being on share widget""" |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
69 share_widget.close() |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
70 return True |
4d3a0c4f2430
core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents:
344
diff
changeset
|
71 |
376 | 72 def on_extra_menu_init(self, extra_menu): |
73 pass | |
74 | |
372
1481f09c9175
settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents:
370
diff
changeset
|
75 def updateParamsExtra(self, extra): |
1481f09c9175
settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents:
370
diff
changeset
|
76 pass |
1481f09c9175
settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents:
370
diff
changeset
|
77 |
400
71f51198478c
android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents:
378
diff
changeset
|
78 def check_plugin_permissions(self, plug_info, callback, errback): |
71f51198478c
android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents:
378
diff
changeset
|
79 """Check that plugin permissions for this platform are granted""" |
71f51198478c
android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents:
378
diff
changeset
|
80 callback() |
71f51198478c
android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents:
378
diff
changeset
|
81 |
344
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
82 def open_url(self, url, wid=None): |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
83 """Open an URL in the way appropriate for the platform |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
84 |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
85 @param url(str): URL to open |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
86 @param wid(CagouWidget, None): widget requesting the opening |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
87 it may influence the way the URL is opened |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
88 """ |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
89 parsed_url = urlparse(url) |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
90 if parsed_url.scheme == "aesgcm" and wid is not None: |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
91 # aesgcm files need to be decrypted first |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
92 # so we download them before opening |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
93 name = Path(parsed_url.path).name.strip() or "unnamed_file" |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
94 log.info(f"downloading/decrypting file {name!r}") |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
95 dest_path = files_utils.get_unique_name(Path(G.host.downloads_dir)/name) |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
96 quick_widget = G.host.getAncestorWidget(wid, QuickWidget) |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
97 if quick_widget is None: |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
98 log.error("Can't find ancestor QuickWidget") |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
99 return |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
100 profile = quick_widget.profile |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
101 G.host.bridge.fileDownloadComplete( |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
102 url, |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
103 str(dest_path), |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
104 '', |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
105 profile, |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
106 callback=webbrowser.open, |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
107 errback=G.host.errback |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
108 ) |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
109 else: |
83697218b9b2
core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents:
342
diff
changeset
|
110 webbrowser.open(url) |