annotate cagou/plugins/plugin_transfer_file.py @ 408:355326a3501c

platform (base): added a menu item to create a new window
author Goffi <goffi@goffi.org>
date Wed, 19 Feb 2020 09:49:20 +0100
parents 1da3c379205b
children 3c9ba4a694ef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
379
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
1 #!/usr/bin/env python3
1da3c379205b fixed shebangs
Goffi <goffi@goffi.org>
parents: 378
diff changeset
2
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client
378
4d660b252487 dates update
Goffi <goffi@goffi.org>
parents: 312
diff changeset
5 # Copyright (C) 2016-2020 Jérôme Poisson (goffi@goffi.org)
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
279
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
20 import threading
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
21 import sys
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
22 from functools import partial
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from sat.core import log as logging
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sat.core.i18n import _
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from kivy.uix.boxlayout import BoxLayout
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from kivy import properties
279
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
27 from kivy.clock import Clock
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
28 from plyer import filechooser, storagepath
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
29
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
30 log = logging.getLogger(__name__)
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
31
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 PLUGIN_INFO = {
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
34 "name": _("file"),
97
5d2289127bb7 menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
35 "main": "FileTransmitter",
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
36 "description": _("transmit a local file"),
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
37 "icon_medium": "{media}/icons/muchoslava/png/fichier_50.png",
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 }
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
40
279
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
41 class FileChooserBox(BoxLayout):
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
42 callback = properties.ObjectProperty()
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
43 cancel_cb = properties.ObjectProperty()
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
44 default_path = properties.StringProperty()
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
45
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
46
97
5d2289127bb7 menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents: 86
diff changeset
47 class FileTransmitter(BoxLayout):
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 callback = properties.ObjectProperty()
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 cancel_cb = properties.ObjectProperty()
279
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
50 native_filechooser = True
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
51 default_path = storagepath.get_home_dir()
86
c711be670ecd core, chat: upload plugin system:
Goffi <goffi@goffi.org>
parents:
diff changeset
52
279
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
53 def __init__(self, *args, **kwargs):
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
54 if sys.platform == 'android':
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
55 self.native_filechooser = False
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
56 self.default_path = storagepath.get_downloads_dir()
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
57
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
58 super(FileTransmitter, self).__init__(*args, **kwargs)
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
59
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
60 if self.native_filechooser:
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
61 thread = threading.Thread(target=self._nativeFileChooser)
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
62 thread.start()
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
63 else:
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
64 self.add_widget(FileChooserBox(default_path = self.default_path,
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
65 callback=self.onFiles,
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
66 cancel_cb=partial(self.cancel_cb, self)))
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
67
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
68 def _nativeFileChooser(self, *args, **kwargs):
312
772c170b47a9 Python3 port:
Goffi <goffi@goffi.org>
parents: 282
diff changeset
69 title=_("Please select a file to upload")
279
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
70 files = filechooser.open_file(title=title,
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
71 path=self.default_path,
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
72 multiple=False,
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
73 preview=True)
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
74 # we want to leave the thread when calling onFiles, so we use Clock
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
75 Clock.schedule_once(lambda *args: self.onFiles(files=files), 0)
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
76
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
77 def onFiles(self, files):
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
78 if files:
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
79 self.callback(files[0])
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
80 else:
aea973de55d9 transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents: 126
diff changeset
81 self.cancel_cb(self)