1 /* Copyright (c) 2007, Nokia Corporation
6 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
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>
16 #include <string.h> /* For memcpy() */
18 G_DEFINE_TYPE (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
20 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
21 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
23 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
25 struct _EasysetupProviderComboBoxPrivate
31 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
32 GValue *value, GParamSpec *pspec)
34 switch (property_id) {
36 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
41 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
42 const GValue *value, GParamSpec *pspec)
44 switch (property_id) {
46 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
51 easysetup_provider_combo_box_dispose (GObject *object)
53 if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
54 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
58 easysetup_provider_combo_box_finalize (GObject *object)
60 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
62 g_object_unref (G_OBJECT (priv->model));
64 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
68 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
70 GObjectClass *object_class = G_OBJECT_CLASS (klass);
72 g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
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;
82 MODEL_COL_ID = 1 /* a string, not an int. */
88 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
90 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
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.
96 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
98 /* Setup the combo box: */
99 GtkComboBox *combobox = GTK_COMBO_BOX (self);
100 gtk_combo_box_set_model (combobox, priv->model);
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);
109 /* The application should call easysetup_provider_combo_box_fill()
110 * to actually add some rows. */
113 EasysetupProviderComboBox*
114 easysetup_provider_combo_box_new (void)
116 return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
119 void easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets, gint country_id)
121 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
123 /* Remove any existing rows: */
124 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
125 gtk_list_store_clear (liststore);
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);
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)
136 const gchar* provider_name = *iter_provider_names;
140 const gchar* provider_id = *iter_provider_ids;
144 /* printf("debug: provider_name=%s\n", provider_name); */
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);
151 ++iter_provider_names;
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);
160 /* Add the "Other" item: */
161 /* Note that ID 0 means "Other" for us: */
162 /* TODO: We need a Logical ID for this text. */
164 gtk_list_store_append (liststore, &iter);
165 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("Other..."), -1);
167 /* Select the "Other" item: */
168 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
172 * Returns the MCC number of the selected provider,
173 * or NULL if no provider was selected, or "Other" was selected.
176 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
179 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
181 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
184 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
188 return NULL; /* Failed. */