aboutsummaryrefslogtreecommitdiffstats
path: root/libempathy-gtk/empathy-account-selector-dialog.c
blob: 0dd67ea7cf84d4391d5a2deff18fd6b2700b3f43 (plain) (blame)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * Copyright (C) 2011 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
 *
 * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
 */

#include "config.h"
#include "empathy-account-selector-dialog.h"

enum
{
  PROP_ACCOUNTS = 1
};

struct _EmpathyAccountSelectorDialogPrivate
{
  GList *accounts;

  GtkWidget *treeview;
  GtkListStore *model;
};

enum
{
  COL_ACCOUNT,
  COL_ICON,
  COL_NAME,
  NUM_COL
};

G_DEFINE_TYPE (EmpathyAccountSelectorDialog, empathy_account_selector_dialog, \
    GTK_TYPE_DIALOG)

static void
empathy_account_selector_dialog_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) object;

  switch (property_id)
    {
      case PROP_ACCOUNTS:
        {
          GList *list;

          list = g_value_get_pointer (value);

          self->priv->accounts = g_list_copy (list);
          g_list_foreach (self->priv->accounts, (GFunc) g_object_ref, NULL);
          break;
        }
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
    }
}

static void
empathy_account_selector_dialog_constructed (GObject *obj)
{
  EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) obj;
  GList *l;

  for (l = self->priv->accounts; l != NULL; l = g_list_next (l))
    {
      TpAccount *account = l->data;

      gtk_list_store_insert_with_values (GTK_LIST_STORE (self->priv->model),
          NULL, -1,
          COL_ACCOUNT, account,
          COL_ICON, tp_account_get_icon_name (account),
          COL_NAME, tp_account_get_display_name (account),
          -1);
    }

  G_OBJECT_CLASS (empathy_account_selector_dialog_parent_class)->constructed (
      obj);
}

static void
empathy_account_selector_dialog_dispose (GObject *obj)
{
  EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) obj;

  g_list_free_full (self->priv->accounts, g_object_unref);
  self->priv->accounts = NULL;

  tp_clear_object (&self->priv->model);

  G_OBJECT_CLASS (empathy_account_selector_dialog_parent_class)->dispose (obj);
}

static void
empathy_account_selector_dialog_class_init (
    EmpathyAccountSelectorDialogClass *klass)
{
  GObjectClass *oclass = G_OBJECT_CLASS (klass);
  GParamSpec *spec;

  oclass->set_property = empathy_account_selector_dialog_set_property;
  oclass->constructed = empathy_account_selector_dialog_constructed;
  oclass->dispose = empathy_account_selector_dialog_dispose;

  spec = g_param_spec_pointer ("accounts", "accounts", "GList of TpAccount",
      G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
  g_object_class_install_property (oclass, PROP_ACCOUNTS, spec);

  g_type_class_add_private (klass,
      sizeof (EmpathyAccountSelectorDialogPrivate));
}

static void
empathy_account_selector_dialog_init (EmpathyAccountSelectorDialog *self)
{
  GtkWidget *box;
  GtkCellRenderer *cell;
  GtkTreeViewColumn *column;

  self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
      EMPATHY_TYPE_ACCOUNT_SELECTOR_DIALOG,
      EmpathyAccountSelectorDialogPrivate);

  self->priv->model = gtk_list_store_new (NUM_COL,
      TP_TYPE_ACCOUNT,  /* account */
      G_TYPE_STRING,    /* icon name */
      G_TYPE_STRING);   /* name */

  /* Create treeview */
  self->priv->treeview = gtk_tree_view_new_with_model (
      GTK_TREE_MODEL (self->priv->model));

  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (self->priv->treeview),
      FALSE);

  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_expand (column, TRUE);
  gtk_tree_view_append_column (GTK_TREE_VIEW (self->priv->treeview), column);

  /* icon */
  cell = gtk_cell_renderer_pixbuf_new ();
  gtk_tree_view_column_pack_start (column, cell, FALSE);
  gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_ICON);

  /* text */
  cell = gtk_cell_renderer_text_new ();

  gtk_tree_view_column_pack_start (column, cell, TRUE);
  gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);

  box = gtk_dialog_get_content_area (GTK_DIALOG (self));
  gtk_box_pack_start (GTK_BOX (box), self->priv->treeview, TRUE, TRUE, 0);

  gtk_widget_show (self->priv->treeview);
}

GtkWidget *
empathy_account_selector_dialog_new (GList *accounts)
{
  return g_object_new (EMPATHY_TYPE_ACCOUNT_SELECTOR_DIALOG,
      "accounts", accounts,
      NULL);
}

TpAccount *
empathy_account_selector_dialog_dup_selected (
    EmpathyAccountSelectorDialog *self)
{
  GtkTreeSelection *selection;
  GtkTreeIter iter;
  GtkTreeModel *model;
  TpAccount *account;

  selection = gtk_tree_view_get_selection (
      GTK_TREE_VIEW (self->priv->treeview));

  if (!gtk_tree_selection_get_selected (selection, &model, &iter))
    return NULL;

  gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);

  return account;
}