changeset 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 444ba439530f
children b0461363bc65
files cagou/plugins/plugin_transfer_file.kv cagou/plugins/plugin_transfer_file.py
diffstat 2 files changed, 48 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/plugins/plugin_transfer_file.kv	Wed Mar 20 09:29:44 2019 +0100
+++ b/cagou/plugins/plugin_transfer_file.kv	Wed Mar 20 09:29:44 2019 +0100
@@ -18,18 +18,18 @@
 #:import platform kivy.utils.platform
 
 
-<FileTransmitter>:
+<FileChooserBox>:
     orientation: "vertical"
     FileChooserListView:
         id: filechooser
-        rootpath: "/" if platform == 'android' else expanduser('~')
+        path: root.default_path
     Button:
         text: "choose"
         size_hint: 1, None
         height: dp(50)
-        on_release: root.onTransmitOK(filechooser)
+        on_release: root.callback(filechooser.selection)
     Button:
         text: "cancel"
         size_hint: 1, None
         height: dp(50)
-        on_release: root.cancel_cb(root)
+        on_release: root.cancel_cb()
--- a/cagou/plugins/plugin_transfer_file.py	Wed Mar 20 09:29:44 2019 +0100
+++ b/cagou/plugins/plugin_transfer_file.py	Wed Mar 20 09:29:44 2019 +0100
@@ -17,12 +17,17 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-
+import threading
+import sys
+from functools import partial
 from sat.core import log as logging
-log = logging.getLogger(__name__)
 from sat.core.i18n import _
 from kivy.uix.boxlayout import BoxLayout
 from kivy import properties
+from kivy.clock import Clock
+from plyer import filechooser, storagepath
+
+log = logging.getLogger(__name__)
 
 
 PLUGIN_INFO = {
@@ -33,11 +38,44 @@
 }
 
 
+class FileChooserBox(BoxLayout):
+    callback = properties.ObjectProperty()
+    cancel_cb = properties.ObjectProperty()
+    default_path = properties.StringProperty()
+
+
 class FileTransmitter(BoxLayout):
     callback = properties.ObjectProperty()
     cancel_cb = properties.ObjectProperty()
+    native_filechooser = True
+    default_path = storagepath.get_home_dir()
 
-    def onTransmitOK(self, filechooser):
-        if filechooser.selection:
-            file_path = filechooser.selection[0]
-            self.callback(file_path)
+    def __init__(self, *args, **kwargs):
+        if sys.platform == 'android':
+            self.native_filechooser = False
+            self.default_path = storagepath.get_downloads_dir()
+
+        super(FileTransmitter, self).__init__(*args, **kwargs)
+
+        if self.native_filechooser:
+            thread = threading.Thread(target=self._nativeFileChooser)
+            thread.start()
+        else:
+            self.add_widget(FileChooserBox(default_path = self.default_path,
+                                           callback=self.onFiles,
+                                           cancel_cb=partial(self.cancel_cb, self)))
+
+    def _nativeFileChooser(self, *args, **kwargs):
+        title=_(u"Please select a file to upload")
+        files = filechooser.open_file(title=title,
+                                      path=self.default_path,
+                                      multiple=False,
+                                      preview=True)
+        # we want to leave the thread when calling onFiles, so we use Clock
+        Clock.schedule_once(lambda *args: self.onFiles(files=files), 0)
+
+    def onFiles(self, files):
+        if files:
+            self.callback(files[0])
+        else:
+            self.cancel_cb(self)