empathy-contact-selector.c 13.7 KB
Newer Older
1
/*
2
*  Copyright (C) 2007 Marco Barisione <marco@barisione.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
*  Copyright (C) 2008 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
*
Xavier Claessens's avatar
Xavier Claessens committed
19 20
*  Authors: Marco Barisione <marco@barisione.org>
*           Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21 22
*/

Xavier Claessens's avatar
Xavier Claessens committed
23 24
#include "config.h"

25
#include <glib/gi18n-lib.h>
26 27 28 29
#include <gtk/gtk.h>

#include <libempathy/empathy-contact.h>
#include <libempathy-gtk/empathy-contact-list-store.h>
30 31
#include <libempathy/empathy-utils.h>

32 33
#include "empathy-contact-selector.h"

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/**
 * SECTION:empathy-contact-selector
 * @title:EmpathyContactSelector
 * @short_description: A widget used to choose from a list of contacts.
 * @include: libempathy-gtk/empathy-contact-selector.h
 *
 * #EmpathyContactSelector is a widget which extends #GtkComboBox to provide
 * a chooser of available contacts.
 */

/**
 * EmpathyContactSelector:
 * @parent: parent object
 *
 * Widget which extends #GtkComboBox to provide a chooser of available contacts.
 */

51 52 53 54 55 56
G_DEFINE_TYPE (EmpathyContactSelector, empathy_contact_selector,
    GTK_TYPE_COMBO_BOX)

enum
{
  PROP_0,
57
  PROP_CONTACT_LIST
58 59
};

60 61
#define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyContactSelector)
typedef struct
62
{
63
  EmpathyContactList *contact_list;
64
  EmpathyContactListStore *store;
65
  GtkTreeModel *model;
66
  gboolean dispose_run;
67
} EmpathyContactSelectorPriv;
68

69 70
static void contact_selector_manage_blank_contact (
    EmpathyContactSelector *selector);
71

72
static guint
73
contact_selector_get_number_online_contacts (GtkTreeModel *model)
74 75 76 77
{
  GtkTreeIter tmp_iter;
  gboolean is_online;
  guint number_online_contacts = 0;
Jonny Lamb's avatar
Jonny Lamb committed
78
  gboolean ok;
79

80 81
  for (ok = gtk_tree_model_get_iter_first (model, &tmp_iter);
      ok; ok = gtk_tree_model_iter_next (model, &tmp_iter))
82
    {
83
      gtk_tree_model_get (model,
Jonny Lamb's avatar
Jonny Lamb committed
84 85 86 87
          &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE,
          &is_online, -1);
      if (is_online)
        number_online_contacts++;
88 89 90 91 92
    }

  return number_online_contacts;
}

93
static gboolean
94
contact_selector_get_iter_for_blank_contact (GtkTreeStore *model,
95
                                             GtkTreeIter *blank_iter)
96 97 98 99
{
  GtkTreeIter tmp_iter;
  EmpathyContact *tmp_contact;
  gboolean is_present = FALSE;
Jonny Lamb's avatar
Jonny Lamb committed
100
  gboolean ok;
101

102 103
  for (ok = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &tmp_iter);
      ok; ok = gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &tmp_iter))
104
    {
105
      gtk_tree_model_get (GTK_TREE_MODEL (model),
Jonny Lamb's avatar
Jonny Lamb committed
106 107 108
          &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
          &tmp_contact, -1);
      if (tmp_contact == NULL)
109
        {
Jonny Lamb's avatar
Jonny Lamb committed
110 111 112 113
          *blank_iter = tmp_iter;
          is_present = TRUE;
          break;
        }
114
      g_object_unref (tmp_contact);
115 116 117 118 119
    }

  return is_present;
}

120
static void
121
contact_selector_add_blank_contact (EmpathyContactSelector *selector)
122 123
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
124
  GtkTreeIter blank_iter, iter;
125

126
  gtk_tree_store_insert_with_values (
127
      GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0,
128
      EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
129
      EMPATHY_CONTACT_LIST_STORE_COL_NAME, (_("Select a contact")),
130
      EMPATHY_CONTACT_LIST_STORE_COL_IS_ONLINE, FALSE, -1);
131 132 133 134 135

  /* look up blank_iter in the filter model */
  g_return_if_fail (gtk_tree_model_filter_convert_child_iter_to_iter (
      GTK_TREE_MODEL_FILTER (priv->model), &iter, &blank_iter));

136
  g_signal_handlers_block_by_func (selector,
137
      contact_selector_manage_blank_contact, selector);
138
  gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &iter);
139
  g_signal_handlers_unblock_by_func (selector,
140
      contact_selector_manage_blank_contact, selector);
141 142
}

143
static void
144
contact_selector_remove_blank_contact (EmpathyContactSelector *selector)
145 146 147 148
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  GtkTreeIter blank_iter;

149 150
  if (contact_selector_get_iter_for_blank_contact
      (GTK_TREE_STORE (priv->store), &blank_iter))
