comparison libervia/desktop_kivy/plugins/plugin_transfer_file.py @ 493:b3cedbee561d

refactoring: rename `cagou` to `libervia.desktop_kivy` + update imports and names following backend changes
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 18:26:16 +0200
parents cagou/plugins/plugin_transfer_file.py@203755bbe0fe
children d78728d7fd6a
comparison
equal deleted inserted replaced
492:5114bbb5daa3 493:b3cedbee561d
1 #!/usr/bin/env python3
2
3
4 #Libervia Desktop-Kivy
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
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
20 import threading
21 import sys
22 from functools import partial
23 from libervia.backend.core import log as logging
24 from libervia.backend.core.i18n import _
25 from kivy.uix.boxlayout import BoxLayout
26 from kivy import properties
27 from kivy.clock import Clock
28 from plyer import filechooser, storagepath
29
30 log = logging.getLogger(__name__)
31
32
33 PLUGIN_INFO = {
34 "name": _("file"),
35 "main": "FileTransmitter",
36 "description": _("transmit a local file"),
37 "icon_medium": "{media}/icons/muchoslava/png/fichier_50.png",
38 }
39
40
41 class FileChooserBox(BoxLayout):
42 callback = properties.ObjectProperty()
43 cancel_cb = properties.ObjectProperty()
44 default_path = properties.StringProperty()
45
46
47 class FileTransmitter(BoxLayout):
48 callback = properties.ObjectProperty()
49 cancel_cb = properties.ObjectProperty()
50 native_filechooser = True
51 default_path = storagepath.get_home_dir()
52
53 def __init__(self, *args, **kwargs):
54 if sys.platform == 'android':
55 self.native_filechooser = False
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._native_file_chooser)
62 thread.start()
63 else:
64 self.add_widget(FileChooserBox(default_path = self.default_path,
65 callback=self.on_files,
66 cancel_cb=partial(self.cancel_cb, self)))
67
68 def _native_file_chooser(self, *args, **kwargs):
69 title=_("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 on_files, so we use Clock
75 Clock.schedule_once(lambda *args: self.on_files(files=files), 0)
76
77 def on_files(self, files):
78 if files:
79 self.callback(files[0])
80 else:
81 self.cancel_cb(self)