Updated "connection-error" signal callback.
[python-purple] / buddy.pyx
index d12fd58..843069f 100644 (file)
--- a/buddy.pyx
+++ b/buddy.pyx
 cimport purple
 
 cdef class Buddy:
 cimport purple
 
 cdef class Buddy:
-    """ Buddy class """
-    cdef blist.PurpleBuddy *__buddy
+    """
+    Buddy class
+    @param name
+    @param account
+    """
 
 
-    def __init__(self):
-        self.__buddy = NULL
+    cdef object __account
+    cdef object __name
+    cdef object __exists
 
 
-    def new_buddy(self, acc, char *scr, char *alias):
-        self.__buddy = blist.c_purple_buddy_new(<account.PurpleAccount *>acc.__account, scr, alias)
+    def __init__(self, name, account):
+        self.__name = name
+        self.__account = account
 
 
-    def get_alias(self):
-        return blist.c_purple_buddy_get_alias_only(self.__buddy)
+        if self._get_structure() != NULL:
+            self.__exists = True
+        else:
+            self.__exists = False
 
 
-    def get_name(self):
-        return blist.c_purple_buddy_get_name(self.__buddy)
+    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 __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):
+        if self.__exists:
+            return self.__account
+        else:
+            return None
+    account = property(__get_account)
+
+    def __get_alias(self):
+        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)
+
+    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
+    group = property(__get_group)
+
+    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)
+
+    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)
+
+    def set_alias(self, alias):
+        if self.__exists:
+            blist.purple_blist_alias_buddy(self._get_structure(), alias)
+            return True
+        else:
+            return False
+
+    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)
+
+            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