2 * empathy-call-factory.c - Source for EmpathyCallFactory
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <telepathy-glib/account-channel-request.h>
26 #include <telepathy-glib/simple-handler.h>
27 #include <telepathy-glib/interfaces.h>
28 #include <telepathy-glib/util.h>
30 #include <telepathy-yell/telepathy-yell.h>
32 #include <libempathy/empathy-client-factory.h>
33 #include <libempathy/empathy-request-util.h>
34 #include <libempathy/empathy-tp-contact-factory.h>
35 #include <libempathy/empathy-utils.h>
37 #include "empathy-call-factory.h"
38 #include "empathy-call-handler.h"
39 #include "src-marshal.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
42 #include <libempathy/empathy-debug.h>
44 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
46 static void handle_channels (TpBaseClient *client,
48 TpConnection *connection,
50 GList *requests_satisfied,
51 gint64 user_action_time,
52 TpHandleChannelsContext *context);
54 static void approve_channels (TpBaseClient *client,
56 TpConnection *connection,
58 TpChannelDispatchOperation *dispatch_operation,
59 TpAddDispatchOperationContext *context);
69 static guint signals[LAST_SIGNAL] = {0};
71 static GObject *call_factory = NULL;
74 empathy_call_factory_init (EmpathyCallFactory *obj)
76 TpBaseClient *client = (TpBaseClient *) obj;
78 tp_base_client_take_approver_filter (client, tp_asv_new (
79 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
80 TPY_IFACE_CHANNEL_TYPE_CALL,
81 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
82 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
85 tp_base_client_take_handler_filter (client, tp_asv_new (
86 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
87 TPY_IFACE_CHANNEL_TYPE_CALL,
88 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
89 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
92 tp_base_client_take_handler_filter (client, tp_asv_new (
93 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
94 TPY_IFACE_CHANNEL_TYPE_CALL,
95 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
96 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
97 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
100 tp_base_client_take_handler_filter (client, tp_asv_new (
101 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
102 TPY_IFACE_CHANNEL_TYPE_CALL,
103 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
104 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
105 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
108 tp_base_client_add_handler_capabilities_varargs (client,
109 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
110 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
111 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
116 empathy_call_factory_constructor (GType type, guint n_construct_params,
117 GObjectConstructParam *construct_params)
119 g_return_val_if_fail (call_factory == NULL, NULL);
121 call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
122 (type, n_construct_params, construct_params);
123 g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
129 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
131 GObjectClass *object_class = G_OBJECT_CLASS (klass);
132 TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
134 object_class->constructor = empathy_call_factory_constructor;
136 base_clt_cls->handle_channels = handle_channels;
137 base_clt_cls->add_dispatch_operation = approve_channels;
139 signals[NEW_CALL_HANDLER] =
140 g_signal_new ("new-call-handler",
141 G_TYPE_FROM_CLASS (klass),
142 G_SIGNAL_RUN_LAST, 0,
144 _src_marshal_VOID__OBJECT_BOOLEAN,
146 2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
148 signals[INCOMING_CALL] =
149 g_signal_new ("incoming-call",
150 G_TYPE_FROM_CLASS (klass),
151 G_SIGNAL_RUN_LAST, 0,
153 _src_marshal_BOOLEAN__UINT_OBJECT_OBJECT_OBJECT,
155 4, G_TYPE_UINT, TPY_TYPE_CALL_CHANNEL,
156 TP_TYPE_CHANNEL_DISPATCH_OPERATION,
157 TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
161 empathy_call_factory_initialise (void)
163 EmpathyCallFactory *self;
164 EmpathyClientFactory *factory;
165 TpAccountManager *am;
167 g_return_val_if_fail (call_factory == NULL, NULL);
169 am = tp_account_manager_dup ();
170 factory = empathy_client_factory_dup ();
172 self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
173 "account-manager", am,
175 "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
179 g_object_unref (factory);
185 empathy_call_factory_get (void)
187 g_return_val_if_fail (call_factory != NULL, NULL);
189 return EMPATHY_CALL_FACTORY (call_factory);
193 call_channel_got_contact (TpConnection *connection,
194 EmpathyContact *contact,
197 GObject *weak_object)
199 EmpathyCallFactory *factory = EMPATHY_CALL_FACTORY (weak_object);
200 EmpathyCallHandler *handler;
201 TpyCallChannel *call = TPY_CALL_CHANNEL (user_data);
205 /* FIXME use hangup with an appropriate error */
206 tp_channel_close_async (TP_CHANNEL (call), NULL, NULL);
210 handler = empathy_call_handler_new_for_channel (call, contact);
212 g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
215 g_object_unref (handler);
219 call_channel_ready (EmpathyCallFactory *factory,
220 TpyCallChannel *call)
222 TpChannel *channel = TP_CHANNEL (call);
225 id = tp_channel_get_identifier (channel);
227 /* The ready callback has a reference, so pass that on */
228 empathy_tp_contact_factory_get_from_id (
229 tp_channel_borrow_connection (channel),
231 call_channel_got_contact,
234 (GObject *) factory);
238 call_channel_ready_cb (TpyCallChannel *call,
240 EmpathyCallFactory *factory)
244 g_object_get (call, "ready", &ready, NULL);
248 call_channel_ready (factory, call);
253 handle_channels (TpBaseClient *client,
255 TpConnection *connection,
257 GList *requests_satisfied,
258 gint64 user_action_time,
259 TpHandleChannelsContext *context)
261 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
264 for (l = channels; l != NULL; l = g_list_next (l))
266 TpChannel *channel = l->data;
267 TpyCallChannel *call;
270 if (tp_proxy_get_invalidated (channel) != NULL)
273 if (tp_channel_get_channel_type_id (channel) !=
274 TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
277 if (!TPY_IS_CALL_CHANNEL (channel))
280 call = TPY_CALL_CHANNEL (channel);
282 /* Take a ref to keep while hopping through the async callbacks */
284 g_object_get (call, "ready", &ready, NULL);
287 tp_g_signal_connect_object (call, "notify::ready",
288 G_CALLBACK (call_channel_ready_cb), self, 0);
290 call_channel_ready (self, call);
293 tp_handle_channels_context_accept (context);
296 static TpyCallChannel *
297 find_call_channel (GList *channels)
301 for (l = channels; l != NULL; l = g_list_next (l))
303 TpChannel *channel = l->data;
306 if (tp_proxy_get_invalidated (channel) != NULL)
309 channel_type = tp_channel_get_channel_type_id (channel);
311 if (channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
312 return TPY_CALL_CHANNEL (channel);
319 approve_channels (TpBaseClient *client,
321 TpConnection *connection,
323 TpChannelDispatchOperation *dispatch_operation,
324 TpAddDispatchOperationContext *context)
326 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
327 TpyCallChannel *channel;
329 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, "" };
330 gboolean handled = FALSE;
332 channel = find_call_channel (channels);
336 DEBUG ("Failed to find the main channel; ignoring");
337 error.message = "Unknown channel";
341 handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
345 DEBUG ("Unknown handle, ignoring");
346 error.code = TP_ERROR_INVALID_HANDLE;
347 error.message = "Unknown handle";
351 g_signal_emit (self, signals[INCOMING_CALL], 0,
352 handle, channel, dispatch_operation, context,
358 /* There was no call window so the context wasn't handled. */
359 DEBUG ("Call with a contact for which there's no existing "
360 "call window, ignoring");
361 error.message = "No call window with this contact";
364 tp_add_dispatch_operation_context_fail (context, &error);
368 empathy_call_factory_register (EmpathyCallFactory *self,
371 return tp_base_client_register (TP_BASE_CLIENT (self), error);