151
    gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
152 153
}

154
static void
155
contact_selector_manage_sensitivity (EmpathyContactSelector *selector)
156 157
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
158
  guint number_online_contacts;
Xavier Claessens's avatar
Xavier Claessens committed
159

160
  number_online_contacts = contact_selector_get_number_online_contacts (priv->model);
161

Xavier Claessens's avatar
Xavier Claessens committed
162
  if (number_online_contacts != 0)
163
    gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
164
  else
165
    gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
166 167 168
}

static void
169
contact_selector_manage_blank_contact (EmpathyContactSelector *selector)
170
{
171
  gboolean is_popup_shown;
172

173
  g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
174

175
  if (is_popup_shown)
176
    {
177
      contact_selector_remove_blank_contact (selector);
178 179
    }
  else
180
    {
181
      if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
182
        {
183
          contact_selector_add_blank_contact (selector);
184
        }
185 186
      else
        {
187
          contact_selector_remove_blank_contact (selector);
188
        }
189
    }
190

191
  contact_selector_manage_sensitivity (selector);
192 193
}

194
static GObject *
195 196 197
contact_selector_constructor (GType type,
                              guint n_construct_params,
                              GObjectConstructParam *construct_params)
198
{
199 200 201 202
  GObject *object;
  EmpathyContactSelector *contact_selector;
  EmpathyContactSelectorPriv *priv;
  GtkCellLayout *cell_layout;
203 204
  GtkCellRenderer *renderer;

205
  object = G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor
206 207 208 209 210
    (type, n_construct_params, construct_params);
  priv = GET_PRIV (object);
  contact_selector = EMPATHY_CONTACT_SELECTOR (object);
  cell_layout = GTK_CELL_LAYOUT (object);

211 212
  priv->store = empathy_contact_list_store_new (priv->contact_list);

213
  g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
214
      "show-offline", FALSE, "show-groups", FALSE, "show-protocols", FALSE,
215
      "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
216

217 218 219 220 221 222 223 224
  g_signal_connect_swapped (priv->store, "row-changed",
      G_CALLBACK (contact_selector_manage_sensitivity),
      contact_selector);
  g_signal_connect_swapped (contact_selector, "changed",
      G_CALLBACK (contact_selector_manage_blank_contact),
      contact_selector);
  g_signal_connect_swapped (contact_selector, "notify::popup-shown",
      G_CALLBACK (contact_selector_manage_blank_contact),
225
      contact_selector);
226

227 228 229
  priv->model = gtk_tree_model_filter_new (GTK_TREE_MODEL (priv->store), NULL);

  gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector), priv->model);
230 231 232
  gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);

  renderer = gtk_cell_renderer_pixbuf_new ();
