]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-selector-dialog.c
local-xmpp-assistant-widget: increase row-spacing
[empathy.git] / libempathy-gtk / empathy-account-selector-dialog.c
1 /*
2  * Copyright (C) 2011 Collabora Ltd.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Guillaume Desmottes <guillaume.desmottes@collabora.co.uk>
19  */
20
21 #include "config.h"
22
23 #include "empathy-account-selector-dialog.h"
24
25 enum
26 {
27   PROP_ACCOUNTS = 1
28 };
29
30 struct _EmpathyAccountSelectorDialogPrivate
31 {
32   GList *accounts;
33
34   GtkWidget *treeview;
35   GtkListStore *model;
36 };
37
38 enum
39 {
40   COL_ACCOUNT,
41   COL_ICON,
42   COL_NAME,
43   NUM_COL
44 };
45
46 G_DEFINE_TYPE (EmpathyAccountSelectorDialog, empathy_account_selector_dialog, \
47     GTK_TYPE_DIALOG)
48
49 static void
50 empathy_account_selector_dialog_set_property (GObject *object,
51     guint property_id,
52     const GValue *value,
53     GParamSpec *pspec)
54 {
55   EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) object;
56
57   switch (property_id)
58     {
59       case PROP_ACCOUNTS:
60         {
61           GList *list;
62
63           list = g_value_get_pointer (value);
64
65           self->priv->accounts = g_list_copy (list);
66           g_list_foreach (self->priv->accounts, (GFunc) g_object_ref, NULL);
67           break;
68         }
69       default:
70         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
71     }
72 }
73
74 static void
75 empathy_account_selector_dialog_constructed (GObject *obj)
76 {
77   EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) obj;
78   GList *l;
79
80   for (l = self->priv->accounts; l != NULL; l = g_list_next (l))
81     {
82       TpAccount *account = l->data;
83
84       gtk_list_store_insert_with_values (GTK_LIST_STORE (self->priv->model),
85           NULL, -1,
86           COL_ACCOUNT, account,
87           COL_ICON, tp_account_get_icon_name (account),
88           COL_NAME, tp_account_get_display_name (account),
89           -1);
90     }
91
92   G_OBJECT_CLASS (empathy_account_selector_dialog_parent_class)->constructed (
93       obj);
94 }
95
96 static void
97 empathy_account_selector_dialog_dispose (GObject *obj)
98 {
99   EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) obj;
100
101   g_list_free_full (self->priv->accounts, g_object_unref);
102   self->priv->accounts = NULL;
103
104   tp_clear_object (&self->priv->model);
105
106   G_OBJECT_CLASS (empathy_account_selector_dialog_parent_class)->dispose (obj);
107 }
108
109 static void
110 empathy_account_selector_dialog_class_init (
111     EmpathyAccountSelectorDialogClass *klass)
112 {
113   GObjectClass *oclass = G_OBJECT_CLASS (klass);
114   GParamSpec *spec;
115
116   oclass->set_property = empathy_account_selector_dialog_set_property;
117   oclass->constructed = empathy_account_selector_dialog_constructed;
118   oclass->dispose = empathy_account_selector_dialog_dispose;
119
120   spec = g_param_spec_pointer ("accounts", "accounts", "GList of TpAccount",
121       G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
122   g_object_class_install_property (oclass, PROP_ACCOUNTS, spec);
123
124   g_type_class_add_private (klass,
125       sizeof (EmpathyAccountSelectorDialogPrivate));
126 }
127
128 static void
129 empathy_account_selector_dialog_init (EmpathyAccountSelectorDialog *self)
130 {
131   GtkWidget *box;
132   GtkCellRenderer *cell;
133   GtkTreeViewColumn *column;
134
135   self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
136       EMPATHY_TYPE_ACCOUNT_SELECTOR_DIALOG,
137       EmpathyAccountSelectorDialogPrivate);
138
139   self->priv->model = gtk_list_store_new (NUM_COL,
140       TP_TYPE_ACCOUNT,  /* account */
141       G_TYPE_STRING,    /* icon name */
142       G_TYPE_STRING);   /* name */
143
144   /* Create treeview */
145   self->priv->treeview = gtk_tree_view_new_with_model (
146       GTK_TREE_MODEL (self->priv->model));
147
148   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (self->priv->treeview),
149       FALSE);
150
151   column = gtk_tree_view_column_new ();
152   gtk_tree_view_column_set_expand (column, TRUE);
153   gtk_tree_view_append_column (GTK_TREE_VIEW (self->priv->treeview), column);
154
155   /* icon */
156   cell = gtk_cell_renderer_pixbuf_new ();
157   gtk_tree_view_column_pack_start (column, cell, FALSE);
158   gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_ICON);
159
160   /* text */
161   cell = gtk_cell_renderer_text_new ();
162
163   gtk_tree_view_column_pack_start (column, cell, TRUE);
164   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
165
166   box = gtk_dialog_get_content_area (GTK_DIALOG (self));
167   gtk_box_pack_start (GTK_BOX (box), self->priv->treeview, TRUE, TRUE, 0);
168
169   gtk_widget_show (self->priv->treeview);
170 }
171
172 GtkWidget *
173 empathy_account_selector_dialog_new (GList *accounts)
174 {
175   return g_object_new (EMPATHY_TYPE_ACCOUNT_SELECTOR_DIALOG,
176       "accounts", accounts,
177       NULL);
178 }
179
180 TpAccount *
181 empathy_account_selector_dialog_dup_selected (
182     EmpathyAccountSelectorDialog *self)
183 {
184   GtkTreeSelection *selection;
185   GtkTreeIter iter;
186   GtkTreeModel *model;
187   TpAccount *account;
188
189   selection = gtk_tree_view_get_selection (
190       GTK_TREE_VIEW (self->priv->treeview));
191
192   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
193     return NULL;
194
195   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
196
197   return account;
198 }