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
23 #include <libempathy/empathy-client-factory.h>
24 #include <libempathy/empathy-request-util.h>
26 #include "empathy-call-factory.h"
27 #include "empathy-call-handler.h"
29 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
30 #include <libempathy/empathy-debug.h>
32 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
34 static void handle_channels (TpBaseClient *client,
36 TpConnection *connection,
38 GList *requests_satisfied,
39 gint64 user_action_time,
40 TpHandleChannelsContext *context);
42 static void approve_channels (TpBaseClient *client,
44 TpConnection *connection,
46 TpChannelDispatchOperation *dispatch_operation,
47 TpAddDispatchOperationContext *context);
57 static guint signals[LAST_SIGNAL] = {0};
59 static GObject *call_factory = NULL;
62 empathy_call_factory_init (EmpathyCallFactory *obj)
64 TpBaseClient *client = (TpBaseClient *) obj;
66 tp_base_client_take_approver_filter (client, tp_asv_new (
67 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
68 TP_IFACE_CHANNEL_TYPE_CALL,
69 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
70 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
73 tp_base_client_take_handler_filter (client, tp_asv_new (
74 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
75 TP_IFACE_CHANNEL_TYPE_CALL,
76 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
77 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
80 tp_base_client_take_handler_filter (client, tp_asv_new (
81 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
82 TP_IFACE_CHANNEL_TYPE_CALL,
83 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
84 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
85 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
88 tp_base_client_take_handler_filter (client, tp_asv_new (
89 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
90 TP_IFACE_CHANNEL_TYPE_CALL,
91 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
92 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
93 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
96 tp_base_client_add_handler_capabilities_varargs (client,
97 "org.freedesktop.Telepathy.Channel.Type.Call1/audio",
98 "org.freedesktop.Telepathy.Channel.Type.Call1/video",
99 "org.freedesktop.Telepathy.Channel.Type.Call1/ice",
100 "org.freedesktop.Telepathy.Channel.Type.Call1/gtalk-p2p",
101 "org.freedesktop.Telepathy.Channel.Type.Call1/video/h264",
106 empathy_call_factory_constructor (GType type, guint n_construct_params,
107 GObjectConstructParam *construct_params)
109 g_return_val_if_fail (call_factory == NULL, NULL);
111 call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
112 (type, n_construct_params, construct_params);
113 g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
119 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
121 GObjectClass *object_class = G_OBJECT_CLASS (klass);
122 TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
124 object_class->constructor = empathy_call_factory_constructor;
126 base_clt_cls->handle_channels = handle_channels;
127 base_clt_cls->add_dispatch_operation = approve_channels;
129 signals[NEW_CALL_HANDLER] =
130 g_signal_new ("new-call-handler",
131 G_TYPE_FROM_CLASS (klass),
132 G_SIGNAL_RUN_LAST, 0,
134 g_cclosure_marshal_generic,
136 2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
138 signals[INCOMING_CALL] =
139 g_signal_new ("incoming-call",
140 G_TYPE_FROM_CLASS (klass),
141 G_SIGNAL_RUN_LAST, 0,
143 g_cclosure_marshal_generic,
145 4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
146 TP_TYPE_CHANNEL_DISPATCH_OPERATION,
147 TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
151 empathy_call_factory_initialise (void)
153 EmpathyCallFactory *self;
154 EmpathyClientFactory *factory;
155 TpAccountManager *am;
157 g_return_val_if_fail (call_factory == NULL, NULL);
159 am = tp_account_manager_dup ();
160 factory = empathy_client_factory_dup ();
162 self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
163 "account-manager", am,
165 "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
169 g_object_unref (factory);
175 empathy_call_factory_get (void)
177 g_return_val_if_fail (call_factory != NULL, NULL);
179 return EMPATHY_CALL_FACTORY (call_factory);
183 handle_channels (TpBaseClient *client,
185 TpConnection *connection,
187 GList *requests_satisfied,
188 gint64 user_action_time,
189 TpHandleChannelsContext *context)
191 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
194 for (l = channels; l != NULL; l = g_list_next (l))
196 TpChannel *channel = l->data;
198 TpContact *tp_contact;
199 EmpathyContact *contact;
200 EmpathyCallHandler *handler;
202 if (tp_proxy_get_invalidated (channel) != NULL)
205 if (tp_channel_get_channel_type_id (channel) !=
206 TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
209 if (!TP_IS_CALL_CHANNEL (channel))
212 call = TP_CALL_CHANNEL (channel);
214 tp_contact = tp_channel_get_target_contact (channel);
215 contact = empathy_contact_dup_from_tp_contact (tp_contact);
216 handler = empathy_call_handler_new_for_channel (call, contact);
218 g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
221 g_object_unref (handler);
222 g_object_unref (contact);
225 tp_handle_channels_context_accept (context);
228 static TpCallChannel *
229 find_call_channel (GList *channels)
233 for (l = channels; l != NULL; l = g_list_next (l))
235 TpChannel *channel = l->data;
238 if (tp_proxy_get_invalidated (channel) != NULL)
241 channel_type = tp_channel_get_channel_type_id (channel);
243 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
244 return TP_CALL_CHANNEL (channel);
251 approve_channels (TpBaseClient *client,
253 TpConnection *connection,
255 TpChannelDispatchOperation *dispatch_operation,
256 TpAddDispatchOperationContext *context)
258 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
259 TpCallChannel *channel;
261 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT, "" };
262 gboolean handled = FALSE;
264 channel = find_call_channel (channels);
268 DEBUG ("Failed to find the main channel; ignoring");
269 error.message = "Unknown channel";
273 handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
277 DEBUG ("Unknown handle, ignoring");
278 error.code = TP_ERROR_INVALID_HANDLE;
279 error.message = "Unknown handle";
283 g_signal_emit (self, signals[INCOMING_CALL], 0,
284 handle, channel, dispatch_operation, context,
290 /* There was no call window so the context wasn't handled. */
291 DEBUG ("Call with a contact for which there's no existing "
292 "call window, ignoring");
293 error.message = "No call window with this contact";
296 tp_add_dispatch_operation_context_fail (context, &error);
300 empathy_call_factory_register (EmpathyCallFactory *self,
303 return tp_base_client_register (TP_BASE_CLIENT (self), error);