changeset 399:3ed53803b3b3

core: added getLastResource method
author Goffi <goffi@goffi.org>
date Thu, 06 Oct 2011 21:16:03 +0200
parents cb0285372818
children 22788653ae8d
files frontends/src/bridge/DBus.py src/bridge/DBus.py src/bridge/bridge_constructor/bridge_template.ini src/core/sat_main.py src/tools/memory.py
diffstat 5 files changed, 40 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/bridge/DBus.py	Wed Oct 05 16:49:57 2011 +0200
+++ b/frontends/src/bridge/DBus.py	Thu Oct 06 21:16:03 2011 +0200
@@ -92,6 +92,9 @@
     def getHistory(self, from_jid, to_jid, size):
         return self.db_core_iface.getHistory(from_jid, to_jid, size)
 
+    def getLastResource(self, contact_jid, profile_key="@DEFAULT@"):
+        return unicode(self.db_core_iface.getLastResource(contact_jid, profile_key))
+
     def getMenuHelp(self, category, name, menu_type):
         return unicode(self.db_core_iface.getMenuHelp(category, name, menu_type))
 
--- a/src/bridge/DBus.py	Wed Oct 05 16:49:57 2011 +0200
+++ b/src/bridge/DBus.py	Thu Oct 06 21:16:03 2011 +0200
@@ -197,6 +197,12 @@
         return self.cb["getHistory"](unicode(from_jid), unicode(to_jid), size)
 
     @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
+                         in_signature='ss', out_signature='s',
+                         async_callbacks=None)
+    def getLastResource(self, contact_jid, profile_key="@DEFAULT@"):
+        return self.cb["getLastResource"](unicode(contact_jid), unicode(profile_key))
+
+    @dbus.service.method(const_INT_PREFIX+const_CORE_SUFFIX,
                          in_signature='sss', out_signature='s',
                          async_callbacks=None)
     def getMenuHelp(self, category, name, menu_type):
--- a/src/bridge/bridge_constructor/bridge_template.ini	Wed Oct 05 16:49:57 2011 +0200
+++ b/src/bridge/bridge_constructor/bridge_template.ini	Thu Oct 06 21:16:03 2011 +0200
@@ -257,6 +257,17 @@
  - list of attributes as in [newContact]
  - groups where the contact is
 
+[getLastResource]
+type=method
+category=core
+sig_in=ss
+sig_out=s
+param_1_default="@DEFAULT@"
+doc=Return the last resource connected for a contact
+doc_param_0=contact_jid: jid of the contact
+doc_param_1=%(doc_profile_key)s
+doc_return=the last resource connected of the contact, or ""
+
 [getPresenceStatus]
 type=method
 category=core
--- a/src/core/sat_main.py	Wed Oct 05 16:49:57 2011 +0200
+++ b/src/core/sat_main.py	Thu Oct 06 21:16:03 2011 +0200
@@ -124,6 +124,7 @@
         self.bridge.register("asyncConnect", self.asyncConnect)
         self.bridge.register("disconnect", self.disconnect)
         self.bridge.register("getContacts", self.memory.getContacts)
+        self.bridge.register("getLastResource", self.memory.getLastResource)
         self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus)
         self.bridge.register("getWaitingSub", self.memory.getWaitingSub)
         self.bridge.register("sendMessage", self.sendMessage)
--- a/src/tools/memory.py	Wed Oct 05 16:49:57 2011 +0200
+++ b/src/tools/memory.py	Thu Oct 06 21:16:03 2011 +0200
@@ -419,6 +419,7 @@
         self.host = host
         self.contacts={}
         self.presenceStatus={}
+        self.lastResource={} #tmp, will be refactored with bdd integration
         self.subscriptions={}
         self.params=Param(host)
         self.history={}  #used to store chat history (key: short jid)
@@ -677,6 +678,19 @@
                 attr, groups = self.contacts[profile][contact]
                 ret.append([contact, attr, groups ])
         return ret
+
+    def getLastResource(self, contact, profile_key):
+        """Return the last resource used by a contact
+        @param contact: contact jid (unicode)
+        @param profile_key: %(doc_profile_key)s"""
+        profile = self.getProfileName(profile_key)
+        if not profile:
+            error(_('Asking contacts for a non-existant profile'))
+            return ""
+        try:
+            return self.lastResource[profile][jid.JID(contact).userhost()]
+        except:
+            return ""
     
     def addPresenceStatus(self, contact_jid, show, priority, statuses, profile_key):
         profile = self.getProfileName(profile_key)
@@ -685,9 +699,14 @@
             return
         if not self.presenceStatus.has_key(profile):
             self.presenceStatus[profile] = {}
+        if not self.lastResource.has_key(profile):
+            self.lastResource[profile] = {}
         if not self.presenceStatus[profile].has_key(contact_jid.userhost()):
             self.presenceStatus[profile][contact_jid.userhost()] = {}
         resource = jid.parse(contact_jid.full())[2] or ''
+        if resource:
+            self.lastResource[profile][contact_jid.userhost()] = resource
+
         self.presenceStatus[profile][contact_jid.userhost()][resource] = (show, priority, statuses)
 
     def addWaitingSub(self, type, contact_jid, profile_key):