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 <glib/gi18n-lib.h>
25 #include <telepathy-glib/telepathy-glib.h>
27 #include <telepathy-yell/telepathy-yell.h>
29 #include <libnotify/notification.h>
31 #include <libempathy/empathy-channel-factory.h>
32 #include <libempathy/empathy-utils.h>
34 #include <libempathy-gtk/empathy-images.h>
35 #include <libempathy-gtk/empathy-notify-manager.h>
37 #include <extensions/extensions.h>
39 #include "empathy-call-observer.h"
41 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
42 #include <libempathy/empathy-debug.h>
44 struct _EmpathyCallObserverPriv {
45 EmpathyNotifyManager *notify_mgr;
47 TpBaseClient *observer;
49 /* Ongoing calls, as reffed TpChannels */
53 /* The Call Observer looks at incoming and outgoing calls, and
54 * autorejects incoming ones if there are ongoing ones, since
55 * we don't cope with simultaneous calls quite well yet.
56 * At some point, we should ask the user if he wants to put the
57 * current call on hold and answer the incoming one instead,
58 * see https://bugzilla.gnome.org/show_bug.cgi?id=623348
60 G_DEFINE_TYPE (EmpathyCallObserver, empathy_call_observer, G_TYPE_OBJECT);
62 static EmpathyCallObserver * observer_singleton = NULL;
65 on_channel_closed (TpProxy *proxy,
69 EmpathyCallObserver *self)
71 DEBUG ("channel %s has been invalidated; stop observing it",
72 tp_proxy_get_object_path (proxy));
74 self->priv->channels = g_list_remove (self->priv->channels, proxy);
75 g_object_unref (proxy);
80 EmpathyCallObserver *self;
81 TpObserveChannelsContext *context;
82 TpChannel *main_channel;
85 static AutoRejectCtx *
86 auto_reject_ctx_new (EmpathyCallObserver *self,
87 TpObserveChannelsContext *context,
88 TpChannel *main_channel)
90 AutoRejectCtx *ctx = g_slice_new (AutoRejectCtx);
92 ctx->self = g_object_ref (self);
93 ctx->context = g_object_ref (context);
94 ctx->main_channel = g_object_ref (main_channel);
99 auto_reject_ctx_free (AutoRejectCtx *ctx)
101 g_object_unref (ctx->self);
102 g_object_unref (ctx->context);
103 g_object_unref (ctx->main_channel);
104 g_slice_free (AutoRejectCtx, ctx);
108 get_contact_cb (TpConnection *connection,
110 TpContact * const *contacts,
112 const TpHandle *failed,
115 GObject *weak_object)
117 EmpathyCallObserver *self = (EmpathyCallObserver *) weak_object;
118 NotifyNotification *notification;
120 gchar *summary, *body;
121 EmpathyContact *emp_contact;
126 DEBUG ("Failed to get TpContact; ignoring notification bubble");
130 contact = contacts[0];
132 summary = g_strdup_printf (_("Missed call from %s"),
133 tp_contact_get_alias (contact));
134 body = g_strdup_printf (
135 _("%s just tried to call you, but you were in another call."),
136 tp_contact_get_alias (contact));
138 notification = notify_notification_new (summary, body, NULL);
140 emp_contact = empathy_contact_dup_from_tp_contact (contact);
141 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
142 self->priv->notify_mgr, emp_contact, EMPATHY_IMAGE_AVATAR_DEFAULT);
146 notify_notification_set_icon_from_pixbuf (notification, pixbuf);
147 g_object_unref (pixbuf);
150 notify_notification_show (notification, NULL);
152 g_object_unref (notification);
155 g_object_unref (emp_contact);
159 display_reject_notification (EmpathyCallObserver *self,
163 TpContactFeature features[] = { TP_CONTACT_FEATURE_ALIAS,
164 TP_CONTACT_FEATURE_AVATAR_DATA };
166 handle = tp_channel_get_handle (channel, NULL);
168 tp_connection_get_contacts_by_handle (tp_channel_borrow_connection (channel),
169 1, &handle, G_N_ELEMENTS (features), features, get_contact_cb,
170 g_object_ref (channel), g_object_unref, G_OBJECT (self));
174 find_main_channel (GList *channels)
178 for (l = channels; l != NULL; l = g_list_next (l))
180 TpChannel *channel = l->data;
183 if (tp_proxy_get_invalidated (channel) != NULL)
186 channel_type = tp_channel_get_channel_type_id (channel);
188 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
189 channel_type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL)
197 has_ongoing_calls (EmpathyCallObserver *self)
201 for (l = self->priv->channels; l != NULL; l = l->next)
203 TpChannel *channel = TP_CHANNEL (l->data);
204 GQuark type = tp_channel_get_channel_type_id (channel);
206 /* Check that Call channels are not ended */
207 if (type == TPY_IFACE_QUARK_CHANNEL_TYPE_CALL &&
208 tpy_call_channel_get_state (TPY_CALL_CHANNEL (channel), NULL, NULL)
209 == TPY_CALL_STATE_ENDED)
219 claim_and_leave_cb (GObject *source,
220 GAsyncResult *result,
223 AutoRejectCtx *ctx = user_data;
224 GError *error = NULL;
226 if (!tp_channel_dispatch_operation_leave_channels_finish (
227 TP_CHANNEL_DISPATCH_OPERATION (source), result, &error))
229 DEBUG ("Failed to reject call: %s", error->message);
231 g_error_free (error);
235 display_reject_notification (ctx->self, ctx->main_channel);
238 auto_reject_ctx_free (ctx);
242 observe_channels (TpSimpleObserver *observer,
244 TpConnection *connection,
246 TpChannelDispatchOperation *dispatch_operation,
248 TpObserveChannelsContext *context,
251 EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (user_data);
255 channel = find_main_channel (channels);
258 GError err = { TP_ERRORS, TP_ERROR_INVALID_ARGUMENT,
259 "Unknown channel type" };
261 DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");
263 tp_observe_channels_context_fail (context, &err);
267 /* Autoreject if there are other ongoing calls */
268 if (has_ongoing_calls (self))
270 AutoRejectCtx *ctx = auto_reject_ctx_new (self, context, channel);
272 DEBUG ("Autorejecting incoming call since there are others in "
273 "progress: %s", tp_proxy_get_object_path (channel));
275 tp_channel_dispatch_operation_leave_channels_async (dispatch_operation,
276 TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
277 claim_and_leave_cb, ctx);
279 tp_observe_channels_context_accept (context);
283 if ((error = tp_proxy_get_invalidated (channel)) != NULL)
285 DEBUG ("The channel has already been invalidated: %s",
288 tp_observe_channels_context_fail (context, error);
292 DEBUG ("Observing channel %s", tp_proxy_get_object_path (channel));
294 tp_g_signal_connect_object (channel, "invalidated",
295 G_CALLBACK (on_channel_closed), self, 0);
296 self->priv->channels = g_list_prepend (self->priv->channels,
297 g_object_ref (channel));
299 tp_observe_channels_context_accept (context);
303 observer_constructor (GType type,
305 GObjectConstructParam *props)
309 if (observer_singleton)
311 retval = g_object_ref (observer_singleton);
315 retval = G_OBJECT_CLASS (empathy_call_observer_parent_class)->constructor
316 (type, n_props, props);
318 observer_singleton = EMPATHY_CALL_OBSERVER (retval);
319 g_object_add_weak_pointer (retval, (gpointer) &observer_singleton);
326 observer_dispose (GObject *object)
328 EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);
330 tp_clear_object (&self->priv->notify_mgr);
331 tp_clear_object (&self->priv->observer);
332 g_list_free_full (self->priv->channels, g_object_unref);
333 self->priv->channels = NULL;
337 empathy_call_observer_class_init (EmpathyCallObserverClass *klass)
339 GObjectClass *object_class = G_OBJECT_CLASS (klass);
341 object_class->dispose = observer_dispose;
342 object_class->constructor = observer_constructor;
344 g_type_class_add_private (object_class, sizeof (EmpathyCallObserverPriv));
348 empathy_call_observer_init (EmpathyCallObserver *self)
350 EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
351 EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
353 EmpathyChannelFactory *factory;
354 GError *error = NULL;
358 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
360 dbus = tp_dbus_daemon_dup (&error);
363 DEBUG ("Failed to get TpDBusDaemon: %s", error->message);
364 g_error_free (error);
368 self->priv->observer = tp_simple_observer_new (dbus, TRUE,
369 "Empathy.CallObserver", FALSE,
370 observe_channels, self, NULL);
372 factory = empathy_channel_factory_dup ();
373 tp_base_client_set_channel_factory (self->priv->observer,
374 TP_CLIENT_CHANNEL_FACTORY (factory));
375 g_object_unref (factory);
377 /* Observe Call and StreamedMedia channels */
378 tp_base_client_take_observer_filter (self->priv->observer,
380 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
381 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
382 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
383 TP_HANDLE_TYPE_CONTACT,
385 tp_base_client_take_observer_filter (self->priv->observer,
387 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
388 TPY_IFACE_CHANNEL_TYPE_CALL,
389 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
390 TP_HANDLE_TYPE_CONTACT,
393 tp_base_client_set_observer_delay_approvers (self->priv->observer, TRUE);
395 if (!tp_base_client_register (self->priv->observer, &error))
397 DEBUG ("Failed to register observer: %s", error->message);
398 g_error_free (error);
401 g_object_unref (dbus);
404 EmpathyCallObserver *
405 empathy_call_observer_dup_singleton (void)
407 return g_object_new (EMPATHY_TYPE_CALL_OBSERVER, NULL);