Removind Account's dependency and adding _get_structure() function
[python-purple] / purple.pyx
index 00ce7fc..26a972f 100644 (file)
@@ -55,6 +55,8 @@ include "request_cbs.pxd"
 #include "roomlist_cbs.pxd"
 include "signal_cbs.pxd"
 
+include "util.pxd"
+
 cdef class Purple:
     """ Purple class.
 
@@ -63,39 +65,41 @@ cdef class Purple:
     @parm default_path: Full path for libpurple user files.
     """
 
-    cdef object accounts
-
     def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
-        self.accounts = {}
         if app_name is not __APP_NAME__:
             __APP_NAME__ = app_name
 
         if default_path is not __DEFAULT_PATH__:
             __DEFAULT_PATH__ = default_path
 
-        debug.c_purple_debug_set_enabled(debug_enabled)
-        util.c_purple_util_set_user_dir(default_path)
+        debug.purple_debug_set_enabled(debug_enabled)
+        util.purple_util_set_user_dir(default_path)
         plugin.c_purple_plugins_add_search_path(default_path)
 
         # adds glib iteration inside ecore main loop
         ecore.timer_add(0.001, self.__glib_iteration_when_idle)
 
+    def __get_ui_name(self):
+        return __APP_NAME__
+    ui_name = property(__get_ui_name)
+
     def destroy(self):
         core.c_purple_core_quit()
 
     cdef void __core_ui_ops_ui_prefs_init(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
+        debug.purple_debug_info("core_ui_ops", "%s", "ui_prefs_init\n")
         prefs.c_purple_prefs_load()
 
         prefs.c_purple_prefs_add_none("/carman")
 
     cdef void __core_ui_ops_debug_init(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
+        debug.purple_debug_info("core_ui_ops", "%s", "debug_ui_init\n")
+        pass
 
     cdef void __core_ui_ops_ui_init(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
+        debug.purple_debug_info("core_ui_ops", "%s", "ui_init\n")
 
-        account.c_purple_accounts_set_ui_ops(&c_account_ui_ops)
+        account.purple_accounts_set_ui_ops(&c_account_ui_ops)
         connection.c_purple_connections_set_ui_ops(&c_conn_ui_ops)
         blist.c_purple_blist_set_ui_ops(&c_blist_ui_ops)
         conversation.c_purple_conversations_set_ui_ops(&c_conv_ui_ops)
@@ -106,11 +110,11 @@ cdef class Purple:
         #roomlist.c_purple_roomlist_set_ui_ops(&c_rlist_ui_ops)
 
     cdef void __core_ui_ops_quit(self):
-        debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
+        debug.purple_debug_info("core_ui_ops", "%s", "quit\n")
 
         global c_ui_info
 
-        account.c_purple_accounts_set_ui_ops(NULL)
+        account.purple_accounts_set_ui_ops(NULL)
         connection.c_purple_connections_set_ui_ops(NULL)
         blist.c_purple_blist_set_ui_ops(NULL)
         conversation.c_purple_conversations_set_ui_ops(NULL)
@@ -137,24 +141,9 @@ cdef class Purple:
         glib.g_main_context_iteration(NULL, False)
         return True
 
-    def purple_init(self, callbacks_dict=None):
+    def purple_init(self):
         """ Initializes libpurple """
 
-        if callbacks_dict is not None:
-            global account_cbs
-            global blist_cbs
-            global connection_cbs
-            global conversation_cbs
-            global notify_cbs
-            global request_cbs
-
-            account_cbs = callbacks_dict["account"]
-            blist_cbs = callbacks_dict["blist"]
-            connection_cbs = callbacks_dict["connection"]
-            conversation_cbs = callbacks_dict["conversation"]
-            notify_cbs = callbacks_dict["notify"]
-            request_cbs = callbacks_dict["request"]
-
         c_account_ui_ops.notify_added = notify_added
         c_account_ui_ops.status_changed = status_changed
         c_account_ui_ops.request_add = request_add
@@ -234,12 +223,14 @@ cdef class Purple:
         # initialize purple core
         ret = core.c_purple_core_init(__APP_NAME__)
         if ret is False:
-            debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
+            debug.purple_debug_fatal("main", "%s", "libpurple " \
+                                       "initialization failed.\n")
             return False
 
         # check if there is another instance of libpurple running
         if core.c_purple_core_ensure_single_instance() == False:
-            debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
+            debug.purple_debug_fatal("main", "%s", "Another instance of " \
+                                      "libpurple is already running.\n")
             core.c_purple_core_quit()
             return False
 
@@ -252,9 +243,27 @@ cdef class Purple:
 
         return ret
 
-    def connect(self):
-        conn = Connection()
-        conn.connect()
+    def add_callback(self, type, name, callback):
+        """
+        Adds a callback with given name inside callback's type.
+
+        @param type     Callback type (e.g. "account")
+        @param name     Callback name (e.g. "notify-added")
+        @param callback Callback to be called
+        """
+        global account_cbs
+        global blist_cbs
+        global connection_cbs
+        global conversation_cbs
+        global notify_cbs
+        global request_cbs
+
+        { "account": account_cbs,
+          "blist": blist_cbs,
+          "connection": connection_cbs,
+          "conversation": conversation_cbs,
+          "notify": notify_cbs,
+          "request": request_cbs }[type][name] = callback
 
     def signal_connect(self, name=None, cb=None):
         cdef int handle
@@ -270,7 +279,27 @@ cdef class Purple:
         global signal_cbs
         signal_cbs[name] = cb
 
-        if name == "buddy-signed-off":
+        if name == "signed-on":
+            signals.c_purple_signal_connect(
+                    connection.c_purple_connections_get_handle(),
+                    "signed-on", &handle,
+                    <signals.PurpleCallback> signal_signed_on_cb, NULL)
+        elif name == "signed-off":
+            signals.c_purple_signal_connect(
+                    connection.c_purple_connections_get_handle(),
+                    "signed-off", &handle,
+                    <signals.PurpleCallback> signal_signed_off_cb, NULL)
+        elif name == "connection-error":
+            signals.c_purple_signal_connect(
+                    connection.c_purple_connections_get_handle(),
+                    "connection-error", &handle,
+                    <signals.PurpleCallback> signal_connection_error_cb, NULL)
+        elif name == "buddy-signed-on":
+            signals.c_purple_signal_connect(
+                    blist.c_purple_blist_get_handle(),
+                    "buddy-signed-on", &handle,
+                    <signals.PurpleCallback> signal_buddy_signed_on_cb, NULL)
+        elif name == "buddy-signed-off":
             signals.c_purple_signal_connect(
                     blist.c_purple_blist_get_handle(),
                     "buddy-signed-off", &handle,
@@ -285,47 +314,46 @@ cdef class Purple:
                     jabber, "jabber-receiving-xmlnode", &handle,
                     <signals.PurpleCallback> jabber_receiving_xmlnode_cb, NULL)
 
-    def new_account(self, username, protocol_id):
-        acc = Account(username, protocol_id)
-        return acc
-
-    def accounts_init(self):
+    def accounts_get_all(self):
         cdef glib.GList *iter
         cdef account.PurpleAccount *acc
-        iter = account.c_purple_accounts_get_all()
+        cdef char *username
+        cdef char *protocol_id
+
+        iter = account.purple_accounts_get_all()
+        account_list = []
+
         while iter:
             acc = <account.PurpleAccount *> iter.data
+
             if <account.PurpleAccount *>acc:
-                self.account_add(acc.username.split("/")[0], acc.protocol_id, "172.18.216.211", 8080)
+                username = <char *> account.purple_account_get_username(acc)
+                protocol_id = <char *> account.purple_account_get_protocol_id(acc)
+
+                if username != NULL and protocol_id != NULL:
+                    account_list.append(Account(self, username, protocol_id))
             iter = iter.next
 
-    def account_add(self, username, protocol_id, host, port):
-        if not self.account_verify(username):
-            acc = purple.Account(username, protocol_id)
-            self.accounts_add_dict(username, acc)
-            if not account.c_purple_accounts_find(username, protocol_id):
-                acc.proxy.set_type(purple.ProxyInfoType().HTTP)
-                acc.proxy.set_host(host)
-                acc.proxy.set_port(port)
-                acc.save_into_xml()
-        else:
-            print "Exists account"
-
-    def account_verify(self, acc_username):
-        if self.accounts:
-            for username in self.accounts.keys():
-                if acc_username == username:
-                    return self.accounts[username]
-        return None
-
-    def accounts_add_dict(self, username, acc):
-        self.accounts[username] = acc
-
-    def accounts_get_dict(self):
-        return self.accounts
+        return account_list
+
+    def protocols_get_all(self):
+        cdef glib.GList *iter
+        cdef plugin.PurplePlugin *pp
+
+        iter = plugin.c_purple_plugins_get_protocols()
+        protocol_list = []
+        while iter:
+            pp = <plugin.PurplePlugin*> iter.data
+            if pp.info and pp.info.name:
+                protocol_list.append[Protocol(pp.info.id)]
+            iter = iter.next
+        return protocol_list
 
+include "protocol.pyx"
+#include "plugin.pyx"
 include "proxy.pyx"
+#include "protocol.pyx"
 include "account.pyx"
 include "buddy.pyx"
-include "connection.pyx"
+#include "connection.pyx"
 include "conversation.pyx"