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