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