remove released flag
[empathy-mirror.git] / src / empathy-call-factory.c
blob45469e7b6f82ad9c95dda0e8c260612889b16ad8
1 /*
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
21 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
26 #include <telepathy-glib/telepathy-glib.h>
28 #include <libempathy/empathy-client-factory.h>
29 #include <libempathy/empathy-request-util.h>
30 #include <libempathy/empathy-utils.h>
32 #include "empathy-call-factory.h"
33 #include "empathy-call-handler.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include <libempathy/empathy-debug.h>
38 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
40 static void handle_channels (TpBaseClient *client,
41 TpAccount *account,
42 TpConnection *connection,
43 GList *channels,
44 GList *requests_satisfied,
45 gint64 user_action_time,
46 TpHandleChannelsContext *context);
48 static void approve_channels (TpBaseClient *client,
49 TpAccount *account,
50 TpConnection *connection,
51 GList *channels,
52 TpChannelDispatchOperation *dispatch_operation,
53 TpAddDispatchOperationContext *context);
55 /* signal enum */
56 enum
58 NEW_CALL_HANDLER,
59 INCOMING_CALL,
60 LAST_SIGNAL
63 static guint signals[LAST_SIGNAL] = {0};
65 static GObject *call_factory = NULL;
67 static void
68 empathy_call_factory_init (EmpathyCallFactory *obj)
70 TpBaseClient *client = (TpBaseClient *) obj;
72 tp_base_client_take_approver_filter (client, tp_asv_new (
73 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
74 TP_IFACE_CHANNEL_TYPE_CALL,
75 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
76 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
77 NULL));
79 tp_base_client_take_handler_filter (client, tp_asv_new (
80 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
81 TP_IFACE_CHANNEL_TYPE_CALL,
82 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
83 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
84 NULL));
86 tp_base_client_take_handler_filter (client, tp_asv_new (
87 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
88 TP_IFACE_CHANNEL_TYPE_CALL,
89 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
90 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
91 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
92 NULL));
94 tp_base_client_take_handler_filter (client, tp_asv_new (
95 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
96 TP_IFACE_CHANNEL_TYPE_CALL,
97 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
98 G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
99 TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
100 NULL));
102 tp_base_client_add_handler_capabilities_varargs (client,
103 "org.freedesktop.Telepathy.Channel.Type.Call1/audio",
104 "org.freedesktop.Telepathy.Channel.Type.Call1/video",
105 "org.freedesktop.Telepathy.Channel.Type.Call1/ice",
106 "org.freedesktop.Telepathy.Channel.Type.Call1/gtalk-p2p",
107 "org.freedesktop.Telepathy.Channel.Type.Call1/video/h264",
108 NULL);
111 static GObject *
112 empathy_call_factory_constructor (GType type, guint n_construct_params,
113 GObjectConstructParam *construct_params)
115 g_return_val_if_fail (call_factory == NULL, NULL);
117 call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
118 (type, n_construct_params, construct_params);
119 g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
121 return call_factory;
124 static void
125 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
127 GObjectClass *object_class = G_OBJECT_CLASS (klass);
128 TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
130 object_class->constructor = empathy_call_factory_constructor;
132 base_clt_cls->handle_channels = handle_channels;
133 base_clt_cls->add_dispatch_operation = approve_channels;
135 signals[NEW_CALL_HANDLER] =
136 g_signal_new ("new-call-handler",
137 G_TYPE_FROM_CLASS (klass),
138 G_SIGNAL_RUN_LAST, 0,
139 NULL, NULL,
140 g_cclosure_marshal_generic,
141 G_TYPE_NONE,
142 2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
144 signals[INCOMING_CALL] =
145 g_signal_new ("incoming-call",
146 G_TYPE_FROM_CLASS (klass),
147 G_SIGNAL_RUN_LAST, 0,
148 NULL, NULL,
149 g_cclosure_marshal_generic,
150 G_TYPE_BOOLEAN,
151 4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
152 TP_TYPE_CHANNEL_DISPATCH_OPERATION,
153 TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
156 EmpathyCallFactory *
157 empathy_call_factory_initialise (void)
159 EmpathyCallFactory *self;
160 EmpathyClientFactory *factory;
161 TpAccountManager *am;
163 g_return_val_if_fail (call_factory == NULL, NULL);
165 am = tp_account_manager_dup ();
166 factory = empathy_client_factory_dup ();
168 self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
169 "account-manager", am,
170 "factory", factory,
171 "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
172 NULL));
174 g_object_unref (am);
175 g_object_unref (factory);
177 return self;
180 EmpathyCallFactory *
181 empathy_call_factory_get (void)
183 g_return_val_if_fail (call_factory != NULL, NULL);
185 return EMPATHY_CALL_FACTORY (call_factory);
188 static void
189 handle_channels (TpBaseClient *client,
190 TpAccount *account,
191 TpConnection *connection,
192 GList *channels,
193 GList *requests_satisfied,
194 gint64 user_action_time,
195 TpHandleChannelsContext *context)
197 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
198 GList *l;
200 for (l = channels; l != NULL; l = g_list_next (l))
202 TpChannel *channel = l->data;
203 TpCallChannel *call;
204 TpContact *tp_contact;
205 EmpathyContact *contact;
206 EmpathyCallHandler *handler;
208 if (tp_proxy_get_invalidated (channel) != NULL)
209 continue;
211 if (tp_channel_get_channel_type_id (channel) !=
212 TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
213 continue;
215 if (!TP_IS_CALL_CHANNEL (channel))
216 continue;
218 call = TP_CALL_CHANNEL (channel);
220 tp_contact = tp_channel_get_target_contact (channel);
221 contact = empathy_contact_dup_from_tp_contact (tp_contact);
222 handler = empathy_call_handler_new_for_channel (call, contact);
224 g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
225 handler, FALSE);
227 g_object_unref (handler);
228 g_object_unref (contact);
231 tp_handle_channels_context_accept (context);
234 static TpCallChannel *
235 find_call_channel (GList *channels)
237 GList *l;
239 for (l = channels; l != NULL; l = g_list_next (l))
241 TpChannel *channel = l->data;
242 GQuark channel_type;
244 if (tp_proxy_get_invalidated (channel) != NULL)
245 continue;
247 channel_type = tp_channel_get_channel_type_id (channel);
249 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
250 return TP_CALL_CHANNEL (channel);
253 return NULL;
256 static void
257 approve_channels (TpBaseClient *client,
258 TpAccount *account,
259 TpConnection *connection,
260 GList *channels,
261 TpChannelDispatchOperation *dispatch_operation,
262 TpAddDispatchOperationContext *context)
264 EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
265 TpCallChannel *channel;
266 guint handle;
267 GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT, "" };
268 gboolean handled = FALSE;
270 channel = find_call_channel (channels);
272 if (channel == NULL)
274 DEBUG ("Failed to find the main channel; ignoring");
275 error.message = "Unknown channel";
276 goto out;
279 handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
281 if (handle == 0)
283 DEBUG ("Unknown handle, ignoring");
284 error.code = TP_ERROR_INVALID_HANDLE;
285 error.message = "Unknown handle";
286 goto out;
289 g_signal_emit (self, signals[INCOMING_CALL], 0,
290 handle, channel, dispatch_operation, context,
291 &handled);
293 if (handled)
294 return;
296 /* There was no call window so the context wasn't handled. */
297 DEBUG ("Call with a contact for which there's no existing "
298 "call window, ignoring");
299 error.message = "No call window with this contact";
301 out:
302 tp_add_dispatch_operation_context_fail (context, &error);
305 gboolean
306 empathy_call_factory_register (EmpathyCallFactory *self,
307 GError **error)
309 return tp_base_client_register (TP_BASE_CLIENT (self), error);