empathy-connection-aggregator.c 7.14 KB
Newer Older
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
/*
 * empathy-connection-aggregator.c - Source for EmpathyConnectionAggregator
 * Copyright (C) 2010 Collabora Ltd.
 * @author Cosimo Cecchi <cosimo.cecchi@collabora.co.uk>
 *
 * 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
 */

#include <config.h>

#include "empathy-connection-aggregator.h"

#include <telepathy-glib/telepathy-glib.h>

#define DEBUG_FLAG EMPATHY_DEBUG_OTHER
#include "empathy-debug.h"
#include "empathy-utils.h"


#include "extensions/extensions.h"

34
G_DEFINE_TYPE (EmpathyConnectionAggregator, empathy_connection_aggregator,
35 36
    G_TYPE_OBJECT);

37 38 39 40 41 42 43
enum {
  EVENT_CONTACT_LIST_CHANGED,
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];

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
struct _EmpathyConnectionAggregatorPriv {
  TpAccountManager *mgr;

  /* List of owned TpConnection */
  GList *conns;
};

static void
empathy_connection_aggregator_dispose (GObject *object)
{
  EmpathyConnectionAggregator *self = (EmpathyConnectionAggregator *) object;

  g_clear_object (&self->priv->mgr);

  g_list_free_full (self->priv->conns, g_object_unref);
  self->priv->conns = NULL;

  G_OBJECT_CLASS (empathy_connection_aggregator_parent_class)->dispose (object);
}

static void
empathy_connection_aggregator_class_init (
    EmpathyConnectionAggregatorClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);

  oclass->dispose = empathy_connection_aggregator_dispose;

72 73 74 75 76 77 78 79 80 81
  signals[EVENT_CONTACT_LIST_CHANGED] =
    g_signal_new ("contact-list-changed",
      G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST,
      0,
      NULL, NULL,
      g_cclosure_marshal_generic,
      G_TYPE_NONE,
      2, G_TYPE_PTR_ARRAY, G_TYPE_PTR_ARRAY);

82 83 84
  g_type_class_add_private (klass, sizeof (EmpathyConnectionAggregatorPriv));
}

