empathy-individual-store-channel.c 10.5 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
/*
 * Copyright (C) 2005-2007 Imendio AB
 * Copyright (C) 2007-2011 Collabora Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301  USA
 *
 * Authors: Mikael Hallendal <micke@imendio.com>
 *          Martyn Russell <martyn@imendio.com>
 *          Xavier Claessens <xclaesse@gmail.com>
 *          Travis Reitter <travis.reitter@collabora.co.uk>
 */

#include "config.h"
27
#include "empathy-individual-store-channel.h"
28

29 30
#include <tp-account-widgets/tpaw-pixbuf-utils.h>

31
#include "empathy-utils.h"
32 33
#include "empathy-ui-utils.h"
#include "empathy-images.h"
34 35

#define DEBUG_FLAG EMPATHY_DEBUG_CONTACT
36
#include "empathy-debug.h"
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

struct _EmpathyIndividualStoreChannelPriv
{
  TpChannel *channel;

  /* TpContact => FolksIndividual
   * We keep the individuals we have added to the store so can easily remove
   * them when their TpContact leaves the channel. */
  GHashTable *individuals;
};

enum
{
  PROP_0,
  PROP_CHANNEL,
};


G_DEFINE_TYPE (EmpathyIndividualStoreChannel, empathy_individual_store_channel,
    EMPATHY_TYPE_INDIVIDUAL_STORE);

static void
add_members (EmpathyIndividualStoreChannel *self,
    GPtrArray *members)
{
  EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
  guint i;

  for (i = 0; i < members->len; i++)
    {
      TpContact *contact = g_ptr_array_index (members, i);
      FolksIndividual *individual;

      if (g_hash_table_lookup (self->priv->individuals, contact) != NULL)
        continue;

73
      individual = empathy_ensure_individual_from_tp_contact (contact);
74 75
      if (individual == NULL)
        return;
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

      DEBUG ("%s joined channel %s", tp_contact_get_identifier (contact),
          tp_proxy_get_object_path (self->priv->channel));

      individual_store_add_individual_and_connect (store, individual);

      /* Pass the individual reference to the hash table */
      g_hash_table_insert (self->priv->individuals, g_object_ref (contact),
          individual);
    }
}

static void
remove_members (EmpathyIndividualStoreChannel *self,
    GPtrArray *members)
{
  EmpathyIndividualStore *store = (EmpathyIndividualStore *) self;
  guint i;

  for (i = 0; i < members->len; i++)
    {
      TpContact *contact = g_ptr_array_index (members, i);
      FolksIndividual *individual;

      individual = g_hash_table_lookup (self->priv->individuals, contact);
      if (individual == NULL)
        continue;

      DEBUG ("%s left channel %s", tp_contact_get_identifier (contact),
          tp_proxy_get_object_path (self->priv->channel));

      individual_store_remove_individual_and_disconnect (store, individual);

      g_hash_table_remove (self->priv->individuals, contact);
    }
}

static void
group_contacts_changed_cb (TpChannel *channel,
    GPtrArray *added,
    GPtrArray *removed,
    GPtrArray *local_pending,
    GPtrArray *remote_pending,
    TpContact *actor,
    GHashTable *details,
    gpointer user_data)
{
  EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
      user_data);

  remove_members (self, removed);
  add_members (self, added);
}

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
static void
individual_store_channel_contact_chat_state_changed (TpTextChannel *channel,
    TpContact *tp_contact,
    TpChannelChatState state,
    EmpathyIndividualStoreChannel *self)
{
  FolksIndividual *individual;
  EmpathyContact *contact = NULL;
  GList *iters, *l;
  GdkPixbuf *pixbuf;

  contact = empathy_contact_dup_from_tp_contact (tp_contact);
  if (empathy_contact_is_user (contact))
    {
      /* We don't care about our own chat composing states */
      goto finally;
    }

  DEBUG ("Contact %s entered chat state %d",
      tp_contact_get_identifier (tp_contact), state);

  individual = g_hash_table_lookup (self->priv->individuals, tp_contact);
  if (individual == NULL)
    {
      g_warning ("individual is NULL");
      goto finally;
    }

  iters = empathy_individual_store_find_contact (
      EMPATHY_INDIVIDUAL_STORE (self), individual);

  if (state == TP_CHANNEL_CHAT_STATE_COMPOSING)
    {
      gchar *icon_filename =
164
          tpaw_filename_from_icon_name (EMPATHY_IMAGE_TYPING,
165 166 167 168 169 170 171 172 173
            GTK_ICON_SIZE_MENU);

      pixbuf = gdk_pixbuf_new_from_file (icon_filename, NULL);
      g_free (icon_filename);
    }
  else
    {
       pixbuf = empathy_individual_store_get_individual_status_icon (
           EMPATHY_INDIVIDUAL_STORE (self), individual);
174 175 176

       /* Take a ref as the 'if' blocks creates a new pixbuf */
       g_object_ref (pixbuf);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    }

  for (l = iters; l != NULL; l = l->next)
    {
      gtk_tree_store_set (GTK_TREE_STORE (self), l->data,
          EMPATHY_INDIVIDUAL_STORE_COL_ICON_STATUS, pixbuf,
          -1);
    }
  /* Store takes it's own ref */
  g_object_unref (pixbuf);

  empathy_individual_store_free_iters (iters);

  finally:
    g_object_unref (contact);
}

