Mercurial > libervia-desktop-kivy
annotate cagou/plugins/plugin_transfer_file.py @ 282:1b835bcfa663
date update
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 25 Mar 2019 07:11:09 +0100 |
parents | aea973de55d9 |
children | 772c170b47a9 |
rev | line source |
---|---|
86 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
282 | 5 # Copyright (C) 2016-2019 Jérôme Poisson (goffi@goffi.org) |
86 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
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 | 23 from sat.core import log as logging |
24 from sat.core.i18n import _ | |
25 from kivy.uix.boxlayout import BoxLayout | |
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 | 31 |
32 | |
33 PLUGIN_INFO = { | |
34 "name": _(u"file"), | |
97
5d2289127bb7
menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents:
86
diff
changeset
|
35 "main": "FileTransmitter", |
5d2289127bb7
menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents:
86
diff
changeset
|
36 "description": _(u"transmit a local file"), |
5d2289127bb7
menu (upload): better menu using dedicated widget:
Goffi <goffi@goffi.org>
parents:
86
diff
changeset
|
37 "icon_medium": u"{media}/icons/muchoslava/png/fichier_50.png", |
86 | 38 } |
39 | |
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 | 48 callback = properties.ObjectProperty() |
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 | 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): |
aea973de55d9
transfer (file): use native file chooser on desktop:
Goffi <goffi@goffi.org>
parents:
126
diff
changeset
|
69 title=_(u"Please select a file to upload") |
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) |