comparison src/plugins/plugin_xep_0048.py @ 986:224cafc67324

jp: added bookmarks subcommands
author Goffi <goffi@goffi.org>
date Mon, 07 Apr 2014 16:24:29 +0200
parents 9ebdba4ab907
children 93359853e4bc
comparison
equal deleted inserted replaced
985:9ebdba4ab907 986:224cafc67324
56 self.host = host 56 self.host = host
57 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True) 57 # self.__menu_id = host.registerCallback(self._bookmarksMenu, with_data=True)
58 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True) 58 self.__bm_save_id = host.registerCallback(self._bookmarksSaveCb, with_data=True)
59 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks")) 59 host.importMenu((D_("Communication"), D_("bookmarks")), self._bookmarksMenu, security_limit=0, help_string=D_("Use and manage bookmarks"))
60 self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True) 60 self.__selected_id = host.registerCallback(self._bookmarkSelectedCb, with_data=True)
61 host.bridge.addMethod("bookmarksList", ".plugin", in_sign='sss', out_sign='a{sa{sa{ss}}}', method=self._bookmarksList, async=True)
62 host.bridge.addMethod("bookmarksRemove", ".plugin", in_sign='ssss', out_sign='', method=self._bookmarksRemove, async=True)
63 host.bridge.addMethod("bookmarksAdd", ".plugin", in_sign='ssa{ss}ss', out_sign='', method=self._bookmarksAdd, async=True)
61 try: 64 try:
62 self.private_plg = self.host.plugins["XEP-0049"] 65 self.private_plg = self.host.plugins["XEP-0049"]
63 except KeyError: 66 except KeyError:
64 self.private_plg = None 67 self.private_plg = None
65 try: 68 try:
289 - "private": Private XML storage (XEP-0049) 292 - "private": Private XML storage (XEP-0049)
290 - "local": Store in SàT database 293 - "local": Store in SàT database
291 @param profile_key: %(doc_profile_key)s 294 @param profile_key: %(doc_profile_key)s
292 """ 295 """
293 assert storage_type in ('auto', 'pubsub', 'private', 'local') 296 assert storage_type in ('auto', 'pubsub', 'private', 'local')
297 if type_ == XEP_0048.URL_TYPE and {'autojoin', 'nick'}.intersection(data.keys()):
298 raise ValueError("autojoin or nick can't be used with URLs")
294 client = self.host.getClient(profile_key) 299 client = self.host.getClient(profile_key)
295 if storage_type == 'auto': 300 if storage_type == 'auto':
296 if client.bookmarks_pubsub is not None: 301 if client.bookmarks_pubsub is not None:
297 storage_type = 'pubsub' 302 storage_type = 'pubsub'
298 elif client.bookmarks_private is not None: 303 elif client.bookmarks_private is not None:
346 debug("Bookmark is not present in private storage") 351 debug("Bookmark is not present in private storage")
347 352
348 if storage_type == 'pubsub': 353 if storage_type == 'pubsub':
349 raise NotImplementedError 354 raise NotImplementedError
350 355
356 def _bookmarksList(self, type_, storage_location, profile_key=C.PROF_KEY_NONE):
357 """Return stored bookmarks
358
359 @param type_: bookmark type, one of:
360 - XEP_0048.MUC_TYPE: Multi-User chat room
361 - XEP_0048.URL_TYPE: web page URL
362 @param storage_location: can be:
363 - 'all'
364 - 'local'
365 - 'private'
366 - 'pubsub'
367 @param profile_key: %(doc_profile_key)s
368 @param return (dict): (key: storage_location, value dict) with:
369 - value (dict): (key: bookmark_location, value: bookmark data)
370 """
371 client = self.host.getClient(profile_key)
372 ret = {}
373 ret_d = defer.succeed(ret)
374
375 def fillBookmarks(dummy, _storage_location):
376 bookmarks_ori = getattr(client, "bookmarks_" + _storage_location)
377 if bookmarks_ori is None:
378 return ret
379 data = bookmarks_ori[type_]
380 for bookmark in data:
381 ret[_storage_location][bookmark.full()] = data[bookmark].copy()
382 return ret
383
384 for _storage_location in ('local', 'private', 'pubsub'):
385 if storage_location in ('all', _storage_location):
386 ret[_storage_location] = {}
387 if _storage_location in ('private',):
388 # we update distant bookmarks, just in case an other client added something
389 d = self._getServerBookmarks(_storage_location, client.profile)
390 else:
391 d = defer.succeed(None)
392 d.addCallback(fillBookmarks, _storage_location)
393 ret_d.addCallback(lambda dummy: d)
394
395 return ret_d
396
397 def _bookmarksRemove(self, type_, location, storage_location, profile_key=C.PROF_KEY_NONE):
398 """Return stored bookmarks
399
400 @param type_: bookmark type, one of:
401 - XEP_0048.MUC_TYPE: Multi-User chat room
402 - XEP_0048.URL_TYPE: web page URL
403 @param location: dependeding on type_, can be a MUC room jid or an url
404 @param storage_location: can be:
405 - "all": remove from everywhere
406 - "pubsub": PubSub private storage (XEP-0223)
407 - "private": Private XML storage (XEP-0049)
408 - "local": Store in SàT database
409 @param profile_key: %(doc_profile_key)s
410 """
411 if type_ == XEP_0048.MUC_TYPE:
412 location = jid.JID(location)
413 return self.removeBookmark(type_, location, storage_location, profile_key)
414
415 def _bookmarksAdd(self, type_, location, data, storage_type="auto", profile_key=C.PROF_KEY_NONE):
416 if type_ == XEP_0048.MUC_TYPE:
417 location = jid.JID(location)
418 return self.addBookmark(type_, location, data, storage_type, profile_key)
419
351 def cmd_bookmark(self, mess_data, profile): 420 def cmd_bookmark(self, mess_data, profile):
352 """(Un)bookmark a MUC room 421 """(Un)bookmark a MUC room
353 422
354 @command (group): [autojoin | remove] 423 @command (group): [autojoin | remove]
355 - autojoin: join room automatically on connection 424 - autojoin: join room automatically on connection