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