Get buddies bindings
[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     ctypedef char* const_guchar_ptr "const guchar *"
25
26 cdef extern from "time.h":
27     ctypedef long int time_t
28
29 include "libpurple/account.pxd"
30 include "libpurple/buddyicon.pxd"
31 include "libpurple/blist.pxd"
32 include "libpurple/connection.pxd"
33 include "libpurple/conversation.pxd"
34 include "libpurple/core.pxd"
35 include "libpurple/debug.pxd"
36 include "libpurple/eventloop.pxd"
37 include "libpurple/ft.pxd"
38 include "libpurple/idle.pxd"
39 include "libpurple/notify.pxd"
40 include "libpurple/plugin.pxd"
41 include "libpurple/pounce.pxd"
42 include "libpurple/prefs.pxd"
43 include "libpurple/proxy.pxd"
44 include "libpurple/request.pxd"
45 include "libpurple/roomlist.pxd"
46 include "libpurple/signals.pxd"
47 include "libpurple/status.pxd"
48 include "libpurple/savedstatuses.pxd"
49 include "libpurple/util.pxd"
50
51 cdef extern from "c_purple.h":
52     void connect_to_signals_for_demonstration_purposes_only ()
53     guint glib_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function, gpointer data)
54
55 import ecore
56
57 __DEFAULT_PATH__ = "/tmp"
58 __APP_NAME__ = "carman-purple-python"
59 __APP_VERSION__ = "0.1"
60
61 global __DEFAULT_PATH__
62 global __APP_NAME__
63 global __APP_VERSION__
64
65 cdef class Purple:
66     """ Purple class """
67     cdef PurpleCoreUiOps c_core_ui_ops
68     cdef PurpleEventLoopUiOps c_eventloop_ui_ops
69     cdef GHashTable *c_ui_info
70
71     def __cinit__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
72         self.c_ui_info = NULL
73
74         if app_name is not __APP_NAME__:
75             __APP_NAME__ = app_name
76
77         if default_path is not __DEFAULT_PATH__:
78             __DEFAULT_PATH__ = default_path
79
80         c_purple_debug_set_enabled(debug_enabled)
81         c_purple_util_set_user_dir(default_path)
82         c_purple_plugins_add_search_path(default_path)
83
84         # adds glib iteration inside ecore main loop
85         ecore.idler_add(self.__glib_iteration_when_idle)
86      # __cinit__
87
88     def __del__(self):
89         c_purple_core_quit()
90     # __del__
91
92     cdef void __core_ui_ops_ui_prefs_init(self):
93         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
94         c_purple_prefs_load()
95
96         c_purple_prefs_add_none("/carman")
97     # __core_ui_ops_ui_prefs_init
98
99     cdef void __core_ui_ops_debug_init(self):
100         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
101     # __core_ui_ops_debug_init
102
103     cdef void __core_ui_ops_ui_init(self):
104         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
105
106         # FIXME: Add core ui initialization here
107     # __core_ui_ops_ui_init
108
109     cdef void __core_ui_ops_quit(self):
110         c_purple_debug(PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
111         c_purple_accounts_set_ui_ops(NULL)
112         c_purple_connections_set_ui_ops(NULL)
113         c_purple_blist_set_ui_ops(NULL)
114         c_purple_conversations_set_ui_ops(NULL)
115         c_purple_notify_set_ui_ops(NULL)
116         c_purple_request_set_ui_ops(NULL)
117         c_purple_xfers_set_ui_ops(NULL)
118         c_purple_roomlist_set_ui_ops(NULL)
119
120         if self.c_ui_info:
121             g_hash_table_destroy(self.c_ui_info)
122     # __core_ui_ops_quit
123
124     cdef GHashTable *__core_ui_ops_get_ui_info(self):
125         if self.c_ui_info == NULL:
126             self.c_ui_info = g_hash_table_new(g_str_hash, g_str_equal)
127
128             g_hash_table_insert(self.c_ui_info, "name", <gpointer> __APP_NAME__)
129             g_hash_table_insert(self.c_ui_info, "version", <gpointer> __APP_VERSION__)
130         return self.c_ui_info
131     # __core_ui_ops_get_ui_info
132
133     def __glib_iteration_when_idle(self):
134         g_main_context_iteration(NULL, False)
135         return True
136     # __glib_iteration_when_idle
137
138     def purple_init(self):
139         """ Initializes libpurple """
140         # initialize c_core_ui_ops structure
141         self.c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
142         self.c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
143         self.c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
144         self.c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
145         self.c_core_ui_ops.get_ui_info = <GHashTable* (*)()> self.__core_ui_ops_get_ui_info
146
147         c_purple_core_set_ui_ops(&self.c_core_ui_ops)
148
149         # initialize c_eventloop_ui_ops structure
150         self.c_eventloop_ui_ops.timeout_add = g_timeout_add
151         self.c_eventloop_ui_ops.timeout_remove = g_source_remove
152         self.c_eventloop_ui_ops.input_add = glib_input_add
153         self.c_eventloop_ui_ops.input_remove = g_source_remove
154         self.c_eventloop_ui_ops.input_get_error = NULL
155         self.c_eventloop_ui_ops.timeout_add_seconds = g_timeout_add_seconds
156
157         c_purple_eventloop_set_ui_ops(&self.c_eventloop_ui_ops)
158
159         # initialize purple core
160         ret = c_purple_core_init(__APP_NAME__)
161         if ret is False:
162             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
163             return False
164
165         # check if there is another instance of libpurple running
166         if c_purple_core_ensure_single_instance() == False:
167             c_purple_debug(PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
168             c_purple_core_quit()
169             return False
170
171         # create and load the buddy list
172         c_purple_set_blist(c_purple_blist_new())
173         c_purple_blist_load()
174
175         # load pounces
176         c_purple_pounces_load()
177
178         return ret
179     # purple_init
180
181     def get_protocols(self):
182         cdef GList *iter
183         cdef PurplePlugin *plugin
184         protocols = []
185         iter = c_purple_plugins_get_protocols()
186         while iter:
187             plugin = <PurplePlugin*> iter.data
188             if plugin.info and plugin.info.name:
189                 protocols += [(plugin.info.id, plugin.info.name)]
190             iter = iter.next
191         return protocols
192     # get_protocols
193
194     def connect(self):
195         conn = Connection()
196         conn.connect()
197     # connect
198 # Purple
199
200 include "account.pxd"
201 include "buddy.pxd"
202 include "connection.pxd"
203 include "conversation.pxd"