0ca9b87a6d95d4802d187d7c6f1affc637b4c047
[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/core.h":
29     ctypedef struct PurpleCoreUiOps:
30         void (*ui_prefs_init) ()
31         void (*debug_ui_init) ()
32         void (*ui_init) ()
33         void (*quit) ()
34         GHashTable (*get_ui_info) ()
35
36     gboolean c_purple_core_init "purple_core_init" (const_char_ptr ui_name)
37     void c_purple_core_quit "purple_core_quit" ()
38     void c_purple_core_set_ui_ops "purple_core_set_ui_ops" (PurpleCoreUiOps *ops)
39     gboolean c_purple_core_ensure_single_instance "purple_core_ensure_single_instance" ()
40
41 cdef extern from "libpurple/debug.h":
42     ctypedef enum PurpleDebugLevel:
43         PURPLE_DEBUG_ALL
44         PURPLE_DEBUG_MISC
45         PURPLE_DEBUG_INFO
46         PURPLE_DEBUG_WARNING
47         PURPLE_DEBUG_ERROR
48         PURPLE_DEBUG_FATAL
49
50     void c_purple_debug "purple_debug" (PurpleDebugLevel level, const_char_ptr category, const_char_ptr format)
51     void c_purple_debug_set_enabled "purple_debug_set_enabled" (gboolean debug_enabled)
52
53 cdef extern from "libpurple/eventloop.h":
54     ctypedef enum PurpleInputCondition:
55         PURPLE_INPUT_READ
56         PURPLE_INPUT_WRITE
57
58     ctypedef void (*PurpleInputFunction) (gpointer , gint, PurpleInputCondition)
59
60     ctypedef struct PurpleEventLoopUiOps:
61         guint (*timeout_add) (guint interval, GSourceFunc function, gpointer data)
62         gboolean (*timeout_remove) (guint handle)
63         guint (*input_add) (int fd, PurpleInputCondition cond, PurpleInputFunction func, gpointer user_data)
64         gboolean (*input_remove) (guint handle)
65         int (*input_get_error) (int fd, int *error)
66         guint (*timeout_add_seconds)(guint interval, GSourceFunc function, gpointer data)
67
68     void c_purple_eventloop_set_ui_ops "purple_eventloop_set_ui_ops" (PurpleEventLoopUiOps *ops)
69
70 cdef extern from "libpurple/plugin.h":
71     void c_purple_plugins_add_search_path "purple_plugins_add_search_path" (const_char_ptr path)
72
73 cdef extern from "libpurple/prefs.h":
74     void c_purple_prefs_rename "purple_prefs_rename" (const_char_ptr oldname, const_char_ptr newname)
75     const_char_ptr c_purple_prefs_get_string "purple_prefs_get_string" (const_char_ptr name)
76     gboolean c_purple_prefs_load "purple_prefs_load" ()
77
78 cdef extern from "libpurple/util.h":
79     void c_purple_util_set_user_dir "purple_util_set_user_dir" (char *dir)
80
81 cdef extern from "c_purple.h":
82      void set_uiops()
83      guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, gpointer data)
84
85 __DEFAULT_PATH__ = "/home/user/MyDocs/Carman"
86 __APP_NAME__ = "carman-purple-python"
87 __APP_VERSION__ = "0.1"
88
89 global __DEFAULT_PATH__
90 global __APP_NAME__
91
92 def debug_misc(category, format):
93     if category == None:
94         c_purple_debug(PURPLE_DEBUG_MISC, NULL, format)
95     else:
96         c_purple_debug(PURPLE_DEBUG_MISC, category, format)
97 # debug_misc
98
99 def debug_info(category, format):
100     if category == None:
101         c_purple_debug(PURPLE_DEBUG_INFO, NULL, format)
102     else:
103         c_purple_debug(PURPLE_DEBUG_INFO, category, format)
104 # debug_info
105
106 def debug_warning(category, format):
107     if category == None:
108         c_purple_debug(PURPLE_DEBUG_WARNING, NULL, format)
109     else:
110         c_purple_debug(PURPLE_DEBUG_WARNING, category, format)
111 # debug_warning
112
113 def debug_error(category, format):
114     if category == None:
115         c_purple_debug(PURPLE_DEBUG_ERROR, NULL, format)
116     else:
117         c_purple_debug(PURPLE_DEBUG_ERROR, category, format)
118 # debug_error
119
120 def debug_fatal(category, format):
121     if category == None:
122         c_purple_debug(PURPLE_DEBUG_FATAL, NULL, format)
123     else:
124         c_purple_debug(PURPLE_DEBUG_FATAL, category, format)
125 # debug_fatal
126
127 cdef void core_ui_ops_ui_prefs_init():
128     debug_info("core_ui_ops", "ui_prefs_init")
129 # core_ui_ops_ui_prefs_init
130
131 cdef void core_ui_ops_debug_ui_init():
132     debug_info("core_ui_ops", "debug_ui_init")
133 # core_ui_ops_debug_ui_init
134
135 cdef void core_ui_ops_ui_init():
136     debug_info("core_ui_ops", "ui_init")
137 # core_ui_ops_ui_init
138
139 cdef void core_ui_ops_quit():
140     debug_info("core_ui_ops", "quit")
141 # core_ui_ops_quit
142
143 cdef class Purple:
144     def __cinit__(self):
145         cdef PurpleCoreUiOps c_core_ui_ops
146         c_core_ui_ops.ui_prefs_init = core_ui_ops_ui_prefs_init
147         c_core_ui_ops.debug_ui_init = core_ui_ops_debug_ui_init
148         c_core_ui_ops.ui_init = core_ui_ops_ui_init
149         c_core_ui_ops.quit = core_ui_ops_quit
150         c_core_ui_ops.get_ui_info = NULL # FIXME
151
152         cdef PurpleEventLoopUiOps c_eventloop_ui_ops
153         c_eventloop_ui_ops.timeout_add = g_timeout_add
154         c_eventloop_ui_ops.timeout_remove = g_source_remove
155         c_eventloop_ui_ops.input_add = glib_input_add
156         c_eventloop_ui_ops.input_remove = g_source_remove
157         c_eventloop_ui_ops.input_get_error = NULL
158         c_eventloop_ui_ops.timeout_add_seconds = g_timeout_add_seconds
159
160         self.debug_set_enabled(True)
161         self.util_set_user_dir(__DEFAULT_PATH__)
162         self.plugin_add_search_path(__DEFAULT_PATH__)
163
164         c_purple_core_set_ui_ops(&c_core_ui_ops)
165         c_purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
166
167         ret = self.core_init(__APP_NAME__)
168         if ret is False:
169             debug_info("main", "Exiting because libpurple initialization failed.")
170             return
171
172         # check if there is another instance of libpurple running
173         if self.core_ensure_single_instance() == False:
174             debug_info("main", "Exiting because another instance of libpurple is already running.")
175             self.core_quit()
176             return
177     # __init__
178
179     def __del__(self):
180         self.core_quit()
181     # __del__
182
183     def core_ensure_single_instance(self):
184         return c_purple_core_ensure_single_instance()
185     # core_ensure_single_instance
186
187     def core_init(self, ui_name):
188         return c_purple_core_init(ui_name)
189     # core_init
190
191     def core_quit(self):
192         c_purple_core_quit()
193     # core_quit
194
195     def debug_set_enabled(self, debug_enabled):
196         c_purple_debug_set_enabled(debug_enabled)
197     # debug_set_enabled
198
199     def plugin_add_search_path(self, path):
200         c_purple_plugins_add_search_path(path)
201     # plugin_add_search_path
202
203     def prefs_rename(self, old_name, new_name):
204         c_purple_prefs_rename(old_name, new_name)
205     # prefs_rename
206
207     def prefs_get_string(self, name):
208         return c_purple_prefs_get_string(name)
209     # prefs_get_string
210
211     def prefs_load(self):
212         return c_purple_prefs_load()
213     # prefs_load
214
215     def util_set_user_dir(self, dir):
216         c_purple_util_set_user_dir(dir)
217     # util_set_user_dir
218
219 include "core/account.pxd"
220 include "core/buddy.pxd"
221 #include "core/blist.pxd"
222 #include "core/connection.pxd"
223 #include "core/core.pxd"
224 #include "core/idle.pxd"
225 #include "core/pounce.pxd"