Conversation class implementation
[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     ctypedef char* const_guchar_ptr "const guchar *"
27
28 cdef extern from "time.h":
29     ctypedef long int time_t
30
31 cdef extern from "libpurple/blist.h":
32     ctypedef struct PurpleBuddyList:
33         pass
34
35     void c_purple_set_blist "purple_set_blist" (PurpleBuddyList *list)
36     void c_purple_blist_load "purple_blist_load" ()
37     PurpleBuddyList* c_purple_blist_new "purple_blist_new" ()
38
39 cdef extern from "libpurple/core.h":
40     ctypedef struct PurpleCoreUiOps:
41         void (*ui_prefs_init) ()
42         void (*debug_ui_init) ()
43         void (*ui_init) ()
44         void (*quit) ()
45         GHashTable* (*get_ui_info) ()
46
47     gboolean c_purple_core_init "purple_core_init" (const_char_ptr ui_name)
48     void c_purple_core_quit "purple_core_quit" ()
49     void c_purple_core_set_ui_ops "purple_core_set_ui_ops" (PurpleCoreUiOps *ops)
50     gboolean c_purple_core_ensure_single_instance "purple_core_ensure_single_instance" ()
51
52 cdef extern from "libpurple/debug.h":
53     ctypedef enum PurpleDebugLevel:
54         PURPLE_DEBUG_ALL
55         PURPLE_DEBUG_MISC
56         PURPLE_DEBUG_INFO
57         PURPLE_DEBUG_WARNING
58         PURPLE_DEBUG_ERROR
59         PURPLE_DEBUG_FATAL
60
61     void c_purple_debug "purple_debug" (PurpleDebugLevel level, const_char_ptr category, const_char_ptr format)
62     void c_purple_debug_set_enabled "purple_debug_set_enabled" (gboolean debug_enabled)
63
64 cdef extern from "libpurple/eventloop.h":
65     ctypedef enum PurpleInputCondition:
66         PURPLE_INPUT_READ
67         PURPLE_INPUT_WRITE
68
69     ctypedef void (*PurpleInputFunction) (gpointer , gint, PurpleInputCondition)
70
71     ctypedef struct PurpleEventLoopUiOps:
72         guint (*timeout_add) (guint interval, GSourceFunc function, gpointer data)
73         gboolean (*timeout_remove) (guint handle)
74         guint (*input_add) (int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer user_data)
75         gboolean (*input_remove) (guint handle)
76         int (*input_get_error) (int fd, int *error)
77         guint (*timeout_add_seconds)(guint interval, GSourceFunc function, gpointer data)
78
79     void c_purple_eventloop_set_ui_ops "purple_eventloop_set_ui_ops" (PurpleEventLoopUiOps *ops)
80
81 cdef extern from "libpurple/plugin.h":
82     ctypedef struct PurplePlugin
83
84     cdef struct _PurplePluginInfo:
85         char *id
86         char *name
87     ctypedef _PurplePluginInfo PurplePluginInfo
88
89     cdef struct _PurplePlugin:
90         PurplePluginInfo *info                # The plugin information.
91     ctypedef _PurplePlugin PurplePlugin
92
93     void c_purple_plugins_add_search_path "purple_plugins_add_search_path" (const_char_ptr path)
94     GList *c_purple_plugins_get_protocols "purple_plugins_get_protocols" ()
95
96 cdef extern from "libpurple/pounce.h":
97     gboolean c_purple_pounces_load "purple_pounces_load" ()
98
99 cdef extern from "libpurple/prefs.h":
100     void c_purple_prefs_add_none "purple_prefs_add_none" (const_char_ptr name)
101     void c_purple_prefs_rename "purple_prefs_rename" (const_char_ptr oldname, const_char_ptr newname)
102     const_char_ptr c_purple_prefs_get_string "purple_prefs_get_string" (const_char_ptr name)
103     gboolean c_purple_prefs_load "purple_prefs_load" ()
104
105 cdef extern from "libpurple/util.h":
106     void c_purple_util_set_user_dir "purple_util_set_user_dir" (char *dir)
107
108 cdef extern from "c_purple.h":
109      guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, gpointer data)
110      void glib_main_loop()
111
112 __DEFAULT_PATH__ = "/tmp"
113 __APP_NAME__ = "carman-purple-python"
114 __APP_VERSION__ = "0.1"
115
116 global __DEFAULT_PATH__
117 global __APP_NAME__
118 global __APP_VERSION__
119
120 cdef class Purple:
121     """ Purple class """
122     cdef PurpleCoreUiOps c_core_ui_ops
123     cdef PurpleEventLoopUiOps c_eventloop_ui_ops
124     cdef GHashTable *c_ui_info
125
126     def __cinit__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
127         self.c_ui_info = NULL
128
129         if app_name is not __APP_NAME__:
130             __APP_NAME__ = app_name
131
132         if default_path is not __DEFAULT_PATH__:
133             __DEFAULT_PATH__ = default_path
134
135         c_purple_debug_set_enabled(debug_enabled)
136         c_purple_util_set_user_dir(default_path)
137         c_purple_plugins_add_search_path(default_path)
138
139         # adds glib iteration inside ecore main loop
140         ecore.idler_add(self.__glib_iteration_when_idle)
141      # __cinit__
142
143     def __del__(self):
144         c_purple_core_quit()
145     # __del__
146
147     cdef void __core_ui_ops_ui_prefs_init(self):
148         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
149         c_purple_prefs_load()
150
151         c_purple_prefs_add_none("/carman")
152         c_purple_prefs_add_none("/carman/plugins")
153     # __core_ui_ops_ui_prefs_init
154
155     cdef void __core_ui_ops_debug_init(self):
156         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
157     # __core_ui_ops_debug_init
158
159     cdef void __core_ui_ops_ui_init(self):
160         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
161     # __core_ui_ops_ui_init
162
163     cdef void __core_ui_ops_quit(self):
164         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
165     # __core_ui_ops_quit
166
167     cdef GHashTable *__core_ui_ops_get_ui_info(self):
168         if self.c_ui_info == NULL:
169             self.c_ui_info = g_hash_table_new(g_str_hash, g_str_equal)
170
171             g_hash_table_insert(self.c_ui_info, "name", <gpointer> __APP_NAME__)
172             g_hash_table_insert(self.c_ui_info, "version", <gpointer> __APP_VERSION__)
173         return self.c_ui_info
174     # __core_ui_ops_get_ui_info
175
176     def __glib_iteration_when_idle(self):
177         g_main_context_iteration(NULL, False)
178         return True
179     # __glib_iteration_when_idle
180
181     def purple_init(self):
182         """ Initializes libpurple """
183         # initialize c_core_ui_ops structure
184         self.c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
185         self.c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
186         self.c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
187         self.c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
188         self.c_core_ui_ops.get_ui_info = <GHashTable* (*)()> self.__core_ui_ops_get_ui_info
189
190         c_purple_core_set_ui_ops(&self.c_core_ui_ops)
191
192         # initialize c_eventloop_ui_ops structure
193         self.c_eventloop_ui_ops.timeout_add = g_timeout_add
194         self.c_eventloop_ui_ops.timeout_remove = g_source_remove
195         self.c_eventloop_ui_ops.input_add = glib_input_add
196         self.c_eventloop_ui_ops.input_remove = g_source_remove
197         self.c_eventloop_ui_ops.input_get_error = NULL
198         self.c_eventloop_ui_ops.timeout_add_seconds = g_timeout_add_seconds
199
200         c_purple_eventloop_set_ui_ops(&self.c_eventloop_ui_ops)
201
202         # initialize purple core
203         ret = c_purple_core_init(__APP_NAME__)
204         if ret is False:
205             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
206             return False
207
208         # check if there is another instance of libpurple running
209         if c_purple_core_ensure_single_instance() == False:
210             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
211             c_purple_core_quit()
212             return False
213
214         # create and load the buddy list
215         c_purple_set_blist(c_purple_blist_new())
216         c_purple_blist_load()
217
218         # load pounces
219         c_purple_pounces_load()
220
221         return ret
222     # core_init
223
224 # Purple
225
226     def get_protocols(self):
227         cdef GList *iter
228         cdef PurplePlugin *plugin
229         protocols = []
230         iter = c_purple_plugins_get_protocols()
231         while iter:
232             plugin = <PurplePlugin*> iter.data
233             if plugin.info and plugin.info.name:
234                 protocols += [(plugin.info.id, plugin.info.name)]
235             iter = iter.next
236         return protocols
237
238     def connect(self):
239         conn = Connection()
240         conn.connect()
241
242     def run_loop(self):
243         glib_main_loop()
244
245 include "core/account.pxd"
246 include "core/buddy.pxd"
247 include "glib.pxd"
248 #include "core/blist.pxd"
249 include "core/connection.pxd"
250 #include "core/core.pxd"
251 #include "core/idle.pxd"
252 #include "core/pounce.pxd"
253 include "core/conversation.pxd"