Mercurial > libervia-desktop-kivy
annotate cagou/plugins/plugin_wid_file_sharing.py @ 194:a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
- if header is empty, current path is shown as hint
- if text is entered, it's used to filter files/dirs in the current path
- if text is a path (i.e. if it contains a "/"), current path is changed to specified one when header is validated
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 23 May 2018 18:44:15 +0200 |
parents | 62198e00a2b7 |
children | 519b3a29743c |
rev | line source |
---|---|
192 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 # Cagou: desktop/mobile frontend for Salut à Toi XMPP client | |
5 # Copyright (C) 2016-2018 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 | |
21 from sat.core import log as logging | |
22 from sat.core import exceptions | |
23 log = logging.getLogger(__name__) | |
24 from sat.core.i18n import _ | |
25 from sat.tools.common import files_utils | |
26 from sat_frontends.quick_frontend import quick_widgets | |
27 from sat_frontends.tools import jid | |
28 from cagou.core.constants import Const as C | |
29 from cagou.core import cagou_widget | |
30 from cagou import G | |
31 from kivy import properties | |
32 from kivy.uix.label import Label | |
33 from kivy.uix.button import Button | |
34 from kivy.uix.boxlayout import BoxLayout | |
35 from kivy.garden import modernmenu | |
36 from kivy.clock import Clock | |
37 from kivy.metrics import dp | |
38 from kivy.animation import Animation | |
39 from functools import partial | |
40 import os.path | |
41 import json | |
42 | |
43 | |
44 PLUGIN_INFO = { | |
45 "name": _(u"file sharing"), | |
46 "main": "FileSharing", | |
47 "description": _(u"share/transfer files between devices"), | |
48 "icon_symbol": u"exchange", | |
49 } | |
50 MODE_VIEW = u"view" | |
51 MODE_LOCAL = u"local" | |
194
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
52 SELECT_INSTRUCTIONS = _(u"Please select entities to share with") |
192 | 53 |
54 | |
55 dist = modernmenu.dist | |
56 | |
57 | |
58 class ModeBtn(Button): | |
59 | |
60 def __init__(self, parent, **kwargs): | |
61 super(ModeBtn, self).__init__(**kwargs) | |
62 parent.bind(mode=self.on_mode) | |
63 self.on_mode(parent, parent.mode) | |
64 | |
65 def on_mode(self, parent, new_mode): | |
66 if new_mode == MODE_VIEW: | |
67 self.text = _(u"view shared files") | |
68 elif new_mode == MODE_LOCAL: | |
69 self.text = _(u"share local files") | |
70 else: | |
71 exceptions.InternalError(u"Unknown mode: {mode}".format(mode=new_mode)) | |
72 | |
73 | |
74 class Identities(object): | |
75 | |
76 def __init__(self, entity_ids): | |
77 identities = {} | |
78 for cat, type_, name in entity_ids: | |
79 identities.setdefault(cat, {}).setdefault(type_, []).append(name) | |
80 self.identities = identities | |
81 | |
82 @property | |
83 def name(self): | |
84 return self.identities.values()[0].values()[0][0] | |
85 | |
86 | |
87 class ItemWidget(BoxLayout): | |
88 click_timeout = properties.NumericProperty(0.4) | |
89 base_width = properties.NumericProperty(dp(100)) | |
90 | |
91 def __init__(self, sharing_wid, name): | |
92 self.sharing_wid = sharing_wid | |
93 self.name = name | |
94 super(ItemWidget, self).__init__() | |
95 | |
96 def on_touch_down(self, touch): | |
97 if not self.collide_point(*touch.pos): | |
98 return | |
99 t = partial(self.open_menu, touch) | |
100 touch.ud['menu_timeout'] = t | |
101 Clock.schedule_once(t, self.click_timeout) | |
102 return super(ItemWidget, self).on_touch_down(touch) | |
103 | |
104 def do_item_action(self, touch): | |
105 pass | |
106 | |
107 def on_touch_up(self, touch): | |
108 if touch.ud.get('menu_timeout'): | |
109 Clock.unschedule(touch.ud['menu_timeout']) | |
110 if self.collide_point(*touch.pos) and self.sharing_wid.menu is None: | |
111 self.do_item_action(touch) | |
112 return super(ItemWidget, self).on_touch_up(touch) | |
113 | |
114 def open_menu(self, touch, dt): | |
115 self.sharing_wid.open_menu(self, touch) | |
116 del touch.ud['menu_timeout'] | |
117 | |
118 def getMenuChoices(self): | |
119 """return choice adapted to selected item | |
120 | |
121 @return (list[dict]): choices ad expected by ModernMenu | |
122 """ | |
123 return [] | |
124 | |
125 | |
126 class PathWidget(ItemWidget): | |
127 | |
128 def __init__(self, sharing_wid, filepath): | |
129 name = os.path.basename(filepath) | |
130 self.filepath = os.path.normpath(filepath) | |
131 if self.filepath == u'.': | |
132 self.filepath = u'' | |
133 super(PathWidget, self).__init__(sharing_wid, name) | |
134 | |
135 @property | |
136 def is_dir(self): | |
137 raise NotImplementedError | |
138 | |
139 def do_item_action(self, touch): | |
140 if self.is_dir: | |
141 self.sharing_wid.current_dir = self.filepath | |
142 | |
143 def open_menu(self, touch, dt): | |
144 log.debug(_(u"opening menu for {path}").format(path=self.filepath)) | |
145 super(PathWidget, self).open_menu(touch, dt) | |
146 | |
147 | |
148 class LocalPathWidget(PathWidget): | |
149 | |
150 @property | |
151 def is_dir(self): | |
152 return os.path.isdir(self.filepath) | |
153 | |
154 def getMenuChoices(self): | |
155 choices = [] | |
156 if self.shared: | |
157 choices.append(dict(text=_(u'unshare'), | |
158 index=len(choices)+1, | |
159 callback=self.sharing_wid.unshare)) | |
160 else: | |
161 choices.append(dict(text=_(u'share'), | |
162 index=len(choices)+1, | |
163 callback=self.sharing_wid.share)) | |
164 return choices | |
165 | |
166 | |
167 class RemotePathWidget(PathWidget): | |
168 | |
169 def __init__(self, sharing_wid, filepath, type_): | |
170 self.type_ = type_ | |
171 super(RemotePathWidget, self).__init__(sharing_wid, filepath) | |
172 | |
173 @property | |
174 def is_dir(self): | |
175 return self.type_ == C.FILE_TYPE_DIRECTORY | |
176 | |
177 def do_item_action(self, touch): | |
178 if self.is_dir: | |
179 if self.filepath == u'..': | |
180 self.sharing_wid.remote_entity = u'' | |
181 else: | |
182 super(RemotePathWidget, self).do_item_action(touch) | |
183 else: | |
184 self.sharing_wid.request_item(self) | |
185 return True | |
186 | |
187 | |
188 class DeviceWidget(ItemWidget): | |
189 | |
190 def __init__(self, sharing_wid, entity_jid, identities): | |
191 self.entity_jid = entity_jid | |
192 self.identities = identities | |
193 self.own_device = entity_jid.bare == next(G.host.profiles.itervalues()).whoami | |
194 name = self.identities.name if self.own_device else self.entity_jid.node | |
195 super(DeviceWidget, self).__init__(sharing_wid, name) | |
196 | |
197 def do_item_action(self, touch): | |
198 self.sharing_wid.remote_entity = self.entity_jid | |
199 self.sharing_wid.remote_dir = u'' | |
200 | |
201 | |
202 class CategorySeparator(Label): | |
203 pass | |
204 | |
205 | |
206 class Menu(modernmenu.ModernMenu): | |
207 pass | |
208 | |
209 | |
210 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget): | |
211 SINGLE=False | |
212 float_layout = properties.ObjectProperty() | |
213 layout = properties.ObjectProperty() | |
214 mode = properties.OptionProperty(MODE_LOCAL, options=[MODE_VIEW, MODE_LOCAL]) | |
215 local_dir = properties.StringProperty(os.path.expanduser(u'~')) | |
216 remote_dir = properties.StringProperty(u'') | |
217 remote_entity = properties.StringProperty(u'') | |
218 shared_paths = properties.ListProperty() | |
219 signals_registered = False | |
220 | |
221 def __init__(self, host, target, profiles): | |
222 self._filter_last = u'' | |
223 self._filter_anim = Animation(width = 0, | |
224 height = 0, | |
225 opacity = 0, | |
226 d = 0.5) | |
227 quick_widgets.QuickWidget.__init__(self, host, target, profiles) | |
228 cagou_widget.CagouWidget.__init__(self) | |
229 self.mode_btn = ModeBtn(self) | |
230 self.mode_btn.bind(on_release=self.change_mode) | |
231 self.headerInputAddExtra(self.mode_btn) | |
232 self.bind(local_dir=self.update_view, | |
233 remote_dir=self.update_view, | |
234 remote_entity=self.update_view) | |
235 self.update_view() | |
236 self.menu = None | |
237 self.menu_item = None | |
238 self.float_layout.bind(children=self.clean_fl_children) | |
239 if not FileSharing.signals_registered: | |
240 # FIXME: we use this hack (registering the signal for the whole class) now | |
241 # as there is currently no unregisterSignal available in bridges | |
242 G.host.registerSignal("FISSharedPathNew", handler=FileSharing.shared_path_new, iface="plugin") | |
243 G.host.registerSignal("FISSharedPathRemoved", handler=FileSharing.shared_path_removed, iface="plugin") | |
244 FileSharing.signals_registered = True | |
245 G.host.bridge.FISLocalSharesGet(self.profile, | |
246 callback=self.fill_paths, | |
247 errback=G.host.errback) | |
248 | |
249 @property | |
250 def current_dir(self): | |
251 return self.local_dir if self.mode == MODE_LOCAL else self.remote_dir | |
252 | |
253 @current_dir.setter | |
254 def current_dir(self, new_dir): | |
255 if self.mode == MODE_LOCAL: | |
256 self.local_dir = new_dir | |
257 else: | |
258 self.remote_dir = new_dir | |
259 | |
260 def fill_paths(self, shared_paths): | |
261 self.shared_paths.extend(shared_paths) | |
262 | |
263 def change_mode(self, mode_btn): | |
264 self.clear_menu() | |
265 opt = self.__class__.mode.options | |
266 new_idx = (opt.index(self.mode)+1) % len(opt) | |
267 self.mode = opt[new_idx] | |
268 | |
269 def on_mode(self, instance, new_mode): | |
270 print(instance) | |
271 self.update_view(None, self.local_dir) | |
272 | |
194
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
273 def onHeaderInput(self): |
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
274 if u'/' in self.header_input.text or self.header_input.text == u'~': |
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
275 self.current_dir = os.path.expanduser(self.header_input.text) |
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
276 |
192 | 277 def onHeaderInputComplete(self, wid, text): |
278 """we filter items when text is entered in input box""" | |
279 text = text.strip().lower() | |
194
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
280 if u'/' in text: |
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
281 return |
192 | 282 filtering = len(text)>len(self._filter_last) |
283 self._filter_last = text | |
284 for child in self.layout.children: | |
285 if not isinstance(child, ItemWidget): | |
286 continue | |
287 if child.name == u'..': | |
288 continue | |
289 if text in child.name.lower(): | |
290 self._filter_anim.cancel(child) | |
291 child.width = child.base_width | |
292 child.height = child.minimum_height | |
293 child.opacity = 1 | |
294 elif (filtering | |
295 and child.opacity > 0 | |
296 and not self._filter_anim.have_properties_to_animate(child)): | |
297 self._filter_anim.start(child) | |
298 | |
299 ## remote sharing callback ## | |
300 | |
301 def _discoFindByFeaturesCb(self, data): | |
302 entities_services, entities_own, entities_roster = data | |
303 for entities_map, title in ((entities_services, | |
304 _(u'services')), | |
305 (entities_own, | |
306 _(u'your devices')), | |
307 (entities_roster, | |
308 _(u'your contacts devices'))): | |
309 if entities_map: | |
310 self.layout.add_widget(CategorySeparator(text=title)) | |
311 for entity_str, entity_ids in entities_map.iteritems(): | |
312 entity_jid = jid.JID(entity_str) | |
313 item = DeviceWidget(self, | |
314 entity_jid, | |
315 Identities(entity_ids)) | |
316 self.layout.add_widget(item) | |
317 | |
318 def discover_devices(self): | |
319 """Looks for devices handling file "File Information Sharing" and display them""" | |
320 try: | |
321 namespace = self.host.ns_map['fis'] | |
322 except KeyError: | |
323 msg = _(u"can't find file information sharing namespace, is the plugin running?") | |
324 log.warning(msg) | |
325 G.host.addNote(_(u"missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR) | |
326 return | |
327 self.host.bridge.discoFindByFeatures( | |
328 [namespace], [], False, True, True, True, self.profile, | |
329 callback=self._discoFindByFeaturesCb, | |
330 errback=partial(G.host.errback, | |
331 title=_(u"shared folder error"), | |
332 message=_(u"can't check sharing devices: {msg}"))) | |
333 | |
334 def FISListCb(self, files_data): | |
335 for file_data in files_data: | |
336 filepath = os.path.join(self.current_dir, file_data[u'name']) | |
337 item = RemotePathWidget( | |
338 self, | |
339 filepath=filepath, | |
340 type_=file_data[u'type']) | |
341 self.layout.add_widget(item) | |
342 | |
343 def FISListEb(self, failure_): | |
344 self.remote_dir = u'' | |
345 G.host.addNote( | |
346 _(u"shared folder error"), | |
347 _(u"can't list files for {remote_entity}: {msg}").format( | |
348 remote_entity=self.remote_entity, | |
349 msg=failure_), | |
350 level=C.XMLUI_DATA_LVL_WARNING) | |
351 | |
352 ## view generation ## | |
353 | |
354 def update_view(self, *args): | |
355 """update items according to current mode, entity and dir""" | |
356 log.debug(u'updating {}, {}'.format(self.current_dir, args)) | |
357 self.layout.clear_widgets() | |
358 self.header_input.text = u'' | |
194
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
359 self.header_input.hint_text = self.current_dir |
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
360 |
192 | 361 if self.mode == MODE_LOCAL: |
362 filepath = os.path.join(self.local_dir, u'..') | |
363 self.layout.add_widget(LocalPathWidget(sharing_wid=self, filepath=filepath)) | |
364 files = sorted(os.listdir(self.local_dir)) | |
365 for f in files: | |
366 filepath = os.path.join(self.local_dir, f) | |
367 self.layout.add_widget(LocalPathWidget(sharing_wid=self, filepath=filepath)) | |
368 elif self.mode == MODE_VIEW: | |
369 if not self.remote_entity: | |
370 self.discover_devices() | |
371 else: | |
372 # we always a way to go back | |
373 # so user can return to previous list even in case of error | |
374 parent_path = os.path.join(self.remote_dir, u'..') | |
375 item = RemotePathWidget( | |
376 self, | |
377 filepath = parent_path, | |
378 type_ = C.FILE_TYPE_DIRECTORY) | |
379 self.layout.add_widget(item) | |
380 self.host.bridge.FISList( | |
381 self.remote_entity, | |
382 self.remote_dir, | |
383 {}, | |
384 self.profile, | |
385 callback=self.FISListCb, | |
386 errback=self.FISListEb) | |
387 | |
388 ## menu methods ## | |
389 | |
390 def clean_fl_children(self, layout, children): | |
391 """insure that self.menu and self.menu_item are None when menu is dimissed""" | |
392 if self.menu is not None and self.menu not in children: | |
393 self.menu = self.menu_item = None | |
394 | |
395 def clear_menu(self): | |
396 """remove menu if there is one""" | |
397 if self.menu is not None: | |
398 self.menu.dismiss() | |
399 self.menu = None | |
400 self.menu_item = None | |
401 | |
402 def open_menu(self, item, touch): | |
403 """open menu for item | |
404 | |
405 @param item(PathWidget): item when the menu has been requested | |
406 @param touch(kivy.input.MotionEvent): touch data | |
407 """ | |
408 if self.menu_item == item: | |
409 return | |
410 self.clear_menu() | |
411 pos = self.to_widget(*touch.pos) | |
412 choices = item.getMenuChoices() | |
413 if not choices: | |
414 return | |
415 self.menu = Menu(choices=choices, | |
416 center=pos, | |
417 size_hint=(None, None)) | |
418 self.float_layout.add_widget(self.menu) | |
419 self.menu.start_display(touch) | |
420 self.menu_item = item | |
421 | |
422 ## Share methods ## | |
423 | |
424 def share(self, menu): | |
425 item = self.menu_item | |
426 self.clear_menu() | |
427 G.host.bridge.FISSharePath( | |
428 item.name, | |
429 item.filepath, | |
430 json.dumps({}, ensure_ascii=False), | |
431 self.profile, | |
432 callback=lambda name: G.host.addNote( | |
433 _(u"sharing folder"), | |
434 _(u"{name} is now shared").format(name=name)), | |
435 errback=partial(G.host.errback, | |
436 title=_(u"sharing folder"), | |
437 message=_(u"can't share folder: {msg}"))) | |
438 | |
439 def unshare(self, menu): | |
440 item = self.menu_item | |
441 self.clear_menu() | |
442 G.host.bridge.FISUnsharePath( | |
443 item.filepath, | |
444 self.profile, | |
445 callback=lambda: G.host.addNote( | |
446 _(u"sharing folder"), | |
447 _(u"{name} is not shared anymore").format(name=item.name)), | |
448 errback=partial(G.host.errback, | |
449 title=_(u"sharing folder"), | |
450 message=_(u"can't unshare folder: {msg}"))) | |
451 | |
452 def fileJingleRequestCb(self, progress_id, item, dest_path): | |
453 G.host.addNote( | |
454 _(u"file request"), | |
455 _(u"{name} download started at {dest_path}").format( | |
456 name = item.name, | |
457 dest_path = dest_path)) | |
458 | |
459 def request_item(self, item): | |
460 """Retrieve an item from remote entity | |
461 | |
462 @param item(RemotePathWidget): item to retrieve | |
463 """ | |
464 path, name = os.path.split(item.filepath) | |
465 assert name | |
466 assert self.remote_entity | |
467 extra = {'path': path} | |
468 dest_path = files_utils.get_unique_name(os.path.join(G.host.downloads_dir, name)) | |
469 G.host.bridge.fileJingleRequest(self.remote_entity, | |
470 dest_path, | |
471 name, | |
472 u'', | |
473 u'', | |
474 extra, | |
475 self.profile, | |
476 callback=partial(self.fileJingleRequestCb, | |
477 item=item, | |
478 dest_path=dest_path), | |
479 errback=partial(G.host.errback, | |
480 title = _(u"file request error"), | |
481 message = _(u"can't request file: {msg}"))) | |
482 | |
483 @classmethod | |
484 def shared_path_new(cls, shared_path, name, profile): | |
485 for wid in G.host.getVisibleList(cls): | |
486 if shared_path not in wid.shared_paths: | |
487 wid.shared_paths.append(shared_path) | |
488 | |
489 @classmethod | |
490 def shared_path_removed(cls, shared_path, profile): | |
491 for wid in G.host.getVisibleList(cls): | |
492 if shared_path in wid.shared_paths: | |
493 wid.shared_paths.remove(shared_path) | |
494 else: | |
495 log.warning(_(u"shared path {path} not found in {widget}".format( | |
496 path = shared_path, widget = wid))) |