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 <libempathy/empathy-client-factory.h>
31 #include <libempathy/empathy-request-util.h>
32 #include <libempathy/empathy-tp-contact-factory.h>
33 #include <libempathy/empathy-utils.h>
35 #include "empathy-call-factory.h"
36 #include "empathy-call-handler.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
39 #include <libempathy/empathy-debug.h>
41 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
43 static void handle_channels (TpBaseClient *client,
45 TpConnection *connection,
47 GList *requests_satisfied,
48 gint64 user_action_time,
49 TpHandleChannelsContext *context);
51 static void approve_channels (TpBaseClient *client,
53 TpConnection *connection,
55 TpChannelDispatchOperation *dispatch_operation,
56 TpAddDispatchOperationContext *context);
66 static guint signals[LAST_SIGNAL] = {0};
68 static GObject *call_factory = NULL;
71 empathy_call_factory_init (EmpathyCallFactory *obj)
73 TpBaseClient *client = (TpBaseClient *) obj;
75 tp_base_client_take_approver_filter (client, tp_asv_new (
76 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
77 TP_IFACE_CHANNEL_TYPE_CALL,
78 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
79 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
82 tp_base_client_take_handler_filter (client, tp_asv_new (
83 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
84 TP_IFACE_CHANNEL_TYPE_CALL,
85 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
86 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
89 tp_base_client_take_handler_filter (client, tp_asv_new (
90 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
91 TP_IFACE_CHANNEL_TYPE_CALL,
92 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
93 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
94 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
97 tp_base_client_take_handler_filter (client, tp_asv_new (
98 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
99 TP_IFACE_CHANNEL_TYPE_CALL,
100 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
101 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
102 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
105 tp_base_client_add_handler_capabilities_varargs (client,
106 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
107 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
108 "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
113 empathy_call_factory_constructor (GType type, guint n_construct_params,
114 GObjectConstructParam *construct_params)
116 g_return_val_if_fail (call_factory == NULL, NULL);
118 call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
119 (type, n_construct_params, construct_params);
120 g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
126 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
128 GObjectClass *object_class = G_OBJECT_CLASS (klass);
129 TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
131 object_class->constructor = empathy_call_factory_constructor;
133 base_clt_cls->handle_channels = handle_channels;
134 base_clt_cls->add_dispatch_operation = approve_channels;
136 signals[NEW_CALL_HANDLER] =
137 g_signal_new ("new-call-handler",
138 G_TYPE_FROM_CLASS (klass),
139 G_SIGNAL_RUN_LAST, 0,
141 g_cclosure_marshal_generic,
143 2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
145 signals[INCOMING_CALL] =
146 g_signal_new ("incoming-call",
147 G_TYPE_FROM_CLASS (klass),
148 G_SIGNAL_RUN_LAST, 0,
150 g_cclosure_marshal_generic,
152 4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
153 TP_TYPE_CHANNEL_DISPATCH_OPERATION,
154 TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
158 empathy_call_factory_initialise (void)
160 EmpathyCallFactory *self;
161 EmpathyClientFactory *factory;
162 TpAccountManager *am;
164 g_return_val_if_fail (call_factory == NULL, NULL);
166 am = tp_account_manager_dup ();
167 factory = empathy_client_factory_dup ();
169 self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
170 "account-manager", am,
172 "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
176 g_object_unref (factory);
182 empathy_call_factory_get (void)
184 g_return_val_if_fail (call_factory != NULL, NULL);
186 return EMPATHY_CALL_FACTORY (call_factory);
190 handle_channels (TpBaseClient *client,
192 TpConnection *connection,
194 GList *requests_satisfied,
195 gint64 user_action_time,
196 TpHandleChannelsContext *context)
198 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
201 for (l = channels; l != NULL; l = g_list_next (l))
203 TpChannel *channel = l->data;
205 TpContact *tp_contact;
206 EmpathyContact *contact;
207 EmpathyCallHandler *handler;
209 if (tp_proxy_get_invalidated (channel) != NULL)
212 if (tp_channel_get_channel_type_id (channel) !=
213 TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
216 if (!TP_IS_CALL_CHANNEL (channel))
219 call = TP_CALL_CHANNEL (channel);
221 tp_contact = tp_channel_get_target_contact (channel);
222 contact = empathy_contact_dup_from_tp_contact (tp_contact);
223 handler = empathy_call_handler_new_for_channel (call, contact);
225 g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
228 g_object_unref (handler);
229 g_object_unref (contact);
232 tp_handle_channels_context_accept (context);
235 static TpCallChannel *
236 find_call_channel (GList *channels)
240 for (l = channels; l != NULL; l = g_list_next (l))
242 TpChannel *channel = l->data;
245 if (tp_proxy_get_invalidated (channel) != NULL)
248 channel_type = tp_channel_get_channel_type_id (channel);
250 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
251 return TP_CALL_CHANNEL (channel);
258 approve_channels (TpBaseClient *client,
260 TpConnection *connection,
262 TpChannelDispatchOperation *dispatch_operation,
263 TpAddDispatchOperationContext *context)
265 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
266 TpCallChannel *channel;
268 GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, "" };
269 gboolean handled = FALSE;
271 channel = find_call_channel (channels);
275 DEBUG ("Failed to find the main channel; ignoring");
276 error.message = "Unknown channel";
280 handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
284 DEBUG ("Unknown handle, ignoring");
285 error.code = TP_ERROR_INVALID_HANDLE;
286 error.message = "Unknown handle";
290 g_signal_emit (self, signals[INCOMING_CALL], 0,
291 handle, channel, dispatch_operation, context,
297 /* There was no call window so the context wasn't handled. */
298 DEBUG ("Call with a contact for which there's no existing "
299 "call window, ignoring");
300 error.message = "No call window with this contact";
303 tp_add_dispatch_operation_context_fail (context, &error);
307 empathy_call_factory_register (EmpathyCallFactory *self,
310 return tp_base_client_register (TP_BASE_CLIENT (self), error);