empathy-roster-group.c 5.84 KB
Newer Older
1 2 3 4 5
#include "config.h"
#include "empathy-roster-group.h"

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

6
G_DEFINE_TYPE (EmpathyRosterGroup, empathy_roster_group, GTK_TYPE_LIST_BOX_ROW)
7 8 9 10

enum
{
  PROP_NAME = 1,
11
  PROP_ICON,
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  N_PROPS
};

/*
enum
{
  LAST_SIGNAL
};

static guint signals[LAST_SIGNAL];
*/

struct _EmpathyRosterGroupPriv
{
  gchar *name;
27
  gchar *icon_name;
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

  /* Widgets associated with this group. EmpathyRosterGroup is not responsible
   * of packing/displaying these widgets. This hash table is a just a set
   * to keep track of them. */
  GHashTable *widgets;
};

static void
empathy_roster_group_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  EmpathyRosterGroup *self = EMPATHY_ROSTER_GROUP (object);

  switch (property_id)
    {
      case PROP_NAME:
        g_value_set_string (value, self->priv->name);
        break;
48 49 50
      case PROP_ICON:
        g_value_set_string (value, self->priv->icon_name);
        break;
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
empathy_roster_group_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  EmpathyRosterGroup *self = EMPATHY_ROSTER_GROUP (object);

  switch (property_id)
    {
      case PROP_NAME:
        g_assert (self->priv->name == NULL); /* construct-only */
        self->priv->name = g_value_dup_string (value);
        break;
71 72 73 74
      case PROP_ICON:
        g_assert (self->priv->icon_name == NULL); /* construct-only */
        self->priv->icon_name = g_value_dup_string (value);
        break;
75 76 77 78 79 80 81 82 83 84 85 86 87
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
empathy_roster_group_constructed (GObject *object)
{
  EmpathyRosterGroup *self = EMPATHY_ROSTER_GROUP (object);
  void (*chain_up) (GObject *) =
      ((GObjectClass *) empathy_roster_group_parent_class)->constructed;
  gchar *tmp;
88
  GtkWidget *box, *label;
89 90 91 92 93 94

  if (chain_up != NULL)
    chain_up (object);

  g_assert (self->priv->name != NULL);

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);

  /* Icon, if any */
  if (!tp_str_empty (self->priv->icon_name))
    {
      GtkWidget *icon;

      icon = gtk_image_new_from_icon_name (self->priv->icon_name,
          GTK_ICON_SIZE_MENU);

      if (icon != NULL)
        gtk_box_pack_start (GTK_BOX (box), icon, FALSE, FALSE, 0);
    }

  /* Label */
110
  tmp = g_strdup_printf ("<b>%s</b>", self->priv->name);
111
  label = gtk_label_new (tmp);
112
  g_free (tmp);
113 114 115 116 117 118

  gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
  gtk_box_pack_start (GTK_BOX (box), label, TRUE, TRUE, 0);

  gtk_widget_show_all (box);

119
  gtk_expander_set_label_widget (self->expander, box);
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
}

static void
empathy_roster_group_dispose (GObject *object)
{
  EmpathyRosterGroup *self = EMPATHY_ROSTER_GROUP (object);
  void (*chain_up) (GObject *) =
      ((GObjectClass *) empathy_roster_group_parent_class)->dispose;

  tp_clear_pointer (&self->priv->widgets, g_hash_table_unref);

  if (chain_up != NULL)
    chain_up (object);
}

static void
empathy_roster_group_finalize (GObject *object)
{
  EmpathyRosterGroup *self = EMPATHY_ROSTER_GROUP (object);
  void (*chain_up) (GObject *) =
      ((GObjectClass *) empathy_roster_group_parent_class)->finalize;

  g_free (self->priv->name);
143
  g_free (self->priv->icon_name);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

  if (chain_up != NULL)
    chain_up (object);
}

static void
empathy_roster_group_class_init (
    EmpathyRosterGroupClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
  GParamSpec *spec;

  oclass->get_property = empathy_roster_group_get_property;
  oclass->set_property = empathy_roster_group_set_property;
  oclass->constructed = empathy_roster_group_constructed;
  oclass->dispose = empathy_roster_group_dispose;
  oclass->finalize = empathy_roster_group_finalize;

  spec = g_param_spec_string ("name", "Name",
      "Group name",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (oclass, PROP_NAME, spec);

168 169 170 171 172 173
  spec = g_param_spec_string ("icon", "Icon",
      "Icon name",
      NULL,
      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (oclass, PROP_ICON, spec);

174 175 176 177 178 179 180 181 182 183
  g_type_class_add_private (klass, sizeof (EmpathyRosterGroupPriv));
}

static void
empathy_roster_group_init (EmpathyRosterGroup *self)
{
  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      EMPATHY_TYPE_ROSTER_GROUP, EmpathyRosterGroupPriv);

  self->priv->widgets = g_hash_table_new (NULL, NULL);
184 185 186 187 188 189 190

  self->expander = GTK_EXPANDER (gtk_expander_new (NULL));
  gtk_expander_set_use_markup (self->expander, TRUE);
  gtk_expander_set_expanded (self->expander, TRUE);
  gtk_widget_show (GTK_WIDGET (self->expander));

  gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->expander));
191 192 193
}

GtkWidget *
194 195
empathy_roster_group_new (const gchar *name,
    const gchar *icon)
196 197 198
{
  return g_object_new (EMPATHY_TYPE_ROSTER_GROUP,
      "name", name,
199
      "icon", icon,
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 233 234 235 236 237 238 239 240 241
      NULL);
}

const gchar *
empathy_roster_group_get_name (EmpathyRosterGroup *self)
{
  return self->priv->name;
}

guint
empathy_roster_group_add_widget (EmpathyRosterGroup *self,
    GtkWidget *widget)
{
  g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);

  g_hash_table_add (self->priv->widgets, widget);

  return empathy_roster_group_get_widgets_count (self);
}

guint
empathy_roster_group_remove_widget (EmpathyRosterGroup *self,
    GtkWidget *widget)
{
  g_return_val_if_fail (GTK_IS_WIDGET (widget), 0);

  g_hash_table_remove (self->priv->widgets, widget);

  return empathy_roster_group_get_widgets_count (self);
}

guint
empathy_roster_group_get_widgets_count (EmpathyRosterGroup *self)
{
  return g_hash_table_size (self->priv->widgets);
}

GList *
empathy_roster_group_get_widgets (EmpathyRosterGroup *self)
{
  return g_hash_table_get_keys (self->priv->widgets);
}