empathy-contact-selector.c 10.5 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.h>
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
#include <gtk/gtk.h>

#include <libempathy/empathy-contact.h>
#include <libempathy-gtk/empathy-contact-list-store.h>
#include "empathy-contact-selector.h"

G_DEFINE_TYPE (EmpathyContactSelector, empathy_contact_selector,
    GTK_TYPE_COMBO_BOX)

#define GET_PRIV(object) (G_TYPE_INSTANCE_GET_PRIVATE \
    ((object), EMPATHY_TYPE_CONTACT_SELECTOR, EmpathyContactSelectorPriv))

enum
{
  PROP_0,
  PROP_STORE
};

typedef struct _EmpathyContactSelectorPriv EmpathyContactSelectorPriv;

struct _EmpathyContactSelectorPriv
{
  EmpathyContactListStore *store;
49
  gboolean is_blank_set;
50 51
};

52
static void changed_cb (GtkComboBox *widget, gpointer data);
53 54
static gboolean get_iter_for_blank_contact (GtkTreeStore *store,
    GtkTreeIter *blank_iter);
55 56 57 58 59 60 61 62 63 64


EmpathyContact *
empathy_contact_selector_get_selected (EmpathyContactSelector *selector)
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  EmpathyContact *contact = NULL;
  GtkTreeIter iter;

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

67 68
  gtk_tree_model_get (GTK_TREE_MODEL (priv->store), &iter,
      EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, &contact, -1);
69 70 71 72 73 74

  return contact;
}


static void
75 76 77 78 79
set_blank_contact (EmpathyContactSelector *selector)
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  GtkTreeIter blank_iter;

80 81 82 83
  gtk_tree_store_insert (GTK_TREE_STORE (priv->store), &blank_iter, NULL, 0);
  gtk_tree_store_set (GTK_TREE_STORE (priv->store), &blank_iter,
      EMPATHY_CONTACT_LIST_STORE_COL_CONTACT, NULL,
      EMPATHY_CONTACT_LIST_STORE_COL_NAME, ("Select a contact"), -1);
84 85 86
  g_signal_handlers_block_by_func(selector, changed_cb, NULL);
  gtk_combo_box_set_active_iter (GTK_COMBO_BOX (selector), &blank_iter);
  g_signal_handlers_unblock_by_func(selector, changed_cb, NULL);
87
  priv->is_blank_set = TRUE;
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}


static void
notify_popup_shown_cb (GtkComboBox *widget,
                       GParamSpec *property,
                       gpointer data)
{
  EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  GtkTreeIter blank_iter;
  gboolean shown;

  g_object_get (widget, property->name, &shown, NULL);

103 104
  if (shown)
    {
105
      if (get_iter_for_blank_contact (GTK_TREE_STORE (priv->store), &blank_iter))
106
        {
107
          gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
108 109 110 111
          priv->is_blank_set = FALSE;
        }
    }
  else
112
    {
113 114 115
      if (gtk_combo_box_get_active (widget) == -1)
        {
          set_blank_contact (selector);
116
          if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store),
117 118 119
              NULL) == 1)
          gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
        }
120
    }
121 122 123 124 125 126 127 128 129 130
}


static void
changed_cb (GtkComboBox *widget,
            gpointer data)
{
  EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  GtkTreeIter blank_iter;
131 132 133 134 135 136
  gboolean shown;

  g_object_get (widget, "popup-shown", &shown, NULL);

  if (shown)
    return;
137 138 139 140

  if (gtk_combo_box_get_active (widget) == -1)
    {
      set_blank_contact (selector);
141
      if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store),
142 143
        NULL) == 1)
        gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
144 145 146
    }
  else
    {
147
      if (get_iter_for_blank_contact (GTK_TREE_STORE (priv->store), &blank_iter))
148
        {
149
          gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
150
          priv->is_blank_set = FALSE;
151 152 153 154 155 156
        }
    }
}


static gboolean
157 158
get_iter_for_blank_contact (GtkTreeStore *store,
                            GtkTreeIter *blank_iter)
159 160 161 162
{
  GtkTreePath *path;
  GtkTreeIter tmp_iter;
  EmpathyContact *tmp_contact;
163
  gboolean is_present = FALSE;
164 165

  path = gtk_tree_path_new_first ();
166
  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
167 168 169
    {
      do
        {
170 171 172
          gtk_tree_model_get (GTK_TREE_MODEL (store),
              &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
              &tmp_contact, -1);
173
          if (tmp_contact == NULL)
174
            {
175 176
              *blank_iter = tmp_iter;
              is_present = TRUE;
177 178
              break;
            }
179
        } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter));
