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