233 234
  gtk_cell_layout_pack_start (cell_layout, renderer, FALSE);
  gtk_cell_layout_set_attributes (cell_layout, renderer,
235
      "pixbuf", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
236 237

  renderer = gtk_cell_renderer_text_new ();
238 239
  gtk_cell_layout_pack_start (cell_layout, renderer, TRUE);
  gtk_cell_layout_set_attributes (cell_layout, renderer,
240
      "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
241

242 243
  contact_selector_manage_blank_contact (contact_selector);
  contact_selector_manage_sensitivity (contact_selector);
244 245 246 247 248 249 250

  return object;
}

static void
empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
{
251 252 253 254 255
  EmpathyContactSelectorPriv *priv =
      G_TYPE_INSTANCE_GET_PRIVATE (empathy_contact_selector,
      EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv);

  empathy_contact_selector->priv = priv;
256 257

  priv->dispose_run = FALSE;
258 259 260
}

static void
261 262 263 264
contact_selector_set_property (GObject *object,
                               guint prop_id,
                               const GValue *value,
                               GParamSpec *pspec)
265
{
266
  EmpathyContactSelectorPriv *priv = GET_PRIV (object);
267 268 269

  switch (prop_id)
    {
270 271
      case PROP_CONTACT_LIST:
        priv->contact_list = g_value_dup_object (value);
272 273 274 275 276 277 278 279
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
280 281 282 283
contact_selector_get_property (GObject *object,
                               guint prop_id,
                               GValue *value,
                               GParamSpec *pspec)
284
{
285
  EmpathyContactSelectorPriv *priv = GET_PRIV (object);
286 287 288

  switch (prop_id)
    {
289 290
      case PROP_CONTACT_LIST:
        g_value_set_object (value, priv->contact_list);
291 292 293 294 295 296 297 298
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
299
contact_selector_dispose (GObject *object)
300
{
301 302 303
  EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (object);
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);

304 305 306 307 308
  if (priv->dispose_run)
    return;

  priv->dispose_run = TRUE;

309 310 311 312 313 314
  if (priv->contact_list)
    {
      g_object_unref (priv->contact_list);
      priv->contact_list = NULL;
    }

315 316 317 318 319 320
  if (priv->model)
    {
      g_object_unref (priv->model);
      priv->model = NULL;
    }

Xavier Claessens's avatar
Xavier Claessens committed
321 322 323 324 325
  if (priv->store)
    {
      g_object_unref (priv->store);
      priv->store = NULL;
    }
326 327 328 329 330 331 332 333

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

static void
empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
334 335 336 337
  object_class->constructor = contact_selector_constructor;
  object_class->dispose = contact_selector_dispose;
  object_class->set_property = contact_selector_set_property;
  object_class->get_property = contact_selector_get_property;
338 339
  g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));

340 341 342 343 344 345
  /**
   * EmpathyContactSelector:contact-list:
   *
   * An #EmpathyContactList containing the contacts for the
   * #EmpathyContactSelector.
   */
346 347 348
  g_object_class_install_property (object_class, PROP_CONTACT_LIST,
      g_param_spec_object ("contact-list", "contact list", "contact list",
      EMPATHY_TYPE_CONTACT_LIST, G_PARAM_CONSTRUCT_ONLY |
349 350 351
      G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
}

352 353 354 355 356 357 358 359 360
/**
 * empathy_contact_selector_new:
 * @contact_list: an #EmpathyContactList containing the contacts to list in
 * the contact selector
 *
 * Creates a new #EmpathyContactSelector.
 *
 * Return value: A new #EmpathyContactSelector
 */
361
GtkWidget *
362
empathy_contact_selector_new (EmpathyContactList *contact_list)
363
{
364 365
  g_return_val_if_fail (EMPATHY_IS_CONTACT_LIST (contact_list), NULL);

366 367
  return GTK_WIDGET (g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR,
      "contact-list", contact_list, NULL));
368
}
369

370 371 372 373 374
/**
 * empathy_contact_selector_dup_selected:
 * @selector: An #EmpathyContactSelector
 *
 * Returns a new reference to the contact which is currently selected in
375 376
 * @selector, or %NULL if there is no contact selected. The returned contact
 * should be unrefed with g_object_unref() when finished with.
377 378 379
 *
 * Return value: A new reference to the contact currently selected, or %NULL
 */
380
EmpathyContact *
381
empathy_contact_selector_dup_selected (EmpathyContactSelector *selector)
382 383 384 385 386 387 388 389 390 391
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  EmpathyContact *contact = NULL;
  GtkTreeIter iter;

  g_return_val_if_fail (EMPATHY_IS_CONTACT_SELECTOR (selector), NULL);

  if (!gtk_combo_box_get_active_iter (GTK_COMBO_BOX (selector), &iter))
    return NULL;

392
  gtk_tree_model_get (priv->model, &iter,
393 394 395 396
      EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);

  return contact;
}
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

typedef struct
{
  EmpathyContactSelectorFilterFunc func;
  gpointer user_data;
} FilterData;

static void
filter_data_free (gpointer data)
{
  g_slice_free (FilterData, data);
}

static gboolean
contact_selector_filter_visible_func (GtkTreeModel *model,
				      GtkTreeIter *iter,
				      gpointer user_data)
{
  EmpathyContact *contact;
416
  gboolean visible = TRUE;
417 418
  FilterData *data = (FilterData *) user_data;

419 420 421
  gtk_tree_model_get (model, iter,
      EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact,
      -1);
422 423 424

  if (contact != NULL)
    {
425
      visible = data->func (contact, data->user_data);
426 427 428 429 430 431 432

      g_object_unref (contact);
    }

  return visible;
}

433 434 435 436 437 438 439 440 441 442 443 444 445
/**
 * empathy_contact_selector_set_visible:
 * @selector: an #EmpathyContactSelector
 * @func: an #EmpathyContactSelectorFilterFunc to filter the contacts
 * @user_data: data to pass to @func or %NULL
 *
 * Sets a filter on the @selector so only contacts that return %TRUE
 * when passed into @func are visible.
 *
 * A typical usage for this function would be to only show contacts that
 * can send or receive files. In this case, one could use the
 * empathy_contact_can_send_files() function
 */
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
void
empathy_contact_selector_set_visible (EmpathyContactSelector *selector,
                                      EmpathyContactSelectorFilterFunc func,
                                      gpointer user_data)
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  FilterData *data;

  data = g_slice_new0 (FilterData);
  data->func = func;
  data->user_data = user_data;

  gtk_tree_model_filter_set_visible_func (GTK_TREE_MODEL_FILTER (priv->model),
      contact_selector_filter_visible_func, data, filter_data_free);

  gtk_tree_model_filter_refilter (GTK_TREE_MODEL_FILTER (priv->model));
}
463 464 465 466 467 468 469 470 471 472 473

/**
 * EmpathyContactSelectorFilterFunc:
 * @contact: an #EmpathyContact
 * @user_data: user data or %NULL
 *
 * A function which decides whether the contact indicated by @contact
 * is visible.
 *
 * Return value: whether @contact is visible
 */