Mercurial > libervia-desktop-kivy
annotate cagou/plugins/plugin_wid_file_sharing.py @ 242:c2503168fab7
plugin chat (encryption): added a button to display trust management UI.
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 03 Jan 2019 10:52:57 +0100 |
parents | 7918a5668304 |
children | 0700293df0e6 |
rev | line source |
---|---|
238
7918a5668304
plugin remote: remote controllers plugin first draft:
Goffi <goffi@goffi.org>
parents:
237
diff
changeset
|
1 #!/usr/bin/env python2 |
192 | 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 | |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
222
diff
changeset
|
30 from cagou.core.menu import EntitiesSelectorMenu, TouchMenuBehaviour |
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 |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
222
diff
changeset
|
32 from cagou.core.common_widgets import (Identities, ItemWidget, DeviceWidget, |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
222
diff
changeset
|
33 CategorySeparator) |
192 | 34 from cagou import G |
35 from kivy import properties | |
36 from kivy.uix.label import Label | |
37 from kivy.uix.button import Button | |
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
|
38 from kivy import utils as kivy_utils |
192 | 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 |
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
|
54 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
|
55 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
|
56 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
|
57 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
|
58 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
|
59 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
|
60 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
|
61 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
|
62 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
|
63 expanduser = os.path.expanduser |
192 | 64 |
65 | |
66 class ModeBtn(Button): | |
67 | |
68 def __init__(self, parent, **kwargs): | |
69 super(ModeBtn, self).__init__(**kwargs) | |
70 parent.bind(mode=self.on_mode) | |
71 self.on_mode(parent, parent.mode) | |
72 | |
73 def on_mode(self, parent, new_mode): | |
74 if new_mode == MODE_VIEW: | |
75 self.text = _(u"view shared files") | |
76 elif new_mode == MODE_LOCAL: | |
77 self.text = _(u"share local files") | |
78 else: | |
79 exceptions.InternalError(u"Unknown mode: {mode}".format(mode=new_mode)) | |
80 | |
81 | |
82 class PathWidget(ItemWidget): | |
83 | |
222 | 84 def __init__(self, filepath, main_wid, **kw): |
192 | 85 name = os.path.basename(filepath) |
86 self.filepath = os.path.normpath(filepath) | |
87 if self.filepath == u'.': | |
88 self.filepath = u'' | |
222 | 89 super(PathWidget, self).__init__(name=name, main_wid=main_wid, **kw) |
192 | 90 |
91 @property | |
92 def is_dir(self): | |
93 raise NotImplementedError | |
94 | |
95 def do_item_action(self, touch): | |
96 if self.is_dir: | |
222 | 97 self.main_wid.current_dir = self.filepath |
192 | 98 |
99 def open_menu(self, touch, dt): | |
100 log.debug(_(u"opening menu for {path}").format(path=self.filepath)) | |
101 super(PathWidget, self).open_menu(touch, dt) | |
102 | |
103 | |
104 class LocalPathWidget(PathWidget): | |
105 | |
106 @property | |
107 def is_dir(self): | |
108 return os.path.isdir(self.filepath) | |
109 | |
110 def getMenuChoices(self): | |
111 choices = [] | |
112 if self.shared: | |
113 choices.append(dict(text=_(u'unshare'), | |
114 index=len(choices)+1, | |
222 | 115 callback=self.main_wid.unshare)) |
192 | 116 else: |
117 choices.append(dict(text=_(u'share'), | |
118 index=len(choices)+1, | |
222 | 119 callback=self.main_wid.share)) |
192 | 120 return choices |
121 | |
122 | |
123 class RemotePathWidget(PathWidget): | |
124 | |
222 | 125 def __init__(self, main_wid, filepath, type_, **kw): |
192 | 126 self.type_ = type_ |
222 | 127 super(RemotePathWidget, self).__init__(filepath, main_wid=main_wid, **kw) |
192 | 128 |
129 @property | |
130 def is_dir(self): | |
131 return self.type_ == C.FILE_TYPE_DIRECTORY | |
132 | |
133 def do_item_action(self, touch): | |
134 if self.is_dir: | |
135 if self.filepath == u'..': | |
222 | 136 self.main_wid.remote_entity = u'' |
192 | 137 else: |
138 super(RemotePathWidget, self).do_item_action(touch) | |
139 else: | |
222 | 140 self.main_wid.request_item(self) |
192 | 141 return True |
142 | |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
222
diff
changeset
|
143 class SharingDeviceWidget(DeviceWidget): |
206
890b48e41998
plugin file sharing: use appropriate symbol when discovering devices
Goffi <goffi@goffi.org>
parents:
205
diff
changeset
|
144 |
192 | 145 def do_item_action(self, touch): |
222 | 146 self.main_wid.remote_entity = self.entity_jid |
147 self.main_wid.remote_dir = u'' | |
192 | 148 |
149 | |
222 | 150 class FileSharing(quick_widgets.QuickWidget, cagou_widget.CagouWidget, FilterBehavior, |
151 TouchMenuBehaviour): | |
192 | 152 SINGLE=False |
153 layout = properties.ObjectProperty() | |
154 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
|
155 local_dir = properties.StringProperty(expanduser(u'~')) |
192 | 156 remote_dir = properties.StringProperty(u'') |
157 remote_entity = properties.StringProperty(u'') | |
158 shared_paths = properties.ListProperty() | |
159 signals_registered = False | |
160 | |
161 def __init__(self, host, target, profiles): | |
162 quick_widgets.QuickWidget.__init__(self, host, target, profiles) | |
163 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
|
164 FilterBehavior.__init__(self) |
222 | 165 TouchMenuBehaviour.__init__(self) |
192 | 166 self.mode_btn = ModeBtn(self) |
167 self.mode_btn.bind(on_release=self.change_mode) | |
168 self.headerInputAddExtra(self.mode_btn) | |
169 self.bind(local_dir=self.update_view, | |
170 remote_dir=self.update_view, | |
171 remote_entity=self.update_view) | |
172 self.update_view() | |
173 if not FileSharing.signals_registered: | |
174 # FIXME: we use this hack (registering the signal for the whole class) now | |
175 # as there is currently no unregisterSignal available in bridges | |
217
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
176 G.host.registerSignal("FISSharedPathNew", |
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
177 handler=FileSharing.shared_path_new, |
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
178 iface="plugin") |
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
179 G.host.registerSignal("FISSharedPathRemoved", |
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
180 handler=FileSharing.shared_path_removed, |
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
181 iface="plugin") |
192 | 182 FileSharing.signals_registered = True |
183 G.host.bridge.FISLocalSharesGet(self.profile, | |
184 callback=self.fill_paths, | |
185 errback=G.host.errback) | |
186 | |
187 @property | |
188 def current_dir(self): | |
189 return self.local_dir if self.mode == MODE_LOCAL else self.remote_dir | |
190 | |
191 @current_dir.setter | |
192 def current_dir(self, new_dir): | |
193 if self.mode == MODE_LOCAL: | |
194 self.local_dir = new_dir | |
195 else: | |
196 self.remote_dir = new_dir | |
197 | |
198 def fill_paths(self, shared_paths): | |
199 self.shared_paths.extend(shared_paths) | |
200 | |
201 def change_mode(self, mode_btn): | |
202 self.clear_menu() | |
203 opt = self.__class__.mode.options | |
204 new_idx = (opt.index(self.mode)+1) % len(opt) | |
205 self.mode = opt[new_idx] | |
206 | |
207 def on_mode(self, instance, new_mode): | |
208 self.update_view(None, self.local_dir) | |
209 | |
194
a68c9baa6694
plugin file sharing: use header hint to show current path, and open new path:
Goffi <goffi@goffi.org>
parents:
192
diff
changeset
|
210 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
|
211 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
|
212 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
|
213 |
196
519b3a29743c
utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
194
diff
changeset
|
214 def onHeaderInputComplete(self, wid, text, **kwargs): |
192 | 215 """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
|
216 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
|
217 return |
196
519b3a29743c
utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
194
diff
changeset
|
218 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
|
219 text, |
519b3a29743c
utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
194
diff
changeset
|
220 lambda c: c.name, |
519b3a29743c
utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
194
diff
changeset
|
221 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
|
222 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
|
223 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
|
224 lambda c: c.name == u'..']) |
519b3a29743c
utils, plugin file sharing: new utils module, with a FilterBehavior:
Goffi <goffi@goffi.org>
parents:
194
diff
changeset
|
225 |
192 | 226 |
227 ## remote sharing callback ## | |
228 | |
229 def _discoFindByFeaturesCb(self, data): | |
230 entities_services, entities_own, entities_roster = data | |
231 for entities_map, title in ((entities_services, | |
232 _(u'services')), | |
233 (entities_own, | |
234 _(u'your devices')), | |
235 (entities_roster, | |
236 _(u'your contacts devices'))): | |
237 if entities_map: | |
238 self.layout.add_widget(CategorySeparator(text=title)) | |
239 for entity_str, entity_ids in entities_map.iteritems(): | |
240 entity_jid = jid.JID(entity_str) | |
237
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
222
diff
changeset
|
241 item = SharingDeviceWidget( |
059c5b39032d
plugin file sharing: moved common discovery widgets to new core.common_widgets module
Goffi <goffi@goffi.org>
parents:
222
diff
changeset
|
242 self, entity_jid, Identities(entity_ids)) |
192 | 243 self.layout.add_widget(item) |
214
6a98d70a3a66
file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents:
213
diff
changeset
|
244 if not entities_services and not entities_own and not entities_roster: |
6a98d70a3a66
file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents:
213
diff
changeset
|
245 self.layout.add_widget(Label( |
6a98d70a3a66
file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents:
213
diff
changeset
|
246 size_hint=(1, 1), |
6a98d70a3a66
file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents:
213
diff
changeset
|
247 halign='center', |
6a98d70a3a66
file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents:
213
diff
changeset
|
248 text_size=self.size, |
6a98d70a3a66
file sharing: display a message when not sharing device has been found
Goffi <goffi@goffi.org>
parents:
213
diff
changeset
|
249 text=_(u"No sharing device found"))) |
192 | 250 |
251 def discover_devices(self): | |
252 """Looks for devices handling file "File Information Sharing" and display them""" | |
253 try: | |
254 namespace = self.host.ns_map['fis'] | |
255 except KeyError: | |
217
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
256 msg = _(u"can't find file information sharing namespace, " |
286f14127f61
plugin file sharing: started to limit line to 90 chars as a test
Goffi <goffi@goffi.org>
parents:
214
diff
changeset
|
257 u"is the plugin running?") |
192 | 258 log.warning(msg) |
259 G.host.addNote(_(u"missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR) | |
260 return | |
261 self.host.bridge.discoFindByFeatures( | |
199
b80d275e437f
plugin file sharing: use new local_device argument of discoFindByFeatures
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
262 [namespace], [], False, True, True, True, False, self.profile, |
192 | 263 callback=self._discoFindByFeaturesCb, |
264 errback=partial(G.host.errback, | |
265 title=_(u"shared folder error"), | |
266 message=_(u"can't check sharing devices: {msg}"))) | |
267 | |
268 def FISListCb(self, files_data): | |
269 for file_data in files_data: | |
270 filepath = os.path.join(self.current_dir, file_data[u'name']) | |
271 item = RemotePathWidget( | |
272 filepath=filepath, | |
222 | 273 main_wid=self, |
192 | 274 type_=file_data[u'type']) |
275 self.layout.add_widget(item) | |
276 | |
277 def FISListEb(self, failure_): | |
278 self.remote_dir = u'' | |
279 G.host.addNote( | |
280 _(u"shared folder error"), | |
281 _(u"can't list files for {remote_entity}: {msg}").format( | |
282 remote_entity=self.remote_entity, | |
283 msg=failure_), | |
284 level=C.XMLUI_DATA_LVL_WARNING) | |
285 | |
286 ## view generation ## | |
287 | |
288 def update_view(self, *args): | |
289 """update items according to current mode, entity and dir""" | |
290 log.debug(u'updating {}, {}'.format(self.current_dir, args)) | |
291 self.layout.clear_widgets() | |
292 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
|
293 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
|
294 |
192 | 295 if self.mode == MODE_LOCAL: |
296 filepath = os.path.join(self.local_dir, u'..') | |
222 | 297 self.layout.add_widget(LocalPathWidget(filepath=filepath, main_wid=self)) |
213
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
298 try: |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
299 files = sorted(os.listdir(self.local_dir)) |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
300 except OSError as e: |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
301 msg = _(u"can't list files in \"{local_dir}\": {msg}").format( |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
302 local_dir=self.local_dir, |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
303 msg=e) |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
304 G.host.addNote( |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
305 _(u"shared folder error"), |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
306 msg, |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
307 level=C.XMLUI_DATA_LVL_WARNING) |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
308 self.local_dir = expanduser(u'~') |
423d462ea739
file sharing: catch OS errors while trying to list files on a local dir
Goffi <goffi@goffi.org>
parents:
212
diff
changeset
|
309 return |
192 | 310 for f in files: |
311 filepath = os.path.join(self.local_dir, f) | |
222 | 312 self.layout.add_widget(LocalPathWidget(filepath=filepath, |
313 main_wid=self)) | |
192 | 314 elif self.mode == MODE_VIEW: |
315 if not self.remote_entity: | |
316 self.discover_devices() | |
317 else: | |
318 # we always a way to go back | |
319 # so user can return to previous list even in case of error | |
320 parent_path = os.path.join(self.remote_dir, u'..') | |
321 item = RemotePathWidget( | |
322 filepath = parent_path, | |
222 | 323 main_wid=self, |
192 | 324 type_ = C.FILE_TYPE_DIRECTORY) |
325 self.layout.add_widget(item) | |
326 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
|
327 unicode(self.remote_entity), |
192 | 328 self.remote_dir, |
329 {}, | |
330 self.profile, | |
331 callback=self.FISListCb, | |
332 errback=self.FISListEb) | |
333 | |
334 ## Share methods ## | |
335 | |
198
60b63c3e63a1
plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents:
196
diff
changeset
|
336 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
|
337 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
|
338 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
|
339 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
|
340 else: |
60b63c3e63a1
plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents:
196
diff
changeset
|
341 access = {} |
60b63c3e63a1
plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents:
196
diff
changeset
|
342 |
192 | 343 G.host.bridge.FISSharePath( |
344 item.name, | |
345 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
|
346 json.dumps(access, ensure_ascii=False), |
192 | 347 self.profile, |
348 callback=lambda name: G.host.addNote( | |
349 _(u"sharing folder"), | |
350 _(u"{name} is now shared").format(name=name)), | |
351 errback=partial(G.host.errback, | |
352 title=_(u"sharing folder"), | |
353 message=_(u"can't share folder: {msg}"))) | |
354 | |
198
60b63c3e63a1
plugin file sharing: use new EntitiesSelectorMenu to select entities which can access shared files
Goffi <goffi@goffi.org>
parents:
196
diff
changeset
|
355 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
|
356 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
|
357 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
|
358 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
|
359 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
|
360 |
192 | 361 def unshare(self, menu): |
362 item = self.menu_item | |
363 self.clear_menu() | |
364 G.host.bridge.FISUnsharePath( | |
365 item.filepath, | |
366 self.profile, | |
367 callback=lambda: G.host.addNote( | |
368 _(u"sharing folder"), | |
369 _(u"{name} is not shared anymore").format(name=item.name)), | |
370 errback=partial(G.host.errback, | |
371 title=_(u"sharing folder"), | |
372 message=_(u"can't unshare folder: {msg}"))) | |
373 | |
374 def fileJingleRequestCb(self, progress_id, item, dest_path): | |
375 G.host.addNote( | |
376 _(u"file request"), | |
377 _(u"{name} download started at {dest_path}").format( | |
378 name = item.name, | |
379 dest_path = dest_path)) | |
380 | |
381 def request_item(self, item): | |
382 """Retrieve an item from remote entity | |
383 | |
384 @param item(RemotePathWidget): item to retrieve | |
385 """ | |
386 path, name = os.path.split(item.filepath) | |
387 assert name | |
388 assert self.remote_entity | |
389 extra = {'path': path} | |
390 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
|
391 G.host.bridge.fileJingleRequest(unicode(self.remote_entity), |
192 | 392 dest_path, |
393 name, | |
394 u'', | |
395 u'', | |
396 extra, | |
397 self.profile, | |
398 callback=partial(self.fileJingleRequestCb, | |
399 item=item, | |
400 dest_path=dest_path), | |
401 errback=partial(G.host.errback, | |
402 title = _(u"file request error"), | |
403 message = _(u"can't request file: {msg}"))) | |
404 | |
405 @classmethod | |
406 def shared_path_new(cls, shared_path, name, profile): | |
407 for wid in G.host.getVisibleList(cls): | |
408 if shared_path not in wid.shared_paths: | |
409 wid.shared_paths.append(shared_path) | |
410 | |
411 @classmethod | |
412 def shared_path_removed(cls, shared_path, profile): | |
413 for wid in G.host.getVisibleList(cls): | |
414 if shared_path in wid.shared_paths: | |
415 wid.shared_paths.remove(shared_path) | |
416 else: | |
417 log.warning(_(u"shared path {path} not found in {widget}".format( | |
418 path = shared_path, widget = wid))) |