* optimize the parsing of the provider data, particularly the
[modest] / src / maemo / easysetup / modest-easysetup-provider-combo-box.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "modest-easysetup-provider-combo-box.h"
31 #include <gtk/gtkliststore.h>
32 #include <gtk/gtkcelllayout.h>
33 #include <gtk/gtkcellrenderertext.h>
34 #include <glib/gi18n.h>
35
36 #include <stdlib.h>
37 #include <string.h> /* For memcpy() */
38
39 /* Include config.h so that _() works: */
40 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 G_DEFINE_TYPE (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
45
46 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
47         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
48
49 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
50
51 struct _EasysetupProviderComboBoxPrivate
52 {
53         GtkTreeModel *model;
54 };
55
56 static void
57 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
58                                                                                                                         GValue *value, GParamSpec *pspec)
59 {
60         switch (property_id) {
61         default:
62                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
63         }
64 }
65
66 static void
67 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
68                                                                                                                         const GValue *value, GParamSpec *pspec)
69 {
70         switch (property_id) {
71         default:
72                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
73         }
74 }
75
76 static void
77 easysetup_provider_combo_box_dispose (GObject *object)
78 {
79         if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
80                 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
81 }
82
83 static void
84 easysetup_provider_combo_box_finalize (GObject *object)
85 {
86         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
87
88         g_object_unref (G_OBJECT (priv->model));
89
90         G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
91 }
92
93 static void
94 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
95 {
96         GObjectClass *object_class = G_OBJECT_CLASS (klass);
97
98         g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
99
100         object_class->get_property = easysetup_provider_combo_box_get_property;
101         object_class->set_property = easysetup_provider_combo_box_set_property;
102         object_class->dispose = easysetup_provider_combo_box_dispose;
103         object_class->finalize = easysetup_provider_combo_box_finalize;
104 }
105
106 enum MODEL_COLS {
107         MODEL_COL_NAME = 0,
108         MODEL_COL_ID = 1 /* a string, not an int. */
109 };
110
111
112
113 static void
114 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
115 {
116         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
117
118         /* Create a tree model for the combo box,
119          * with a string for the name, and a string for the ID (e.g. "vodafone.it").
120          * This must match our MODEL_COLS enum constants.
121          */
122         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
123
124         /* Setup the combo box: */
125         GtkComboBox *combobox = GTK_COMBO_BOX (self);
126         gtk_combo_box_set_model (combobox, priv->model);
127
128         /* Provider column:
129          * The ID model column in not shown in the view. */
130         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
131         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
132         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
133         "text", MODEL_COL_NAME, NULL);
134         
135         /* The application should call easysetup_provider_combo_box_fill()
136          * to actually add some rows. */
137 }
138
139 EasysetupProviderComboBox*
140 easysetup_provider_combo_box_new (void)
141 {
142         return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
143 }
144
145 void
146 easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets,
147                                    gint mcc)
148 {       
149         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
150         
151         /* Remove any existing rows: */
152         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
153         gtk_list_store_clear (liststore);
154         
155         GSList *provider_ids_used_already = NULL;
156         
157         /* Add the appropriate rows for this country, from the presets file: */
158         gchar ** provider_ids = NULL;
159         gchar ** provider_names = modest_presets_get_providers (presets, mcc, 
160                                                                 TRUE /* include_globals */, &provider_ids);     
161         gchar ** iter_provider_names = provider_names;
162         gchar ** iter_provider_ids = provider_ids;
163         while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids) {
164                 const gchar* provider_name = *iter_provider_names;
165                 const gchar* provider_id = *iter_provider_ids;
166                 
167                 /* Prevent duplicate providers: */
168                 if (g_slist_find_custom (provider_ids_used_already, 
169                                          provider_id, (GCompareFunc)strcmp) == NULL) {
170                         /* printf("debug: provider_name=%s\n", provider_name); */
171
172                         /* Add the row: */
173                         GtkTreeIter iter;
174                         gtk_list_store_append (liststore, &iter);
175                         
176                         gtk_list_store_set(liststore, &iter, 
177                                            MODEL_COL_ID, provider_id, 
178                                            MODEL_COL_NAME, provider_name, -1);
179                         
180                         provider_ids_used_already = g_slist_prepend (
181                                 provider_ids_used_already, (gpointer)g_strdup (provider_id));
182                 }
183
184                         ++iter_provider_names;
185                         ++iter_provider_ids;
186                 }
187
188         /* Free the result of modest_presets_get_providers()
189          * as specified by its documentation: */
190         g_strfreev (provider_names);
191         g_strfreev (provider_ids);
192
193         
194         /* Add the "Other" item: */
195         /* Note that ID 0 means "Other" for us: */
196         /* TODO: We need a Logical ID for this text. */
197         GtkTreeIter iter;
198         gtk_list_store_prepend (liststore, &iter);
199         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("mcen_va_serviceprovider_other"), -1);
200         
201         /* Select the "Other" item: */
202         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
203         
204         g_slist_foreach (provider_ids_used_already, (GFunc)g_free, NULL);
205         g_slist_free (provider_ids_used_already);
206 }
207
208 /**
209  * Returns the MCC number of the selected provider, 
210  * or NULL if no provider was selected, or "Other" was selected. 
211  */
212 gchar*
213 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
214 {
215         GtkTreeIter active;
216         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
217         if (found) {
218                 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
219
220                 gchar *id = NULL;
221                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
222                 return g_strdup(id);    
223         }
224
225         return NULL; /* Failed. */
226 }