6bf24282d4be41eebd07b9b70a64dcd965d6bd6b
[python-purple] / purple.pyx
1 #
2 #  Copyright (c) 2008 INdT - Instituto Nokia de Tecnologia
3 #
4 #  This file is part of python-purple.
5 #
6 #  python-purple is free software: you can redistribute it and/or modify
7 #  it under the terms of the GNU General Public License as published by
8 #  the Free Software Foundation, either version 3 of the License, or
9 #  (at your option) any later version.
10 #
11 #  python-purple is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15 #
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19
20 include "glib.pxd"
21
22 import ecore
23
24 cdef extern from *:
25     ctypedef char* const_char_ptr "const char *"
26
27 cdef extern from "time.h":
28     ctypedef long int time_t
29
30 cdef extern from "libpurple/blist.h":
31     ctypedef struct PurpleBuddyList:
32         pass
33
34     void c_purple_set_blist "purple_set_blist" (PurpleBuddyList *list)
35     void c_purple_blist_load "purple_blist_load" ()
36     PurpleBuddyList* c_purple_blist_new "purple_blist_new" ()
37
38 cdef extern from "libpurple/core.h":
39     ctypedef struct PurpleCoreUiOps:
40         void (*ui_prefs_init) ()
41         void (*debug_ui_init) ()
42         void (*ui_init) ()
43         void (*quit) ()
44         GHashTable* (*get_ui_info) ()
45
46     gboolean c_purple_core_init "purple_core_init" (const_char_ptr ui_name)
47     void c_purple_core_quit "purple_core_quit" ()
48     void c_purple_core_set_ui_ops "purple_core_set_ui_ops" (PurpleCoreUiOps *ops)
49     gboolean c_purple_core_ensure_single_instance "purple_core_ensure_single_instance" ()
50
51 cdef extern from "libpurple/debug.h":
52     ctypedef enum PurpleDebugLevel:
53         PURPLE_DEBUG_ALL
54         PURPLE_DEBUG_MISC
55         PURPLE_DEBUG_INFO
56         PURPLE_DEBUG_WARNING
57         PURPLE_DEBUG_ERROR
58         PURPLE_DEBUG_FATAL
59
60     void c_purple_debug "purple_debug" (PurpleDebugLevel level, const_char_ptr category, const_char_ptr format)
61     void c_purple_debug_set_enabled "purple_debug_set_enabled" (gboolean debug_enabled)
62
63 cdef extern from "libpurple/eventloop.h":
64     ctypedef enum PurpleInputCondition:
65         PURPLE_INPUT_READ
66         PURPLE_INPUT_WRITE
67
68     ctypedef void (*PurpleInputFunction) (gpointer , gint, PurpleInputCondition)
69
70     ctypedef struct PurpleEventLoopUiOps:
71         guint (*timeout_add) (guint interval, GSourceFunc function, gpointer data)
72         gboolean (*timeout_remove) (guint handle)
73         guint (*input_add) (int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer user_data)
74         gboolean (*input_remove) (guint handle)
75         int (*input_get_error) (int fd, int *error)
76         guint (*timeout_add_seconds)(guint interval, GSourceFunc function, gpointer data)
77
78     void c_purple_eventloop_set_ui_ops "purple_eventloop_set_ui_ops" (PurpleEventLoopUiOps *ops)
79
80 cdef extern from "libpurple/plugin.h":
81     ctypedef struct PurplePlugin
82
83     cdef struct _PurplePluginInfo:
84         char *id
85         char *name
86     ctypedef _PurplePluginInfo PurplePluginInfo
87
88     cdef struct _PurplePlugin:
89         PurplePluginInfo *info                # The plugin information.
90     ctypedef _PurplePlugin PurplePlugin
91
92     void c_purple_plugins_add_search_path "purple_plugins_add_search_path" (const_char_ptr path)
93     GList *c_purple_plugins_get_protocols "purple_plugins_get_protocols" ()
94
95 cdef extern from "libpurple/pounce.h":
96     gboolean c_purple_pounces_load "purple_pounces_load" ()
97
98 cdef extern from "libpurple/prefs.h":
99     void c_purple_prefs_add_none "purple_prefs_add_none" (const_char_ptr name)
100     void c_purple_prefs_rename "purple_prefs_rename" (const_char_ptr oldname, const_char_ptr newname)
101     const_char_ptr c_purple_prefs_get_string "purple_prefs_get_string" (const_char_ptr name)
102     gboolean c_purple_prefs_load "purple_prefs_load" ()
103
104 cdef extern from "libpurple/util.h":
105     void c_purple_util_set_user_dir "purple_util_set_user_dir" (char *dir)
106
107 cdef extern from "c_purple.h":
108      guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, gpointer data)
109      void glib_main_loop()
110
111 __DEFAULT_PATH__ = "/tmp"
112 __APP_NAME__ = "carman-purple-python"
113 __APP_VERSION__ = "0.1"
114
115 global __DEFAULT_PATH__
116 global __APP_NAME__
117 global __APP_VERSION__
118
119 cdef class Purple:
120     """ Purple class """
121     cdef PurpleCoreUiOps c_core_ui_ops
122     cdef PurpleEventLoopUiOps c_eventloop_ui_ops
123     cdef GHashTable *c_ui_info
124
125     def __cinit__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
126         self.c_ui_info = NULL
127
128         if app_name is not __APP_NAME__:
129             __APP_NAME__ = app_name
130
131         if default_path is not __DEFAULT_PATH__:
132             __DEFAULT_PATH__ = default_path
133
134         c_purple_debug_set_enabled(debug_enabled)
135         c_purple_util_set_user_dir(default_path)
136         c_purple_plugins_add_search_path(default_path)
137
138         # adds glib iteration inside ecore main loop
139         ecore.idler_add(self.__glib_iteration_when_idle)
140      # __cinit__
141
142     def __del__(self):
143         c_purple_core_quit()
144     # __del__
145
146     cdef void __core_ui_ops_ui_prefs_init(self):
147         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
148         c_purple_prefs_load()
149
150         c_purple_prefs_add_none("/carman")
151         c_purple_prefs_add_none("/carman/plugins")
152     # __core_ui_ops_ui_prefs_init
153
154     cdef void __core_ui_ops_debug_init(self):
155         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
156     # __core_ui_ops_debug_init
157
158     cdef void __core_ui_ops_ui_init(self):
159         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
160     # __core_ui_ops_ui_init
161
162     cdef void __core_ui_ops_quit(self):
163         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
164     # __core_ui_ops_quit
165
166     cdef GHashTable *__core_ui_ops_get_ui_info(self):
167         if self.c_ui_info == NULL:
168             self.c_ui_info = g_hash_table_new(g_str_hash, g_str_equal)
169
170             g_hash_table_insert(self.c_ui_info, "name", <gpointer> __APP_NAME__)
171             g_hash_table_insert(self.c_ui_info, "version", <gpointer> __APP_VERSION__)
172         return self.c_ui_info
173     # __core_ui_ops_get_ui_info
174
175     def __glib_iteration_when_idle(self):
176         g_main_context_iteration(NULL, False)
177         return True
178     # __glib_iteration_when_idle
179
180     def purple_init(self):
181         """ Initializes libpurple """
182         # initialize c_core_ui_ops structure
183         self.c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
184         self.c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
185         self.c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
186         self.c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
187         self.c_core_ui_ops.get_ui_info = <GHashTable* (*)()> self.__core_ui_ops_get_ui_info
188
189         c_purple_core_set_ui_ops(&self.c_core_ui_ops)
190
191         # initialize c_eventloop_ui_ops structure
192         self.c_eventloop_ui_ops.timeout_add = g_timeout_add
193         self.c_eventloop_ui_ops.timeout_remove = g_source_remove
194         self.c_eventloop_ui_ops.input_add = glib_input_add
195         self.c_eventloop_ui_ops.input_remove = g_source_remove
196         self.c_eventloop_ui_ops.input_get_error = NULL
197         self.c_eventloop_ui_ops.timeout_add_seconds = g_timeout_add_seconds
198
199         c_purple_eventloop_set_ui_ops(&self.c_eventloop_ui_ops)
200
201         # initialize purple core
202         ret = c_purple_core_init(__APP_NAME__)
203         if ret is False:
204             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
205             return False
206
207         # check if there is another instance of libpurple running
208         if c_purple_core_ensure_single_instance() == False:
209             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
210             c_purple_core_quit()
211             return False
212
213         # create and load the buddy list
214         c_purple_set_blist(c_purple_blist_new())
215         c_purple_blist_load()
216
217         # load pounces
218         c_purple_pounces_load()
219
220         return ret
221     # core_init
222
223 # Purple
224
225     def get_protocols(self):
226         cdef GList *iter
227         cdef PurplePlugin *plugin
228         protocols = []
229         iter = c_purple_plugins_get_protocols()
230         while iter:
231             plugin = <PurplePlugin*> iter.data
232             if plugin.info and plugin.info.name:
233                 protocols += [(plugin.info.id, plugin.info.name)]
234             iter = iter.next
235         return protocols
236
237     def connect(self):
238         conn = Connection()
239         conn.connect()
240
241     def run_loop(self):
242         glib_main_loop()
243
244 include "core/account.pxd"
245 include "core/buddy.pxd"
246 include "glib.pxd"
247 #include "core/blist.pxd"
248 include "core/connection.pxd"
249 #include "core/core.pxd"
250 #include "core/idle.pxd"