85 86 87 88 89 90 91 92 93
static void
contact_list_changed_cb (TpConnection *conn,
    GPtrArray *added,
    GPtrArray *removed,
    EmpathyConnectionAggregator *self)
{
  g_signal_emit (self, signals[EVENT_CONTACT_LIST_CHANGED], 0, added, removed);
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void
conn_invalidated_cb (TpConnection *conn,
    guint domain,
    gint code,
    gchar *message,
    EmpathyConnectionAggregator *self)
{
  self->priv->conns = g_list_remove (self->priv->conns, conn);

  g_object_unref (conn);
}

static void
check_connection (EmpathyConnectionAggregator *self,
    TpConnection *conn)
{
110 111
  GPtrArray *contacts;

112 113 114 115 116 117
  if (g_list_find (self->priv->conns, conn) != NULL)
    return;

  self->priv->conns = g_list_prepend (self->priv->conns,
      g_object_ref (conn));

118 119 120 121 122 123 124 125 126 127 128 129 130 131
  tp_g_signal_connect_object (conn, "contact-list-changed",
      G_CALLBACK (contact_list_changed_cb), self, 0);

  contacts = tp_connection_dup_contact_list (conn);
  if (contacts != NULL)
    {
      GPtrArray *empty;

      empty = g_ptr_array_new ();

      contact_list_changed_cb (conn, contacts, empty, self);
      g_ptr_array_unref (empty);
    }

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
  tp_g_signal_connect_object (conn, "invalidated",
      G_CALLBACK (conn_invalidated_cb), self, 0);
}

static void
check_account (EmpathyConnectionAggregator *self,
    TpAccount *account)
{
  TpConnection *conn;

  conn = tp_account_get_connection (account);
  if (conn != NULL)
    check_connection (self, conn);
}

static void
account_conn_changed_cb (TpAccount *account,
    GParamSpec *spec,
    EmpathyConnectionAggregator *self)
{
  check_account (self, account);
}

static void
add_account (EmpathyConnectionAggregator *self,
    TpAccount *account)
{
  check_account (self, account);

  tp_g_signal_connect_object (account, "notify::connection",
      G_CALLBACK (account_conn_changed_cb), self, 0);
}

static void
account_validity_changed_cb (TpAccountManager *manager,
    TpAccount *account,
    gboolean valid,
    EmpathyConnectionAggregator *self)
{
  if (valid)
    add_account (self, account);
}

static void
am_prepare_cb (GObject *source,
    GAsyncResult *result,
    gpointer user_data)
{
  EmpathyConnectionAggregator *self = EMPATHY_CONNECTION_AGGREGATOR (user_data);
  GError *error = NULL;
  GList *accounts, *l;

  if (!tp_proxy_prepare_finish (source, result, &error))
    {
      DEBUG ("Failed to prepare account manager: %s", error->message);
      g_error_free (error);
      goto out;
    }

  accounts = tp_account_manager_get_valid_accounts (self->priv->mgr);
  for (l = accounts; l != NULL; l = g_list_next (l))
    {
      TpAccount *account = l->data;

      add_account (self, account);
    }

  tp_g_signal_connect_object (self->priv->mgr, "account-validity-changed",
      G_CALLBACK (account_validity_changed_cb), self, 0);

  g_list_free (accounts);

out:
  g_object_unref (self);
}

static void
empathy_connection_aggregator_init (EmpathyConnectionAggregator *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_CONNECTION_AGGREGATOR, EmpathyConnectionAggregatorPriv);

  self->priv->mgr = tp_account_manager_dup ();

  tp_proxy_prepare_async (self->priv->mgr, NULL, am_prepare_cb,
      g_object_ref (self));
}

EmpathyConnectionAggregator *
empathy_connection_aggregator_dup_singleton (void)
{
  static EmpathyConnectionAggregator *aggregator = NULL;

  if (G_LIKELY (aggregator != NULL))
      return g_object_ref (aggregator);

  aggregator = g_object_new (EMPATHY_TYPE_CONNECTION_AGGREGATOR, NULL);

  g_object_add_weak_pointer (G_OBJECT (aggregator), (gpointer *) &aggregator);
  return aggregator;
}
233

234
/* (transfer container) */
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
GList *
empathy_connection_aggregator_get_all_groups (EmpathyConnectionAggregator *self)
{
  GList *keys, *l;
  GHashTable *set;

  set = g_hash_table_new (g_str_hash, g_str_equal);

  for (l = self->priv->conns; l != NULL; l = g_list_next (l))
    {
      TpConnection *conn = l->data;
      const gchar * const *groups;
      guint i;

      groups = tp_connection_get_contact_groups (conn);
      if (groups == NULL)
        continue;

      for (i = 0; groups[i] != NULL; i++)
        g_hash_table_insert (set, (gchar *) groups[i], GUINT_TO_POINTER (TRUE));
    }

  keys = g_hash_table_get_keys (set);
  g_hash_table_unref (set);

  return keys;
}
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

GPtrArray *
empathy_connection_aggregator_dup_all_contacts (
    EmpathyConnectionAggregator *self)
{
  GPtrArray *result;
  GList *l;

  result = g_ptr_array_new_with_free_func (g_object_unref);

  for (l = self->priv->conns; l != NULL; l = g_list_next (l))
    {
      TpConnection *conn = l->data;
      GPtrArray *contacts;

      contacts = tp_connection_dup_contact_list (conn);
      if (contacts == NULL)
        continue;

      tp_g_ptr_array_extend (result, contacts);

      /* tp_g_ptr_array_extend() doesn't give us an extra ref */
      g_ptr_array_foreach (contacts, (GFunc) g_object_ref, NULL);

      g_ptr_array_unref (contacts);
    }

  return result;
}