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