Updated "connection-error" signal callback.
[python-purple] / buddy.pyx
index 9fa9ac3..843069f 100644 (file)
--- a/buddy.pyx
+++ b/buddy.pyx
 cimport purple
 
 cdef class Buddy:
 cimport purple
 
 cdef class Buddy:
-    """ Buddy class """
-    cdef blist.PurpleBuddy *c_buddy
-    cdef Account __acc
+    """
+    Buddy class
+    @param name
+    @param account
+    """
+
+    cdef object __account
+    cdef object __name
+    cdef object __exists
+
+    def __init__(self, name, account):
+        self.__name = name
+        self.__account = account
+
+        if self._get_structure() != NULL:
+            self.__exists = True
+        else:
+            self.__exists = False
+
+    cdef blist.PurpleBuddy *_get_structure(self):
+        return blist.purple_find_buddy(account.purple_accounts_find( \
+                self.__account.username, self.__account.protocol.id), \
+                self.__name)
 
 
-    def __init__(self):
-        self.c_buddy = NULL
+    def __get_exists(self):
+        return self.__exists
+    exists = property(__get_exists)
+
+    def __get_name(self):
+        if self.__exists:
+            return <char *> blist.purple_buddy_get_name(self._get_structure())
+        else:
+            return self.__name
+    name = property(__get_name)
 
     def __get_account(self):
 
     def __get_account(self):
-        return self.__acc
-    def __set_account(self, acc):
-        self.__acc = acc
-    account = property(__get_account, __set_account)
+        if self.__exists:
+            return self.__account
+        else:
+            return None
+    account = property(__get_account)
 
     def __get_alias(self):
 
     def __get_alias(self):
-        if self.c_buddy:
-            return <char *>blist.purple_buddy_get_alias_only(self.c_buddy)
+        cdef char *c_alias = NULL
+        c_alias = <char *> blist.purple_buddy_get_alias_only( \
+                self._get_structure())
+        if c_alias:
+            return unicode(c_alias, 'utf-8')
         else:
             return None
     alias = property(__get_alias)
 
         else:
             return None
     alias = property(__get_alias)
 
-    def __get_name(self):
-        if self.c_buddy:
-            return <char *>blist.purple_buddy_get_name(self.c_buddy)
+    def __get_group(self):
+        cdef blist.PurpleGroup *c_group = NULL
+        if self.__exists:
+            c_group = blist.purple_buddy_get_group(self._get_structure())
+            return <char *> blist.purple_group_get_name(c_group)
         else:
             return None
         else:
             return None
-    name = property(__get_name)
+    group = property(__get_group)
 
 
-    """
-    def __get_online(self): # FIXME
-        name = self.name
-        self.c_buddy = blist.purple_find_buddy(self.__acc.c_account, name)
-        return status.purple_presence_is_online(blist.purple_buddy_get_presence(self.c_buddy))
+    def __get_server_alias(self):
+        cdef char *c_server_alias = NULL
+        c_server_alias = <char *> blist.purple_buddy_get_server_alias( \
+                self._get_structure())
+        if c_server_alias:
+            return c_server_alias
+        else:
+            return None
+    server_alias = property(__get_server_alias)
+
+    def __get_contact_alias(self):
+        cdef char *c_contact_alias = NULL
+        c_contact_alias = <char *> blist.purple_buddy_get_contact_alias( \
+                self._get_structure())
+        if c_contact_alias:
+            return c_contact_alias
+        else:
+            return None
+    contact_alias = property(__get_contact_alias)
+
+    def __get_local_alias(self):
+        cdef char *c_local_alias = NULL
+        c_local_alias = <char *> blist.purple_buddy_get_local_alias( \
+                self._get_structure())
+        if c_local_alias:
+            return c_local_alias
+        else:
+            return None
+    local_alias = property(__get_local_alias)
+
+    def __get_available(self):
+        if self.__exists:
+            return status.purple_presence_is_available( \
+                    blist.purple_buddy_get_presence(self._get_structure()))
+        else:
+            return None
+    available = property(__get_available)
+
+    def __get_online(self):
+        if self.__exists:
+            return status.purple_presence_is_online( \
+                    blist.purple_buddy_get_presence(self._get_structure()))
+        else:
+            return None
     online = property(__get_online)
     online = property(__get_online)
-    """
 
 
-    def new_buddy(self, acc, name, alias):
-        self.__acc = acc
-        cdef char *c_name = NULL
-        cdef char *c_alias = NULL
+    def __get_idle(self):
+        if self.__exists:
+            return status.purple_presence_is_idle( \
+                    blist.purple_buddy_get_presence(self._get_structure()))
+        else:
+            return None
+    idle = property(__get_idle)
 
 
-        if name is not None:
-            c_name = name
+    def set_alias(self, alias):
+        if self.__exists:
+            blist.purple_blist_alias_buddy(self._get_structure(), alias)
+            return True
+        else:
+            return False
 
 
-        if alias is not None:
-            c_alias = alias
+    def set_group(self, group):
+        cdef blist.PurpleContact *c_contact = NULL
+        cdef blist.PurpleGroup *c_group = NULL
+        if self.__exists and group:
+            c_group = blist.purple_find_group(group)
+            if c_group == NULL:
+                c_group = blist.purple_group_new(group)
 
 
-        self.c_buddy = blist.purple_buddy_new(<account.PurpleAccount *>\
-                self.__acc.c_account, c_name, c_alias)
+            c_contact = blist.purple_buddy_get_contact(self._get_structure())
+            blist.purple_blist_add_contact(c_contact, c_group, NULL)
+            return True
+        else:
+            return False