1 /* Copyright (c) 2006, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
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>
37 #include <string.h> /* For memcpy() */
39 /* Include config.h so that _() works: */
44 G_DEFINE_TYPE (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
46 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
47 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
49 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
51 struct _EasysetupProviderComboBoxPrivate
57 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
58 GValue *value, GParamSpec *pspec)
60 switch (property_id) {
62 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
67 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
68 const GValue *value, GParamSpec *pspec)
70 switch (property_id) {
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
77 easysetup_provider_combo_box_dispose (GObject *object)
79 if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
80 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
84 easysetup_provider_combo_box_finalize (GObject *object)
86 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
88 g_object_unref (G_OBJECT (priv->model));
90 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
94 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
96 GObjectClass *object_class = G_OBJECT_CLASS (klass);
98 g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
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;
108 MODEL_COL_ID = 1 /* a string, not an int. */
114 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
116 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
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.
122 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
124 /* Setup the combo box: */
125 GtkComboBox *combobox = GTK_COMBO_BOX (self);
126 gtk_combo_box_set_model (combobox, priv->model);
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);
135 /* The application should call easysetup_provider_combo_box_fill()
136 * to actually add some rows. */
139 EasysetupProviderComboBox*
140 easysetup_provider_combo_box_new (void)
142 return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
145 void easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets, GSList * list_country_id)
147 /* If the list is empty then use mcc=0 to get the providers for all countries: */
148 GSList *list = list_country_id;
149 GSList *fake_list = NULL;
150 if (!list_country_id) {
151 fake_list = g_slist_append(fake_list, GUINT_TO_POINTER(0));
156 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
158 /* Remove any existing rows: */
159 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
160 gtk_list_store_clear (liststore);
162 GSList *provider_ids_used_already = NULL;
164 GSList *iter_ids = list;
166 const guint country_id = GPOINTER_TO_UINT (iter_ids->data);
168 /* Add the appropriate rows for this country, from the presets file: */
169 gchar ** provider_ids = NULL;
170 gchar ** provider_names = modest_presets_get_providers (presets, country_id,
171 TRUE /* include_globals */, &provider_ids);
173 gchar ** iter_provider_names = provider_names;
174 gchar ** iter_provider_ids = provider_ids;
175 while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids)
177 const gchar* provider_name = *iter_provider_names;
178 const gchar* provider_id = *iter_provider_ids;
180 /* Prevent duplicate providers: */
181 if (g_slist_find_custom (provider_ids_used_already,
182 provider_id, (GCompareFunc)strcmp) == NULL) {
183 /* printf("debug: provider_name=%s\n", provider_name); */
187 gtk_list_store_append (liststore, &iter);
189 gtk_list_store_set(liststore, &iter,
190 MODEL_COL_ID, provider_id,
191 MODEL_COL_NAME, provider_name, -1);
193 provider_ids_used_already = g_slist_prepend (
194 provider_ids_used_already, (gpointer)g_strdup (provider_id));
197 ++iter_provider_names;
201 /* Free the result of modest_presets_get_providers()
202 * as specified by its documentation: */
203 g_strfreev (provider_names);
204 g_strfreev (provider_ids);
206 iter_ids = g_slist_next (iter_ids);
209 /* Add the "Other" item: */
210 /* Note that ID 0 means "Other" for us: */
211 /* TODO: We need a Logical ID for this text. */
213 gtk_list_store_prepend (liststore, &iter);
214 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("mcen_va_serviceprovider_other"), -1);
216 /* Select the "Other" item: */
217 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
219 g_slist_foreach (provider_ids_used_already, (GFunc)g_free, NULL);
220 g_slist_free (provider_ids_used_already);
223 g_slist_free (fake_list);
227 * Returns the MCC number of the selected provider,
228 * or NULL if no provider was selected, or "Other" was selected.
231 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
234 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
236 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
239 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
243 return NULL; /* Failed. */