comparison libervia/desktop_kivy/plugins/plugin_transfer_file.py @ 514:d78728d7fd6a default tip

plugin wid calls, core: implements WebRTC DataChannel file transfer: - Add a new "file" icon in call UI to send a file via WebRTC. - Handle new preflight mechanism, and WebRTC file transfer. - Native file chooser handling has been moved to new `core.file_chooser` module, and now supports "save" and "dir" modes (based on `plyer`). rel 442
author Goffi <goffi@goffi.org>
date Sat, 06 Apr 2024 13:37:27 +0200
parents b3cedbee561d
children
comparison
equal deleted inserted replaced
513:0fdf3e59aaad 514:d78728d7fd6a
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 3
4 #Libervia Desktop-Kivy 4 # Libervia Desktop-Kivy
5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2016-2021 Jérôme Poisson (goffi@goffi.org)
6 6
7 # This program is free software: you can redistribute it and/or modify 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 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 9 # the Free Software Foundation, either version 3 of the License, or
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 import threading 20 from functools import partial
21 import sys 21 import sys
22 from functools import partial 22
23 from libervia.backend.core import log as logging 23 from libervia.backend.core import log as logging
24 from libervia.backend.core.i18n import _ 24 from libervia.backend.core.i18n import _
25
26 from kivy import properties
25 from kivy.uix.boxlayout import BoxLayout 27 from kivy.uix.boxlayout import BoxLayout
26 from kivy import properties 28 from libervia.desktop_kivy.core import file_chooser
27 from kivy.clock import Clock
28 from plyer import filechooser, storagepath
29 29
30 log = logging.getLogger(__name__) 30 log = logging.getLogger(__name__)
31 31
32 32
33 PLUGIN_INFO = { 33 PLUGIN_INFO = {
42 callback = properties.ObjectProperty() 42 callback = properties.ObjectProperty()
43 cancel_cb = properties.ObjectProperty() 43 cancel_cb = properties.ObjectProperty()
44 default_path = properties.StringProperty() 44 default_path = properties.StringProperty()
45 45
46 46
47 class FileTransmitter(BoxLayout): 47 class FileTransmitter(BoxLayout, file_chooser.FileChooser):
48 callback = properties.ObjectProperty() 48 """Widget to transmit files"""
49 cancel_cb = properties.ObjectProperty()
50 native_filechooser = True
51 default_path = storagepath.get_home_dir()
52 49
53 def __init__(self, *args, **kwargs): 50 def __init__(self, *args, **kwargs):
54 if sys.platform == 'android': 51 if sys.platform == "android":
55 self.native_filechooser = False 52 self.native_filechooser = False
56 self.default_path = storagepath.get_downloads_dir() 53 self.default_path = storagepath.get_downloads_dir()
57 54
58 super(FileTransmitter, self).__init__(*args, **kwargs) 55 super().__init__(*args, **kwargs)
59 56
60 if self.native_filechooser: 57 if self.native_filechooser:
61 thread = threading.Thread(target=self._native_file_chooser) 58 self.open()
62 thread.start()
63 else: 59 else:
64 self.add_widget(FileChooserBox(default_path = self.default_path, 60 self.add_widget(
65 callback=self.on_files, 61 FileChooserBox(
66 cancel_cb=partial(self.cancel_cb, self))) 62 default_path=self.default_path,
67 63 callback=self.on_files,
68 def _native_file_chooser(self, *args, **kwargs): 64 cancel_cb=partial(self.cancel_cb, self),
69 title=_("Please select a file to upload") 65 )
70 files = filechooser.open_file(title=title, 66 )
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)