180 181 182
    }

  gtk_tree_path_free (path);
183 184

  return is_present;
185 186 187 188 189 190 191 192 193 194 195
}


static void
empathy_store_row_changed_cb (EmpathyContactListStore *empathy_store,
                              GtkTreePath *empathy_path,
                              GtkTreeIter *empathy_iter,
                              gpointer data)
{
  EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (data);
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
196
  gint children;
197

198
  children = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
199 200

  if (children == 1 && priv->is_blank_set)
201
      gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
202
  else if (children)
203
      gtk_widget_set_sensitive (GTK_WIDGET (selector), TRUE);
204 205
  else
      gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}


static GObject *
empathy_contact_selector_constructor (GType type,
                                      guint n_construct_params,
                                      GObjectConstructParam *construct_params)
{
  GObject *object =
      G_OBJECT_CLASS (empathy_contact_selector_parent_class)->constructor (type,
      n_construct_params, construct_params);
  EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
  EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);
  GtkCellRenderer *renderer;

  g_object_set (priv->store, "is-compact", TRUE, "show-avatars", FALSE,
222 223
      "show-offline", FALSE, "show-groups", FALSE,
      "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
224 225 226

  g_signal_connect (priv->store, "row-changed",
      G_CALLBACK (empathy_store_row_changed_cb), (gpointer) contact_selector);
227 228 229 230
  g_signal_connect (GTK_COMBO_BOX (contact_selector), "changed",
      G_CALLBACK (changed_cb), NULL);
  g_signal_connect (GTK_COMBO_BOX (contact_selector), "notify::popup-shown",
      G_CALLBACK (notify_popup_shown_cb), NULL);
231 232

  gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
233
      GTK_TREE_MODEL (priv->store));
234 235 236 237 238 239 240
  gtk_widget_set_sensitive (GTK_WIDGET (contact_selector), FALSE);

  /* Status icon */
  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
      renderer, FALSE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
241
      "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
242 243 244 245 246 247

  /* Contact name */
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (contact_selector),
      renderer, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (contact_selector), renderer,
248
      "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
249

250
  set_blank_contact (contact_selector);
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

  object = G_OBJECT (contact_selector);
  return object;
}


static void
empathy_contact_selector_init (EmpathyContactSelector *empathy_contact_selector)
{
}


static void
empathy_contact_selector_set_property (GObject *object,
                                       guint prop_id,
                                       const GValue *value,
                                       GParamSpec *pspec)
{
  EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
  EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);

  switch (prop_id)
    {
      case PROP_STORE:
        priv->store = g_value_dup_object (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}


static void
empathy_contact_selector_get_property (GObject *object,
                                       guint prop_id,
                                       GValue *value,
                                       GParamSpec *pspec)
{
  EmpathyContactSelector *contact_selector = EMPATHY_CONTACT_SELECTOR (object);
  EmpathyContactSelectorPriv *priv = GET_PRIV (contact_selector);

  switch (prop_id)
    {
      case PROP_STORE:
        g_value_set_object (value, priv->store);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}


static void
empathy_contact_selector_dispose (GObject *object)
{
  g_debug ("EmpathyContactSelector - dispose: %p",  object);

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


static void
empathy_contact_selector_finalize (GObject *object)
{
  g_debug ("EmpathyContactSelector - finalize: %p",  object);

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


static void
empathy_contact_selector_class_init (EmpathyContactSelectorClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  object_class->constructor = empathy_contact_selector_constructor;
  object_class->dispose = empathy_contact_selector_dispose;
  object_class->finalize = empathy_contact_selector_finalize;
  object_class->set_property = empathy_contact_selector_set_property;
  object_class->get_property = empathy_contact_selector_get_property;
  g_type_class_add_private (klass, sizeof (EmpathyContactSelectorPriv));

  g_object_class_install_property (object_class, PROP_STORE,
      g_param_spec_object ("store", "store", "store",
      EMPATHY_TYPE_CONTACT_LIST_STORE, G_PARAM_CONSTRUCT_ONLY |
      G_PARAM_READWRITE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
}


EmpathyContactSelector *
empathy_contact_selector_new (EmpathyContactListStore *store)
{
  return g_object_new (EMPATHY_TYPE_CONTACT_SELECTOR, "store", store, NULL);
}