Added set_alias to 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 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 __DEFAULT_PATH__ = "/tmp"
28 __APP_NAME__ = "carman-purple-python"
29 __APP_VERSION__ = "0.1"
30
31 cdef account.PurpleAccountUiOps c_account_ui_ops
32 cdef blist.PurpleBlistUiOps c_blist_ui_ops
33 cdef connection.PurpleConnectionUiOps c_conn_ui_ops
34 cdef conversation.PurpleConversationUiOps c_conv_ui_ops
35 cdef core.PurpleCoreUiOps c_core_ui_ops
36 cdef eventloop.PurpleEventLoopUiOps c_eventloop_ui_ops
37 #cdef ft.PurpleXferUiOps c_ft_ui_ops
38 cdef notify.PurpleNotifyUiOps c_notify_ui_ops
39 #cdef privacy.PurplePrivacyUiOps c_privacy_ui_ops
40 cdef request.PurpleRequestUiOps c_request_ui_ops
41 #cdef roomlist.PurpleRoomlistUiOps c_rlist_ui_ops
42
43 cdef glib.GHashTable *c_ui_info
44
45 c_ui_info = NULL
46
47 include "account_cbs.pxd"
48 include "blist_cbs.pxd"
49 include "connection_cbs.pxd"
50 include "conversation_cbs.pxd"
51 #include "xfer_cbs.pxd"
52 include "notify_cbs.pxd"
53 #include "privacy_cbs.pxd"
54 include "request_cbs.pxd"
55 #include "roomlist_cbs.pxd"
56 include "signal_cbs.pxd"
57
58 include "util.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     def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
69         if app_name is not __APP_NAME__:
70             __APP_NAME__ = app_name
71
72         if default_path is not __DEFAULT_PATH__:
73             __DEFAULT_PATH__ = default_path
74
75         debug.purple_debug_set_enabled(debug_enabled)
76         util.purple_util_set_user_dir(default_path)
77         plugin.purple_plugins_add_search_path(default_path)
78
79         # adds glib iteration inside ecore main loop
80         ecore.timer_add(0.001, self.__glib_iteration_when_idle)
81
82     def __get_ui_name(self):
83         return __APP_NAME__
84     ui_name = property(__get_ui_name)
85
86     def destroy(self):
87         core.purple_core_quit()
88
89     cdef void __core_ui_ops_ui_prefs_init(self):
90         debug.purple_debug_info("core_ui_ops", "%s", "ui_prefs_init\n")
91         prefs.purple_prefs_load()
92
93         prefs.purple_prefs_add_none("/carman")
94
95     cdef void __core_ui_ops_debug_init(self):
96         debug.purple_debug_info("core_ui_ops", "%s", "debug_ui_init\n")
97         pass
98
99     cdef void __core_ui_ops_ui_init(self):
100         debug.purple_debug_info("core_ui_ops", "%s", "ui_init\n")
101
102         account.purple_accounts_set_ui_ops(&c_account_ui_ops)
103         connection.purple_connections_set_ui_ops(&c_conn_ui_ops)
104         blist.purple_blist_set_ui_ops(&c_blist_ui_ops)
105         conversation.purple_conversations_set_ui_ops(&c_conv_ui_ops)
106         notify.purple_notify_set_ui_ops(&c_notify_ui_ops)
107         #privacy.purple_privacy_set_ui_ops(&c_privacy_ui_ops)
108         request.purple_request_set_ui_ops(&c_request_ui_ops)
109         #ft.purple_xfers_set_ui_ops(&c_ft_ui_ops)
110         #roomlist.purple_roomlist_set_ui_ops(&c_rlist_ui_ops)
111
112     cdef void __core_ui_ops_quit(self):
113         debug.purple_debug_info("core_ui_ops", "%s", "quit\n")
114
115         global c_ui_info
116
117         account.purple_accounts_set_ui_ops(NULL)
118         connection.purple_connections_set_ui_ops(NULL)
119         blist.purple_blist_set_ui_ops(NULL)
120         conversation.purple_conversations_set_ui_ops(NULL)
121         notify.purple_notify_set_ui_ops(NULL)
122         #privacy.purple_privacy_set_ui_ops(NULL)
123         request.purple_request_set_ui_ops(NULL)
124         #ft.purple_xfers_set_ui_ops(NULL)
125         #roomlist.purple_roomlist_set_ui_ops(NULL)
126
127         if c_ui_info:
128             glib.g_hash_table_destroy(c_ui_info)
129
130     cdef glib.GHashTable *__core_ui_ops_get_ui_info(self):
131         global c_ui_info
132
133         if c_ui_info == NULL:
134             c_ui_info = glib.g_hash_table_new(glib.g_str_hash, glib.g_str_equal)
135
136             glib.g_hash_table_insert(c_ui_info, "name", <glib.gpointer> __APP_NAME__)
137             glib.g_hash_table_insert(c_ui_info, "version", <glib.gpointer> __APP_VERSION__)
138         return c_ui_info
139
140     def __glib_iteration_when_idle(self):
141         glib.g_main_context_iteration(NULL, False)
142         return True
143
144     def purple_init(self):
145         """ Initializes libpurple """
146
147         c_account_ui_ops.notify_added = notify_added
148         c_account_ui_ops.status_changed = status_changed
149         c_account_ui_ops.request_add = request_add
150         c_account_ui_ops.request_authorize = request_authorize
151         c_account_ui_ops.close_account_request = close_account_request
152
153         c_blist_ui_ops.new_list = new_list
154         c_blist_ui_ops.new_node = new_node
155         c_blist_ui_ops.show = show
156         c_blist_ui_ops.update = update
157         c_blist_ui_ops.remove = remove
158         c_blist_ui_ops.destroy = destroy
159         c_blist_ui_ops.set_visible = set_visible
160         c_blist_ui_ops.request_add_buddy = request_add_buddy
161         c_blist_ui_ops.request_add_chat = request_add_chat
162         c_blist_ui_ops.request_add_group = request_add_group
163
164         c_conn_ui_ops.connect_progress = connect_progress
165         c_conn_ui_ops.connected = connected
166         c_conn_ui_ops.disconnected = disconnected
167         c_conn_ui_ops.notice = notice
168         c_conn_ui_ops.report_disconnect = report_disconnect
169         c_conn_ui_ops.network_connected = network_connected
170         c_conn_ui_ops.network_disconnected = network_disconnected
171         c_conn_ui_ops.report_disconnect_reason = report_disconnect_reason
172
173         c_conv_ui_ops.create_conversation = create_conversation
174         c_conv_ui_ops.destroy_conversation = destroy_conversation
175         c_conv_ui_ops.write_chat = write_chat
176         c_conv_ui_ops.write_im = write_im
177         c_conv_ui_ops.write_conv = write_conv
178         c_conv_ui_ops.chat_add_users = chat_add_users
179         c_conv_ui_ops.chat_rename_user = chat_rename_user
180         c_conv_ui_ops.chat_remove_users = chat_remove_users
181         c_conv_ui_ops.chat_update_user = chat_update_user
182         c_conv_ui_ops.present = present
183         c_conv_ui_ops.has_focus = has_focus
184         c_conv_ui_ops.custom_smiley_add = custom_smiley_add
185         c_conv_ui_ops.custom_smiley_write = custom_smiley_write
186         c_conv_ui_ops.custom_smiley_close = custom_smiley_close
187         c_conv_ui_ops.send_confirm = send_confirm
188
189         c_notify_ui_ops.notify_message = notify_message
190         c_notify_ui_ops.notify_email = notify_email
191         c_notify_ui_ops.notify_emails = notify_emails
192         c_notify_ui_ops.notify_formatted = notify_formatted
193         c_notify_ui_ops.notify_searchresults = notify_searchresults
194         c_notify_ui_ops.notify_searchresults_new_rows = notify_searchresults_new_rows
195         c_notify_ui_ops.notify_userinfo = notify_userinfo
196         c_notify_ui_ops.notify_uri = notify_uri
197         c_notify_ui_ops.close_notify = close_notify
198
199         c_request_ui_ops.request_input = request_input
200         c_request_ui_ops.request_choice = request_choice
201         c_request_ui_ops.request_action = request_action
202         c_request_ui_ops.request_fields = request_fields
203         c_request_ui_ops.request_file = request_file
204         c_request_ui_ops.close_request = close_request
205         c_request_ui_ops.request_folder = request_folder
206
207         c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
208         c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
209         c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
210         c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
211         c_core_ui_ops.get_ui_info = <glib.GHashTable* (*)()> self.__core_ui_ops_get_ui_info
212
213         c_eventloop_ui_ops.timeout_add = glib.g_timeout_add
214         c_eventloop_ui_ops.timeout_remove = glib.g_source_remove
215         c_eventloop_ui_ops.input_add = glib_input_add
216         c_eventloop_ui_ops.input_remove = glib.g_source_remove
217         c_eventloop_ui_ops.input_get_error = NULL
218         c_eventloop_ui_ops.timeout_add_seconds = NULL
219
220         core.purple_core_set_ui_ops(&c_core_ui_ops)
221         eventloop.purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
222
223         # initialize purple core
224         ret = core.purple_core_init(__APP_NAME__)
225         if ret is False:
226             debug.purple_debug_fatal("main", "%s", "libpurple " \
227                                        "initialization failed.\n")
228             return False
229
230         # check if there is another instance of libpurple running
231         if core.purple_core_ensure_single_instance() == False:
232             debug.purple_debug_fatal("main", "%s", "Another instance of " \
233                                       "libpurple is already running.\n")
234             core.purple_core_quit()
235             return False
236
237         # create and load the buddy list
238         blist.purple_set_blist(blist.purple_blist_new())
239         blist.purple_blist_load()
240
241         # load pounces
242         pounce.purple_pounces_load()
243
244         return ret
245
246     def add_callback(self, type, name, callback):
247         """
248         Adds a callback with given name inside callback's type.
249
250         @param type     Callback type (e.g. "account")
251         @param name     Callback name (e.g. "notify-added")
252         @param callback Callback to be called
253         """
254         global account_cbs
255         global blist_cbs
256         global connection_cbs
257         global conversation_cbs
258         global notify_cbs
259         global request_cbs
260
261         { "account": account_cbs,
262           "blist": blist_cbs,
263           "connection": connection_cbs,
264           "conversation": conversation_cbs,
265           "notify": notify_cbs,
266           "request": request_cbs }[type][name] = callback
267
268     def signal_connect(self, name=None, cb=None):
269         cdef int handle
270         cdef plugin.PurplePlugin *jabber
271
272         if name is None:
273             return
274
275         jabber = prpl.purple_find_prpl("prpl-jabber")
276         if jabber == NULL:
277             return
278
279         global signal_cbs
280         signal_cbs[name] = cb
281
282         if name == "signed-on":
283             signals.purple_signal_connect(
284                     connection.purple_connections_get_handle(),
285                     "signed-on", &handle,
286                     <signals.PurpleCallback> signal_signed_on_cb, NULL)
287         elif name == "signed-off":
288             signals.purple_signal_connect(
289                     connection.purple_connections_get_handle(),
290                     "signed-off", &handle,
291                     <signals.PurpleCallback> signal_signed_off_cb, NULL)
292         elif name == "connection-error":
293             signals.purple_signal_connect(
294                     connection.purple_connections_get_handle(),
295                     "connection-error", &handle,
296                     <signals.PurpleCallback> signal_connection_error_cb, NULL)
297         elif name == "buddy-signed-on":
298             signals.purple_signal_connect(
299                     blist.purple_blist_get_handle(),
300                     "buddy-signed-on", &handle,
301                     <signals.PurpleCallback> signal_buddy_signed_on_cb, NULL)
302         elif name == "buddy-signed-off":
303             signals.purple_signal_connect(
304                     blist.purple_blist_get_handle(),
305                     "buddy-signed-off", &handle,
306                     <signals.PurpleCallback> signal_buddy_signed_off_cb, NULL)
307         elif name == "receiving-im-msg":
308             signals.purple_signal_connect(
309                     conversation.purple_conversations_get_handle(),
310                     "receiving-im-msg", &handle,
311                     <signals.PurpleCallback> signal_receiving_im_msg_cb, NULL)
312         elif name == "jabber-receiving-xmlnode":
313             signals.purple_signal_connect(
314                     jabber, "jabber-receiving-xmlnode", &handle,
315                     <signals.PurpleCallback> jabber_receiving_xmlnode_cb, NULL)
316
317     def accounts_get_all(self):
318         cdef glib.GList *iter
319         cdef account.PurpleAccount *acc
320         cdef char *username
321         cdef char *protocol_id
322
323         iter = account.purple_accounts_get_all()
324         account_list = []
325
326         while iter:
327             acc = <account.PurpleAccount *> iter.data
328
329             if <account.PurpleAccount *>acc:
330                 username = <char *> account.purple_account_get_username(acc)
331                 protocol_id = <char *> account.purple_account_get_protocol_id(acc)
332
333                 if username != NULL and protocol_id != NULL:
334                     account_list.append(Account(username, \
335                             Protocol(protocol_id), self))
336             iter = iter.next
337
338         return account_list
339
340     def accounts_get_all_active(self):
341         cdef glib.GList *iter
342         cdef account.PurpleAccount *acc
343         cdef char *username
344         cdef char *protocol_id
345
346         iter = account.purple_accounts_get_all_active()
347         account_list = []
348
349         while iter:
350             acc = <account.PurpleAccount *> iter.data
351
352             if <account.PurpleAccount *>acc:
353                 username = <char *> account.purple_account_get_username(acc)
354                 protocol_id = <char *> account.purple_account_get_protocol_id(acc)
355
356                 if username != NULL and protocol_id != NULL:
357                     account_list.append(Account(username, \
358                             Protocol(protocol_id), self))
359             iter = iter.next
360
361         return account_list
362
363     def protocols_get_all(self):
364         cdef glib.GList *iter
365         cdef plugin.PurplePlugin *pp
366
367         iter = plugin.purple_plugins_get_protocols()
368         protocol_list = []
369         while iter:
370             pp = <plugin.PurplePlugin*> iter.data
371             if pp.info and pp.info.name:
372                 protocol_list.append(Protocol(pp.info.id))
373             iter = iter.next
374         return protocol_list
375
376 include "protocol.pyx"
377 #include "plugin.pyx"
378 include "proxy.pyx"
379 #include "protocol.pyx"
380 include "account.pyx"
381 include "buddy.pyx"
382 #include "connection.pyx"
383 include "conversation.pyx"