annotate cagou/core/platform_/base.py @ 461:3c9ba4a694ef

dates update
author Goffi <goffi@goffi.org>
date Fri, 19 Mar 2021 11:57:19 +0100
parents 2cfb9f7a6b62
children 203755bbe0fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
461
3c9ba4a694ef dates update
Goffi <goffi@goffi.org>
parents: 456
diff changeset
4 # Copyright (C) 2016-2021 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
408
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
19 import sys
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
20 import webbrowser
408
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
21 import subprocess
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
22 import shutil
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
23 from urllib import parse
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from kivy.config import Config as KivyConfig
408
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
25 from sat.core.i18n import _
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
26 from sat.core.log import getLogger
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
27 from sat.core import exceptions
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
28 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
29 from cagou import G
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
30
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
31
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
32 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
33
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 class Platform:
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
36 """Base class to handle platform specific behaviours"""
413
c466678c57b2 chat: control send button visibility in core.platform_, and show it when there are attachments
Goffi <goffi@goffi.org>
parents: 408
diff changeset
37 # set to True to always show the send button in chat
c466678c57b2 chat: control send button visibility in core.platform_, and show it when there are attachments
Goffi <goffi@goffi.org>
parents: 408
diff changeset
38 send_button_visible = False
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
39
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
40 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
41 # 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
42
357
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
43 # 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
44 # 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
45 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
46
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
47 def on_app_build(self, Wid):
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
48 pass
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
49
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
50 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
51 pass
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
52
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
53 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
54 pass
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
55
370
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
56 def do_postInit(self):
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
57 return True
1a12bbd80943 android: autoconnect profile using the new `profileAutoconnectGet` backend method:
Goffi <goffi@goffi.org>
parents: 357
diff changeset
58
342
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
59 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
60 pass
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
61
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
62 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
63 pass
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
64
89799148f894 core: use classes and factory to handle platform specific behaviours in a generic way
Goffi <goffi@goffi.org>
parents:
diff changeset
65 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
66 pass
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
67
357
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
68 def on_key_back_root(self):
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
69 """Back key is called while being on root widget"""
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
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
72 def on_key_back_share(self, share_widget):
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
73 """Back key is called while being on share widget"""
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
74 share_widget.close()
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
75 return True
4d3a0c4f2430 core: better back key (ESC) management:
Goffi <goffi@goffi.org>
parents: 344
diff changeset
76
408
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
77 def _on_new_window(self):
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
78 """Launch a new instance of Cagou to have an extra window"""
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
79 subprocess.Popen(sys.argv)
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
80
376
9ef01266e3fe core: new extra menu:
Goffi <goffi@goffi.org>
parents: 372
diff changeset
81 def on_extra_menu_init(self, extra_menu):
408
355326a3501c platform (base): added a menu item to create a new window
Goffi <goffi@goffi.org>
parents: 400
diff changeset
82 extra_menu.addItem(_('new window'), self._on_new_window)
376
9ef01266e3fe core: new extra menu:
Goffi <goffi@goffi.org>
parents: 372
diff changeset
83
372
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
84 def updateParamsExtra(self, extra):
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
85 pass
1481f09c9175 settings (android): don't expose autoconnect parameters:
Goffi <goffi@goffi.org>
parents: 370
diff changeset
86
400
71f51198478c android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
87 def check_plugin_permissions(self, plug_info, callback, errback):
71f51198478c android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
88 """Check that plugin permissions for this platform are granted"""
71f51198478c android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
89 callback()
71f51198478c android: handle runtime permissions:
Goffi <goffi@goffi.org>
parents: 378
diff changeset
90
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
91 def _open(self, path):
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
92 """Open url or path with appropriate application if possible"""
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
93 try:
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
94 opener = self._opener
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
95 except AttributeError:
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
96 xdg_open_path = shutil.which("xdg-open")
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
97 if xdg_open_path is not None:
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
98 log.debug("xdg-open found, it will be used to open files")
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
99 opener = lambda path: subprocess.Popen([xdg_open_path, path])
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
100 else:
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
101 log.debug("files will be opened with webbrower.open")
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
102 opener = webbrowser.open
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
103 self._opener = opener
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
104
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
105 opener(path)
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
106
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
107
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
108 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
109 """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
110
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
111 @param url(str): URL to open
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
112 @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
113 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
114 """
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
115 parsed_url = parse.urlparse(url)
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
116 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
117 # 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
118 # so we download them before opening
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
119 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
120 if quick_widget is None:
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
121 msg = f"Can't find ancestor QuickWidget of {wid}"
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
122 log.error(msg)
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
123 G.host.errback(exceptions.InternalError(msg))
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
124 return
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
125 G.host.downloadURL(
456
2cfb9f7a6b62 platform (base): call again "open_url" after decrypting a file:
Goffi <goffi@goffi.org>
parents: 421
diff changeset
126 parsed_url, self.open_url, G.host.errback, profile=quick_widget.profile
344
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
127 )
83697218b9b2 core: handle URLs opening in a per-platform way:
Goffi <goffi@goffi.org>
parents: 342
diff changeset
128 else:
421
ddca5a837029 platform (base): use "xdg-open" when available + use new host.downloadURL
Goffi <goffi@goffi.org>
parents: 413
diff changeset
129 self._open(url)