aboutsummaryrefslogtreecommitdiffstats
path: root/src/empathy-call-observer.c
blob: c0aa04170fc498b94b7808207cdd6852a32dc543 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
 * Copyright (C) 2011 Collabora Ltd.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Emilio Pozuelo Monfort <emilio.pozuelo@collabora.co.uk>
 */

#include "config.h"

#include <glib/gi18n-lib.h>

#include "libempathy-gtk/empathy-images.h"
#include "libempathy-gtk/empathy-notify-manager.h"

#include "empathy-call-observer.h"

#define DEBUG_FLAG EMPATHY_DEBUG_VOIP
#include "libempathy/empathy-debug.h"

struct _EmpathyCallObserverPriv {
  EmpathyNotifyManager *notify_mgr;

  TpBaseClient *observer;

  /* Ongoing calls, as reffed TpChannels */
  GList *channels;
};

/* The Call Observer looks at incoming and outgoing calls, and
 * autorejects incoming ones if there are ongoing ones, since
 * we don't cope with simultaneous calls quite well yet.
 * At some point, we should ask the user if he wants to put the
 * current call on hold and answer the incoming one instead,
 * see https://bugzilla.gnome.org/show_bug.cgi?id=623348
 */
G_DEFINE_TYPE (EmpathyCallObserver, empathy_call_observer, G_TYPE_OBJECT);

static EmpathyCallObserver * observer_singleton = NULL;

static void
on_channel_closed (TpProxy *proxy,
    guint    domain,
    gint     code,
    gchar   *message,
    EmpathyCallObserver *self)
{
  DEBUG ("channel %s has been invalidated; stop observing it",
      tp_proxy_get_object_path (proxy));

  self->priv->channels = g_list_remove (self->priv->channels, proxy);
  g_object_unref (proxy);
}

typedef struct
{
  EmpathyCallObserver *self;
  TpObserveChannelsContext *context;
  TpChannel *main_channel;
} AutoRejectCtx;

static AutoRejectCtx *
auto_reject_ctx_new (EmpathyCallObserver *self,
    TpObserveChannelsContext *context,
    TpChannel *main_channel)
{
  AutoRejectCtx *ctx = g_slice_new (AutoRejectCtx);

  ctx->self = g_object_ref (self);
  ctx->context = g_object_ref (context);
  ctx->main_channel = g_object_ref (main_channel);
  return ctx;
}

static void
auto_reject_ctx_free (AutoRejectCtx *ctx)
{
  g_object_unref (ctx->self);
  g_object_unref (ctx->context);
  g_object_unref (ctx->main_channel);
  g_slice_free (AutoRejectCtx, ctx);
}

static void
display_reject_notification (EmpathyCallObserver *self,
    TpChannel *channel)
{
  TpContact *contact;
  NotifyNotification *notification;
  gchar *summary, *body;
  EmpathyContact *emp_contact;
  GdkPixbuf *pixbuf;

  contact = tp_channel_get_target_contact (channel);

  summary = g_strdup_printf (_("Missed call from %s"),
      tp_contact_get_alias (contact));
  body = g_strdup_printf (
      _("%s just tried to call you, but you were in another call."),
      tp_contact_get_alias (contact));

  notification = empathy_notify_manager_create_notification (summary, body,
      NULL);

  emp_contact = empathy_contact_dup_from_tp_contact (contact);
  pixbuf = empathy_notify_manager_get_pixbuf_for_notification (
      self->priv->notify_mgr, emp_contact, EMPATHY_IMAGE_AVATAR_DEFAULT);

  if (pixbuf != NULL)
    {
      notify_notification_set_icon_from_pixbuf (notification, pixbuf);
      g_object_unref (pixbuf);
    }

  notify_notification_show (notification, NULL);

  g_object_unref (notification);
  g_free (summary);
  g_free (body);
  g_object_unref (emp_contact);
}

static TpChannel *
find_main_channel (GList *channels)
{
  GList *l;

  for (l = channels; l != NULL; l = g_list_next (l))
    {
      TpChannel *channel = l->data;
      GQuark channel_type;

      if (tp_proxy_get_invalidated (channel) != NULL)
        continue;

      channel_type = tp_channel_get_channel_type_id (channel);

      if (channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_STREAMED_MEDIA ||
          channel_type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL)
        return channel;
    }

  return NULL;
}

static gboolean
has_ongoing_calls (EmpathyCallObserver *self)
{
  GList *l;

  for (l = self->priv->channels; l != NULL; l = l->next)
    {
      TpChannel *channel = TP_CHANNEL (l->data);
      GQuark type = tp_channel_get_channel_type_id (channel);

      /* Check that Call channels are not ended */
      if (type == TP_IFACE_QUARK_CHANNEL_TYPE_CALL &&
          tp_call_channel_get_state (TP_CALL_CHANNEL (channel),
              NULL, NULL, NULL) == TP_CALL_STATE_ENDED)
        continue;

      return TRUE;
    }

  return FALSE;
}