194
static void
195 196 197
individual_store_channel_set_individual_channel (
    EmpathyIndividualStoreChannel *self,
    TpChannel *channel)
198 199 200
{
  GPtrArray *members;

201 202
  g_assert (self->priv->channel == NULL); /* construct only */
  self->priv->channel = g_object_ref (channel);
203 204 205 206 207 208 209 210 211 212 213

  /* Add initial members */
  members = tp_channel_group_dup_members_contacts (channel);
  if (members != NULL)
    {
      add_members (self, members);
      g_ptr_array_unref (members);
    }

  tp_g_signal_connect_object (channel, "group-contacts-changed",
      G_CALLBACK (group_contacts_changed_cb), self, 0);
214 215 216 217

  tp_g_signal_connect_object (channel, "contact-chat-state-changed",
      G_CALLBACK (individual_store_channel_contact_chat_state_changed),
      self, 0);
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
}

static void
individual_store_channel_dispose (GObject *object)
{
  EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
      object);
  EmpathyIndividualStore *store = EMPATHY_INDIVIDUAL_STORE (object);
  GHashTableIter iter;
  gpointer v;

  g_hash_table_iter_init (&iter, self->priv->individuals);
  while (g_hash_table_iter_next (&iter, NULL, &v))
    {
      FolksIndividual *individual = v;

      empathy_individual_store_disconnect_individual (store, individual);
    }

  tp_clear_pointer (&self->priv->individuals, g_hash_table_unref);
  g_clear_object (&self->priv->channel);

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

static void
individual_store_channel_get_property (GObject *object,
    guint param_id,
    GValue *value,
    GParamSpec *pspec)
{
  EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
      object);

  switch (param_id)
    {
    case PROP_CHANNEL:
      g_value_set_object (value, self->priv->channel);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    };
}

static void
individual_store_channel_set_property (GObject *object,
    guint param_id,
    const GValue *value,
    GParamSpec *pspec)
{
  switch (param_id)
    {
    case PROP_CHANNEL:
      individual_store_channel_set_individual_channel (
          EMPATHY_INDIVIDUAL_STORE_CHANNEL (object),
          g_value_get_object (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
      break;
    };
}

static void
individual_store_channel_reload_individuals (EmpathyIndividualStore *store)
{
  EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
      store);
  GPtrArray *members;
  GList *list, *l;

  /* remove all. The list returned by g_hash_table_get_keys() is valid until
   * the hash table is modified so we can't remove the contact directly in the
   * iteration. */
  members = g_ptr_array_new_with_free_func (g_object_unref);

  list = g_hash_table_get_keys (self->priv->individuals);
  for (l = list; l != NULL; l = g_list_next (l))
    {
      g_ptr_array_add (members, g_object_ref (l->data));
    }

  remove_members (self, members);

  g_list_free (list);
  g_ptr_array_unref (members);

  /* re-add members */
  members = tp_channel_group_dup_members_contacts (self->priv->channel);
  if (members == NULL)
    return;

  add_members (self, members);
  g_ptr_array_unref (members);
}

316 317 318 319 320 321 322 323 324 325
static gboolean
individual_store_channel_initial_loading (EmpathyIndividualStore *store)
{
  EmpathyIndividualStoreChannel *self = EMPATHY_INDIVIDUAL_STORE_CHANNEL (
      store);

  return !tp_proxy_is_prepared (self->priv->channel,
      TP_CHANNEL_FEATURE_CONTACTS);
}

326 327 328 329 330 331 332 333 334 335 336 337 338
static void
empathy_individual_store_channel_class_init (
    EmpathyIndividualStoreChannelClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  EmpathyIndividualStoreClass *store_class = EMPATHY_INDIVIDUAL_STORE_CLASS (
      klass);

  object_class->dispose = individual_store_channel_dispose;
  object_class->get_property = individual_store_channel_get_property;
  object_class->set_property = individual_store_channel_set_property;

  store_class->reload_individuals = individual_store_channel_reload_individuals;
339
  store_class->initial_loading = individual_store_channel_initial_loading;
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

  g_object_class_install_property (object_class,
      PROP_CHANNEL,
      g_param_spec_object ("individual-channel",
          "Individual channel",
          "Individual channel",
          TP_TYPE_CHANNEL,
          G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));

  g_type_class_add_private (object_class,
      sizeof (EmpathyIndividualStoreChannelPriv));
}

static void
empathy_individual_store_channel_init (EmpathyIndividualStoreChannel *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL, EmpathyIndividualStoreChannelPriv);

  self->priv->individuals = g_hash_table_new_full (NULL, NULL, g_object_unref,
      g_object_unref);
}

EmpathyIndividualStoreChannel *
empathy_individual_store_channel_new (TpChannel *channel)
{
  g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);

  return g_object_new (EMPATHY_TYPE_INDIVIDUAL_STORE_CHANNEL,
      "individual-channel", channel, NULL);
}

TpChannel *
empathy_individual_store_channel_get_channel (
    EmpathyIndividualStoreChannel *self)
{
  g_return_val_if_fail (EMPATHY_IS_INDIVIDUAL_STORE_CHANNEL (self), FALSE);

  return self->priv->channel;
}