2 * empathy-call-factory.c - Source for EmpathyCallFactory
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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.
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.
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
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>
30 #include <telepathy-yell/telepathy-yell.h>
32 #include <libempathy/empathy-channel-factory.h>
33 #include <libempathy/empathy-utils.h>
35 #include "empathy-call-factory.h"
36 #include "empathy-call-handler.h"
37 #include "src-marshal.h"
39 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
40 #include <libempathy/empathy-debug.h>
42 G_DEFINE_TYPE(EmpathyCallFactory, empathy_call_factory, G_TYPE_OBJECT)
44 static void handle_channels_cb (TpSimpleHandler *handler,
46 TpConnection *connection,
48 GList *requests_satisfied,
49 gint64 user_action_time,
50 TpHandleChannelsContext *context,
60 static guint signals[LAST_SIGNAL] = {0};
62 /* private structure */
64 TpBaseClient *handler;
65 gboolean dispose_has_run;
66 } EmpathyCallFactoryPriv;
68 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyCallFactory)
70 static GObject *call_factory = NULL;
73 empathy_call_factory_init (EmpathyCallFactory *obj)
75 EmpathyCallFactoryPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (obj,
76 EMPATHY_TYPE_CALL_FACTORY, EmpathyCallFactoryPriv);
78 EmpathyChannelFactory *factory;
83 dbus = tp_dbus_daemon_dup (&error);
86 g_warning ("Failed to get TpDBusDaemon: %s", error->message);
91 priv->handler = tp_simple_handler_new (dbus, FALSE, FALSE,
92 "Empathy.Call", FALSE, handle_channels_cb, obj, NULL);
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);
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,
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,
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,
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",
125 g_object_unref (dbus);
129 empathy_call_factory_constructor (GType type, guint n_construct_params,
130 GObjectConstructParam *construct_params)
132 g_return_val_if_fail (call_factory == NULL, NULL);
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);
142 empathy_call_factory_finalize (GObject *object)
144 /* free any data held directly by the object here */
146 if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize)
147 G_OBJECT_CLASS (empathy_call_factory_parent_class)->finalize (object);
151 empathy_call_factory_dispose (GObject *object)
153 EmpathyCallFactoryPriv *priv = GET_PRIV (object);
155 if (priv->dispose_has_run)
158 priv->dispose_has_run = TRUE;
160 tp_clear_object (&priv->handler);
162 if (G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose)
163 G_OBJECT_CLASS (empathy_call_factory_parent_class)->dispose (object);
167 empathy_call_factory_class_init (
168 EmpathyCallFactoryClass *empathy_call_factory_class)
170 GObjectClass *object_class = G_OBJECT_CLASS (empathy_call_factory_class);
172 g_type_class_add_private (empathy_call_factory_class,
173 sizeof (EmpathyCallFactoryPriv));
175 object_class->constructor = empathy_call_factory_constructor;
176 object_class->dispose = empathy_call_factory_dispose;
177 object_class->finalize = empathy_call_factory_finalize;
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,
184 _src_marshal_VOID__OBJECT_BOOLEAN,
186 2, EMPATHY_TYPE_CALL_HANDLER, G_TYPE_BOOLEAN);
190 empathy_call_factory_initialise (void)
192 g_return_val_if_fail (call_factory == NULL, NULL);
194 return EMPATHY_CALL_FACTORY (g_object_new (EMPATHY_TYPE_CALL_FACTORY, NULL));
198 empathy_call_factory_get (void)
200 g_return_val_if_fail (call_factory != NULL, NULL);
202 return EMPATHY_CALL_FACTORY (call_factory);
206 empathy_call_factory_create_call_request (EmpathyContact *contact,
207 gboolean initial_audio,
208 gboolean initial_video)
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,
218 TPY_PROP_CHANNEL_TYPE_CALL_INITIAL_VIDEO, G_TYPE_BOOLEAN,
224 create_call_channel_cb (GObject *source,
225 GAsyncResult *result,
228 GError *error = NULL;
230 if (!tp_account_channel_request_create_channel_finish (
231 TP_ACCOUNT_CHANNEL_REQUEST (source), result, &error))
233 DEBUG ("Failed to create call channel: %s", error->message);
234 g_error_free (error);
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
245 * Initiate a new Call with @contact.
248 empathy_call_factory_new_call_with_streams (EmpathyContact *contact,
249 gboolean initial_audio,
250 gboolean initial_video,
254 GHashTable *call_request;
256 TpAccountChannelRequest *call_req;
258 call_request = empathy_call_factory_create_call_request (contact,
259 initial_audio, initial_video);
261 account = empathy_contact_get_account (contact);
263 call_req = tp_account_channel_request_new (account, call_request, timestamp);
265 tp_account_channel_request_create_channel_async (call_req, NULL, NULL,
266 create_call_channel_cb, NULL);
268 g_hash_table_unref (call_request);
269 g_object_unref (call_req);
273 create_call_handler (EmpathyCallFactory *factory,
274 TpyCallChannel *call)
276 EmpathyCallHandler *handler;
278 g_return_if_fail (factory != NULL);
280 handler = empathy_call_handler_new_for_channel (call);
282 g_signal_emit (factory, signals[NEW_CALL_HANDLER], 0,
285 g_object_unref (handler);
289 handle_channels_cb (TpSimpleHandler *handler,
291 TpConnection *connection,
293 GList *requests_satisfied,
294 gint64 user_action_time,
295 TpHandleChannelsContext *context,
298 EmpathyCallFactory *self = user_data;
301 for (l = channels; l != NULL; l = g_list_next (l))
303 TpChannel *channel = l->data;
304 TpyCallChannel *call;
306 if (tp_proxy_get_invalidated (channel) != NULL)
309 if (tp_channel_get_channel_type_id (channel) !=
310 TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
313 if (!TPY_IS_CALL_CHANNEL (channel))
316 call = TPY_CALL_CHANNEL (channel);
317 create_call_handler (self, call);
318 g_object_unref (call);
321 tp_handle_channels_context_accept (context);
325 empathy_call_factory_register (EmpathyCallFactory *self,
328 EmpathyCallFactoryPriv *priv = GET_PRIV (self);
330 return tp_base_client_register (priv->handler, error);