diff src/plugins/plugin_xep_0054.py @ 372:f964dcec1611

core: plugins refactored according to bridge + updatedValue now use profile
author Goffi <goffi@goffi.org>
date Wed, 06 Jul 2011 01:06:18 +0200
parents efbfccfed623
children c243f4cb2ad9
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0054.py	Wed Jul 06 01:04:24 2011 +0200
+++ b/src/plugins/plugin_xep_0054.py	Wed Jul 06 01:06:18 2011 +0200
@@ -71,17 +71,21 @@
         self.vcard_cache = host.memory.getPrivate("vcard_cache") or {}  #used to store nicknames and avatar, key = jid
         if not os.path.exists(self.avatar_path):
             os.makedirs(self.avatar_path)
-        host.bridge.addMethod("getCard", ".communication", in_sign='ss', out_sign='s', method=self.getCard)
-        host.bridge.addMethod("getAvatarFile", ".communication", in_sign='s', out_sign='s', method=self.getAvatarFile)
-        host.bridge.addMethod("getCardCache", ".communication", in_sign='s', out_sign='a{ss}', method=self.getCardCache)
+        host.bridge.addMethod("getCard", ".plugin", in_sign='ss', out_sign='s', method=self.getCard)
+        host.bridge.addMethod("getAvatarFile", ".plugin", in_sign='s', out_sign='s', method=self.getAvatarFile)
+        host.bridge.addMethod("getCardCache", ".plugin", in_sign='s', out_sign='a{ss}', method=self.getCardCache)
 
     def getHandler(self, profile):
         return XEP_0054_handler(self)  
    
-    def update_cache(self, jid, name, value):
+    def update_cache(self, jid, name, value, profile):
         """update cache value
         - save value in memory in case of change
         - send updatedValue signal if the value is new or updated
+        @param jid: jid of the owner of the vcard
+        @param name: name of the item which changed
+        @param value: new value of the item
+        @param profile: profile which received the update
         """
         if not self.vcard_cache.has_key(jid.userhost()):
             self.vcard_cache[jid.userhost()] = {}
@@ -91,7 +95,7 @@
         if not old_value or value != old_value:
             cache[name] = value
             self.host.memory.setPrivate("vcard_cache", self.vcard_cache)
-            self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value})
+            self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value}, profile)
 
     def get_cache(self, jid, name):
         """return cached value for jid
@@ -123,7 +127,7 @@
                 return hash
 
     @defer.deferredGenerator
-    def vCard2Dict(self, vcard, target):
+    def vCard2Dict(self, vcard, target, profile):
         """Convert a VCard to a dict, and save binaries"""
         debug (_("parsing vcard"))
         dictionary = {}
@@ -134,7 +138,7 @@
                 dictionary['fullname'] = unicode(elem)
             elif elem.name == 'NICKNAME':
                 dictionary['nick'] = unicode(elem)
-                self.update_cache(target, 'nick', dictionary['nick'])
+                self.update_cache(target, 'nick', dictionary['nick'], profile)
             elif elem.name == 'URL':
                 dictionary['website'] = unicode(elem)
             elif elem.name == 'EMAIL':
@@ -149,18 +153,18 @@
                 if not dictionary["avatar"]:  #can happen in case of e.g. empty photo elem
                     del dictionary['avatar']
                 else:
-                    self.update_cache(target, 'avatar', dictionary['avatar'])
+                    self.update_cache(target, 'avatar', dictionary['avatar'], profile)
             else:
                 info (_('FIXME: [%s] VCard tag is not managed yet') % elem.name)
 
         yield dictionary
 
-    def vcard_ok(self, answer):
+    def vcard_ok(self, answer, profile):
         """Called after the first get IQ"""
         debug (_("VCard found"))
 
         if answer.firstChildElement().name == "vCard":
-            d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]))
+            d = self.vCard2Dict(answer.firstChildElement(), jid.JID(answer["from"]), profile)
             d.addCallback(lambda data: self.host.bridge.actionResult("RESULT", answer['id'], data))
         else:
             error (_("FIXME: vCard not found as first child element"))
@@ -179,13 +183,14 @@
         if not xmlstream:
             error (_('Asking vcard for an non-existant or not connected profile'))
             return ""
+        profile = self.host.memory.getProfileName(profile_key)
         to_jid = jid.JID(target)
         debug(_("Asking for %s's VCard") % to_jid.userhost())
         reg_request=IQ(xmlstream,'get')
         reg_request["from"]=current_jid.full()
         reg_request["to"] = to_jid.userhost()
         query=reg_request.addElement('vCard', NS_VCARD)
-        reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err)
+        reg_request.send(to_jid.userhost()).addCallbacks(self.vcard_ok, self.vcard_err, [profile])
         return reg_request["id"] 
 
     def getAvatarFile(self, hash):