Implement purple.Buddy()
authorRagner Magalhaes <ragner.magalhaes@openbossa.org>
Tue, 2 Dec 2008 20:45:40 +0000 (20:45 +0000)
committerAnderson Briglia <anderson.briglia@openbossa.org>
Sat, 28 Feb 2009 21:11:13 +0000 (17:11 -0400)
Implemented Buddy class and some basic methods. Python application using python-purple, now can has a buddy dictionary in format: buddies = {<buddy.name>: Buddy()}

TODO: Link Buddy() and Account(), maybe using account->ui_data.

Signed-off-by: Anderson Briglia <anderson.briglia@indt.org.br>

git-svn-id: https://garage.maemo.org/svn/carman/branches/carman-0.7-beta2/python-purple@1348 596f6dd7-e928-0410-a184-9e12fd12cf7e

blist_cbs.pxd
buddy.pyx
nullclient-ecore.py

index 763cf51..c1830d6 100644 (file)
@@ -30,7 +30,7 @@ cdef void __group_node_cb(blist.PurpleBlistNode *node, object callback):
     if group.name:
         name = group.name
     else:
     if group.name:
         name = group.name
     else:
-        name = None
+        name = ""
 
     try:
         callback(node.type, name, group.totalsize, group.currentsize, \
 
     try:
         callback(node.type, name, group.totalsize, group.currentsize, \
@@ -44,10 +44,12 @@ cdef void __contact_node_cb(blist.PurpleBlistNode *node, object callback):
     if contact.alias:
         alias = contact.alias
     else:
     if contact.alias:
         alias = contact.alias
     else:
-        alias = None
+        alias = ""
+
+    name = ""
 
     try:
 
     try:
-        callback(node.type, alias, contact.totalsize, contact.currentsize, \
+        callback(node.type, name, alias, contact.totalsize, contact.currentsize, \
                  contact.online)
     except KeyError:
         pass
                  contact.online)
     except KeyError:
         pass
@@ -60,12 +62,12 @@ cdef void __buddy_node_cb(blist.PurpleBlistNode *node, object callback):
     elif buddy.alias:
         alias = buddy.alias
     else:
     elif buddy.alias:
         alias = buddy.alias
     else:
-        alias = None
+        alias = ""
 
     if buddy.name:
         name = buddy.name
     else:
 
     if buddy.name:
         name = buddy.name
     else:
-        name = None
+        name = ""
 
     try:
         callback(node.type, name, alias)
 
     try:
         callback(node.type, name, alias)
@@ -78,7 +80,7 @@ cdef void __chat_node_cb(blist.PurpleBlistNode *node, object callback):
     if chat.alias:
         alias = chat.alias
     else:
     if chat.alias:
         alias = chat.alias
     else:
-        alias = None
+        alias = ""
 
     try:
         callback(node.type, alias)
 
     try:
         callback(node.type, alias)
index 8137d26..29c56b8 100644 (file)
--- a/buddy.pyx
+++ b/buddy.pyx
@@ -21,16 +21,31 @@ cimport purple
 
 cdef class Buddy:
     """ Buddy class """
 
 cdef class Buddy:
     """ Buddy class """
-    cdef blist.PurpleBuddy *__buddy
+    cdef blist.PurpleBuddy *c_buddy
+    cdef Account __acc
 
     def __init__(self):
 
     def __init__(self):
-        self.__buddy = NULL
+        self.c_buddy = NULL
 
     def new_buddy(self, acc, char *scr, char *alias):
 
     def new_buddy(self, acc, char *scr, char *alias):
-        self.__buddy = blist.c_purple_buddy_new(<account.PurpleAccount *>acc.__account, scr, alias)
+        self.__acc = acc
+        self.c_buddy = blist.c_purple_buddy_new(<account.PurpleAccount *>\
+                self.__acc.c_account, scr, alias)
 
 
-    def get_alias(self):
-        return <char *>blist.c_purple_buddy_get_alias_only(self.__buddy)
+    def __get_alias(self):
+        return <char *>blist.c_purple_buddy_get_alias_only(self.c_buddy)
+    alias = property(__get_alias)
+
+    def __get_name(self):
+        return <char *>blist.c_purple_buddy_get_name(self.c_buddy)
+    name = property(__get_name)
+
+    def __get_online(self):
+        name = self.name
+        self.c_buddy = blist.c_purple_find_buddy(self.__acc.c_account, name)
+        if status.c_purple_presence_is_online(blist.c_purple_buddy_get_presence(self.c_buddy)):
+            return True
+        else:
+            return False
+    online = property(__get_online)
 
 
-    def get_name(self):
-        return <char *>blist.c_purple_buddy_get_name(self.__buddy)
index cd99eec..53e970d 100644 (file)
@@ -224,7 +224,7 @@ class NullClientPurple:
     def __init__(self):
         self.p = purple.Purple(debug_enabled=False)
         self.window = MainWindow(self.quit)
     def __init__(self):
         self.p = purple.Purple(debug_enabled=False)
         self.window = MainWindow(self.quit)
-        self.buddies = [] #online buddies
+        self.buddies = {} #all buddies
         self.conversations = {}
         self.account = None
         self.protocol_id = "prpl-jabber"
         self.conversations = {}
         self.account = None
         self.protocol_id = "prpl-jabber"
@@ -240,16 +240,21 @@ class NullClientPurple:
         self.window.add_send_cb(self.send_msg)
         self.window.init_window()
 
         self.window.add_send_cb(self.send_msg)
         self.window.init_window()
 
-    def _purple_update_blist_cb(self, type, name=None, totalsize=None,\
+    def _purple_update_blist_cb(self, type, name=None, alias=None, totalsize=None,\
                              currentsize=None, online=None):
                              currentsize=None, online=None):
-        self.buddies = self.account.get_buddies_online()
         if type == 2:
         if type == 2:
-            if name in self.buddies:
-                self.buddies.append(name)
+            if not self.buddies.has_key(name):
+                b = purple.Buddy()
+                b.new_buddy(self.account, name, alias)
+                self.buddies[name] = b
+            elif self.buddies[name].online is True:
                 self.window.new_buddy(name)
 
     def _purple_signal_sign_off_cb(self, name, bname):
                 self.window.new_buddy(name)
 
     def _purple_signal_sign_off_cb(self, name, bname):
-        self.buddies.remove(bname)
+        if self.buddies.has_key(bname):
+            self.buddies[bname] = None
+            self.buddies.pop(bname)
+            print "[DEBUG]: Buddy removed!"
         self.window.remove_buddy(bname)
 
     def _purple_create_conv_cb(self, name, type):
         self.window.remove_buddy(bname)
 
     def _purple_create_conv_cb(self, name, type):