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 <libnotify/notification.h>
29 #include <libempathy/empathy-utils.h>
31 #include <libempathy-gtk/empathy-images.h>
32 #include <libempathy-gtk/empathy-notify-manager.h>
34 #include <extensions/extensions.h>
36 #include "empathy-call-observer.h"
38 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
39 #include <libempathy/empathy-debug.h>
41 struct _EmpathyCallObserverPriv {
42 EmpathyNotifyManager *notify_mgr;
44 TpBaseClient *observer;
46 /* Ongoing calls, as reffed TpChannels */
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
57 G_DEFINE_TYPE (EmpathyCallObserver, empathy_call_observer, G_TYPE_OBJECT);
59 static EmpathyCallObserver * observer_singleton = NULL;
62 on_channel_closed (TpProxy *proxy,
66 EmpathyCallObserver *self)
68 DEBUG ("channel %s has been invalidated; stop observing it",
69 tp_proxy_get_object_path (proxy));
71 self->priv->channels = g_list_remove (self->priv->channels, proxy);
72 g_object_unref (proxy);
77 EmpathyCallObserver *self;
78 TpObserveChannelsContext *context;
79 TpChannel *main_channel;
82 static AutoRejectCtx *
83 auto_reject_ctx_new (EmpathyCallObserver *self,
84 TpObserveChannelsContext *context,
85 TpChannel *main_channel)
87 AutoRejectCtx *ctx = g_slice_new (AutoRejectCtx);
89 ctx->self = g_object_ref (self);
90 ctx->context = g_object_ref (context);
91 ctx->main_channel = g_object_ref (main_channel);
96 auto_reject_ctx_free (AutoRejectCtx *ctx)
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);
105 display_reject_notification (EmpathyCallObserver *self,
109 NotifyNotification *notification;
110 gchar *summary, *body;
111 EmpathyContact *emp_contact;
114 contact = tp_channel_get_target_contact (channel);
116 summary = g_strdup_printf (_("Missed call from %s"),
117 tp_contact_get_alias (contact));
118 body = g_strdup_printf (
119 _("%s just tried to call you, but you were in another call."),
120 tp_contact_get_alias (contact));
122 notification = empathy_notify_manager_create_notification (summary, body,
125 emp_contact = empathy_contact_dup_from_tp_contact (contact);
126 pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
127 self->priv->notify_mgr, emp_contact, EMPATHY_IMAGE_AVATAR_DEFAULT);
131 notify_notification_set_icon_from_pixbuf (notification, pixbuf);
132 g_object_unref (pixbuf);
135 notify_notification_show (notification, NULL);
137 g_object_unref (notification);
140 g_object_unref (emp_contact);
144 find_main_channel (GList *channels)
148 for (l = channels; l != NULL; l = g_list_next (l))
150 TpChannel *channel = l->data;
153 if (tp_proxy_get_invalidated (channel) != NULL)
156 channel_type = tp_channel_get_channel_type_id (channel);
158 if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
159 channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
167 has_ongoing_calls (EmpathyCallObserver *self)
171 for (l = self->priv->channels; l != NULL; l = l->next)
173 TpChannel *channel = TP_CHANNEL (l->data);
174 GQuark type = tp_channel_get_channel_type_id (channel);
176 /* Check that Call channels are not ended */
177 if (type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL &&
178 tp_call_channel_get_state (TP_CALL_CHANNEL (channel),
179 NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
189 claim_and_leave_cb (GObject *source,
190 GAsyncResult *result,
193 AutoRejectCtx *ctx = user_data;
194 GError *error = NULL;
196 if (!tp_channel_dispatch_operation_leave_channels_finish (
197 TP_CHANNEL_DISPATCH_OPERATION (source), result, &error))
199 DEBUG ("Failed to reject call: %s", error->message);
201 g_error_free (error);
205 display_reject_notification (ctx->self, ctx->main_channel);
208 auto_reject_ctx_free (ctx);
212 observe_channels (TpSimpleObserver *observer,
214 TpConnection *connection,
216 TpChannelDispatchOperation *dispatch_operation,
218 TpObserveChannelsContext *context,
221 EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (user_data);
225 channel = find_main_channel (channels);
228 GError err = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
229 "Unknown channel type" };
231 DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");
233 tp_observe_channels_context_fail (context, &err);
237 /* Autoreject if there are other ongoing calls */
238 if (has_ongoing_calls (self))
240 AutoRejectCtx *ctx = auto_reject_ctx_new (self, context, channel);
242 DEBUG ("Autorejecting incoming call since there are others in "
243 "progress: %s", tp_proxy_get_object_path (channel));
245 tp_channel_dispatch_operation_leave_channels_async (dispatch_operation,
246 TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
247 claim_and_leave_cb, ctx);
249 tp_observe_channels_context_accept (context);
253 if ((error = tp_proxy_get_invalidated (channel)) != NULL)
255 DEBUG ("The channel has already been invalidated: %s",
258 tp_observe_channels_context_fail (context, error);
262 DEBUG ("Observing channel %s", tp_proxy_get_object_path (channel));
264 tp_g_signal_connect_object (channel, "invalidated",
265 G_CALLBACK (on_channel_closed), self, 0);
266 self->priv->channels = g_list_prepend (self->priv->channels,
267 g_object_ref (channel));
269 tp_observe_channels_context_accept (context);
273 observer_constructor (GType type,
275 GObjectConstructParam *props)
279 if (observer_singleton)
281 retval = g_object_ref (observer_singleton);
285 retval = G_OBJECT_CLASS (empathy_call_observer_parent_class)->constructor
286 (type, n_props, props);
288 observer_singleton = EMPATHY_CALL_OBSERVER (retval);
289 g_object_add_weak_pointer (retval, (gpointer) &observer_singleton);
296 observer_dispose (GObject *object)
298 EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);
300 tp_clear_object (&self->priv->notify_mgr);
301 tp_clear_object (&self->priv->observer);
302 g_list_free_full (self->priv->channels, g_object_unref);
303 self->priv->channels = NULL;
307 empathy_call_observer_class_init (EmpathyCallObserverClass *klass)
309 GObjectClass *object_class = G_OBJECT_CLASS (klass);
311 object_class->dispose = observer_dispose;
312 object_class->constructor = observer_constructor;
314 g_type_class_add_private (object_class, sizeof (EmpathyCallObserverPriv));
318 empathy_call_observer_init (EmpathyCallObserver *self)
320 EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
321 EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
322 TpAccountManager *am;
323 GError *error = NULL;
327 self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();
329 am = tp_account_manager_dup ();
331 self->priv->observer = tp_simple_observer_new_with_am (am, TRUE,
332 "Empathy.CallObserver", FALSE,
333 observe_channels, self, NULL);
335 /* Observe Call and StreamedMedia channels */
336 tp_base_client_take_observer_filter (self->priv->observer,
338 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
339 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
340 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
341 TP_HANDLE_TYPE_CONTACT,
343 tp_base_client_take_observer_filter (self->priv->observer,
345 TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
346 TP_IFACE_CHANNEL_TYPE_CALL,
347 TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
348 TP_HANDLE_TYPE_CONTACT,
351 tp_base_client_set_observer_delay_approvers (self->priv->observer, TRUE);
353 if (!tp_base_client_register (self->priv->observer, &error))
355 DEBUG ("Failed to register observer: %s", error->message);
356 g_error_free (error);
362 EmpathyCallObserver *
363 empathy_call_observer_dup_singleton (void)
365 return g_object_new (EMPATHY_TYPE_CALL_OBSERVER, NULL);