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