nullclient-core.py: fix update buddy list
[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 cimport purple
21
22 cdef extern from "c_purple.h":
23     glib.guint glib_input_add(glib.gint fd, eventloop.PurpleInputCondition condition, eventloop.PurpleInputFunction function, glib.gpointer data)
24
25 import ecore
26
27 include "plugin.pyx"
28
29 __DEFAULT_PATH__ = "/tmp"
30 __APP_NAME__ = "carman-purple-python"
31 __APP_VERSION__ = "0.1"
32
33 cdef account.PurpleAccountUiOps c_account_ui_ops
34 cdef blist.PurpleBlistUiOps c_blist_ui_ops
35 cdef connection.PurpleConnectionUiOps c_conn_ui_ops
36 cdef conversation.PurpleConversationUiOps c_conv_ui_ops
37 cdef core.PurpleCoreUiOps c_core_ui_ops
38 cdef eventloop.PurpleEventLoopUiOps c_eventloop_ui_ops
39 #cdef ft.PurpleXferUiOps c_ft_ui_ops
40 cdef notify.PurpleNotifyUiOps c_notify_ui_ops
41 #cdef privacy.PurplePrivacyUiOps c_privacy_ui_ops
42 cdef request.PurpleRequestUiOps c_request_ui_ops
43 #cdef roomlist.PurpleRoomlistUiOps c_rlist_ui_ops
44
45 cdef glib.GHashTable *c_ui_info
46
47 c_ui_info = NULL
48
49 include "account_cbs.pxd"
50 include "blist_cbs.pxd"
51 include "connection_cbs.pxd"
52 include "conversation_cbs.pxd"
53 #include "xfer_cbs.pxd"
54 include "notify_cbs.pxd"
55 #include "privacy_cbs.pxd"
56 include "request_cbs.pxd"
57 #include "roomlist_cbs.pxd"
58 include "signal_cbs.pxd"
59
60 cdef class Purple:
61     """ Purple class.
62
63     @parm debug_enabled: Toggle debug messages.
64     @parm app_name: Set application name.
65     @parm default_path: Full path for libpurple user files.
66     """
67
68     cdef Plugins plugins
69
70     def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
71         if app_name is not __APP_NAME__:
72             __APP_NAME__ = app_name
73
74         if default_path is not __DEFAULT_PATH__:
75             __DEFAULT_PATH__ = default_path
76
77         debug.c_purple_debug_set_enabled(debug_enabled)
78         util.c_purple_util_set_user_dir(default_path)
79         plugin.c_purple_plugins_add_search_path(default_path)
80         self.plugins = Plugins()
81
82         # adds glib iteration inside ecore main loop
83         ecore.timer_add(0.001, self.__glib_iteration_when_idle)
84
85     def __del__(self):
86         core.c_purple_core_quit()
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
94     cdef void __core_ui_ops_debug_init(self):
95         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
96
97     cdef void __core_ui_ops_ui_init(self):
98         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
99
100         account.c_purple_accounts_set_ui_ops(&c_account_ui_ops)
101         connection.c_purple_connections_set_ui_ops(&c_conn_ui_ops)
102         blist.c_purple_blist_set_ui_ops(&c_blist_ui_ops)
103         conversation.c_purple_conversations_set_ui_ops(&c_conv_ui_ops)
104         notify.c_purple_notify_set_ui_ops(&c_notify_ui_ops)
105         #privacy.c_purple_privacy_set_ui_ops(&c_privacy_ui_ops)
106         request.c_purple_request_set_ui_ops(&c_request_ui_ops)
107         #ft.c_purple_xfers_set_ui_ops(&c_ft_ui_ops)
108         #roomlist.c_purple_roomlist_set_ui_ops(&c_rlist_ui_ops)
109
110     cdef void __core_ui_ops_quit(self):
111         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
112
113         global c_ui_info
114
115         account.c_purple_accounts_set_ui_ops(NULL)
116         connection.c_purple_connections_set_ui_ops(NULL)
117         blist.c_purple_blist_set_ui_ops(NULL)
118         conversation.c_purple_conversations_set_ui_ops(NULL)
119         notify.c_purple_notify_set_ui_ops(NULL)
120         #privacy.c_purple_privacy_set_ui_ops(NULL)
121         request.c_purple_request_set_ui_ops(NULL)
122         #ft.c_purple_xfers_set_ui_ops(NULL)
123         #roomlist.c_purple_roomlist_set_ui_ops(NULL)
124
125         if c_ui_info:
126             glib.g_hash_table_destroy(c_ui_info)
127
128     cdef glib.GHashTable *__core_ui_ops_get_ui_info(self):
129         global c_ui_info
130
131         if c_ui_info == NULL:
132             c_ui_info = glib.g_hash_table_new(glib.g_str_hash, glib.g_str_equal)
133
134             glib.g_hash_table_insert(c_ui_info, "name", <glib.gpointer> __APP_NAME__)
135             glib.g_hash_table_insert(c_ui_info, "version", <glib.gpointer> __APP_VERSION__)
136         return c_ui_info
137
138     def __glib_iteration_when_idle(self):
139         glib.g_main_context_iteration(NULL, False)
140         return True
141
142     def purple_init(self, callbacks_dict=None):
143         """ Initializes libpurple """
144
145         if callbacks_dict is not None:
146             global account_cbs
147             global blist_cbs
148             global connection_cbs
149             global conversation_cbs
150             global notify_cbs
151             global request_cbs
152
153             account_cbs = callbacks_dict["account"]
154             blist_cbs = callbacks_dict["blist"]
155             connection_cbs = callbacks_dict["connection"]
156             conversation_cbs = callbacks_dict["conversation"]
157             notify_cbs = callbacks_dict["notify"]
158             request_cbs = callbacks_dict["request"]
159
160         c_account_ui_ops.notify_added = notify_added
161         c_account_ui_ops.status_changed = status_changed
162         c_account_ui_ops.request_add = request_add
163         c_account_ui_ops.request_authorize = request_authorize
164         c_account_ui_ops.close_account_request = close_account_request
165
166         c_blist_ui_ops.new_list = new_list
167         c_blist_ui_ops.new_node = new_node
168         c_blist_ui_ops.show = show
169         c_blist_ui_ops.update = update
170         c_blist_ui_ops.remove = remove
171         c_blist_ui_ops.destroy = destroy
172         c_blist_ui_ops.set_visible = set_visible
173         c_blist_ui_ops.request_add_buddy = request_add_buddy
174         c_blist_ui_ops.request_add_chat = request_add_chat
175         c_blist_ui_ops.request_add_group = request_add_group
176
177         c_conn_ui_ops.connect_progress = connect_progress
178         c_conn_ui_ops.connected = connected
179         c_conn_ui_ops.disconnected = disconnected
180         c_conn_ui_ops.notice = notice
181         c_conn_ui_ops.report_disconnect = report_disconnect
182         c_conn_ui_ops.network_connected = network_connected
183         c_conn_ui_ops.network_disconnected = network_disconnected
184         c_conn_ui_ops.report_disconnect_reason = report_disconnect_reason
185
186         c_conv_ui_ops.create_conversation = create_conversation
187         c_conv_ui_ops.destroy_conversation = destroy_conversation
188         c_conv_ui_ops.write_chat = write_chat
189         c_conv_ui_ops.write_im = write_im
190         c_conv_ui_ops.write_conv = write_conv
191         c_conv_ui_ops.chat_add_users = chat_add_users
192         c_conv_ui_ops.chat_rename_user = chat_rename_user
193         c_conv_ui_ops.chat_remove_users = chat_remove_users
194         c_conv_ui_ops.chat_update_user = chat_update_user
195         c_conv_ui_ops.present = present
196         c_conv_ui_ops.has_focus = has_focus
197         c_conv_ui_ops.custom_smiley_add = custom_smiley_add
198         c_conv_ui_ops.custom_smiley_write = custom_smiley_write
199         c_conv_ui_ops.custom_smiley_close = custom_smiley_close
200         c_conv_ui_ops.send_confirm = send_confirm
201
202         c_notify_ui_ops.notify_message = notify_message
203         c_notify_ui_ops.notify_email = notify_email
204         c_notify_ui_ops.notify_emails = notify_emails
205         c_notify_ui_ops.notify_formatted = notify_formatted
206         c_notify_ui_ops.notify_searchresults = notify_searchresults
207         c_notify_ui_ops.notify_searchresults_new_rows = notify_searchresults_new_rows
208         c_notify_ui_ops.notify_userinfo = notify_userinfo
209         c_notify_ui_ops.notify_uri = notify_uri
210         c_notify_ui_ops.close_notify = close_notify
211
212         c_request_ui_ops.request_input = request_input
213         c_request_ui_ops.request_choice = request_choice
214         c_request_ui_ops.request_action = request_action
215         c_request_ui_ops.request_fields = request_fields
216         c_request_ui_ops.request_file = request_file
217         c_request_ui_ops.close_request = close_request
218         c_request_ui_ops.request_folder = request_folder
219
220         c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
221         c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
222         c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
223         c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
224         c_core_ui_ops.get_ui_info = <glib.GHashTable* (*)()> self.__core_ui_ops_get_ui_info
225
226         c_eventloop_ui_ops.timeout_add = glib.g_timeout_add
227         c_eventloop_ui_ops.timeout_remove = glib.g_source_remove
228         c_eventloop_ui_ops.input_add = glib_input_add
229         c_eventloop_ui_ops.input_remove = glib.g_source_remove
230         c_eventloop_ui_ops.input_get_error = NULL
231         c_eventloop_ui_ops.timeout_add_seconds = glib.g_timeout_add_seconds
232
233         core.c_purple_core_set_ui_ops(&c_core_ui_ops)
234         eventloop.c_purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
235
236         # initialize purple core
237         ret = core.c_purple_core_init(__APP_NAME__)
238         if ret is False:
239             debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
240             return False
241
242         # check if there is another instance of libpurple running
243         if core.c_purple_core_ensure_single_instance() == False:
244             debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
245             core.c_purple_core_quit()
246             return False
247
248         # create and load the buddy list
249         blist.c_purple_set_blist(blist.c_purple_blist_new())
250         blist.c_purple_blist_load()
251
252         # load pounces
253         pounce.c_purple_pounces_load()
254
255         return ret
256
257     def get_protocols(self):
258         return self.plugins.get_protocols()
259
260     def connect(self):
261         conn = Connection()
262         conn.connect()
263
264     def attach_signals(self, __signal_cbs=None):
265         if __signal_cbs is not None:
266             global signal_cbs
267             signal_cbs = __signal_cbs
268
269         cdef int handle
270
271         signals.c_purple_signal_connect(blist.c_purple_blist_get_handle(),
272                 "buddy-signed-off", &handle,
273                 <signals.PurpleCallback> signal_buddy_signed_off_cb, NULL)
274
275         signals.c_purple_signal_connect(
276                 conversation.c_purple_conversations_get_handle(),
277                 "receiving-im-msg", &handle,
278                 <signals.PurpleCallback> signal_receiving_im_msg_cb, NULL)
279
280 include "proxy.pyx"
281 include "account.pyx"
282 include "buddy.pyx"
283 include "connection.pyx"
284 include "conversation.pyx"