static void
claim_and_leave_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  AutoRejectCtx *ctx = user_data;
  GError *error = NULL;

  if (!tp_channel_dispatch_operation_leave_channels_finish (
        TP_CHANNEL_DISPATCH_OPERATION (source), result, &error))
    {
      DEBUG ("Failed to reject call: %s", error->message);

      g_error_free (error);
      goto out;
    }

  display_reject_notification (ctx->self, ctx->main_channel);

out:
  auto_reject_ctx_free (ctx);
}

static void
observe_channels (TpSimpleObserver *observer,
    TpAccount *account,
    TpConnection *connection,
    GList *channels,
    TpChannelDispatchOperation *dispatch_operation,
    GList *requests,
    TpObserveChannelsContext *context,
    gpointer user_data)
{
  EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (user_data);
  TpChannel *channel;
  const GError *error;

  channel = find_main_channel (channels);
  if (channel == NULL)
    {
      GError err = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
          "Unknown channel type" };

      DEBUG ("Didn't find any Call or StreamedMedia channel; ignoring");

      tp_observe_channels_context_fail (context, &err);
      return;
    }

  /* Autoreject if there are other ongoing calls */
  if (has_ongoing_calls (self))
    {
      AutoRejectCtx *ctx = auto_reject_ctx_new (self, context, channel);

      DEBUG ("Autorejecting incoming call since there are others in "
          "progress: %s", tp_proxy_get_object_path (channel));

      tp_channel_dispatch_operation_leave_channels_async (dispatch_operation,
          TP_CHANNEL_GROUP_CHANGE_REASON_BUSY, "Already in a call",
          claim_and_leave_cb, ctx);

      tp_observe_channels_context_accept (context);
      return;
    }

  if ((error = tp_proxy_get_invalidated (channel)) != NULL)
    {
      DEBUG ("The channel has already been invalidated: %s",
          error->message);

      tp_observe_channels_context_fail (context, error);
      return;
    }

  DEBUG ("Observing channel %s", tp_proxy_get_object_path (channel));

  tp_g_signal_connect_object (channel, "invalidated",
      G_CALLBACK (on_channel_closed), self, 0);
  self->priv->channels = g_list_prepend (self->priv->channels,
      g_object_ref (channel));

  tp_observe_channels_context_accept (context);
}

static GObject *
observer_constructor (GType type,
    guint n_props,
    GObjectConstructParam *props)
{
  GObject *retval;

  if (observer_singleton)
    {
      retval = g_object_ref (observer_singleton);
    }
  else
    {
      retval = G_OBJECT_CLASS (empathy_call_observer_parent_class)->constructor
          (type, n_props, props);

      observer_singleton = EMPATHY_CALL_OBSERVER (retval);
      g_object_add_weak_pointer (retval, (gpointer) &observer_singleton);
    }

  return retval;
}

static void
observer_dispose (GObject *object)
{
  EmpathyCallObserver *self = EMPATHY_CALL_OBSERVER (object);

  tp_clear_object (&self->priv->notify_mgr);
  tp_clear_object (&self->priv->observer);
  g_list_free_full (self->priv->channels, g_object_unref);
  self->priv->channels = NULL;
}

static void
empathy_call_observer_class_init (EmpathyCallObserverClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose = observer_dispose;
  object_class->constructor = observer_constructor;

  g_type_class_add_private (object_class, sizeof (EmpathyCallObserverPriv));
}

static void
empathy_call_observer_init (EmpathyCallObserver *self)
{
  EmpathyCallObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
    EMPATHY_TYPE_CALL_OBSERVER, EmpathyCallObserverPriv);
  TpAccountManager *am;
  GError *error = NULL;

  self->priv = priv;

  self->priv->notify_mgr = empathy_notify_manager_dup_singleton ();

  am = tp_account_manager_dup ();

  self->priv->observer = tp_simple_observer_new_with_am (am, TRUE,
      "Empathy.CallObserver", FALSE,
      observe_channels, self, NULL);

  /* Observe Call and StreamedMedia channels */
  tp_base_client_take_observer_filter (self->priv->observer,
      tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
          TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA,
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
          TP_HANDLE_TYPE_CONTACT,
        NULL));
  tp_base_client_take_observer_filter (self->priv->observer,
      tp_asv_new (
        TP_PROP_CHANNEL_CHANNEL_TYPE, G_TYPE_STRING,
          TP_IFACE_CHANNEL_TYPE_CALL,
        TP_PROP_CHANNEL_TARGET_HANDLE_TYPE, G_TYPE_UINT,
          TP_HANDLE_TYPE_CONTACT,
        NULL));

  tp_base_client_set_observer_delay_approvers (self->priv->observer, TRUE);

  if (!tp_base_client_register (self->priv->observer, &error))
    {
      DEBUG ("Failed to register observer: %s", error->message);
      g_error_free (error);
    }

  g_object_unref (am);
}

EmpathyCallObserver *
empathy_call_observer_dup_singleton (void)
{
  return g_object_new (EMPATHY_TYPE_CALL_OBSERVER, NULL);
}