2007-04-04 Murray Cumming <murrayc@murrayc.com>
[modest] / src / maemo / easysetup / modest-easysetup-provider-combo-box.c
1 /* Copyright (c) 2007, Nokia Corporation
2  * All rights reserved.
3  *
4  */
5
6 #include "modest-easysetup-provider-combo-box.h"
7 #include <gtk/gtkliststore.h>
8 #include <gtk/gtkcelllayout.h>
9 #include <gtk/gtkcellrenderertext.h>
10 #include <glib/gi18n.h>
11
12 #include <stdlib.h>
13 #include <string.h> /* For memcpy() */
14
15 /* Include config.h so that _() works: */
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19
20 G_DEFINE_TYPE (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
21
22 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
23         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
24
25 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
26
27 struct _EasysetupProviderComboBoxPrivate
28 {
29         GtkTreeModel *model;
30 };
31
32 static void
33 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
34                                                                                                                         GValue *value, GParamSpec *pspec)
35 {
36         switch (property_id) {
37         default:
38                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
39         }
40 }
41
42 static void
43 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
44                                                                                                                         const GValue *value, GParamSpec *pspec)
45 {
46         switch (property_id) {
47         default:
48                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
49         }
50 }
51
52 static void
53 easysetup_provider_combo_box_dispose (GObject *object)
54 {
55         if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
56                 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
57 }
58
59 static void
60 easysetup_provider_combo_box_finalize (GObject *object)
61 {
62         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
63
64         g_object_unref (G_OBJECT (priv->model));
65
66         G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
67 }
68
69 static void
70 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
71 {
72         GObjectClass *object_class = G_OBJECT_CLASS (klass);
73
74         g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
75
76         object_class->get_property = easysetup_provider_combo_box_get_property;
77         object_class->set_property = easysetup_provider_combo_box_set_property;
78         object_class->dispose = easysetup_provider_combo_box_dispose;
79         object_class->finalize = easysetup_provider_combo_box_finalize;
80 }
81
82 enum MODEL_COLS {
83         MODEL_COL_NAME = 0,
84         MODEL_COL_ID = 1 /* a string, not an int. */
85 };
86
87
88
89 static void
90 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
91 {
92         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
93
94         /* Create a tree model for the combo box,
95          * with a string for the name, and a string for the ID (e.g. "vodafone.it").
96          * This must match our MODEL_COLS enum constants.
97          */
98         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
99
100         /* Setup the combo box: */
101         GtkComboBox *combobox = GTK_COMBO_BOX (self);
102         gtk_combo_box_set_model (combobox, priv->model);
103
104         /* Provider column:
105          * The ID model column in not shown in the view. */
106         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
107         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
108         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
109         "text", MODEL_COL_NAME, NULL);
110         
111         /* The application should call easysetup_provider_combo_box_fill()
112          * to actually add some rows. */
113 }
114
115 EasysetupProviderComboBox*
116 easysetup_provider_combo_box_new (void)
117 {
118         return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
119 }
120
121 void easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets, gint country_id)
122 {       
123         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
124         
125         /* Remove any existing rows: */
126         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
127         gtk_list_store_clear (liststore);
128         
129         /* Add the appropriate rows for this country, from the presets file: */
130         gchar ** provider_ids = NULL;
131         gchar ** provider_names = modest_presets_get_providers (presets, country_id, 
132                 TRUE /* include_globals */, &provider_ids);
133         
134         gchar ** iter_provider_names = provider_names;
135         gchar ** iter_provider_ids = provider_ids;
136         while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids)
137         {
138                 const gchar* provider_name = *iter_provider_names;
139                 if(!provider_name)
140                         continue;
141                         
142                 const gchar* provider_id = *iter_provider_ids;
143                 if(!provider_id)
144                         continue;
145                 
146                 /* printf("debug: provider_name=%s\n", provider_name); */
147         
148                 /* Add the row: */
149                 GtkTreeIter iter;
150                 gtk_list_store_append (liststore, &iter);
151                 gtk_list_store_set(liststore, &iter, MODEL_COL_ID, provider_id, MODEL_COL_NAME, provider_name, -1);
152                 
153                 ++iter_provider_names;
154                 ++iter_provider_ids;    
155         }
156         
157         /* Free the result of modest_presets_get_providers()
158          * as specified by its documentation: */
159         g_strfreev (provider_names);
160         g_strfreev (provider_ids);
161         
162         /* Add the "Other" item: */
163         /* Note that ID 0 means "Other" for us: */
164         /* TODO: We need a Logical ID for this text. */
165         GtkTreeIter iter;
166         gtk_list_store_append (liststore, &iter);
167         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("Other..."), -1);
168         
169         /* Select the "Other" item: */
170         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
171 }
172
173 /**
174  * Returns the MCC number of the selected provider, 
175  * or NULL if no provider was selected, or "Other" was selected. 
176  */
177 gchar*
178 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
179 {
180         GtkTreeIter active;
181         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
182         if (found) {
183                 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
184
185                 gchar *id = NULL;
186                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
187                 return g_strdup(id);    
188         }
189
190         return NULL; /* Failed. */
191 }