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