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