]> git.0d.be Git - empathy.git/blob - libempathy/empathy-call-factory.c
Updated Galician translations for docs
[empathy.git] / libempathy / 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/simple-handler.h>
26 #include <telepathy-glib/interfaces.h>
27 #include <telepathy-glib/util.h>
28
29 #include "empathy-dispatcher.h"
30 #include "empathy-marshal.h"
31 #include "empathy-call-factory.h"
32 #include "empathy-utils.h"
33
34 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, G_TYPE_OBJECT)
35
36 static void handle_channels_cb (TpSimpleHandler *handler,
37     TpAccount *account,
38     TpConnection *connection,
39     GList *channels,
40     GList *requests_satisfied,
41     gint64 user_action_time,
42     TpHandleChannelsContext *context,
43     gpointer user_data);
44
45 /* signal enum */
46 enum
47 {
48     NEW_CALL_HANDLER,
49     LAST_SIGNAL
50 };
51
52 static guint signals[LAST_SIGNAL] = {0};
53
54 /* private structure */
55 typedef struct {
56   TpBaseClient *handler;
57   gboolean dispose_has_run;
58 } EmpathyCallFactoryPriv;
59
60 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallFactory)
61
62 static GObject *call_factory = NULL;
63
64 static void
65 empathy_call_factory_init (EmpathyCallFactory *obj)
66 {
67   EmpathyCallFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
68     EMPATHY_TYPE_CALL_FACTORY, EmpathyCallFactoryPriv);
69   TpDBusDaemon *dbus;
70   GError *error = NULL;
71
72   obj->priv = priv;
73
74   dbus = tp_dbus_daemon_dup (&error);
75   if (dbus == NULL)
76     {
77       g_warning ("Failed to get TpDBusDaemon: %s", error->message);
78       g_error_free (error);
79       return;
80     }
81
82   priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
83       "Empathy.AudioVideo", FALSE, handle_channels_cb, obj, NULL);
84
85   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
86         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
87           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
88         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
89         NULL));
90
91   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
92         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
93           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
94         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
95         TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN, TRUE,
96         NULL));
97
98   tp_base_client_take_handler_filter (priv->handler, tp_asv_new (
99         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
100           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
101         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
102         TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN, TRUE,
103         NULL));
104
105   tp_base_client_add_handler_capabilities_varargs (priv->handler,
106     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/ice-udp",
107     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/gtalk-p2p",
108     "org.freedesktop.Telepathy.Channel.Interface.MediaSignalling/video/h264",
109     NULL);
110
111   g_object_unref (dbus);
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_finalize (GObject *object)
129 {
130   /* free any data held directly by the object here */
131
132   if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize)
133     G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize (object);
134 }
135
136 static void
137 empathy_call_factory_dispose (GObject *object)
138 {
139   EmpathyCallFactoryPriv *priv = GET_PRIV (object);
140
141   if (priv->dispose_has_run)
142     return;
143
144   priv->dispose_has_run = TRUE;
145
146   /* release any references held by the object here */
147
148   if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose)
149     G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose (object);
150 }
151
152 static void
153 empathy_call_factory_class_init (
154   EmpathyCallFactoryClass *empathy_call_factory_class)
155 {
156   GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_factory_class);
157
158   g_type_class_add_private (empathy_call_factory_class,
159     sizeof (EmpathyCallFactoryPriv));
160
161   object_class->constructor = empathy_call_factory_constructor;
162   object_class->dispose = empathy_call_factory_dispose;
163   object_class->finalize = empathy_call_factory_finalize;
164
165   signals[NEW_CALL_HANDLER] =
166     g_signal_new ("new-call-handler",
167       G_TYPE_FROM_CLASS (empathy_call_factory_class),
168       G_SIGNAL_RUN_LAST, 0,
169       NULL, NULL,
170       _empathy_marshal_VOID__OBJECT_BOOLEAN,
171       G_TYPE_NONE,
172       2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
173 }
174
175 EmpathyCallFactory *
176 empathy_call_factory_initialise (void)
177 {
178   g_return_val_if_fail (call_factory == NULL, NULL);
179
180   return EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY, NULL));
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 /**
192  * empathy_call_factory_new_call_with_streams:
193  * @factory: an #EmpathyCallFactory
194  * @contact: an #EmpathyContact
195  * @initial_audio: if %TRUE the call will be started with audio
196  * @initial_video: if %TRUE the call will be started with video
197  *
198  * Initiate a new call with @contact.
199  */
200 void
201 empathy_call_factory_new_call_with_streams (EmpathyContact *contact,
202     gboolean initial_audio,
203     gboolean initial_video,
204     gint64 timestamp,
205     EmpathyDispatcherRequestCb callback,
206     gpointer user_data)
207 {
208   EmpathyDispatcher *dispatcher;
209   GHashTable *request;
210
211   request = tp_asv_new (
212       TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
213         TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
214       TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT, TP_HANDLE_TYPE_CONTACT,
215       TP_PROP_CHANNEL_TARGET_HANDLE, G_TYPE_UINT,
216         empathy_contact_get_handle (contact),
217       TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_AUDIO, G_TYPE_BOOLEAN,
218         initial_audio,
219       TP_PROP_CHANNEL_TYPE_STREAMED_MEDIA_INITIAL_VIDEO, G_TYPE_BOOLEAN,
220         initial_video,
221       NULL);
222
223   dispatcher = empathy_dispatcher_dup_singleton ();
224
225   empathy_dispatcher_create_channel (dispatcher,
226       empathy_contact_get_connection (contact), request, timestamp, callback,
227       user_data);
228
229   g_object_unref (dispatcher);
230 }
231
232 static void
233 create_call_handler (EmpathyCallFactory *factory,
234   EmpathyTpCall *call)
235 {
236   EmpathyCallHandler *handler;
237
238   g_return_if_fail (factory != NULL);
239
240   handler = empathy_call_handler_new_for_channel (call);
241
242   g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
243     handler, FALSE);
244
245   g_object_unref (handler);
246 }
247
248 static void
249 call_status_changed_cb (EmpathyTpCall *call,
250     GParamSpec *spec,
251     EmpathyCallFactory *self)
252 {
253   if (empathy_tp_call_get_status (call) <= EMPATHY_TP_CALL_STATUS_READYING)
254     return;
255
256   create_call_handler (self, call);
257
258   g_signal_handlers_disconnect_by_func (call, call_status_changed_cb, self);
259   g_object_unref (call);
260 }
261
262 static void
263 handle_channels_cb (TpSimpleHandler *handler,
264     TpAccount *account,
265     TpConnection *connection,
266     GList *channels,
267     GList *requests_satisfied,
268     gint64 user_action_time,
269     TpHandleChannelsContext *context,
270     gpointer user_data)
271 {
272   EmpathyCallFactory *self = user_data;
273   GList *l;
274
275   for (l = channels; l != NULL; l = g_list_next (l))
276     {
277       TpChannel *channel = l->data;
278       EmpathyTpCall *call;
279
280       if (tp_proxy_get_invalidated (channel) != NULL)
281         continue;
282
283       if (tp_channel_get_channel_type_id (channel) !=
284           TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA)
285         continue;
286
287       call = empathy_tp_call_new (channel);
288
289       if (empathy_tp_call_get_status (call) <= EMPATHY_TP_CALL_STATUS_READYING)
290         {
291           /* We have to wait that the TpCall is ready as the
292            * call-handler rely on it. */
293           tp_g_signal_connect_object (call, "notify::status",
294               G_CALLBACK (call_status_changed_cb), self, 0);
295           continue;
296         }
297
298       create_call_handler (self, call);
299       g_object_unref (call);
300     }
301
302   tp_handle_channels_context_accept (context);
303 }
304
305 gboolean
306 empathy_call_factory_register (EmpathyCallFactory *self,
307     GError **error)
308 {
309   EmpathyCallFactoryPriv *priv = GET_PRIV (self);
310
311   return tp_base_client_register (priv->handler, error);
312 }