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