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