]> git.0d.be Git - empathy.git/blob - libempathy-gtk/empathy-account-selector-dialog.c
remove released flag
[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 #include "empathy-account-selector-dialog.h"
23
24 enum
25 {
26   PROP_ACCOUNTS = 1
27 };
28
29 struct _EmpathyAccountSelectorDialogPrivate
30 {
31   GList *accounts;
32
33   GtkWidget *treeview;
34   GtkListStore *model;
35 };
36
37 enum
38 {
39   COL_ACCOUNT,
40   COL_ICON,
41   COL_NAME,
42   NUM_COL
43 };
44
45 G_DEFINE_TYPE (EmpathyAccountSelectorDialog, empathy_account_selector_dialog, \
46     GTK_TYPE_DIALOG)
47
48 static void
49 empathy_account_selector_dialog_set_property (GObject *object,
50     guint property_id,
51     const GValue *value,
52     GParamSpec *pspec)
53 {
54   EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) object;
55
56   switch (property_id)
57     {
58       case PROP_ACCOUNTS:
59         {
60           GList *list;
61
62           list = g_value_get_pointer (value);
63
64           self->priv->accounts = g_list_copy (list);
65           g_list_foreach (self->priv->accounts, (GFunc) g_object_ref, NULL);
66           break;
67         }
68       default:
69         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
70     }
71 }
72
73 static void
74 empathy_account_selector_dialog_constructed (GObject *obj)
75 {
76   EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) obj;
77   GList *l;
78
79   for (l = self->priv->accounts; l != NULL; l = g_list_next (l))
80     {
81       TpAccount *account = l->data;
82
83       gtk_list_store_insert_with_values (GTK_LIST_STORE (self->priv->model),
84           NULL, -1,
85           COL_ACCOUNT, account,
86           COL_ICON, tp_account_get_icon_name (account),
87           COL_NAME, tp_account_get_display_name (account),
88           -1);
89     }
90
91   G_OBJECT_CLASS (empathy_account_selector_dialog_parent_class)->constructed (
92       obj);
93 }
94
95 static void
96 empathy_account_selector_dialog_dispose (GObject *obj)
97 {
98   EmpathyAccountSelectorDialog *self = (EmpathyAccountSelectorDialog *) obj;
99
100   g_list_free_full (self->priv->accounts, g_object_unref);
101   self->priv->accounts = NULL;
102
103   tp_clear_object (&self->priv->model);
104
105   G_OBJECT_CLASS (empathy_account_selector_dialog_parent_class)->dispose (obj);
106 }
107
108 static void
109 empathy_account_selector_dialog_class_init (
110     EmpathyAccountSelectorDialogClass *klass)
111 {
112   GObjectClass *oclass = G_OBJECT_CLASS (klass);
113   GParamSpec *spec;
114
115   oclass->set_property = empathy_account_selector_dialog_set_property;
116   oclass->constructed = empathy_account_selector_dialog_constructed;
117   oclass->dispose = empathy_account_selector_dialog_dispose;
118
119   spec = g_param_spec_pointer ("accounts", "accounts", "GList of TpAccount",
120       G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
121   g_object_class_install_property (oclass, PROP_ACCOUNTS, spec);
122
123   g_type_class_add_private (klass,
124       sizeof (EmpathyAccountSelectorDialogPrivate));
125 }
126
127 static void
128 empathy_account_selector_dialog_init (EmpathyAccountSelectorDialog *self)
129 {
130   GtkWidget *box;
131   GtkCellRenderer *cell;
132   GtkTreeViewColumn *column;
133
134   self->priv = G_TYPE_INSTANCE_GET_PRIVATE ((self),
135       EMPATHY_TYPE_ACCOUNT_SELECTOR_DIALOG,
136       EmpathyAccountSelectorDialogPrivate);
137
138   self->priv->model = gtk_list_store_new (NUM_COL,
139       TP_TYPE_ACCOUNT,  /* account */
140       G_TYPE_STRING,    /* icon name */
141       G_TYPE_STRING);   /* name */
142
143   /* Create treeview */
144   self->priv->treeview = gtk_tree_view_new_with_model (
145       GTK_TREE_MODEL (self->priv->model));
146
147   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (self->priv->treeview),
148       FALSE);
149
150   column = gtk_tree_view_column_new ();
151   gtk_tree_view_column_set_expand (column, TRUE);
152   gtk_tree_view_append_column (GTK_TREE_VIEW (self->priv->treeview), column);
153
154   /* icon */
155   cell = gtk_cell_renderer_pixbuf_new ();
156   gtk_tree_view_column_pack_start (column, cell, FALSE);
157   gtk_tree_view_column_add_attribute (column, cell, "icon-name", COL_ICON);
158
159   /* text */
160   cell = gtk_cell_renderer_text_new ();
161
162   gtk_tree_view_column_pack_start (column, cell, TRUE);
163   gtk_tree_view_column_add_attribute (column, cell, "text", COL_NAME);
164
165   box = gtk_dialog_get_content_area (GTK_DIALOG (self));
166   gtk_box_pack_start (GTK_BOX (box), self->priv->treeview, TRUE, TRUE, 0);
167
168   gtk_widget_show (self->priv->treeview);
169 }
170
171 GtkWidget *
172 empathy_account_selector_dialog_new (GList *accounts)
173 {
174   return g_object_new (EMPATHY_TYPE_ACCOUNT_SELECTOR_DIALOG,
175       "accounts", accounts,
176       NULL);
177 }
178
179 TpAccount *
180 empathy_account_selector_dialog_dup_selected (
181     EmpathyAccountSelectorDialog *self)
182 {
183   GtkTreeSelection *selection;
184   GtkTreeIter iter;
185   GtkTreeModel *model;
186   TpAccount *account;
187
188   selection = gtk_tree_view_get_selection (
189       GTK_TREE_VIEW (self->priv->treeview));
190
191   if (!gtk_tree_selection_get_selected (selection, &model, &iter))
192     return NULL;
193
194   gtk_tree_model_get (model, &iter, COL_ACCOUNT, &account, -1);
195
196   return account;
197 }