Moved all Purple class callbacks inside, moved blist and pounce inside Purple.
[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 cdef extern from *:
23     ctypedef char* const_char_ptr "const char *"
24
25 cdef extern from "time.h":
26     ctypedef long int time_t
27
28 cdef extern from "libpurple/blist.h":
29     ctypedef struct PurpleBuddyList:
30         pass
31
32     void c_purple_set_blist "purple_set_blist" (PurpleBuddyList *list)
33     void c_purple_blist_load "purple_blist_load" ()
34     PurpleBuddyList* c_purple_blist_new "purple_blist_new" ()
35
36 cdef extern from "libpurple/core.h":
37     ctypedef struct PurpleCoreUiOps:
38         void (*ui_prefs_init) ()
39         void (*debug_ui_init) ()
40         void (*ui_init) ()
41         void (*quit) ()
42         GHashTable (*get_ui_info) ()
43
44     gboolean c_purple_core_init "purple_core_init" (const_char_ptr ui_name)
45     void c_purple_core_quit "purple_core_quit" ()
46     void c_purple_core_set_ui_ops "purple_core_set_ui_ops" (PurpleCoreUiOps *ops)
47     gboolean c_purple_core_ensure_single_instance "purple_core_ensure_single_instance" ()
48
49 cdef extern from "libpurple/debug.h":
50     ctypedef enum PurpleDebugLevel:
51         PURPLE_DEBUG_ALL
52         PURPLE_DEBUG_MISC
53         PURPLE_DEBUG_INFO
54         PURPLE_DEBUG_WARNING
55         PURPLE_DEBUG_ERROR
56         PURPLE_DEBUG_FATAL
57
58     void c_purple_debug "purple_debug" (PurpleDebugLevel level, const_char_ptr category, const_char_ptr format)
59     void c_purple_debug_set_enabled "purple_debug_set_enabled" (gboolean debug_enabled)
60
61 cdef extern from "libpurple/eventloop.h":
62     ctypedef enum PurpleInputCondition:
63         PURPLE_INPUT_READ
64         PURPLE_INPUT_WRITE
65
66     ctypedef void (*PurpleInputFunction) (gpointer , gint, PurpleInputCondition)
67
68     ctypedef struct PurpleEventLoopUiOps:
69         guint (*timeout_add) (guint interval, GSourceFunc function, gpointer data)
70         gboolean (*timeout_remove) (guint handle)
71         guint (*input_add) (int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer user_data)
72         gboolean (*input_remove) (guint handle)
73         int (*input_get_error) (int fd, int *error)
74         guint (*timeout_add_seconds)(guint interval, GSourceFunc function, gpointer data)
75
76     void c_purple_eventloop_set_ui_ops "purple_eventloop_set_ui_ops" (PurpleEventLoopUiOps *ops)
77
78 cdef extern from "libpurple/plugin.h":
79     void c_purple_plugins_add_search_path "purple_plugins_add_search_path" (const_char_ptr path)
80
81 cdef extern from "libpurple/pounce.h":
82     gboolean c_purple_pounces_load "purple_pounces_load" ()
83
84 cdef extern from "libpurple/prefs.h":
85     void c_purple_prefs_add_none "purple_prefs_add_none" (const_char_ptr name)
86     void c_purple_prefs_rename "purple_prefs_rename" (const_char_ptr oldname, const_char_ptr newname)
87     const_char_ptr c_purple_prefs_get_string "purple_prefs_get_string" (const_char_ptr name)
88     gboolean c_purple_prefs_load "purple_prefs_load" ()
89
90 cdef extern from "libpurple/util.h":
91     void c_purple_util_set_user_dir "purple_util_set_user_dir" (char *dir)
92
93 cdef extern from "c_purple.h":
94      guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, gpointer data)
95
96 __DEFAULT_PATH__ = "/home/user/MyDocs/Carman"
97 __APP_NAME__ = "carman-purple-python"
98 __APP_VERSION__ = "0.1"
99
100 global __DEFAULT_PATH__
101 global __APP_NAME__
102 global __APP_VERSION__
103
104 cdef class Purple:
105     """ Purple class """
106     cdef PurpleCoreUiOps c_core_ui_ops
107     cdef PurpleEventLoopUiOps c_eventloop_ui_ops
108     cdef GHashTable *c_ui_info
109
110     def __cinit__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
111         self.c_ui_info = NULL
112
113         if app_name is not __APP_NAME__:
114             __APP_NAME__ = app_name
115
116         if default_path is not __DEFAULT_PATH__:
117             __DEFAULT_PATH__ = default_path
118
119         c_purple_debug_set_enabled(debug_enabled)
120         c_purple_util_set_user_dir(default_path)
121         c_purple_plugins_add_search_path(default_path)
122      # __cinit__
123
124     def __del__(self):
125         c_purple_core_quit()
126     # __del__
127
128     cdef void __core_ui_ops_ui_prefs_init(self):
129         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
130         c_purple_prefs_load()
131
132         c_purple_prefs_add_none("/carman")
133         c_purple_prefs_add_none("/carman/plugins")
134     # __core_ui_ops_ui_prefs_init
135
136     cdef void __core_ui_ops_debug_init(self):
137         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
138     # __core_ui_ops_debug_init
139
140     cdef void __core_ui_ops_ui_init(self):
141         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
142     # __core_ui_ops_ui_init
143
144     cdef void __core_ui_ops_quit(self):
145         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
146     # __core_ui_ops_quit
147
148     cdef GHashTable *__core_ui_ops_get_ui_info(self):
149         # FIXME: warning: assignment from incompatible pointer type
150         if self.c_ui_info == NULL:
151             self.c_ui_info = g_hash_table_new(g_str_hash, g_str_equal)
152
153             g_hash_table_insert(self.c_ui_info, "name", <gpointer> __APP_NAME__)
154             g_hash_table_insert(self.c_ui_info, "version", <gpointer> __APP_VERSION__)
155         return self.c_ui_info
156     # __core_ui_ops_get_ui_info
157
158     def purple_init(self):
159         """ Initializes libpurple """
160         # initialize c_core_ui_ops structure
161         self.c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
162         self.c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
163         self.c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
164         self.c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
165         self.c_core_ui_ops.get_ui_info = <GHashTable (*)()> self.__core_ui_ops_get_ui_info
166
167         c_purple_core_set_ui_ops(&self.c_core_ui_ops)
168
169         # initialize c_eventloop_ui_ops structure
170         self.c_eventloop_ui_ops.timeout_add = g_timeout_add
171         self.c_eventloop_ui_ops.timeout_remove = g_source_remove
172         self.c_eventloop_ui_ops.input_add = glib_input_add
173         self.c_eventloop_ui_ops.input_remove = g_source_remove
174         self.c_eventloop_ui_ops.input_get_error = NULL
175         self.c_eventloop_ui_ops.timeout_add_seconds = g_timeout_add_seconds
176
177         c_purple_eventloop_set_ui_ops(&self.c_eventloop_ui_ops)
178
179         # initialize purple core
180         ret = c_purple_core_init(__APP_NAME__)
181         if ret is False:
182             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
183             return False
184
185         # check if there is another instance of libpurple running
186         if c_purple_core_ensure_single_instance() == False:
187             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
188             c_purple_core_quit()
189             return False
190
191         # create and load the buddy list
192         c_purple_set_blist(c_purple_blist_new())
193         c_purple_blist_load()
194
195         # load pounces
196         c_purple_pounces_load()
197         return ret
198     # core_init
199
200 # Purple
201
202 include "core/account.pxd"
203 include "core/buddy.pxd"
204 #include "core/connection.pxd"
205 #include "core/core.pxd"
206 #include "core/idle.pxd"