Added PurpleNotifyUiOps callbacks.
[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 request.PurpleRequestUiOps c_request_ui_ops
40 #cdef roomlist.PurpleRoomlistUiOps c_rlist_ui_ops
41
42 cdef glib.GHashTable *c_ui_info
43
44 c_ui_info = NULL
45
46 include "account_cbs.pxd"
47 include "blist_cbs.pxd"
48 include "connection_cbs.pxd"
49 include "conversation_cbs.pxd"
50 include "notify_cbs.pxd"
51
52 cdef class Purple:
53     """ Purple class.
54
55     @parm debug_enabled: Toggle debug messages.
56     @parm app_name: Set application name.
57     @parm default_path: Full path for libpurple user files.
58     """
59
60     def __init__(self, debug_enabled=True, app_name=__APP_NAME__, default_path=__DEFAULT_PATH__):
61         if app_name is not __APP_NAME__:
62             __APP_NAME__ = app_name
63
64         if default_path is not __DEFAULT_PATH__:
65             __DEFAULT_PATH__ = default_path
66
67         debug.c_purple_debug_set_enabled(debug_enabled)
68         util.c_purple_util_set_user_dir(default_path)
69         plugin.c_purple_plugins_add_search_path(default_path)
70
71         # adds glib iteration inside ecore main loop
72         ecore.idler_add(self.__glib_iteration_when_idle)
73
74     def __del__(self):
75         core.c_purple_core_quit()
76
77     cdef void __core_ui_ops_ui_prefs_init(self):
78         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_prefs_init\n")
79         prefs.c_purple_prefs_load()
80
81         prefs.c_purple_prefs_add_none("/carman")
82
83     cdef void __core_ui_ops_debug_init(self):
84         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "debug_ui_init\n")
85
86     cdef void __core_ui_ops_ui_init(self):
87         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "ui_init\n")
88
89         account.c_purple_accounts_set_ui_ops(&c_account_ui_ops)
90         connection.c_purple_connections_set_ui_ops(&c_conn_ui_ops)
91         blist.c_purple_blist_set_ui_ops(&c_blist_ui_ops)
92         conversation.c_purple_conversations_set_ui_ops(&c_conv_ui_ops)
93         notify.c_purple_notify_set_ui_ops(&c_notify_ui_ops)
94         #request.c_purple_request_set_ui_ops(&c_request_ui_ops)
95         #ft.c_purple_xfers_set_ui_ops(&c_ft_ui_ops)
96         #roomlist.c_purple_roomlist_set_ui_ops(&c_rlist_ui_ops)
97
98     cdef void __core_ui_ops_quit(self):
99         debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "core_ui_ops", "quit\n")
100
101         global c_ui_info
102
103         account.c_purple_accounts_set_ui_ops(NULL)
104         connection.c_purple_connections_set_ui_ops(NULL)
105         blist.c_purple_blist_set_ui_ops(NULL)
106         conversation.c_purple_conversations_set_ui_ops(NULL)
107         notify.c_purple_notify_set_ui_ops(NULL)
108         #request.c_purple_request_set_ui_ops(NULL)
109         #ft.c_purple_xfers_set_ui_ops(NULL)
110         #roomlist.c_purple_roomlist_set_ui_ops(NULL)
111
112         if c_ui_info:
113             glib.g_hash_table_destroy(c_ui_info)
114
115     cdef glib.GHashTable *__core_ui_ops_get_ui_info(self):
116         global c_ui_info
117
118         if c_ui_info == NULL:
119             c_ui_info = glib.g_hash_table_new(glib.g_str_hash, glib.g_str_equal)
120
121             glib.g_hash_table_insert(c_ui_info, "name", <glib.gpointer> __APP_NAME__)
122             glib.g_hash_table_insert(c_ui_info, "version", <glib.gpointer> __APP_VERSION__)
123         return c_ui_info
124
125     def __glib_iteration_when_idle(self):
126         glib.g_main_context_iteration(NULL, False)
127         return True
128
129     def purple_init(self, callbacks_dict=None):
130         """ Initializes libpurple """
131
132         if callbacks_dict is not None:
133             global account_cbs
134             global blist_cbs
135             global connection_cbs
136             global conversation_cbs
137             global notify_cbs
138
139             account_cbs = callbacks_dict["account"]
140             blist_cbs = callbacks_dict["blist"]
141             connection_cbs = callbacks_dict["connection"]
142             conversation_cbs = callbacks_dict["conversation"]
143             notify_cbs = callbacks_dict["notify"]
144
145         c_account_ui_ops.notify_added = notify_added
146         c_account_ui_ops.status_changed = status_changed
147         c_account_ui_ops.request_add = request_add
148         c_account_ui_ops.request_authorize = request_authorize
149         c_account_ui_ops.close_account_request = close_account_request
150
151         c_blist_ui_ops.new_list = new_list
152         c_blist_ui_ops.new_node = new_node
153         c_blist_ui_ops.show = show
154         c_blist_ui_ops.update = update
155         c_blist_ui_ops.remove = remove
156         c_blist_ui_ops.destroy = destroy
157         c_blist_ui_ops.set_visible = set_visible
158         c_blist_ui_ops.request_add_buddy = request_add_buddy
159         c_blist_ui_ops.request_add_chat = request_add_chat
160         c_blist_ui_ops.request_add_group = request_add_group
161
162         c_conn_ui_ops.connect_progress = connect_progress
163         c_conn_ui_ops.connected = connected
164         c_conn_ui_ops.disconnected = disconnected
165         c_conn_ui_ops.notice = notice
166         c_conn_ui_ops.report_disconnect = report_disconnect
167         c_conn_ui_ops.network_connected = network_connected
168         c_conn_ui_ops.network_disconnected = network_disconnected
169         c_conn_ui_ops.report_disconnect_reason = report_disconnect_reason
170
171         c_conv_ui_ops.create_conversation = create_conversation
172         c_conv_ui_ops.destroy_conversation = destroy_conversation
173         c_conv_ui_ops.write_chat = write_chat
174         c_conv_ui_ops.write_im = write_im
175         c_conv_ui_ops.write_conv = write_conv
176         c_conv_ui_ops.chat_add_users = chat_add_users
177         c_conv_ui_ops.chat_rename_user = chat_rename_user
178         c_conv_ui_ops.chat_remove_users = chat_remove_users
179         c_conv_ui_ops.chat_update_user = chat_update_user
180         c_conv_ui_ops.present = present
181         c_conv_ui_ops.has_focus = has_focus
182         c_conv_ui_ops.custom_smiley_add = custom_smiley_add
183         c_conv_ui_ops.custom_smiley_write = custom_smiley_write
184         c_conv_ui_ops.custom_smiley_close = custom_smiley_close
185         c_conv_ui_ops.send_confirm = send_confirm
186
187         c_notify_ui_ops.notify_message = notify_message
188         c_notify_ui_ops.notify_email = notify_email
189         c_notify_ui_ops.notify_emails = notify_emails
190         c_notify_ui_ops.notify_formatted = notify_formatted
191         c_notify_ui_ops.notify_searchresults = notify_searchresults
192         c_notify_ui_ops.notify_searchresults_new_rows = notify_searchresults_new_rows
193         c_notify_ui_ops.notify_userinfo = notify_userinfo
194         c_notify_ui_ops.notify_uri = notify_uri
195         c_notify_ui_ops.close_notify = close_notify
196
197         c_core_ui_ops.ui_prefs_init = <void (*)()> self.__core_ui_ops_ui_prefs_init
198         c_core_ui_ops.debug_ui_init = <void (*)()> self.__core_ui_ops_debug_init
199         c_core_ui_ops.ui_init = <void (*)()> self.__core_ui_ops_ui_init
200         c_core_ui_ops.quit = <void (*)()> self.__core_ui_ops_quit
201         c_core_ui_ops.get_ui_info = <glib.GHashTable* (*)()> self.__core_ui_ops_get_ui_info
202
203         c_eventloop_ui_ops.timeout_add = glib.g_timeout_add
204         c_eventloop_ui_ops.timeout_remove = glib.g_source_remove
205         c_eventloop_ui_ops.input_add = glib_input_add
206         c_eventloop_ui_ops.input_remove = glib.g_source_remove
207         c_eventloop_ui_ops.input_get_error = NULL
208         c_eventloop_ui_ops.timeout_add_seconds = glib.g_timeout_add_seconds
209
210         core.c_purple_core_set_ui_ops(&c_core_ui_ops)
211         eventloop.c_purple_eventloop_set_ui_ops(&c_eventloop_ui_ops)
212
213         # initialize purple core
214         ret = core.c_purple_core_init(__APP_NAME__)
215         if ret is False:
216             debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because libpurple initialization failed.\n")
217             return False
218
219         # check if there is another instance of libpurple running
220         if core.c_purple_core_ensure_single_instance() == False:
221             debug.c_purple_debug(debug.PURPLE_DEBUG_INFO, "main", "Exiting because another instance of libpurple is already running.\n")
222             core.c_purple_core_quit()
223             return False
224
225         # create and load the buddy list
226         blist.c_purple_set_blist(blist.c_purple_blist_new())
227         blist.c_purple_blist_load()
228
229         # load pounces
230         pounce.c_purple_pounces_load()
231
232         return ret
233
234     def get_protocols(self):
235         cdef glib.GList *iter
236         cdef plugin.PurplePlugin *__plugin
237         protocols = []
238         iter = plugin.c_purple_plugins_get_protocols()
239         while iter:
240             __plugin = <plugin.PurplePlugin*> iter.data
241             if __plugin.info and __plugin.info.name:
242                 protocols += [(__plugin.info.id, __plugin.info.name)]
243             iter = iter.next
244         return protocols
245
246     def connect(self):
247         conn = Connection()
248         conn.connect()
249
250 include "proxy.pyx"
251 include "account.pyx"
252 include "buddy.pyx"
253 include "connection.pyx"
254 include "conversation.pyx"