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