]> git.0d.be Git - empathy.git/blob - src/empathy-call-observer.c
Autoreject incoming calls if there are others in progress
[empathy.git] / src / empathy-call-observer.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
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.
8  *
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.
13  *
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
17  *
18  * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
19  */
20
21 #include <config.h>
22
23 #include <telepathy-glib/telepathy-glib.h>
24
25 #include <telepathy-yell/telepathy-yell.h>
26
27 #include <libempathy/empathy-channel-factory.h>
28 #include <libempathy/empathy-utils.h>
29
30 #include <extensions/extensions.h>
31
32 #include "empathy-call-observer.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_DISPATCHER
35 #include <libempathy/empathy-debug.h>
36
37 struct _EmpathyCallObserverPriv {
38   TpBaseClient *observer;
39
40   /* Ongoing calls, as reffed TpChannels */
41   GList *channels;
42 };
43
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
50  */
51 G_DEFINE_TYPE (EmpathyCallObserver, empathy_call_observer, G_TYPE_OBJECT);
52
53 static EmpathyCallObserver * observer_singleton = NULL;
54
55 static void
56 on_channel_closed (TpProxy *proxy,
57     guint    domain,
58     gint     code,
59     gchar   *message,
60     EmpathyCallObserver *self)
61 {
62   self->priv->channels = g_list_remove (self->priv->channels, proxy);
63   g_object_unref (proxy);
64 }
65
66 static void
67 on_cdo_claim_cb (GObject *source_object,
68     GAsyncResult *result,
69     gpointer user_data)
70 {
71   TpChannelDispatchOperation *cdo;
72   TpChannel *channel = TP_CHANNEL (user_data);
73   GError *error = NULL;
74
75   cdo = TP_CHANNEL_DISPATCH_OPERATION (source_object);
76
77   tp_channel_dispatch_operation_claim_finish (cdo, result, &error);
78   if (error != NULL)
79     {
80       DEBUG ("Could not claim CDO: %s", error->message);
81       g_error_free (error);
82       return;
83     }
84
85   tp_channel_leave_async (channel,
86       TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
87       NULL, NULL);
88 }
89
90 static TpChannel *
91 find_main_channel (GList *channels)
92 {
93   GList *l;
94
95   for (l = channels; l != NULL; l = g_list_next (l))
96     {
97       TpChannel *channel = l->data;
98       GQuark channel_type;
99
100       if (tp_proxy_get_invalidated (channel) != NULL)
101         continue;
102
103       channel_type = tp_channel_get_channel_type_id (channel);
104
105       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
106           channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
107         return channel;
108     }
109
110   return NULL;
111 }
112
113 static gboolean
114 has_ongoing_calls (EmpathyCallObserver *self)
115 {
116   GList *l;
117
118   for (l = self->priv->channels; l != NULL; l = l->next)
119     {
120       TpChannel *channel = TP_CHANNEL (l->data);
121       GQuark type = tp_channel_get_channel_type_id (channel);
122
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)
127         continue;
128
129       return TRUE;
130     }
131
132   return FALSE;
133 }
134
135 static void
136 observe_channels (TpSimpleObserver *observer,
137     TpAccount *account,
138     TpConnection *connection,
139     GList *channels,
140     TpChannelDispatchOperation *dispatch_operation,
141     GList *requests,
142     TpObserveChannelsContext *context,
143     gpointer user_data)
144 {
145   EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (user_data);
146   TpChannel *channel;
147   const GError *error;
148
149   channel = find_main_channel (channels);
150   if (channel == NULL)
151     {
152       GError err = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
153           "Unknown channel type" };
154
155       DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");
156
157       tp_observe_channels_context_fail (context, &err);
158       return;
159     }
160
161   /* Autoreject if there are other ongoing calls */
162   if (has_ongoing_calls (self))
163     {
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));
168       return;
169     }
170
171   if ((error = tp_proxy_get_invalidated (channel)) != NULL)
172     {
173       DEBUG ("The channel has already been invalidated: %s",
174           error->message);
175       return;
176     }
177
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));
182
183   tp_observe_channels_context_accept (context);
184 }
185
186 static GObject *
187 observer_constructor (GType type,
188     guint n_props,
189     GObjectConstructParam *props)
190 {
191   GObject *retval;
192
193   if (observer_singleton)
194     {
195       retval = g_object_ref (observer_singleton);
196     }
197   else
198     {
199       retval = G_OBJECT_CLASS (empathy_call_observer_parent_class)->constructor
200           (type, n_props, props);
201
202       observer_singleton = EMPATHY_CALL_OBSERVER (retval);
203       g_object_add_weak_pointer (retval, (gpointer) &observer_singleton);
204     }
205
206   return retval;
207 }
208
209 static void
210 observer_dispose (GObject *object)
211 {
212   EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);
213
214   tp_clear_object (&self->priv->observer);
215   g_list_free_full (self->priv->channels, g_object_unref);
216   self->priv->channels = NULL;
217 }
218
219 static void
220 empathy_call_observer_class_init (EmpathyCallObserverClass *klass)
221 {
222   GObjectClass *object_class = G_OBJECT_CLASS (klass);
223
224   object_class->dispose = observer_dispose;
225   object_class->constructor = observer_constructor;
226
227   g_type_class_add_private (object_class, sizeof (EmpathyCallObserverPriv));
228 }
229
230 static void
231 empathy_call_observer_init (EmpathyCallObserver *self)
232 {
233   EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
234     EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
235   TpDBusDaemon *dbus;
236   EmpathyChannelFactory *factory;
237   GError *error = NULL;
238
239   self->priv = priv;
240
241   dbus = tp_dbus_daemon_dup (&error);
242   if (dbus == NULL)
243     {
244       DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
245       g_error_free (error);
246       return;
247     }
248
249   self->priv->observer = tp_simple_observer_new (dbus, FALSE,
250       "Empathy.CallObserver", FALSE,
251       observe_channels, self, NULL);
252
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);
257
258   /* Observe Call and StreamedMedia channels */
259   tp_base_client_take_observer_filter (self->priv->observer,
260       tp_asv_new (
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,
265         NULL));
266   tp_base_client_take_observer_filter (self->priv->observer,
267       tp_asv_new (
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,
272         NULL));
273
274   if (!tp_base_client_register (self->priv->observer, &error))
275     {
276       DEBUG ("Failed to register observer: %s", error->message);
277       g_error_free (error);
278     }
279
280   g_object_unref (dbus);
281 }
282
283 EmpathyCallObserver *
284 empathy_call_observer_dup_singleton (void)
285 {
286   return g_object_new (EMPATHY_TYPE_CALL_OBSERVER, NULL);
287 }