changeset 435:c243f4cb2ad9

plugin XEP-0054: cache now use storage bridge: getCardCache updated to manage profile
author Goffi <goffi@goffi.org>
date Sun, 20 Nov 2011 15:56:51 +0100
parents d7e5df876a04
children 5e9d28ca5109
files frontends/src/bridge/DBus.py frontends/src/quick_frontend/quick_app.py src/bridge/bridge_constructor/dbus_frontend_template.py src/plugins/plugin_xep_0054.py
diffstat 4 files changed, 28 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Sun Nov 20 15:36:45 2011 +0100
+++ b/frontends/src/bridge/DBus.py	Sun Nov 20 15:56:51 2011 +0100
@@ -217,8 +217,8 @@
     def getCard(self, target, profile_key='@DEFAULT@'):
         return self.db_plugin_iface.getCard(target, profile_key)
 
-    def getCardCache(self, target):
-        return self.db_plugin_iface.getCardCache(target)
+    def getCardCache(self, target, profile_key='@DEFAULT@'):
+        return self.db_plugin_iface.getCardCache(target, profile_key)
 
     def getAvatarFile(self, hash):
         return self.db_plugin_iface.getAvatarFile(hash)
--- a/frontends/src/quick_frontend/quick_app.py	Sun Nov 20 15:36:45 2011 +0100
+++ b/frontends/src/quick_frontend/quick_app.py	Sun Nov 20 15:56:51 2011 +0100
@@ -284,7 +284,7 @@
             self.CM.update(from_jid, 'show', show)
             self.CM.update(from_jid, 'statuses', statuses)
             self.CM.update(from_jid, 'groups', groups)
-            cache = self.bridge.getCardCache(from_jid)
+            cache = self.bridge.getCardCache(from_jid, profile)
             if cache.has_key('nick'): 
                 self.CM.update(from_jid, 'nick', unicode(cache['nick']))
             if cache.has_key('avatar'): 
--- a/src/bridge/bridge_constructor/dbus_frontend_template.py	Sun Nov 20 15:36:45 2011 +0100
+++ b/src/bridge/bridge_constructor/dbus_frontend_template.py	Sun Nov 20 15:56:51 2011 +0100
@@ -110,8 +110,8 @@
     def getCard(self, target, profile_key='@DEFAULT@'):
         return self.db_plugin_iface.getCard(target, profile_key)
 
-    def getCardCache(self, target):
-        return self.db_plugin_iface.getCardCache(target)
+    def getCardCache(self, target, profile_key='@DEFAULT@'):
+        return self.db_plugin_iface.getCardCache(target, profile_key)
 
     def getAvatarFile(self, hash):
         return self.db_plugin_iface.getAvatarFile(hash)
--- a/src/plugins/plugin_xep_0054.py	Sun Nov 20 15:36:45 2011 +0100
+++ b/src/plugins/plugin_xep_0054.py	Sun Nov 20 15:56:51 2011 +0100
@@ -26,7 +26,6 @@
 from twisted.words.protocols.jabber import error as jab_error
 from twisted.words.protocols.jabber.xmlstream import IQ
 import os.path
-import pdb
 
 from zope.interface import implements
 
@@ -35,6 +34,7 @@
 from base64 import b64decode
 from hashlib import sha1
 from time import sleep
+from sat.memory.persistent import PersistentBinaryDict
 
 try:
     from twisted.words.protocols.xmlstream import XMPPHandler
@@ -68,7 +68,6 @@
         info(_("Plugin XEP_0054 initialization"))
         self.host = host
         self.avatar_path = os.path.join(self.host.memory.getConfig('', 'local_dir'), AVATAR_PATH)
-        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", ".plugin", in_sign='ss', out_sign='s', method=self.getCard)
@@ -87,23 +86,26 @@
         @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()] = {}
+        client = self.host.getClient(profile)
+        if not jid.userhost() in client._vcard_cache:
+            client._vcard_cache[jid.userhost()] = {}
         
-        cache = self.vcard_cache[jid.userhost()]
-        old_value = cache[name] if cache.has_key(name) else None
+        cache = client._vcard_cache[jid.userhost()]
+        old_value = cache[name] if name in cache else None
         if not old_value or value != old_value:
             cache[name] = value
-            self.host.memory.setPrivate("vcard_cache", self.vcard_cache)
+            client._vcard_cache.force(jid.userhost()) #we force saving of data to storage
             self.host.bridge.updatedValue('card_'+name, {'jid':jid.userhost(), name:value}, profile)
 
-    def get_cache(self, jid, name):
+    def get_cache(self, jid, name, profile):
         """return cached value for jid
         @param jid: target contact
         @param name: name of the value ('nick' or 'avatar')
+        @param profile: %(doc_profile)s
         @return: wanted value or None"""
+        client = self.host.getClient(profile)
         try:
-            return self.vcard_cache[jid.userhost()][name]
+            return client._vcard_cache[jid.userhost()][name]
         except KeyError:
             return None
 
@@ -204,16 +206,22 @@
             return ""
         return filename
 
-    def getCardCache(self, target):
+    def getCardCache(self, target, profile_key):
         """Request for cached values of profile 
         return the cached nickname and avatar if exists, else get VCard
+        @param target: target's jid
+        @param profile_key: %(doc_profile_key)s
         """
+        profile = self.host.memory.getProfileName(profile_key)
+        if not profile:
+            error(_("Profile not found"))
+            return {}
         to_jid = jid.JID(target)
         result = {}
-        nick = self.get_cache(to_jid, 'nick')
+        nick = self.get_cache(to_jid, 'nick', profile)
         if nick:
             result['nick'] = nick
-        avatar = self.get_cache(to_jid, 'avatar')
+        avatar = self.get_cache(to_jid, 'avatar', profile)
         if avatar:
             result['avatar'] = avatar
         return result
@@ -228,6 +236,7 @@
         self.host = plugin_parent.host
 
     def connectionInitialized(self):
+        self.parent._vcard_cache = PersistentBinaryDict(NS_VCARD, self.parent.profile)
         self.xmlstream.addObserver(VCARD_UPDATE, self.update)
     
     def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
@@ -241,11 +250,12 @@
         return the cached nickname if exists, else get VCard
         """
         from_jid = jid.JID(presence['from'])
+        #FIXME: wokkel's data_form should be used here
         x_elem = filter (lambda x:x.name == "x", presence.elements())[0] #We only want the "x" element
         for elem in x_elem.elements():
             if elem.name == 'photo':
                 hash = str(elem)
-                old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar')
+                old_avatar = self.plugin_parent.get_cache(from_jid, 'avatar', self.parent.profile)
                 if not old_avatar or old_avatar != hash:
                     debug(_('New avatar found, requesting vcard'))
                     self.plugin_parent.getCard(from_jid.userhost(), self.parent.profile)