More fixes on purple.pyx.
[python-purple] / purple.pyx
index 3385417..36e01de 100644 (file)
@@ -24,8 +24,6 @@ cdef extern from "c_purple.h":
 
 import ecore
 
-include "plugin.pyx"
-
 __DEFAULT_PATH__ = "/tmp"
 __APP_NAME__ = "carman-purple-python"
 __APP_VERSION__ = "0.1"
@@ -65,9 +63,10 @@ cdef class Purple:
     @parm default_path: Full path for libpurple user files.
     """
 
-    cdef Plugins plugins
+    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
 
@@ -77,12 +76,11 @@ cdef class Purple:
         debug.c_purple_debug_set_enabled(debug_enabled)
         util.c_purple_util_set_user_dir(default_path)
         plugin.c_purple_plugins_add_search_path(default_path)
-        self.plugins = Plugins()
 
         # adds glib iteration inside ecore main loop
         ecore.timer_add(0.001, self.__glib_iteration_when_idle)
 
-    def __del__(self):
+    def destroy(self):
         core.c_purple_core_quit()
 
     cdef void __core_ui_ops_ui_prefs_init(self):
@@ -143,14 +141,12 @@ cdef class Purple:
         """ 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"]
@@ -252,30 +248,85 @@ cdef class Purple:
         # load pounces
         pounce.c_purple_pounces_load()
 
+        # initialize accounts
+        self.load_accounts()
+
         return ret
 
-    def get_protocols(self):
-        return self.plugins.get_protocols()
+    def add_account_cb(self, name, func):
+        global account_cbs
+        account_cbs[name] = func
 
     def connect(self):
         conn = Connection()
         conn.connect()
 
-    def attach_signals(self, __signal_cbs=None):
-        if __signal_cbs is not None:
-            global signal_cbs
-            signal_cbs = __signal_cbs
-
+    def signal_connect(self, name=None, cb=None):
         cdef int handle
-
-        signals.c_purple_signal_connect(blist.c_purple_blist_get_handle(),
-                "buddy-signed-off", &handle,
-                <signals.PurpleCallback> signal_buddy_signed_off_cb, NULL)
-
-        signals.c_purple_signal_connect(
-                conversation.c_purple_conversations_get_handle(),
-                "receiving-im-msg", &handle,
-                <signals.PurpleCallback> signal_receiving_im_msg_cb, NULL)
+        cdef plugin.PurplePlugin *jabber
+
+        if name is None:
+            return
+
+        jabber = prpl.c_purple_find_prpl("prpl-jabber")
+        if jabber == NULL:
+            return
+
+        global signal_cbs
+        signal_cbs[name] = cb
+
+        if name == "buddy-signed-off":
+            signals.c_purple_signal_connect(
+                    blist.c_purple_blist_get_handle(),
+                    "buddy-signed-off", &handle,
+                    <signals.PurpleCallback> signal_buddy_signed_off_cb, NULL)
+        elif name == "receiving-im-msg":
+            signals.c_purple_signal_connect(
+                    conversation.c_purple_conversations_get_handle(),
+                    "receiving-im-msg", &handle,
+                    <signals.PurpleCallback> signal_receiving_im_msg_cb, NULL)
+        elif name == "jabber-receiving-xmlnode":
+            signals.c_purple_signal_connect(
+                    jabber, "jabber-receiving-xmlnode", &handle,
+                    <signals.PurpleCallback> jabber_receiving_xmlnode_cb, NULL)
+
+    def __get_accounts(self):
+        return self.__accounts
+    accounts = property(__get_accounts)
+
+    def new_account(self, username, protocol_id):
+        acc = Account(username, protocol_id)
+        return acc
+
+    def load_accounts(self):
+        cdef glib.GList *iter
+        cdef account.PurpleAccount *acc
+        iter = account.c_purple_accounts_get_all()
+        while iter:
+            acc = <account.PurpleAccount *> iter.data
+            if <account.PurpleAccount *>acc:
+                username = account.c_purple_account_get_username(acc)
+                protocol_id = account.c_purple_account_get_protocol_id(acc)
+                self.account_add(username.split("/")[0], protocol_id, "172.18.216.211", 8080)
+            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[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()
+
+    def account_verify(self, acc_username):
+        if self.__accounts:
+            for username in self.__accounts.keys():
+                if acc_username == username:
+                    return self.__accounts[username]
+        else:
+            return None
 
 include "proxy.pyx"
 include "account.pyx"