comparison cagou/plugins/plugin_wid_file_sharing.py @ 491:203755bbe0fe

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:32 +0200
parents 3c9ba4a694ef
children
comparison
equal deleted inserted replaced
490:962d17c4078c 491:203755bbe0fe
105 105
106 @property 106 @property
107 def is_dir(self): 107 def is_dir(self):
108 return os.path.isdir(self.filepath) 108 return os.path.isdir(self.filepath)
109 109
110 def getMenuChoices(self): 110 def get_menu_choices(self):
111 choices = [] 111 choices = []
112 if self.shared: 112 if self.shared:
113 choices.append(dict(text=_('unshare'), 113 choices.append(dict(text=_('unshare'),
114 index=len(choices)+1, 114 index=len(choices)+1,
115 callback=self.main_wid.unshare)) 115 callback=self.main_wid.unshare))
164 cagou_widget.CagouWidget.__init__(self) 164 cagou_widget.CagouWidget.__init__(self)
165 FilterBehavior.__init__(self) 165 FilterBehavior.__init__(self)
166 TouchMenuBehavior.__init__(self) 166 TouchMenuBehavior.__init__(self)
167 self.mode_btn = ModeBtn(self) 167 self.mode_btn = ModeBtn(self)
168 self.mode_btn.bind(on_release=self.change_mode) 168 self.mode_btn.bind(on_release=self.change_mode)
169 self.headerInputAddExtra(self.mode_btn) 169 self.header_input_add_extra(self.mode_btn)
170 self.bind(local_dir=self.update_view, 170 self.bind(local_dir=self.update_view,
171 remote_dir=self.update_view, 171 remote_dir=self.update_view,
172 remote_entity=self.update_view) 172 remote_entity=self.update_view)
173 self.update_view() 173 self.update_view()
174 if not FileSharing.signals_registered: 174 if not FileSharing.signals_registered:
175 # FIXME: we use this hack (registering the signal for the whole class) now 175 # FIXME: we use this hack (registering the signal for the whole class) now
176 # as there is currently no unregisterSignal available in bridges 176 # as there is currently no unregisterSignal available in bridges
177 G.host.registerSignal("FISSharedPathNew", 177 G.host.register_signal("fis_shared_path_new",
178 handler=FileSharing.shared_path_new, 178 handler=FileSharing.shared_path_new,
179 iface="plugin") 179 iface="plugin")
180 G.host.registerSignal("FISSharedPathRemoved", 180 G.host.register_signal("fis_shared_path_removed",
181 handler=FileSharing.shared_path_removed, 181 handler=FileSharing.shared_path_removed,
182 iface="plugin") 182 iface="plugin")
183 FileSharing.signals_registered = True 183 FileSharing.signals_registered = True
184 G.host.bridge.FISLocalSharesGet(self.profile, 184 G.host.bridge.fis_local_shares_get(self.profile,
185 callback=self.fill_paths, 185 callback=self.fill_paths,
186 errback=G.host.errback) 186 errback=G.host.errback)
187 187
188 @property 188 @property
189 def current_dir(self): 189 def current_dir(self):
206 self.mode = opt[new_idx] 206 self.mode = opt[new_idx]
207 207
208 def on_mode(self, instance, new_mode): 208 def on_mode(self, instance, new_mode):
209 self.update_view(None, self.local_dir) 209 self.update_view(None, self.local_dir)
210 210
211 def onHeaderInput(self): 211 def on_header_wid_input(self):
212 if '/' in self.header_input.text or self.header_input.text == '~': 212 if '/' in self.header_input.text or self.header_input.text == '~':
213 self.current_dir = expanduser(self.header_input.text) 213 self.current_dir = expanduser(self.header_input.text)
214 214
215 def onHeaderInputComplete(self, wid, text, **kwargs): 215 def on_header_wid_input_complete(self, wid, text, **kwargs):
216 """we filter items when text is entered in input box""" 216 """we filter items when text is entered in input box"""
217 if '/' in text: 217 if '/' in text:
218 return 218 return
219 self.do_filter(self.layout, 219 self.do_filter(self.layout,
220 text, 220 text,
225 lambda c: c.name == '..']) 225 lambda c: c.name == '..'])
226 226
227 227
228 ## remote sharing callback ## 228 ## remote sharing callback ##
229 229
230 def _discoFindByFeaturesCb(self, data): 230 def _disco_find_by_features_cb(self, data):
231 entities_services, entities_own, entities_roster = data 231 entities_services, entities_own, entities_roster = data
232 for entities_map, title in ((entities_services, 232 for entities_map, title in ((entities_services,
233 _('services')), 233 _('services')),
234 (entities_own, 234 (entities_own,
235 _('your devices')), 235 _('your devices')),
255 namespace = self.host.ns_map['fis'] 255 namespace = self.host.ns_map['fis']
256 except KeyError: 256 except KeyError:
257 msg = _("can't find file information sharing namespace, " 257 msg = _("can't find file information sharing namespace, "
258 "is the plugin running?") 258 "is the plugin running?")
259 log.warning(msg) 259 log.warning(msg)
260 G.host.addNote(_("missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR) 260 G.host.add_note(_("missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR)
261 return 261 return
262 self.host.bridge.discoFindByFeatures( 262 self.host.bridge.disco_find_by_features(
263 [namespace], [], False, True, True, True, False, self.profile, 263 [namespace], [], False, True, True, True, False, self.profile,
264 callback=self._discoFindByFeaturesCb, 264 callback=self._disco_find_by_features_cb,
265 errback=partial(G.host.errback, 265 errback=partial(G.host.errback,
266 title=_("shared folder error"), 266 title=_("shared folder error"),
267 message=_("can't check sharing devices: {msg}"))) 267 message=_("can't check sharing devices: {msg}")))
268 268
269 def FISListCb(self, files_data): 269 def fis_list_cb(self, files_data):
270 for file_data in files_data: 270 for file_data in files_data:
271 filepath = os.path.join(self.current_dir, file_data['name']) 271 filepath = os.path.join(self.current_dir, file_data['name'])
272 item = RemotePathWidget( 272 item = RemotePathWidget(
273 filepath=filepath, 273 filepath=filepath,
274 main_wid=self, 274 main_wid=self,
275 type_=file_data['type']) 275 type_=file_data['type'])
276 self.layout.add_widget(item) 276 self.layout.add_widget(item)
277 277
278 def FISListEb(self, failure_): 278 def fis_list_eb(self, failure_):
279 self.remote_dir = '' 279 self.remote_dir = ''
280 G.host.addNote( 280 G.host.add_note(
281 _("shared folder error"), 281 _("shared folder error"),
282 _("can't list files for {remote_entity}: {msg}").format( 282 _("can't list files for {remote_entity}: {msg}").format(
283 remote_entity=self.remote_entity, 283 remote_entity=self.remote_entity,
284 msg=failure_), 284 msg=failure_),
285 level=C.XMLUI_DATA_LVL_WARNING) 285 level=C.XMLUI_DATA_LVL_WARNING)
300 files = sorted(os.listdir(self.local_dir)) 300 files = sorted(os.listdir(self.local_dir))
301 except OSError as e: 301 except OSError as e:
302 msg = _("can't list files in \"{local_dir}\": {msg}").format( 302 msg = _("can't list files in \"{local_dir}\": {msg}").format(
303 local_dir=self.local_dir, 303 local_dir=self.local_dir,
304 msg=e) 304 msg=e)
305 G.host.addNote( 305 G.host.add_note(
306 _("shared folder error"), 306 _("shared folder error"),
307 msg, 307 msg,
308 level=C.XMLUI_DATA_LVL_WARNING) 308 level=C.XMLUI_DATA_LVL_WARNING)
309 self.local_dir = expanduser('~') 309 self.local_dir = expanduser('~')
310 return 310 return
322 item = RemotePathWidget( 322 item = RemotePathWidget(
323 filepath = parent_path, 323 filepath = parent_path,
324 main_wid=self, 324 main_wid=self,
325 type_ = C.FILE_TYPE_DIRECTORY) 325 type_ = C.FILE_TYPE_DIRECTORY)
326 self.layout.add_widget(item) 326 self.layout.add_widget(item)
327 self.host.bridge.FISList( 327 self.host.bridge.fis_list(
328 str(self.remote_entity), 328 str(self.remote_entity),
329 self.remote_dir, 329 self.remote_dir,
330 {}, 330 {},
331 self.profile, 331 self.profile,
332 callback=self.FISListCb, 332 callback=self.fis_list_cb,
333 errback=self.FISListEb) 333 errback=self.fis_list_eb)
334 334
335 ## Share methods ## 335 ## Share methods ##
336 336
337 def do_share(self, entities_jids, item): 337 def do_share(self, entities_jids, item):
338 if entities_jids: 338 if entities_jids:
339 access = {'read': {'type': 'whitelist', 339 access = {'read': {'type': 'whitelist',
340 'jids': entities_jids}} 340 'jids': entities_jids}}
341 else: 341 else:
342 access = {} 342 access = {}
343 343
344 G.host.bridge.FISSharePath( 344 G.host.bridge.fis_share_path(
345 item.name, 345 item.name,
346 item.filepath, 346 item.filepath,
347 json.dumps(access, ensure_ascii=False), 347 json.dumps(access, ensure_ascii=False),
348 self.profile, 348 self.profile,
349 callback=lambda name: G.host.addNote( 349 callback=lambda name: G.host.add_note(
350 _("sharing folder"), 350 _("sharing folder"),
351 _("{name} is now shared").format(name=name)), 351 _("{name} is now shared").format(name=name)),
352 errback=partial(G.host.errback, 352 errback=partial(G.host.errback,
353 title=_("sharing folder"), 353 title=_("sharing folder"),
354 message=_("can't share folder: {msg}"))) 354 message=_("can't share folder: {msg}")))
360 callback=partial(self.do_share, item=item)).show() 360 callback=partial(self.do_share, item=item)).show()
361 361
362 def unshare(self, menu): 362 def unshare(self, menu):
363 item = self.menu_item 363 item = self.menu_item
364 self.clear_menu() 364 self.clear_menu()
365 G.host.bridge.FISUnsharePath( 365 G.host.bridge.fis_unshare_path(
366 item.filepath, 366 item.filepath,
367 self.profile, 367 self.profile,
368 callback=lambda: G.host.addNote( 368 callback=lambda: G.host.add_note(
369 _("sharing folder"), 369 _("sharing folder"),
370 _("{name} is not shared anymore").format(name=item.name)), 370 _("{name} is not shared anymore").format(name=item.name)),
371 errback=partial(G.host.errback, 371 errback=partial(G.host.errback,
372 title=_("sharing folder"), 372 title=_("sharing folder"),
373 message=_("can't unshare folder: {msg}"))) 373 message=_("can't unshare folder: {msg}")))
374 374
375 def fileJingleRequestCb(self, progress_id, item, dest_path): 375 def file_jingle_request_cb(self, progress_id, item, dest_path):
376 G.host.addNote( 376 G.host.add_note(
377 _("file request"), 377 _("file request"),
378 _("{name} download started at {dest_path}").format( 378 _("{name} download started at {dest_path}").format(
379 name = item.name, 379 name = item.name,
380 dest_path = dest_path)) 380 dest_path = dest_path))
381 381
387 path, name = os.path.split(item.filepath) 387 path, name = os.path.split(item.filepath)
388 assert name 388 assert name
389 assert self.remote_entity 389 assert self.remote_entity
390 extra = {'path': path} 390 extra = {'path': path}
391 dest_path = files_utils.get_unique_name(os.path.join(G.host.downloads_dir, name)) 391 dest_path = files_utils.get_unique_name(os.path.join(G.host.downloads_dir, name))
392 G.host.bridge.fileJingleRequest(str(self.remote_entity), 392 G.host.bridge.file_jingle_request(str(self.remote_entity),
393 str(dest_path), 393 str(dest_path),
394 name, 394 name,
395 '', 395 '',
396 '', 396 '',
397 extra, 397 extra,
398 self.profile, 398 self.profile,
399 callback=partial(self.fileJingleRequestCb, 399 callback=partial(self.file_jingle_request_cb,
400 item=item, 400 item=item,
401 dest_path=dest_path), 401 dest_path=dest_path),
402 errback=partial(G.host.errback, 402 errback=partial(G.host.errback,
403 title = _("file request error"), 403 title = _("file request error"),
404 message = _("can't request file: {msg}"))) 404 message = _("can't request file: {msg}")))
405 405
406 @classmethod 406 @classmethod
407 def shared_path_new(cls, shared_path, name, profile): 407 def shared_path_new(cls, shared_path, name, profile):
408 for wid in G.host.getVisibleList(cls): 408 for wid in G.host.get_visible_list(cls):
409 if shared_path not in wid.shared_paths: 409 if shared_path not in wid.shared_paths:
410 wid.shared_paths.append(shared_path) 410 wid.shared_paths.append(shared_path)
411 411
412 @classmethod 412 @classmethod
413 def shared_path_removed(cls, shared_path, profile): 413 def shared_path_removed(cls, shared_path, profile):
414 for wid in G.host.getVisibleList(cls): 414 for wid in G.host.get_visible_list(cls):
415 if shared_path in wid.shared_paths: 415 if shared_path in wid.shared_paths:
416 wid.shared_paths.remove(shared_path) 416 wid.shared_paths.remove(shared_path)
417 else: 417 else:
418 log.warning(_("shared path {path} not found in {widget}".format( 418 log.warning(_("shared path {path} not found in {widget}".format(
419 path = shared_path, widget = wid))) 419 path = shared_path, widget = wid)))