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