Mercurial > libervia-desktop-kivy
comparison cagou/plugins/plugin_transfer_file.py @ 279:aea973de55d9
transfer (file): use native file chooser on desktop:
native file chooser is nicer and often more powerful than Kivy FileChooser.
Kivy FileChooser is still activable with an option, for platforms where native file chooser is not easy/possible to use. Kivy FileChooser is used on Android.
downloads_dir is now used as starting path on Android.
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 20 Mar 2019 09:29:44 +0100 |
parents | cd99f70ea592 |
children | 1b835bcfa663 |
comparison
equal
deleted
inserted
replaced
278:444ba439530f | 279:aea973de55d9 |
---|---|
15 # GNU Affero General Public License for more details. | 15 # GNU Affero General Public License for more details. |
16 | 16 |
17 # You should have received a copy of the GNU Affero General Public License | 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/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 | 20 import threading |
21 import sys | |
22 from functools import partial | |
21 from sat.core import log as logging | 23 from sat.core import log as logging |
22 log = logging.getLogger(__name__) | |
23 from sat.core.i18n import _ | 24 from sat.core.i18n import _ |
24 from kivy.uix.boxlayout import BoxLayout | 25 from kivy.uix.boxlayout import BoxLayout |
25 from kivy import properties | 26 from kivy import properties |
27 from kivy.clock import Clock | |
28 from plyer import filechooser, storagepath | |
29 | |
30 log = logging.getLogger(__name__) | |
26 | 31 |
27 | 32 |
28 PLUGIN_INFO = { | 33 PLUGIN_INFO = { |
29 "name": _(u"file"), | 34 "name": _(u"file"), |
30 "main": "FileTransmitter", | 35 "main": "FileTransmitter", |
31 "description": _(u"transmit a local file"), | 36 "description": _(u"transmit a local file"), |
32 "icon_medium": u"{media}/icons/muchoslava/png/fichier_50.png", | 37 "icon_medium": u"{media}/icons/muchoslava/png/fichier_50.png", |
33 } | 38 } |
34 | 39 |
35 | 40 |
41 class FileChooserBox(BoxLayout): | |
42 callback = properties.ObjectProperty() | |
43 cancel_cb = properties.ObjectProperty() | |
44 default_path = properties.StringProperty() | |
45 | |
46 | |
36 class FileTransmitter(BoxLayout): | 47 class FileTransmitter(BoxLayout): |
37 callback = properties.ObjectProperty() | 48 callback = properties.ObjectProperty() |
38 cancel_cb = properties.ObjectProperty() | 49 cancel_cb = properties.ObjectProperty() |
50 native_filechooser = True | |
51 default_path = storagepath.get_home_dir() | |
39 | 52 |
40 def onTransmitOK(self, filechooser): | 53 def __init__(self, *args, **kwargs): |
41 if filechooser.selection: | 54 if sys.platform == 'android': |
42 file_path = filechooser.selection[0] | 55 self.native_filechooser = False |
43 self.callback(file_path) | 56 self.default_path = storagepath.get_downloads_dir() |
57 | |
58 super(FileTransmitter, self).__init__(*args, **kwargs) | |
59 | |
60 if self.native_filechooser: | |
61 thread = threading.Thread(target=self._nativeFileChooser) | |
62 thread.start() | |
63 else: | |
64 self.add_widget(FileChooserBox(default_path = self.default_path, | |
65 callback=self.onFiles, | |
66 cancel_cb=partial(self.cancel_cb, self))) | |
67 | |
68 def _nativeFileChooser(self, *args, **kwargs): | |
69 title=_(u"Please select a file to upload") | |
70 files = filechooser.open_file(title=title, | |
71 path=self.default_path, | |
72 multiple=False, | |
73 preview=True) | |
74 # we want to leave the thread when calling onFiles, so we use Clock | |
75 Clock.schedule_once(lambda *args: self.onFiles(files=files), 0) | |
76 | |
77 def onFiles(self, files): | |
78 if files: | |
79 self.callback(files[0]) | |
80 else: | |
81 self.cancel_cb(self) |