]> git.0d.be Git - empathy.git/blob - src/empathy-call-factory.c
Add an EmpathyCallFactory class
[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-channel-factory.h>
33 #include <libempathy/empathy-utils.h>
34
35 #include "empathy-call-factory.h"
36 #include "empathy-call-handler.h"
37 #include "src-marshal.h"
38
39 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
40 #include <libempathy/empathy-debug.h>
41
42 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, G_TYPE_OBJECT)
43
44 static void handle_channels_cb (TpSimpleHandler *handler,
45     TpAccount *account,
46     TpConnection *connection,
47     GList *channels,
48     GList *requests_satisfied,
49     gint64 user_action_time,
50     TpHandleChannelsContext *context,
51     gpointer user_data);
52
53 /* signal enum */
54 enum
55 {
56     NEW_CALL_HANDLER,
57     LAST_SIGNAL
58 };
59
60 static guint signals[LAST_SIGNAL] = {0};
61
62 /* private structure */
63 typedef struct {
64   TpBaseClient *handler;
65   gboolean dispose_has_run;
66 } EmpathyCallFactoryPriv;
67
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallFactory)
69
70 static GObject *call_factory = NULL;
71
72 static void
73 empathy_call_factory_init (EmpathyCallFactory *obj)
74 {
75   EmpathyCallFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
76     EMPATHY_TYPE_CALL_FACTORY, EmpathyCallFactoryPriv);
77   TpDBusDaemon *dbus;
78   EmpathyChannelFactory *factory;
79   GError *error = NULL;
80
81   obj->priv = priv;
82
83   dbus = tp_dbus_daemon_dup (&error);
84   if (dbus == NULL)
85     {
86       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
87       g_error_free (error);
88       return;
89     }
90
91   priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
92       "Empathy.Call", FALSE, handle_channels_cb, obj, NULL);
93
94   factory = empathy_channel_factory_new ();
95   tp_base_client_set_channel_factory (priv->handler,
96       TP_CLIENT_CHANNEL_FACTORY (factory));
97   g_object_unref (factory);
98
99   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
100         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
101           TPY_IFACE_CHANNEL_TYPE_CALL,
102         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
103         NULL));
104
105   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
106         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
107           TPY_IFACE_CHANNEL_TYPE_CALL,
108         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
109         TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
110         NULL));
111
112   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
113         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
114           TPY_IFACE_CHANNEL_TYPE_CALL,
115         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
116         TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
117         NULL));
118
119   tp_base_client_add_handler_capabilities_varargs (priv->handler,
120     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
121     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
122     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
123     NULL);
124
125   g_object_unref (dbus);
126 }
127
128 static GObject *
129 empathy_call_factory_constructor (GType type, guint n_construct_params,
130   GObjectConstructParam *construct_params)
131 {
132   g_return_val_if_fail (call_factory == NULL, NULL);
133
134   call_factory = G_OBJECT_CLASS (empathy_call_factory_parent_class)->constructor
135           (type, n_construct_params, construct_params);
136   g_object_add_weak_pointer (call_factory, (gpointer)&call_factory);
137
138   return call_factory;
139 }
140
141 static void
142 empathy_call_factory_finalize (GObject *object)
143 {
144   /* free any data held directly by the object here */
145
146   if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize)
147     G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize (object);
148 }
149
150 static void
151 empathy_call_factory_dispose (GObject *object)
152 {
153   EmpathyCallFactoryPriv *priv = GET_PRIV (object);
154
155   if (priv->dispose_has_run)
156     return;
157
158   priv->dispose_has_run = TRUE;
159
160   tp_clear_object (&priv->handler);
161
162   if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose)
163     G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose (object);
164 }
165
166 static void
167 empathy_call_factory_class_init (
168   EmpathyCallFactoryClass *empathy_call_factory_class)
169 {
170   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_factory_class);
171
172   g_type_class_add_private (empathy_call_factory_class,
173     sizeof (EmpathyCallFactoryPriv));
174
175   object_class->constructor = empathy_call_factory_constructor;
176   object_class->dispose = empathy_call_factory_dispose;
177   object_class->finalize = empathy_call_factory_finalize;
178
179   signals[NEW_CALL_HANDLER] =
180     g_signal_new ("new-call-handler",
181       G_TYPE_FROM_CLASS (empathy_call_factory_class),
182       G_SIGNAL_RUN_LAST, 0,
183       NULL, NULL,
184       _src_marshal_VOID__OBJECT_BOOLEAN,
185       G_TYPE_NONE,
186       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
187 }
188
189 EmpathyCallFactory *
190 empathy_call_factory_initialise (void)
191 {
192   g_return_val_if_fail (call_factory == NULL, NULL);
193
194   return EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY, NULL));
195 }
196
197 EmpathyCallFactory *
198 empathy_call_factory_get (void)
199 {
200   g_return_val_if_fail (call_factory != NULL, NULL);
201
202   return EMPATHY_CALL_FACTORY (call_factory);
203 }
204
205 GHashTable *
206 empathy_call_factory_create_call_request (EmpathyContact *contact,
207     gboolean initial_audio,
208     gboolean initial_video)
209 {
210   return tp_asv_new (
211       TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
212         TPY_IFACE_CHANNEL_TYPE_CALL,
213       TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
214       TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
215         empathy_contact_get_handle (contact),
216       TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_AUDIO, G_TYPE_BOOLEAN,
217         initial_audio,
218       TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
219         initial_video,
220       NULL);
221 }
222
223 static void
224 create_call_channel_cb (GObject *source,
225     GAsyncResult *result,
226     gpointer user_data)
227 {
228   GError *error = NULL;
229
230   if (!tp_account_channel_request_create_channel_finish (
231         TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
232     {
233       DEBUG ("Failed to create call channel: %s", error->message);
234       g_error_free (error);
235     }
236 }
237
238 /**
239  * empathy_call_factory_new_call_with_streams:
240  * @factory: an #EmpathyCallFactory
241  * @contact: an #EmpathyContact
242  * @initial_audio: if %TRUE the call will be started with audio
243  * @initial_video: if %TRUE the call will be started with video
244  *
245  * Initiate a new Call with @contact.
246  */
247 void
248 empathy_call_factory_new_call_with_streams (EmpathyContact *contact,
249     gboolean initial_audio,
250     gboolean initial_video,
251     gint64 timestamp,
252     gpointer user_data)
253 {
254   GHashTable *call_request;
255   TpAccount *account;
256   TpAccountChannelRequest *call_req;
257
258   call_request = empathy_call_factory_create_call_request (contact,
259       initial_audio, initial_video);
260
261   account = empathy_contact_get_account (contact);
262
263   call_req = tp_account_channel_request_new (account, call_request, timestamp);
264
265   tp_account_channel_request_create_channel_async (call_req, NULL, NULL,
266       create_call_channel_cb, NULL);
267
268   g_hash_table_unref (call_request);
269   g_object_unref (call_req);
270 }
271
272 static void
273 create_call_handler (EmpathyCallFactory *factory,
274   TpyCallChannel *call)
275 {
276   EmpathyCallHandler *handler;
277
278   g_return_if_fail (factory != NULL);
279
280   handler = empathy_call_handler_new_for_channel (call);
281
282   g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
283     handler, FALSE);
284
285   g_object_unref (handler);
286 }
287
288 static void
289 handle_channels_cb (TpSimpleHandler *handler,
290     TpAccount *account,
291     TpConnection *connection,
292     GList *channels,
293     GList *requests_satisfied,
294     gint64 user_action_time,
295     TpHandleChannelsContext *context,
296     gpointer user_data)
297 {
298   EmpathyCallFactory *self = user_data;
299   GList *l;
300
301   for (l = channels; l != NULL; l = g_list_next (l))
302     {
303       TpChannel *channel = l->data;
304       TpyCallChannel *call;
305
306       if (tp_proxy_get_invalidated (channel) != NULL)
307         continue;
308
309       if (tp_channel_get_channel_type_id (channel) !=
310           TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
311         continue;
312
313       if (!TPY_IS_CALL_CHANNEL (channel))
314         continue;
315
316       call = TPY_CALL_CHANNEL (channel);
317       create_call_handler (self, call);
318       g_object_unref (call);
319     }
320
321   tp_handle_channels_context_accept (context);
322 }
323
324 gboolean
325 empathy_call_factory_register (EmpathyCallFactory *self,
326     GError **error)
327 {
328   EmpathyCallFactoryPriv *priv = GET_PRIV (self);
329
330   return tp_base_client_register (priv->handler, error);
331 }