]> git.0d.be Git - empathy.git/blob - src/empathy-call-factory.c
Merge remote-tracking branch 'origin/gnome-3-8'
[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 #include "config.h"
22 #include "empathy-client-factory.h"
23
24 #include "empathy-call-factory.h"
25 #include "empathy-call-handler.h"
26 #include "empathy-request-util.h"
27
28 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
29 #include "empathy-debug.h"
30
31 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, TP_TYPE_BASE_CLIENT)
32
33 static void handle_channels (TpBaseClient *client,
34     TpAccount *account,
35     TpConnection *connection,
36     GList *channels,
37     GList *requests_satisfied,
38     gint64 user_action_time,
39     TpHandleChannelsContext *context);
40
41 static void approve_channels (TpBaseClient *client,
42     TpAccount *account,
43     TpConnection *connection,
44     GList *channels,
45     TpChannelDispatchOperation *dispatch_operation,
46     TpAddDispatchOperationContext *context);
47
48 /* signal enum */
49 enum
50 {
51     NEW_CALL_HANDLER,
52     INCOMING_CALL,
53     LAST_SIGNAL
54 };
55
56 static guint signals[LAST_SIGNAL] = {0};
57
58 static GObject *call_factory = NULL;
59
60 static void
61 empathy_call_factory_init (EmpathyCallFactory *obj)
62 {
63   TpBaseClient *client = (TpBaseClient *) obj;
64
65   tp_base_client_take_approver_filter (client, tp_asv_new (
66         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
67           TP_IFACE_CHANNEL_TYPE_CALL,
68         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
69           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
70         NULL));
71
72   tp_base_client_take_handler_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));
78
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         TP_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
85         NULL));
86
87   tp_base_client_take_handler_filter (client, tp_asv_new (
88         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
89           TP_IFACE_CHANNEL_TYPE_CALL,
90         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE,
91           G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
92         TP_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
93         NULL));
94
95   tp_base_client_add_handler_capabilities_varargs (client,
96       "org.freedesktop.Telepathy.Channel.Type.Call1/audio",
97       "org.freedesktop.Telepathy.Channel.Type.Call1/video",
98       "org.freedesktop.Telepathy.Channel.Type.Call1/ice",
99       "org.freedesktop.Telepathy.Channel.Type.Call1/gtalk-p2p",
100       "org.freedesktop.Telepathy.Channel.Type.Call1/video/h264",
101       NULL);
102 }
103
104 static GObject *
105 empathy_call_factory_constructor (GType type, guint n_construct_params,
106   GObjectConstructParam *construct_params)
107 {
108   g_return_val_if_fail (call_factory == NULL, NULL);
109
110   call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
111           (type, n_construct_params, construct_params);
112   g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
113
114   return call_factory;
115 }
116
117 static void
118 empathy_call_factory_class_init (EmpathyCallFactoryClass *klass)
119 {
120   GObjectClass *object_class = G_OBJECT_CLASS (klass);
121   TpBaseClientClass *base_clt_cls = TP_BASE_CLIENT_CLASS (klass);
122
123   object_class->constructor = empathy_call_factory_constructor;
124
125   base_clt_cls->handle_channels = handle_channels;
126   base_clt_cls->add_dispatch_operation = approve_channels;
127
128   signals[NEW_CALL_HANDLER] =
129     g_signal_new ("new-call-handler",
130       G_TYPE_FROM_CLASS (klass),
131       G_SIGNAL_RUN_LAST, 0,
132       NULL, NULL,
133       g_cclosure_marshal_generic,
134       G_TYPE_NONE,
135       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
136
137   signals[INCOMING_CALL] =
138     g_signal_new ("incoming-call",
139       G_TYPE_FROM_CLASS (klass),
140       G_SIGNAL_RUN_LAST, 0,
141       NULL, NULL,
142       g_cclosure_marshal_generic,
143       G_TYPE_BOOLEAN,
144       4, G_TYPE_UINT, TP_TYPE_CALL_CHANNEL,
145       TP_TYPE_CHANNEL_DISPATCH_OPERATION,
146       TP_TYPE_ADD_DISPATCH_OPERATION_CONTEXT);
147 }
148
149 EmpathyCallFactory *
150 empathy_call_factory_initialise (void)
151 {
152   EmpathyCallFactory *self;
153   EmpathyClientFactory *factory;
154   TpAccountManager *am;
155
156   g_return_val_if_fail (call_factory == NULL, NULL);
157
158   am = tp_account_manager_dup ();
159   factory = empathy_client_factory_dup ();
160
161   self = EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY,
162       "account-manager", am,
163       "factory", factory,
164       "name", EMPATHY_CALL_BUS_NAME_SUFFIX,
165       NULL));
166
167   g_object_unref (am);
168   g_object_unref (factory);
169
170   return self;
171 }
172
173 EmpathyCallFactory *
174 empathy_call_factory_get (void)
175 {
176   g_return_val_if_fail (call_factory != NULL, NULL);
177
178   return EMPATHY_CALL_FACTORY (call_factory);
179 }
180
181 static void
182 handle_channels (TpBaseClient *client,
183     TpAccount *account,
184     TpConnection *connection,
185     GList *channels,
186     GList *requests_satisfied,
187     gint64 user_action_time,
188     TpHandleChannelsContext *context)
189 {
190   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
191   GList *l;
192
193   for (l = channels; l != NULL; l = g_list_next (l))
194     {
195       TpChannel *channel = l->data;
196       TpCallChannel *call;
197       TpContact *tp_contact;
198       EmpathyContact *contact;
199       EmpathyCallHandler *handler;
200
201       if (tp_proxy_get_invalidated (channel) != NULL)
202         continue;
203
204       if (tp_channel_get_channel_type_id (channel) !=
205           TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
206         continue;
207
208       if (!TP_IS_CALL_CHANNEL (channel))
209         continue;
210
211       call = TP_CALL_CHANNEL (channel);
212
213       tp_contact = tp_channel_get_target_contact (channel);
214       contact = empathy_contact_dup_from_tp_contact (tp_contact);
215       handler = empathy_call_handler_new_for_channel (call, contact);
216
217       g_signal_emit (self, signals[NEW_CALL_HANDLER], 0,
218           handler, FALSE);
219
220       g_object_unref (handler);
221       g_object_unref (contact);
222     }
223
224   tp_handle_channels_context_accept (context);
225 }
226
227 static TpCallChannel *
228 find_call_channel (GList *channels)
229 {
230   GList *l;
231
232   for (l = channels; l != NULL; l = g_list_next (l))
233     {
234       TpChannel *channel = l->data;
235       GQuark channel_type;
236
237       if (tp_proxy_get_invalidated (channel) != NULL)
238         continue;
239
240       channel_type = tp_channel_get_channel_type_id (channel);
241
242       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
243         return TP_CALL_CHANNEL (channel);
244     }
245
246   return NULL;
247 }
248
249 static void
250 approve_channels (TpBaseClient *client,
251     TpAccount *account,
252     TpConnection *connection,
253     GList *channels,
254     TpChannelDispatchOperation *dispatch_operation,
255     TpAddDispatchOperationContext *context)
256 {
257   EmpathyCallFactory *self = EMPATHY_CALL_FACTORY (client);
258   TpCallChannel *channel;
259   guint handle;
260   GError error = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT, "" };
261   gboolean handled = FALSE;
262
263   channel = find_call_channel (channels);
264
265   if (channel == NULL)
266     {
267       DEBUG ("Failed to find the main channel; ignoring");
268       error.message = "Unknown channel";
269       goto out;
270     }
271
272   handle = tp_channel_get_handle (TP_CHANNEL (channel), NULL);
273
274   if (handle == 0)
275     {
276       DEBUG ("Unknown handle, ignoring");
277       error.code = TP_ERROR_INVALID_HANDLE;
278       error.message = "Unknown handle";
279       goto out;
280     }
281
282   g_signal_emit (self, signals[INCOMING_CALL], 0,
283       handle, channel, dispatch_operation, context,
284       &handled);
285
286   if (handled)
287     return;
288
289   /* There was no call window so the context wasn't handled. */
290   DEBUG ("Call with a contact for which there's no existing "
291     "call window, ignoring");
292   error.message = "No call window with this contact";
293
294  out:
295   tp_add_dispatch_operation_context_fail (context, &error);
296 }
297
298 gboolean
299 empathy_call_factory_register (EmpathyCallFactory *self,
300     GError **error)
301 {
302   return tp_base_client_register (TP_BASE_CLIENT (self), error);
303 }