summaryrefslogtreecommitdiff
path: root/telepathy-logger/observer.c
blob: aa7fa0fa4823e96b7e313b318e729c0a341f7ae8 (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 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: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
 */

#include "config.h"
#include "observer.h"

#include <glib.h>
#include <telepathy-glib/interfaces.h>
#include <telepathy-glib/channel.h>
#include <telepathy-glib/gtypes.h>
#include <telepathy-glib/dbus.h>
#include <telepathy-glib/svc-generic.h>
#include <telepathy-glib/svc-client.h>
#include <telepathy-glib/account-manager.h>

#include <telepathy-logger/conf.h>
#include <telepathy-logger/channel.h>
#include <telepathy-logger/channel-factory.h>
#include <telepathy-logger/log-manager.h>
#include <telepathy-logger/util.h>

#define DEBUG_FLAG TPL_DEBUG_OBSERVER
#include <telepathy-logger/debug.h>

/**
 * SECTION:observer
 * @title: TplObserver
 * @short_description: TPL Observer main class, used to handle received
 * signals
 * @see_also: #TpSvcClientObserver
 *
 * The Telepathy Logger's Observer implements
 * org.freedesktop.Telepathy.Client.Observer DBus interface and is called by
 * the Channel Dispatcher when a new channel is created, in order to log
 * received signals.
 *
 * Since: 0.1
 */

/**
 * TplObserver:
 *
 * The Telepathy Logger's Observer implements
 * org.freedesktop.Telepathy.Client.Observer DBus interface and is called by
 * the Channel Dispatcher when a new channel is created, in order to log
 * received signals using its #LogManager.
 *
 * This object is a signleton, any call to tpl_observer_new will return the
 * same object with an incremented reference counter. One has to
 * unreference the object properly when the used reference is not used
 * anymore.
 *
 * This object will register to it's DBus interface when
 * tpl_observer_register_dbus is called, ensuring that the registration will
 * happen only once per singleton instance.
 *
 * Since: 0.1
 */

/**
 * TplObserverClass:
 *
 * The class of a #TplObserver.
 */

static void tpl_observer_finalize (GObject * obj);
static void tpl_observer_dispose (GObject * obj);
static void observer_iface_init (gpointer, gpointer);
static void got_tpl_channel_text_ready_cb (GObject *obj, GAsyncResult *result,
    gpointer user_data);
static TplChannelFactory tpl_observer_get_channel_factory (TplObserver *self);
static GHashTable *tpl_observer_get_channel_map (TplObserver *self);
static void tpl_observer_get_open_channels (void);


#define GET_PRIV(obj) TPL_GET_PRIV (obj, TplObserver)
struct _TplObserverPriv
{
    /* mapping channel_path->TplChannel instances */
    GHashTable *channel_map;
    TplLogManager *logmanager;
    gboolean  dbus_registered;
    TplChannelFactory channel_factory;
};

typedef struct
{
  guint chan_n;
  DBusGMethodInvocation *dbus_ctx;
} ObservingContext;

static TplObserver *observer_singleton = NULL;

static const char *client_interfaces[] = {
  TP_IFACE_CLIENT_OBSERVER,
  NULL
};

enum
{
  PROP_0,
  PROP_INTERFACES,
  PROP_CHANNEL_FILTER,
  PROP_REGISTERED_CHANNELS
};

G_DEFINE_TYPE_WITH_CODE (TplObserver, tpl_observer, G_TYPE_OBJECT,
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_DBUS_PROPERTIES,
      tp_dbus_properties_mixin_iface_init);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT, NULL);
    G_IMPLEMENT_INTERFACE (TP_TYPE_SVC_CLIENT_OBSERVER, observer_iface_init);
    );


