]> git.0d.be Git - empathy.git/blob - src/empathy-call-factory.c
Updated Irish translation
[empathy.git] / src / empathy-call-factory.c
1 /*
2  * empathy-call-factory.c - Source for EmpathyCallFactory
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
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>
29
30 #include <telepathy-yell/telepathy-yell.h>
31
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>
36
37 #include "empathy-call-factory.h"
38 #include "empathy-call-handler.h"
39 #include "src-marshal.h"
40
41 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
42 #include <libempathy/empathy-debug.h>
43
44 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
45
46 static void handle_channels (TpBaseClient *client,
47     TpAccount *account,
48     TpConnection *connection,
49     GList *channels,
50     GList *requests_satisfied,
51     gint64 user_action_time,
52     TpHandleChannelsContext *context);
53
54 static void approve_channels (TpBaseClient *client,
55     TpAccount *account,
56     TpConnection *connection,
57     GList *channels,
58     TpChannelDispatchOperation *dispatch_operation,
59     TpAddDispatchOperationContext *context);
60
61 /* signal enum */
62 enum
63 {
64     NEW_CALL_HANDLER,
65     INCOMING_CALL,
66     LAST_SIGNAL
67 };
68
69 static guint signals[LAST_SIGNAL] = {0};
70
71 static GObject *call_factory = NULL;
72
73 static void
74 empathy_call_factory_init (EmpathyCallFactory *obj)
75 {
76   TpBaseClient *client = (TpBaseClient *) obj;
77
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,
83         NULL));
84
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,
90         NULL));
91
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,
98         NULL));
99
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,
106         NULL));
107
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",
112     NULL);
113 }
114
115 static GObject *
116 empathy_call_factory_constructor (GType type, guint n_construct_params,
117   GObjectConstructParam *construct_params)
118 {
119   g_return_val_if_fail (call_factory == NULL, NULL);
120
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);
124
125   return call_factory;
126 }
127
128 static void
129 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
130 {
131   GObjectClass *object_class = G_OBJECT_CLASS (klass);
132   TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
133
134   object_class->constructor = empathy_call_factory_constructor;
135
136   base_clt_cls->handle_channels = handle_channels;
137   base_clt_cls->add_dispatch_operation = approve_channels;
138
139   signals[NEW_CALL_HANDLER] =
140     g_signal_new ("new-call-handler",
141       G_TYPE_FROM_CLASS (klass),
142       G_SIGNAL_RUN_LAST, 0,
143       NULL, NULL,
144       _src_marshal_VOID__OBJECT_BOOLEAN,
145       G_TYPE_NONE,
146       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
147
148   signals[INCOMING_CALL] =
149     g_signal_new ("incoming-call",
150       G_TYPE_FROM_CLASS (klass),
151       G_SIGNAL_RUN_LAST, 0,
152       NULL, NULL,
153       _src_marshal_BOOLEAN__UINT_OBJECT_OBJECT_OBJECT,
154       G_TYPE_BOOLEAN,
155       4, G_TYPE_UINT, TPY_TYPE_CALL_CHANNEL,
156       TP_TYPE_CHANNEL_DISPATCH_OPERATION,
157       TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
158 }
159
160 EmpathyCallFactory *
161 empathy_call_factory_initialise (void)
162 {
163   EmpathyCallFactory *self;
164   EmpathyClientFactory *factory;
165   TpAccountManager *am;
166
167   g_return_val_if_fail (call_factory == NULL, NULL);
168
169   am = tp_account_manager_dup ();
170   factory = empathy_client_factory_dup ();
171
172   self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
173       "account-manager", am,
174       "factory", factory,
175       "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
176       NULL));
177
178   g_object_unref (am);
179   g_object_unref (factory);
180
181   return self;
182 }
183
184 EmpathyCallFactory *
185 empathy_call_factory_get (void)
186 {
187   g_return_val_if_fail (call_factory != NULL, NULL);
188
189   return EMPATHY_CALL_FACTORY (call_factory);
190 }
191
192 static void
193 call_channel_got_contact (TpConnection *connection,
194   EmpathyContact *contact,
195   const GError *error,
196   gpointer user_data,
197   GObject *weak_object)
198 {
199   EmpathyCallFactory *factory = EMPATHY_CALL_FACTORY (weak_object);
200   EmpathyCallHandler *handler;
201   TpyCallChannel *call = TPY_CALL_CHANNEL (user_data);
202
203   if (contact == NULL)
204     {
205       /* FIXME use hangup with an appropriate error */
206       tp_channel_close_async (TP_CHANNEL (call), NULL, NULL);
207       return;
208     }
209
210   handler = empathy_call_handler_new_for_channel (call, contact);
211
212   g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
213     handler, FALSE);
214
215   g_object_unref (handler);
216 }
217
218 static void
219 call_channel_ready (EmpathyCallFactory *factory,
220   TpyCallChannel *call)
221 {
222   TpChannel *channel = TP_CHANNEL (call);
223   const gchar *id;
224
225   id = tp_channel_get_identifier (channel);
226
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),
230     id,
231     call_channel_got_contact,
232     channel,
233     g_object_unref,
234     (GObject *) factory);
235 }
236
237 static void
238 call_channel_ready_cb (TpyCallChannel *call,
239   GParamSpec *spec,
240   EmpathyCallFactory *factory)
241 {
242   gboolean ready;
243
244   g_object_get (call, "ready", &ready, NULL);
245   if (!ready)
246     return;
247
248   call_channel_ready (factory, call);
249 }
250
251
252 static void
253 handle_channels (TpBaseClient *client,
254     TpAccount *account,
255     TpConnection *connection,
256     GList *channels,
257     GList *requests_satisfied,
258     gint64 user_action_time,
259     TpHandleChannelsContext *context)
260 {
261   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
262   GList *l;
263
264   for (l = channels; l != NULL; l = g_list_next (l))
265     {
266       TpChannel *channel = l->data;
267       TpyCallChannel *call;
268       gboolean ready;
269
270       if (tp_proxy_get_invalidated (channel) != NULL)
271         continue;
272
273       if (tp_channel_get_channel_type_id (channel) !=
274           TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
275         continue;
276
277       if (!TPY_IS_CALL_CHANNEL (channel))
278         continue;
279
280       call = TPY_CALL_CHANNEL (channel);
281
282       /* Take a ref to keep while hopping through the async callbacks */
283       g_object_ref (call);
284       g_object_get (call, "ready", &ready, NULL);
285
286       if (!ready)
287         tp_g_signal_connect_object (call, "notify::ready",
288           G_CALLBACK (call_channel_ready_cb), self, 0);
289       else
290         call_channel_ready (self, call);
291     }
292
293   tp_handle_channels_context_accept (context);
294 }
295
296 static TpyCallChannel *
297 find_call_channel (GList *channels)
298 {
299   GList *l;
300
301   for (l = channels; l != NULL; l = g_list_next (l))
302     {
303       TpChannel *channel = l->data;
304       GQuark channel_type;
305
306       if (tp_proxy_get_invalidated (channel) != NULL)
307         continue;
308
309       channel_type = tp_channel_get_channel_type_id (channel);
310
311       if (channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
312         return TPY_CALL_CHANNEL (channel);
313     }
314
315   return NULL;
316 }
317
318 static void
319 approve_channels (TpBaseClient *client,
320     TpAccount *account,
321     TpConnection *connection,
322     GList *channels,
323     TpChannelDispatchOperation *dispatch_operation,
324     TpAddDispatchOperationContext *context)
325 {
326   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
327   TpyCallChannel *channel;
328   guint handle;
329   GError error = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT, "" };
330   gboolean handled = FALSE;
331
332   channel = find_call_channel (channels);
333
334   if (channel == NULL)
335     {
336       DEBUG ("Failed to find the main channel; ignoring");
337       error.message = "Unknown channel";
338       goto out;
339     }
340
341   handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
342
343   if (handle == 0)
344     {
345       DEBUG ("Unknown handle, ignoring");
346       error.code = TP_ERROR_INVALID_HANDLE;
347       error.message = "Unknown handle";
348       goto out;
349     }
350
351   g_signal_emit (self, signals[INCOMING_CALL], 0,
352       handle, channel, dispatch_operation, context,
353       &handled);
354
355   if (handled)
356     return;
357
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";
362
363  out:
364   tp_add_dispatch_operation_context_fail (context, &error);
365 }
366
367 gboolean
368 empathy_call_factory_register (EmpathyCallFactory *self,
369     GError **error)
370 {
371   return tp_base_client_register (TP_BASE_CLIENT (self), error);
372 }