summaryrefslogtreecommitdiff
path: root/tp-gtk-contact-selector.c
blob: a723c77931fd74041e8a4a50cf64cbfeb868e478 (plain)
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
27
28
29
30
31
32
33
34
35
36
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
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
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
130
131
132
133
/*
 * tp-gtk-contact-selector.c
 *
 * TpGtkContactSelector
 *
 * (c) 2009, Collabora Ltd.
 *
 * Authors:
 *    Danielle Madeley <danielle.madeley@collabora.co.uk>
 */

#include "tp-gtk-contact-selector.h"
#include "tp-gtk-roster-model.h"
#include "tp-gtk-roster-model-filter.h"

#define GET_PRIVATE(obj)	(G_TYPE_INSTANCE_GET_PRIVATE ((obj), TP_GTK_TYPE_CONTACT_SELECTOR, TpGTKContactSelectorPrivate))

G_DEFINE_TYPE (TpGtkContactSelector, tp_gtk_contact_selector,
    GTK_TYPE_COMBO_BOX_ENTRY);

typedef struct _TpGtkContactSelectorPrivate TpGtkContactSelectorPrivate;
struct _TpGtkContactSelectorPrivate
{
};

static void
tp_gtk_contact_selector_class_init (TpGtkContactSelectorClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  static gboolean style_set = FALSE;

  if (!style_set)
    {
      gtk_rc_parse_string (
          "style \"tp-gtk-contact-selector\" {\n"
          "   GtkComboBox::appears-as-list = 1\n"
          "}"
          "class \"TpGtkContactSelector\" style \"tp-gtk-contact-selector\"\n"
          );
      style_set = TRUE;
    }

  g_type_class_add_private (gobject_class, sizeof (TpGtkContactSelectorPrivate));
}

static void
alias_column_data_func (GtkCellLayout   *self,
                        GtkCellRenderer *renderer,
                        GtkTreeModel    *model,
                        GtkTreeIter     *iter,
                        gpointer         data)
{
  TpGtkRosterModelColumnType type;
  char *alias, *identifier, *markup;

  gtk_tree_model_get (model, iter,
      TP_GTK_ROSTER_MODEL_COLUMN_TYPE, &type,
      TP_GTK_ROSTER_MODEL_ALIAS, &alias,
      TP_GTK_ROSTER_MODEL_IDENTIFIER, &identifier,
      -1);

  if (type == TP_GTK_ROSTER_MODEL_COLUMN_TYPE_GROUP)
    {
      markup = g_strdup_printf ("<b>%s</b>", alias);
    }
  else
    {
      markup = g_strdup_printf (
          "%s\n<span size=\"small\" color=\"darkgray\">%s</span>",
          alias, identifier);
    }

  g_object_set (renderer, "markup", markup, NULL);

  g_free (alias);
  g_free (identifier);
  g_free (markup);
}

static void
tp_gtk_contact_selector_init (TpGtkContactSelector *self)
{
  GtkCellRenderer *renderer;

  /* set up the renderers */
  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), renderer, FALSE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), renderer,
      "icon-name", TP_GTK_ROSTER_MODEL_STATUS_ICON,
      NULL);

  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), renderer, TRUE);
  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (self), renderer,
      alias_column_data_func, NULL, NULL);

  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), renderer, FALSE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), renderer,
      "pixbuf", TP_GTK_ROSTER_MODEL_AVATAR,
      NULL);
}

GtkWidget *
tp_gtk_contact_selector_new (GtkTreeModel *model)
{
  GtkWidget *self;
  GtkTreeModel *filter = NULL;

  g_return_val_if_fail (model == NULL ||
                        TP_GTK_IS_ROSTER_MODEL (model) ||
                        TP_GTK_IS_ROSTER_MODEL_FILTER (model),
                        NULL);

  if (model == NULL)
    {

      model = tp_gtk_roster_model_new ();
      filter = tp_gtk_roster_model_filter_new (TP_GTK_ROSTER_MODEL (model));
      g_object_unref (model);

      model = filter;
    }

  self = g_object_new (TP_GTK_TYPE_CONTACT_SELECTOR,
                       "model", model,
                       NULL);

  if (filter != NULL)
    g_object_unref (filter);

  return self;
}