comparison cagou/plugins/plugin_wid_file_sharing.py @ 205:9cefc9f8efc9

plugin file sharing: use external storage directory instead of home (which is "/data") on Android when expanding "~"
author Goffi <goffi@goffi.org>
date Fri, 25 May 2018 12:01:47 +0200
parents e20796eea873
children 890b48e41998
comparison
equal deleted inserted replaced
204:37638765c97b 205:9cefc9f8efc9
35 from kivy.uix.button import Button 35 from kivy.uix.button import Button
36 from kivy.uix.boxlayout import BoxLayout 36 from kivy.uix.boxlayout import BoxLayout
37 from kivy.garden import modernmenu 37 from kivy.garden import modernmenu
38 from kivy.clock import Clock 38 from kivy.clock import Clock
39 from kivy.metrics import dp 39 from kivy.metrics import dp
40 from kivy import utils as kivy_utils
40 from functools import partial 41 from functools import partial
41 import os.path 42 import os.path
42 import json 43 import json
43 44
44 45
50 } 51 }
51 MODE_VIEW = u"view" 52 MODE_VIEW = u"view"
52 MODE_LOCAL = u"local" 53 MODE_LOCAL = u"local"
53 SELECT_INSTRUCTIONS = _(u"Please select entities to share with") 54 SELECT_INSTRUCTIONS = _(u"Please select entities to share with")
54 55
55 56 if kivy_utils.platform == "android":
56 dist = modernmenu.dist 57 from jnius import autoclass
58 Environment = autoclass("android.os.Environment")
59 base_dir = Environment.getExternalStorageDirectory().getAbsolutePath()
60 def expanduser(path):
61 if path == u'~' or path.startswith(u'~/'):
62 return path.replace(u'~', base_dir, 1)
63 return path
64 else:
65 expanduser = os.path.expanduser
57 66
58 67
59 class ModeBtn(Button): 68 class ModeBtn(Button):
60 69
61 def __init__(self, parent, **kwargs): 70 def __init__(self, parent, **kwargs):
211 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget, FilterBehavior): 220 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget, FilterBehavior):
212 SINGLE=False 221 SINGLE=False
213 float_layout = properties.ObjectProperty() 222 float_layout = properties.ObjectProperty()
214 layout = properties.ObjectProperty() 223 layout = properties.ObjectProperty()
215 mode = properties.OptionProperty(MODE_LOCAL, options=[MODE_VIEW, MODE_LOCAL]) 224 mode = properties.OptionProperty(MODE_LOCAL, options=[MODE_VIEW, MODE_LOCAL])
216 local_dir = properties.StringProperty(os.path.expanduser(u'~')) 225 local_dir = properties.StringProperty(expanduser(u'~'))
217 remote_dir = properties.StringProperty(u'') 226 remote_dir = properties.StringProperty(u'')
218 remote_entity = properties.StringProperty(u'') 227 remote_entity = properties.StringProperty(u'')
219 shared_paths = properties.ListProperty() 228 shared_paths = properties.ListProperty()
220 signals_registered = False 229 signals_registered = False
221 230
262 opt = self.__class__.mode.options 271 opt = self.__class__.mode.options
263 new_idx = (opt.index(self.mode)+1) % len(opt) 272 new_idx = (opt.index(self.mode)+1) % len(opt)
264 self.mode = opt[new_idx] 273 self.mode = opt[new_idx]
265 274
266 def on_mode(self, instance, new_mode): 275 def on_mode(self, instance, new_mode):
267 print(instance)
268 self.update_view(None, self.local_dir) 276 self.update_view(None, self.local_dir)
269 277
270 def onHeaderInput(self): 278 def onHeaderInput(self):
271 if u'/' in self.header_input.text or self.header_input.text == u'~': 279 if u'/' in self.header_input.text or self.header_input.text == u'~':
272 self.current_dir = os.path.expanduser(self.header_input.text) 280 self.current_dir = expanduser(self.header_input.text)
273 281
274 def onHeaderInputComplete(self, wid, text, **kwargs): 282 def onHeaderInputComplete(self, wid, text, **kwargs):
275 """we filter items when text is entered in input box""" 283 """we filter items when text is entered in input box"""
276 if u'/' in text: 284 if u'/' in text:
277 return 285 return