static void
tpl_observer_observe_channels (TpSvcClientObserver *self,
    const char *account,
    const char *connection,
    const GPtrArray *channels,
    const char *dispatch_op,
    const GPtrArray *requests_satisfied,
    GHashTable *observer_info,
    DBusGMethodInvocation *dbus_context)
{
  TpAccount *tp_acc = NULL;
  TpConnection *tp_conn = NULL;
  TpDBusDaemon *tp_bus_daemon = NULL;
  TplChannelFactory chan_factory;
  TplConf *conf;
  GError *error = NULL;
  ObservingContext *observing_ctx = NULL;
  const gchar *chan_type;
  guint i;

  g_return_if_fail (!TPL_STR_EMPTY (account));
  g_return_if_fail (!TPL_STR_EMPTY (connection));

  if (dbus_context == NULL)
    DEBUG ("called during open channel inspection, not by the Channel "
        "Dispatcher. OK.");

  chan_factory = tpl_observer_get_channel_factory (TPL_OBSERVER (self));

  /* Check if logging if enabled globally and for the given account_path,
   * return imemdiatly if it's not */
  conf = tpl_conf_dup ();
  if (!tpl_conf_is_globally_enabled (conf, &error))
    {
      if (error != NULL)
        DEBUG ("%s", error->message);
      else
        DEBUG ("Logging is globally disabled. Skipping channel logging.");

      goto error;
    }
  if (tpl_conf_is_account_ignored (conf, account, &error))
    {
      DEBUG ("Logging is disabled for account %s. "
          "Channel associated to this account. "
          "Skipping this channel logging.", account);

      goto error;
    }
  g_object_unref (conf);

  /* Instantiating objects to pass to - or needed by them - the Tpl Channel
   * Factory in order to obtain a TplChannelXXX instance */
  tp_bus_daemon = tp_dbus_daemon_dup (&error);
  if (tp_bus_daemon == NULL)
    {
      DEBUG ("Failed to acquire bus daemon: %s", error->message);
      goto error;
    }

  tp_acc = tp_account_new (tp_bus_daemon, account, &error);
  if (tp_acc == NULL)
    {
      DEBUG ("Failed to acquire account proxy for %s: %s", account,
          error->message);
      goto error;
    }

  tp_conn = tp_connection_new (tp_bus_daemon, NULL, connection, &error);
  if (tp_conn == NULL)
    {
      DEBUG ("Failed to acquire connection proxy for %s: %s", connection,
          error->message);
      goto error;
    }

  /* Parallelize TplChannel preparations, when the last one will be ready, the
   * counter will be 0 and tp_svc_client_observer_return_from_observe_channels
   * can be called */
  observing_ctx = g_slice_new0 (ObservingContext);
  observing_ctx->chan_n = channels->len;
  observing_ctx->dbus_ctx = dbus_context;

  /* channels is of type a(oa{sv}) */
  for (i = 0; i < channels->len; i++)
    {
      GValueArray *channel = g_ptr_array_index (channels, i);
      TplChannel *tpl_chan;
      GHashTable *prop_map;
      gchar *path;

      path = g_value_get_boxed (g_value_array_get_nth (channel, 0));
      /* d.bus.propertyName.str/gvalue hash */
      prop_map = g_value_get_boxed (g_value_array_get_nth (channel, 1));

      chan_type = tp_asv_get_string (prop_map, TP_PROP_CHANNEL_CHANNEL_TYPE);
      tpl_chan = chan_factory (chan_type, tp_conn, path, prop_map, tp_acc,
          &error);
      if (tpl_chan == NULL)
        {
          DEBUG ("%s", error->message);
          g_clear_error (&error);
          continue;
        }
      PATH_DEBUG (tpl_chan, "Starting preparation fo TplChannel instance");
      tpl_channel_call_when_ready (tpl_chan, got_tpl_channel_text_ready_cb,
          observing_ctx);
    }

  g_object_unref (tp_acc);
  g_object_unref (tp_conn);
  g_object_unref (tp_bus_daemon);

  return;

error:
  if (tp_acc != NULL)
    g_object_unref (tp_acc);
  if (tp_conn != NULL)
    g_object_unref (tp_conn);
  if (tp_bus_daemon != NULL)
    g_object_unref (tp_bus_daemon);
  g_clear_error (&error);

  /* observer_channels has been called by the Channel Dispatcher */
  if (dbus_context != NULL)
    {
      DEBUG ("Returning from observe channels on error condition. "
          "Unable to log the channel");
      tp_svc_client_observer_return_from_observe_channels (dbus_context);
    }
}


