From 5380b684cd8cba1e66729c0918e7d93187ab4705 Mon Sep 17 00:00:00 2001 From: Goffi Date: Thu, 6 Oct 2011 21:16:03 +0200 Subject: [PATCH] core: added getLastResource method --- frontends/src/bridge/DBus.py | 3 +++ src/bridge/DBus.py | 6 ++++++ .../bridge_constructor/bridge_template.ini | 11 +++++++++++ src/core/sat_main.py | 1 + src/tools/memory.py | 19 +++++++++++++++++++ 5 files changed, 40 insertions(+) diff --git a/frontends/src/bridge/DBus.py b/frontends/src/bridge/DBus.py index 15aaee0a..ef447775 100644 --- a/frontends/src/bridge/DBus.py +++ b/frontends/src/bridge/DBus.py @@ -92,6 +92,9 @@ class DBusBridgeFrontend(BridgeFrontend): 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)) diff --git a/src/bridge/DBus.py b/src/bridge/DBus.py index 5bffd61f..b6df7ef3 100755 --- a/src/bridge/DBus.py +++ b/src/bridge/DBus.py @@ -196,6 +196,12 @@ class DbusObject(dbus.service.Object): def getHistory(self, from_jid, to_jid, size): 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) diff --git a/src/bridge/bridge_constructor/bridge_template.ini b/src/bridge/bridge_constructor/bridge_template.ini index f970c938..ca13a0fa 100644 --- a/src/bridge/bridge_constructor/bridge_template.ini +++ b/src/bridge/bridge_constructor/bridge_template.ini @@ -257,6 +257,17 @@ doc_return=array of tuples with the following values: - 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 diff --git a/src/core/sat_main.py b/src/core/sat_main.py index 66aeb8c7..2f506f53 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -124,6 +124,7 @@ class SAT(service.Service): 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) diff --git a/src/tools/memory.py b/src/tools/memory.py index 687a2a3f..c00fd341 100644 --- a/src/tools/memory.py +++ b/src/tools/memory.py @@ -419,6 +419,7 @@ class Memory: 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 @@ class Memory: 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 @@ class Memory: 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):