empathy-contact-selector.c 10.2 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 55 56 57 58 59 60 61 62


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))
63
    return NULL;
64

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

  return contact;
}


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
static gboolean
get_iter_for_blank_contact (GtkTreeStore *store,
                            GtkTreeIter *blank_iter)
{
  GtkTreePath *path;
  GtkTreeIter tmp_iter;
  EmpathyContact *tmp_contact;
  gboolean is_present = FALSE;

  path = gtk_tree_path_new_first ();
  if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &tmp_iter, path))
    {
      do
        {
          gtk_tree_model_get (GTK_TREE_MODEL (store),
              &tmp_iter, EMPATHY_CONTACT_LIST_STORE_COL_CONTACT,
              &tmp_contact, -1);
          if (tmp_contact == NULL)
            {
              *blank_iter = tmp_iter;
              is_present = TRUE;
              break;
            }
        } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &tmp_iter));
    }

  gtk_tree_path_free (path);

  return is_present;
}


104
static void
105 106 107 108 109
set_blank_contact (EmpathyContactSelector *selector)
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  GtkTreeIter blank_iter;

110 111 112 113
  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);
114 115 116
  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);
117
  priv->is_blank_set = TRUE;
118 119 120
}


121 122 123 124 125 126 127 128 129 130 131 132 133 134
static void
unset_blank_contact (EmpathyContactSelector *selector)
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
  GtkTreeIter blank_iter;

  if (get_iter_for_blank_contact (GTK_TREE_STORE (priv->store), &blank_iter))
    {
      gtk_tree_store_remove (GTK_TREE_STORE (priv->store), &blank_iter);
      priv->is_blank_set = FALSE;
    }
}


135
static void
136
manage_blank_contact (EmpathyContactSelector *selector)
137 138
{
  EmpathyContactSelectorPriv *priv = GET_PRIV (selector);
139
  gboolean is_popup_shown;
140

141
  g_object_get (selector, "popup-shown", &is_popup_shown, NULL);
142

143
  if (is_popup_shown)
144
    {
145
      unset_blank_contact (selector);
146 147
    }
  else
148
    {
149
      if (gtk_combo_box_get_active (GTK_COMBO_BOX (selector)) == -1)
150 151
        {
          set_blank_contact (selector);
152
          if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store),
153 154 155
              NULL) == 1)
          gtk_widget_set_sensitive (GTK_WIDGET (selector), FALSE);
        }
156 157 158 159
      else
        {
          unset_blank_contact (selector);
        }
160
    }
161 162 163 164
}


static void
165 166 167
notify_popup_shown_cb (GtkComboBox *widget,
                       GParamSpec *property,
                       gpointer data)
168 169
{
  EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);
170

171 172
  manage_blank_contact (selector);
}
173

174

175 176 177 178 179 180 181
static void
changed_cb (GtkComboBox *widget,
            gpointer data)
{
  EmpathyContactSelector *selector = EMPATHY_CONTACT_SELECTOR (widget);

  manage_blank_contact (selector);
182 183 184
}


185 186 187 188 189 190 191 192
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);
193
  gint children;
194

195
  children = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->store), NULL);
196 197

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


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,
219 220
      "show-offline", FALSE, "show-groups", FALSE,
      "sort-criterium", EMPATHY_CONTACT_LIST_STORE_SORT_NAME, NULL);
221 222 223

  g_signal_connect (priv->store, "row-changed",
      G_CALLBACK (empathy_store_row_changed_cb), (gpointer) contact_selector);
224 225 226 227
  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);
228 229

  gtk_combo_box_set_model (GTK_COMBO_BOX (contact_selector),
230
      GTK_TREE_MODEL (priv->store));
231 232 233 234 235 236 237
  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,
238
      "icon-name", EMPATHY_CONTACT_LIST_STORE_COL_ICON_STATUS, NULL);
239 240 241 242 243 244

  /* 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,
245
      "text", EMPATHY_CONTACT_LIST_STORE_COL_NAME, NULL);
246

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

  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);
}