static void
got_tpl_channel_text_ready_cb (GObject *obj,
    GAsyncResult *result,
    gpointer user_data)
{
  ObservingContext *observing_ctx = user_data;
  DBusGMethodInvocation *dbus_ctx = observing_ctx->dbus_ctx;

  observing_ctx->chan_n -= 1;
  if (observing_ctx->chan_n == 0)
    {
      if (dbus_ctx != NULL)
        {
          /* observer_channels has been called by the Channel Dispatcher */
          DEBUG ("Returning from observe channels");
          tp_svc_client_observer_return_from_observe_channels (dbus_ctx);
        }
      g_slice_free (ObservingContext, observing_ctx);
    }
}


static void
tpl_observer_get_property (GObject *self,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  GPtrArray *array;
  GList *key_list;
  GHashTable *map;

  switch (property_id)
    {
      case PROP_INTERFACES:
        g_value_set_boxed (value, client_interfaces);
        break;

      case PROP_CHANNEL_FILTER:
        /* create an empty filter - which means all channels */
        array = g_ptr_array_new ();
        map = g_hash_table_new (NULL, NULL);
        g_ptr_array_add (array, map);
        g_value_set_boxed (value, array);
        break;

      case PROP_REGISTERED_CHANNELS:
        array = g_ptr_array_new ();
        key_list = g_hash_table_get_keys (tpl_observer_get_channel_map (
              TPL_OBSERVER (self)));
        g_ptr_array_add (array, key_list);
        g_value_set_boxed (value, array);
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (self, property_id, pspec);
        break;
    }
}

/* Singleton Constructor */
static GObject *
tpl_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 (tpl_observer_parent_class)->constructor (type,
          n_props, props);

      observer_singleton = TPL_OBSERVER (retval);
      g_object_add_weak_pointer (retval, (gpointer *) & observer_singleton);
    }

  return retval;
}


