Buddy class
[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 cdef void ui_init():
86     pass
87
88 class Purple(object):
89     def __init__(self):
90         cdef PurpleCoreUiOps c_core_ui_ops
91         c_core_ui_ops.ui_prefs_init = NULL
92         c_core_ui_ops.debug_ui_init = NULL
93         c_core_ui_ops.ui_init = ui_init
94         c_core_ui_ops.quit = NULL
95         c_core_ui_ops.get_ui_info = NULL
96
97         cdef PurpleEventLoopUiOps c_eventloop_ui_ops
98         c_eventloop_ui_ops.timeout_add = g_timeout_add
99         c_eventloop_ui_ops.timeout_remove = g_source_remove
100         c_eventloop_ui_ops.input_add = glib_input_add
101         c_eventloop_ui_ops.input_remove = g_source_remove
102         c_eventloop_ui_ops.input_get_error = NULL
103         c_eventloop_ui_ops.timeout_add_seconds = NULL
104
105         self.DEFAULT_PATH = "/home/user/MyDocs/Carman"
106         self.APP_NAME = "carman-purple-python"
107
108         self.debug_set_enabled(True)
109         self.util_set_user_dir(self.DEFAULT_PATH)
110         self.plugin_add_search_path(self.DEFAULT_PATH)
111
112         c_purple_core_set_ui_ops(&c_core_ui_ops)
113         c_purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
114
115         ret = self.core_init(self.APP_NAME)
116         if ret is False:
117             self.debug_info("main", "Exiting because libpurple initialization failed.")
118             return
119
120         # check if there is another instance of libpurple running
121         if self.core_ensure_single_instance() == False:
122             self.debug_info("main", "Exiting because another instance of libpurple is already running.")
123             self.core_quit()
124             return
125     # __init__
126
127     def __del__(self):
128         self.core_quit()
129     # __del__
130
131     def core_ensure_single_instance(self):
132         return c_purple_core_ensure_single_instance()
133     # core_ensure_single_instance
134
135     def core_init(self, ui_name):
136         return c_purple_core_init(ui_name)
137     # core_init
138
139     def core_quit(self):
140         c_purple_core_quit()
141     # core_quit
142
143     def debug_misc(self, category, format):
144         if category == None:
145             c_purple_debug(PURPLE_DEBUG_MISC, NULL, format)
146         else:
147             c_purple_debug(PURPLE_DEBUG_MISC, category, format)
148     # debug_misc
149
150     def debug_info(self, category, format):
151         if category == None:
152             c_purple_debug(PURPLE_DEBUG_INFO, NULL, format)
153         else:
154             c_purple_debug(PURPLE_DEBUG_INFO, category, format)
155     # debug_info
156
157     def debug_warning(self, category, format):
158         if category == None:
159             c_purple_debug(PURPLE_DEBUG_WARNING, NULL, format)
160         else:
161             c_purple_debug(PURPLE_DEBUG_WARNING, category, format)
162     # debug_warning
163
164     def debug_error(self, category, format):
165         if category == None:
166             c_purple_debug(PURPLE_DEBUG_ERROR, NULL, format)
167         else:
168             c_purple_debug(PURPLE_DEBUG_ERROR, category, format)
169     # debug_error
170
171     def debug_fatal(self, category, format):
172         if category == None:
173             c_purple_debug(PURPLE_DEBUG_FATAL, NULL, format)
174         else:
175             c_purple_debug(PURPLE_DEBUG_FATAL, category, format)
176     # debug_fatal
177
178     def debug_set_enabled(self, debug_enabled):
179         c_purple_debug_set_enabled(debug_enabled)
180     # debug_set_enabled
181
182     def plugin_add_search_path(self, path):
183         c_purple_plugins_add_search_path(path)
184     # plugin_add_search_path
185
186     def prefs_rename(self, old_name, new_name):
187         c_purple_prefs_rename(old_name, new_name)
188     # prefs_rename
189
190     def prefs_get_string(self, name):
191         return c_purple_prefs_get_string(name)
192     # prefs_get_string
193
194     def prefs_load(self):
195         return c_purple_prefs_load()
196     # prefs_load
197
198     def util_set_user_dir(self, dir):
199         c_purple_util_set_user_dir(dir)
200     # util_set_user_dir
201
202 include "core/account.pxd"
203 include "core/buddy.pxd"
204 #include "core/blist.pxd"
205 #include "core/connection.pxd"
206 #include "core/core.pxd"
207 #include "core/idle.pxd"
208 #include "core/pounce.pxd"