2 * Copyright (C) 2011 Collabora Ltd.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
23 #include <telepathy-glib/telepathy-glib.h>
25 #include <telepathy-yell/telepathy-yell.h>
27 #include <libempathy/empathy-channel-factory.h>
28 #include <libempathy/empathy-utils.h>
30 #include <extensions/extensions.h>
32 #include "empathy-call-observer.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
35 #include <libempathy/empathy-debug.h>
37 struct _EmpathyCallObserverPriv {
38 TpBaseClient *observer;
40 /* Ongoing calls, as reffed TpChannels */
44 /* The Call Observer looks at incoming and outgoing calls, and
45 * autorejects incoming ones if there are ongoing ones, since
46 * we don't cope with simultaneous calls quite well yet.
47 * At some point, we should ask the user if he wants to put the
48 * current call on hold and answer the incoming one instead,
49 * see https://bugzilla.gnome.org/show_bug.cgi?id=623348
51 G_DEFINE_TYPE (EmpathyCallObserver, empathy_call_observer, G_TYPE_OBJECT);
53 static EmpathyCallObserver * observer_singleton = NULL;
56 on_channel_closed (TpProxy *proxy,
60 EmpathyCallObserver *self)
62 self->priv->channels = g_list_remove (self->priv->channels, proxy);
63 g_object_unref (proxy);
67 on_cdo_claim_cb (GObject *source_object,
71 TpChannelDispatchOperation *cdo;
72 TpChannel *channel = TP_CHANNEL (user_data);
75 cdo = TP_CHANNEL_DISPATCH_OPERATION (source_object);
77 tp_channel_dispatch_operation_claim_finish (cdo, result, &error);
80 DEBUG ("Could not claim CDO: %s", error->message);
85 tp_channel_leave_async (channel,
86 TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
91 find_main_channel (GList *channels)
95 for (l = channels; l != NULL; l = g_list_next (l))
97 TpChannel *channel = l->data;
100 if (tp_proxy_get_invalidated (channel) != NULL)
103 channel_type = tp_channel_get_channel_type_id (channel);
105 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
106 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
114 has_ongoing_calls (EmpathyCallObserver *self)
118 for (l = self->priv->channels; l != NULL; l = l->next)
120 TpChannel *channel = TP_CHANNEL (l->data);
121 GQuark type = tp_channel_get_channel_type_id (channel);
123 /* Check that Call channels are not ended */
124 if (type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL &&
125 tpy_call_channel_get_state (TPY_CALL_CHANNEL (channel), NULL, NULL)
126 == TPY_CALL_STATE_ENDED)
136 observe_channels (TpSimpleObserver *observer,
138 TpConnection *connection,
140 TpChannelDispatchOperation *dispatch_operation,
142 TpObserveChannelsContext *context,
145 EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (user_data);
149 channel = find_main_channel (channels);
152 GError err = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
153 "Unknown channel type" };
155 DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");
157 tp_observe_channels_context_fail (context, &err);
161 /* Autoreject if there are other ongoing calls */
162 if (has_ongoing_calls (self))
164 DEBUG ("Autorejecting incoming call since there are others in "
165 "progress: %s", tp_proxy_get_object_path (channel));
166 tp_channel_dispatch_operation_claim_async (dispatch_operation,
167 on_cdo_claim_cb, g_object_ref (channel));
171 if ((error = tp_proxy_get_invalidated (channel)) != NULL)
173 DEBUG ("The channel has already been invalidated: %s",
178 tp_g_signal_connect_object (channel, "invalidated",
179 G_CALLBACK (on_channel_closed), self, 0);
180 self->priv->channels = g_list_prepend (self->priv->channels,
181 g_object_ref (channel));
183 tp_observe_channels_context_accept (context);
187 observer_constructor (GType type,
189 GObjectConstructParam *props)
193 if (observer_singleton)
195 retval = g_object_ref (observer_singleton);
199 retval = G_OBJECT_CLASS (empathy_call_observer_parent_class)->constructor
200 (type, n_props, props);
202 observer_singleton = EMPATHY_CALL_OBSERVER (retval);
203 g_object_add_weak_pointer (retval, (gpointer) &observer_singleton);
210 observer_dispose (GObject *object)
212 EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);
214 tp_clear_object (&self->priv->observer);
215 g_list_free_full (self->priv->channels, g_object_unref);
216 self->priv->channels = NULL;
220 empathy_call_observer_class_init (EmpathyCallObserverClass *klass)
222 GObjectClass *object_class = G_OBJECT_CLASS (klass);
224 object_class->dispose = observer_dispose;
225 object_class->constructor = observer_constructor;
227 g_type_class_add_private (object_class, sizeof (EmpathyCallObserverPriv));
231 empathy_call_observer_init (EmpathyCallObserver *self)
233 EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
234 EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
236 EmpathyChannelFactory *factory;
237 GError *error = NULL;
241 dbus = tp_dbus_daemon_dup (&error);
244 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
245 g_error_free (error);
249 self->priv->observer = tp_simple_observer_new (dbus, FALSE,
250 "Empathy.CallObserver", FALSE,
251 observe_channels, self, NULL);
253 factory = empathy_channel_factory_dup ();
254 tp_base_client_set_channel_factory (self->priv->observer,
255 TP_CLIENT_CHANNEL_FACTORY (factory));
256 g_object_unref (factory);
258 /* Observe Call and StreamedMedia channels */
259 tp_base_client_take_observer_filter (self->priv->observer,
261 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
262 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
263 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
264 TP_HANDLE_TYPE_CONTACT,
266 tp_base_client_take_observer_filter (self->priv->observer,
268 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
269 TPY_IFACE_CHANNEL_TYPE_CALL,
270 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
271 TP_HANDLE_TYPE_CONTACT,
274 if (!tp_base_client_register (self->priv->observer, &error))
276 DEBUG ("Failed to register observer: %s", error->message);
277 g_error_free (error);
280 g_object_unref (dbus);
283 EmpathyCallObserver *
284 empathy_call_observer_dup_singleton (void)
286 return g_object_new (EMPATHY_TYPE_CALL_OBSERVER, NULL);