static void
tpl_observer_class_init (TplObserverClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructor = tpl_observer_constructor;
  object_class->finalize = tpl_observer_finalize;
  object_class->dispose = tpl_observer_dispose;
  object_class->get_property = tpl_observer_get_property;

  /* D-Bus properties are exposed as GObject properties through the
   * TpDBusPropertiesMixin properties on the Client interface */
  static TpDBusPropertiesMixinPropImpl client_props[] = {
    {"Interfaces", "interfaces", NULL},
    {NULL}
  };

  /* properties on the Client.Observer interface */
  static TpDBusPropertiesMixinPropImpl client_observer_props[] = {
    {"ObserverChannelFilter", "channel-filter", NULL},
    {NULL}
  };

  /* complete list of interfaces with properties */
  static TpDBusPropertiesMixinIfaceImpl prop_interfaces[] = {
    {TP_IFACE_CLIENT,
     tp_dbus_properties_mixin_getter_gobject_properties,
     NULL,
     client_props},
    {TP_IFACE_CLIENT_OBSERVER,
     tp_dbus_properties_mixin_getter_gobject_properties,
     NULL,
     client_observer_props},
    {NULL}
  };

  /**
   * TplObserver:interfaces:
   *
   * Interfaces implemented by this object.
   */
  g_object_class_install_property (object_class, PROP_INTERFACES,
      g_param_spec_boxed ("interfaces", "Interfaces",
        "Available D-Bus Interfaces", G_TYPE_STRV, G_PARAM_READABLE |
        G_PARAM_STATIC_STRINGS));

  /**
   * TplObserver:channel-filter:
   *
   * Channels that this object will accept and manage from the Channel
   * Dispatcher
   */
  g_object_class_install_property (object_class, PROP_CHANNEL_FILTER,
      g_param_spec_boxed ("channel-filter",
        "Channel Filter",
        "Filter for channels we observe",
        TP_ARRAY_TYPE_CHANNEL_CLASS_LIST, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  /**
   * TplObserver:registered-channels:
   *
   * A list of channel's paths currently registered to this object.
   *
   * One can receive change notifications on this property by connecting
   * to the #GObject::notify signal and using this property as the signal
   * detail.
   */
  g_object_class_install_property (object_class, PROP_REGISTERED_CHANNELS,
      g_param_spec_boxed ("registered-channels",
        "Registered Channels",
        "open TpChannels which the TplObserver is logging",
        TP_ARRAY_TYPE_CHANNEL_CLASS_LIST, G_PARAM_READABLE |
        G_PARAM_STATIC_STRINGS));

  /* call our mixin class init */
  klass->dbus_props_class.interfaces = prop_interfaces;
  tp_dbus_properties_mixin_class_init (object_class, G_STRUCT_OFFSET (
      TplObserverClass, dbus_props_class));

  g_type_class_add_private (object_class, sizeof (TplObserverPriv));
}


static void
tpl_observer_init (TplObserver *self)
{
  TplObserverPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPL_TYPE_OBSERVER, TplObserverPriv);
  self->priv = priv;

  priv->channel_map = g_hash_table_new_full (g_str_hash,
      g_str_equal, g_free, g_object_unref);
  priv->logmanager = tpl_log_manager_dup_singleton ();

  tpl_observer_get_open_channels ();
}


/**
 * tpl_observer_register_dbus:
 * @self: #TplObserver instance, cannot be %NULL.
 * @error: Used to raise an error if DBus registration fails
 *
 * Registers the object using #TPL_OBSERVER_WELL_KNOWN_BUS_NAME well known
 * name.
 *
 * Returns: %TRUE if the registration is successful, %FALSE with @error set if
 * it fails.
 */
gboolean
tpl_observer_register_dbus (TplObserver *self,
    GError **error)
{
  TpDBusDaemon *tp_bus;
  gboolean ret = TRUE;

  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
  g_return_val_if_fail (TPL_IS_OBSERVER (self), FALSE);

  tp_bus = tp_dbus_daemon_dup (error);
  if (tp_bus == NULL)
    {
      ret = FALSE;
      goto out;
    }

  if (!tp_dbus_daemon_request_name (tp_bus, TPL_OBSERVER_WELL_KNOWN_BUS_NAME,
        FALSE, error))
    {
      ret = FALSE;
      goto out;
    }

  dbus_g_connection_register_g_object (
      tp_proxy_get_dbus_connection (TP_PROXY (tp_bus)),
      TPL_OBSERVER_OBJECT_PATH, G_OBJECT (self));
out:
  if (tp_bus != NULL)
    g_object_unref (tp_bus);
  return ret;
}


static void
observer_iface_init (gpointer g_iface,
    gpointer iface_data)
{
  TpSvcClientObserverClass *klass = (TpSvcClientObserverClass *) g_iface;

  tp_svc_client_observer_implement_observe_channels (klass,
      tpl_observer_observe_channels);
}


static void
tpl_observer_dispose (GObject *obj)
{
  TplObserverPriv *priv = GET_PRIV (obj);

  if (priv->channel_map != NULL)
    {
      g_hash_table_unref (priv->channel_map);
      priv->channel_map = NULL;
    }
  if (priv->logmanager != NULL)
    {
      g_object_unref (priv->logmanager);
      priv->logmanager = NULL;
    }

  G_OBJECT_CLASS (tpl_observer_parent_class)->dispose (obj);
}


static void
tpl_observer_finalize (GObject *obj)
{
  G_OBJECT_CLASS (tpl_observer_parent_class)->finalize (obj);
}


TplObserver *
tpl_observer_new (void)
{
  return g_object_new (TPL_TYPE_OBSERVER, NULL);
}


static GHashTable *
tpl_observer_get_channel_map (TplObserver *self)
{
  g_return_val_if_fail (TPL_IS_OBSERVER (self), NULL);

  return GET_PRIV (self)->channel_map;
}


gboolean
tpl_observer_register_channel (TplObserver *self,
    TplChannel *channel)
{
  GHashTable *glob_map = tpl_observer_get_channel_map (self);
  gchar *key;

  g_return_val_if_fail (TPL_IS_OBSERVER (self), FALSE);
  g_return_val_if_fail (TPL_IS_CHANNEL (channel), FALSE);
  g_return_val_if_fail (glob_map != NULL, FALSE);

  /* 'key' will be freed by the hash table on key removal/destruction */
  key = g_strdup (tp_proxy_get_object_path (G_OBJECT (channel)));

  if (g_hash_table_lookup (glob_map, key) != NULL)
    {
      DEBUG ("Channel path found, replacing %s", key);
      g_hash_table_replace (glob_map, key, channel);
    }
  else
    {
      DEBUG ("Channel path not found, registering %s", key);
      g_hash_table_insert (glob_map, key, channel);
    }
  g_object_notify (G_OBJECT (self), "registered-channels");

  g_object_unref (channel);

  return TRUE;
}


/**
 * tpl_observer_unregister_channel:
 * @self: #TplObserver instance, cannot be %NULL.
 * @channel: a #TplChannel cast of a TplChannel subclass instance
 *
 * Un-registers a TplChannel subclass instance, i.e. TplChannelText instance,
 * as TplChannel instance.
 * It is supposed to be called when the Closed signal for a channel is
 * emitted or when an un-recoverable error during the life or a TplChannel
 * happens.
 *
 * Every time that a channel is registered or unregistered, a notification is
 * sent for the 'registered-channels' property.
 *
 * Returns: %TRUE if @channel is registered and can thus be un-registered or
 * %FALSE if the @channel is not currently among registered channels and thus
 * cannot be un-registered.
 */
gboolean
tpl_observer_unregister_channel (TplObserver *self,
    TplChannel *channel)
{
  GHashTable *glob_map = tpl_observer_get_channel_map (self);
  gboolean retval;
  gchar *key;

  g_return_val_if_fail (TPL_IS_OBSERVER (self), FALSE);
  g_return_val_if_fail (TPL_IS_CHANNEL (channel), FALSE);
  g_return_val_if_fail (glob_map != NULL, FALSE);

  key = g_strdup (tp_proxy_get_object_path (TP_PROXY (channel)));

  DEBUG ("Unregistering channel path %s", key);

  /* this will destroy the associated value object: at this point
     the hash table reference should be the only one for the
     value's object
   */
  retval = g_hash_table_remove (glob_map, key);
  if (retval)
    g_object_notify (G_OBJECT (self), "registered-channels");
  g_free (key);
  return retval;
}


static TplChannelFactory
tpl_observer_get_channel_factory (TplObserver *self)
{
  g_return_val_if_fail (TPL_IS_OBSERVER (self), NULL);

  return GET_PRIV (self)->channel_factory;
}


void
tpl_observer_set_channel_factory (TplObserver *self,
    TplChannelFactory factory)
{
  TplObserverPriv *priv = GET_PRIV (self);

  g_return_if_fail (factory != NULL);
  g_return_if_fail (priv->channel_factory == NULL);
  g_return_if_fail (TPL_IS_OBSERVER (self));

  priv->channel_factory = factory;
}


static void
tpl_observer_got_channel_list_cb (TpProxy *proxy,
    const GValue *value,
    const GError *error,
    gpointer user_data,
    GObject *weak_object)
{
  TpAccount *account = TP_ACCOUNT (user_data);
  TpConnection *conn = TP_CONNECTION (proxy);
  TplObserver *observer = tpl_observer_new ();
  GPtrArray *channels;

  g_return_if_fail (TP_IS_CONNECTION (conn));
  g_return_if_fail (TP_IS_ACCOUNT (account));

  if (error != NULL)
    {
      DEBUG ("unable to retrieve channels for connection %s: %s",
          tp_proxy_get_object_path (TP_PROXY (conn)),
          error->message);
      return;
    }

  if (!G_VALUE_HOLDS (value, TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST))
    {
      g_critical ("channel list GValue does not hold "
          "TP_ARRAY_TYPE_CHANNEL_DETAILS_LIST");
      return;
    }

  channels = g_value_get_boxed (value);

  /* call observe_channels with
   * Dispatch_Operation = NULL, Requests_Satisfied = NULL,
   * Observer_Info = NULL and DBusGMethodInvocation = NULL
   * so that it will undertand that it's not been called by a Channel
   * Dispatcher */
  tpl_observer_observe_channels (TP_SVC_CLIENT_OBSERVER (observer),
      tp_proxy_get_object_path (TP_PROXY (account)),
      tp_proxy_get_object_path (TP_PROXY (conn)),
      channels, NULL, NULL, NULL, NULL);
}


static void
tpl_observer_get_open_channels_prepared_connection (TpConnection *conn,
    const GError *error,
    gpointer user_data)
{
  TpAccount *account = TP_ACCOUNT (user_data);

  g_return_if_fail (TP_IS_CONNECTION (conn));
  g_return_if_fail (TP_IS_ACCOUNT (account));

  if (error != NULL)
    {
      DEBUG ("unable to prepare connection for open channel retrieval: %s",
          error->message);
      goto out;
    }

  /* I do not pass the observer as a weak_object or I will leak some
   * references to account and connection in the callback in case is destroyed
   * and the call canceled */
  tp_cli_dbus_properties_call_get (conn, -1,
      TP_IFACE_CONNECTION_INTERFACE_REQUESTS, "Channels",
      tpl_observer_got_channel_list_cb,
      g_object_ref (account), g_object_unref, NULL);

out:
  g_object_unref (account);
}


static void
tpl_observer_get_open_channels_prepare_account_cb (GObject *proxy,
    GAsyncResult *result,
    gpointer user_data)
{
  TpAccount *account = TP_ACCOUNT (proxy);
  TpConnection *conn;
  GError *error = NULL;

  g_return_if_fail (TP_IS_ACCOUNT (account));

  if (!tp_account_prepare_finish (account, result, &error))
    {
      DEBUG ("unable to prapare account: %s", error->message);
      g_error_free (error);
    }

  if (!tp_account_is_enabled (account))
    return;

  conn = tp_account_get_connection (account);

  /* check if account's connection is offline */
  if (conn == NULL)
    return;

  /* ref here, unref in callbacks on error or at the end of the chain */
  tp_connection_call_when_ready (conn,
      tpl_observer_get_open_channels_prepared_connection,
      g_object_ref (account));
}


static void
tpl_observer_prepared_account_manager_cb (GObject *obj,
    GAsyncResult *result,
    gpointer user_data)
{
  TpAccountManager *am = TP_ACCOUNT_MANAGER (obj);
  GList *list, *l;
  GError *error = NULL;

  if (!tp_account_manager_prepare_finish (TP_ACCOUNT_MANAGER (obj), result,
        &error))
    {
      DEBUG ("Unable to prepare connection manager: %s", error->message);
      return;
    }

  /* accountspointed by the list are not referenced */
  list = tp_account_manager_get_valid_accounts (am);

  for (l = list; l != NULL; l = g_list_next (l))
    {
      TpAccount *account = l->data;

      g_assert (TP_IS_ACCOUNT (account));

      tp_account_prepare_async (account, NULL,
          tpl_observer_get_open_channels_prepare_account_cb, NULL);
    }

  g_list_free (list);
}


/* Retrieving open channel */
/* This part can be removed when the Channel Dispatcher will implement a
 * proper API for
 * org.freedesktop.Telepathy.Connection.Interface.Requests.Channels */
static void
tpl_observer_get_open_channels (void)
{
  TpAccountManager *am = tp_account_manager_dup ();

  tp_account_manager_prepare_async (am, NULL,
      tpl_observer_prepared_account_manager_cb, NULL);

  g_object_unref (am);
}