]> git.0d.be Git - empathy.git/blob - src/empathy-call-observer.c
0fecbdca614f2b011f083ae12991ff02efb86e1e
[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 <glib/gi18n-lib.h>
24
25 #include <telepathy-glib/telepathy-glib.h>
26
27 #include <telepathy-yell/telepathy-yell.h>
28
29 #include <libnotify/notification.h>
30
31 #include <libempathy/empathy-utils.h>
32
33 #include <libempathy-gtk/empathy-images.h>
34 #include <libempathy-gtk/empathy-notify-manager.h>
35
36 #include <extensions/extensions.h>
37
38 #include "empathy-call-observer.h"
39
40 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
41 #include <libempathy/empathy-debug.h>
42
43 struct _EmpathyCallObserverPriv {
44   EmpathyNotifyManager *notify_mgr;
45
46   TpBaseClient *observer;
47
48   /* Ongoing calls, as reffed TpChannels */
49   GList *channels;
50 };
51
52 /* The Call Observer looks at incoming and outgoing calls, and
53  * autorejects incoming ones if there are ongoing ones, since
54  * we don't cope with simultaneous calls quite well yet.
55  * At some point, we should ask the user if he wants to put the
56  * current call on hold and answer the incoming one instead,
57  * see https://bugzilla.gnome.org/show_bug.cgi?id=623348
58  */
59 G_DEFINE_TYPE (EmpathyCallObserver, empathy_call_observer, G_TYPE_OBJECT);
60
61 static EmpathyCallObserver * observer_singleton = NULL;
62
63 static void
64 on_channel_closed (TpProxy *proxy,
65     guint    domain,
66     gint     code,
67     gchar   *message,
68     EmpathyCallObserver *self)
69 {
70   DEBUG ("channel %s has been invalidated; stop observing it",
71       tp_proxy_get_object_path (proxy));
72
73   self->priv->channels = g_list_remove (self->priv->channels, proxy);
74   g_object_unref (proxy);
75 }
76
77 typedef struct
78 {
79   EmpathyCallObserver *self;
80   TpObserveChannelsContext *context;
81   TpChannel *main_channel;
82 } AutoRejectCtx;
83
84 static AutoRejectCtx *
85 auto_reject_ctx_new (EmpathyCallObserver *self,
86     TpObserveChannelsContext *context,
87     TpChannel *main_channel)
88 {
89   AutoRejectCtx *ctx = g_slice_new (AutoRejectCtx);
90
91   ctx->self = g_object_ref (self);
92   ctx->context = g_object_ref (context);
93   ctx->main_channel = g_object_ref (main_channel);
94   return ctx;
95 }
96
97 static void
98 auto_reject_ctx_free (AutoRejectCtx *ctx)
99 {
100   g_object_unref (ctx->self);
101   g_object_unref (ctx->context);
102   g_object_unref (ctx->main_channel);
103   g_slice_free (AutoRejectCtx, ctx);
104 }
105
106 static void
107 get_contact_cb (TpConnection *connection,
108     guint n_contacts,
109     TpContact * const *contacts,
110     guint n_failed,
111     const TpHandle *failed,
112     const GError *error,
113     gpointer user_data,
114     GObject *weak_object)
115 {
116   EmpathyCallObserver *self = (EmpathyCallObserver *) weak_object;
117   NotifyNotification *notification;
118   TpContact *contact;
119   gchar *summary, *body;
120   EmpathyContact *emp_contact;
121   GdkPixbuf *pixbuf;
122
123   if (n_contacts != 1)
124     {
125       DEBUG ("Failed to get TpContact; ignoring notification bubble");
126       return;
127     }
128
129   contact = contacts[0];
130
131   summary = g_strdup_printf (_("Missed call from %s"),
132       tp_contact_get_alias (contact));
133   body = g_strdup_printf (
134       _("%s just tried to call you, but you were in another call."),
135       tp_contact_get_alias (contact));
136
137   notification = notify_notification_new (summary, body, NULL);
138
139   emp_contact = empathy_contact_dup_from_tp_contact (contact);
140   pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
141       self->priv->notify_mgr, emp_contact, EMPATHY_IMAGE_AVATAR_DEFAULT);
142
143   if (pixbuf != NULL)
144     {
145       notify_notification_set_icon_from_pixbuf (notification, pixbuf);
146       g_object_unref (pixbuf);
147     }
148
149   notify_notification_show (notification, NULL);
150
151   g_object_unref (notification);
152   g_free (summary);
153   g_free (body);
154   g_object_unref (emp_contact);
155 }
156
157 static void
158 display_reject_notification (EmpathyCallObserver *self,
159     TpChannel *channel)
160 {
161   TpHandle handle;
162   TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
163       TP_CONTACT_FEATURE_AVATAR_DATA };
164
165   handle = tp_channel_get_handle (channel, NULL);
166
167   tp_connection_get_contacts_by_handle (tp_channel_borrow_connection (channel),
168       1, &handle, G_N_ELEMENTS (features), features, get_contact_cb,
169       g_object_ref (channel), g_object_unref, G_OBJECT (self));
170 }
171
172 static TpChannel *
173 find_main_channel (GList *channels)
174 {
175   GList *l;
176
177   for (l = channels; l != NULL; l = g_list_next (l))
178     {
179       TpChannel *channel = l->data;
180       GQuark channel_type;
181
182       if (tp_proxy_get_invalidated (channel) != NULL)
183         continue;
184
185       channel_type = tp_channel_get_channel_type_id (channel);
186
187       if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
188           channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
189         return channel;
190     }
191
192   return NULL;
193 }
194
195 static gboolean
196 has_ongoing_calls (EmpathyCallObserver *self)
197 {
198   GList *l;
199
200   for (l = self->priv->channels; l != NULL; l = l->next)
201     {
202       TpChannel *channel = TP_CHANNEL (l->data);
203       GQuark type = tp_channel_get_channel_type_id (channel);
204
205       /* Check that Call channels are not ended */
206       if (type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL &&
207           tpy_call_channel_get_state (TPY_CALL_CHANNEL (channel), NULL, NULL)
208                == TPY_CALL_STATE_ENDED)
209         continue;
210
211       return TRUE;
212     }
213
214   return FALSE;
215 }
216
217 static void
218 claim_and_leave_cb (GObject *source,
219     GAsyncResult *result,
220     gpointer user_data)
221 {
222   AutoRejectCtx *ctx = user_data;
223   GError *error = NULL;
224
225   if (!tp_channel_dispatch_operation_leave_channels_finish (
226         TP_CHANNEL_DISPATCH_OPERATION (source), result, &error))
227     {
228       DEBUG ("Failed to reject call: %s", error->message);
229
230       g_error_free (error);
231       goto out;
232     }
233
234   display_reject_notification (ctx->self, ctx->main_channel);
235
236 out:
237   auto_reject_ctx_free (ctx);
238 }
239
240 static void
241 observe_channels (TpSimpleObserver *observer,
242     TpAccount *account,
243     TpConnection *connection,
244     GList *channels,
245     TpChannelDispatchOperation *dispatch_operation,
246     GList *requests,
247     TpObserveChannelsContext *context,
248     gpointer user_data)
249 {
250   EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (user_data);
251   TpChannel *channel;
252   const GError *error;
253
254   channel = find_main_channel (channels);
255   if (channel == NULL)
256     {
257       GError err = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
258           "Unknown channel type" };
259
260       DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");
261
262       tp_observe_channels_context_fail (context, &err);
263       return;
264     }
265
266   /* Autoreject if there are other ongoing calls */
267   if (has_ongoing_calls (self))
268     {
269       AutoRejectCtx *ctx = auto_reject_ctx_new (self, context, channel);
270
271       DEBUG ("Autorejecting incoming call since there are others in "
272           "progress: %s", tp_proxy_get_object_path (channel));
273
274       tp_channel_dispatch_operation_leave_channels_async (dispatch_operation,
275           TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
276           claim_and_leave_cb, ctx);
277
278       tp_observe_channels_context_accept (context);
279       return;
280     }
281
282   if ((error = tp_proxy_get_invalidated (channel)) != NULL)
283     {
284       DEBUG ("The channel has already been invalidated: %s",
285           error->message);
286
287       tp_observe_channels_context_fail (context, error);
288       return;
289     }
290
291   DEBUG ("Observing channel %s", tp_proxy_get_object_path (channel));
292
293   tp_g_signal_connect_object (channel, "invalidated",
294       G_CALLBACK (on_channel_closed), self, 0);
295   self->priv->channels = g_list_prepend (self->priv->channels,
296       g_object_ref (channel));
297
298   tp_observe_channels_context_accept (context);
299 }
300
301 static GObject *
302 observer_constructor (GType type,
303     guint n_props,
304     GObjectConstructParam *props)
305 {
306   GObject *retval;
307
308   if (observer_singleton)
309     {
310       retval = g_object_ref (observer_singleton);
311     }
312   else
313     {
314       retval = G_OBJECT_CLASS (empathy_call_observer_parent_class)->constructor
315           (type, n_props, props);
316
317       observer_singleton = EMPATHY_CALL_OBSERVER (retval);
318       g_object_add_weak_pointer (retval, (gpointer) &observer_singleton);
319     }
320
321   return retval;
322 }
323
324 static void
325 observer_dispose (GObject *object)
326 {
327   EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);
328
329   tp_clear_object (&self->priv->notify_mgr);
330   tp_clear_object (&self->priv->observer);
331   g_list_free_full (self->priv->channels, g_object_unref);
332   self->priv->channels = NULL;
333 }
334
335 static void
336 empathy_call_observer_class_init (EmpathyCallObserverClass *klass)
337 {
338   GObjectClass *object_class = G_OBJECT_CLASS (klass);
339
340   object_class->dispose = observer_dispose;
341   object_class->constructor = observer_constructor;
342
343   g_type_class_add_private (object_class, sizeof (EmpathyCallObserverPriv));
344 }
345
346 static void
347 empathy_call_observer_init (EmpathyCallObserver *self)
348 {
349   EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
350     EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
351   TpAccountManager *am;
352   GError *error = NULL;
353
354   self->priv = priv;
355
356   self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
357
358   am = tp_account_manager_dup ();
359
360   self->priv->observer = tp_simple_observer_new_with_am (am, TRUE,
361       "Empathy.CallObserver", FALSE,
362       observe_channels, self, NULL);
363
364   /* Observe Call and StreamedMedia channels */
365   tp_base_client_take_observer_filter (self->priv->observer,
366       tp_asv_new (
367         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
368           TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
369         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
370           TP_HANDLE_TYPE_CONTACT,
371         NULL));
372   tp_base_client_take_observer_filter (self->priv->observer,
373       tp_asv_new (
374         TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
375           TPY_IFACE_CHANNEL_TYPE_CALL,
376         TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
377           TP_HANDLE_TYPE_CONTACT,
378         NULL));
379
380   tp_base_client_set_observer_delay_approvers (self->priv->observer, TRUE);
381
382   if (!tp_base_client_register (self->priv->observer, &error))
383     {
384       DEBUG ("Failed to register observer: %s", error->message);
385       g_error_free (error);
386     }
387
388   g_object_unref (am);
389 }
390
391 EmpathyCallObserver *
392 empathy_call_observer_dup_singleton (void)
393 {
394   return g_object_new (EMPATHY_TYPE_CALL_OBSERVER, NULL);
395 }