1 /* Copyright (c) 2007, Nokia Corporation
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>
13 #include <string.h> /* For memcpy() */
15 /* Include config.h so that _() works: */
20 G_DEFINE_TYPE (EasysetupProviderComboBox, easysetup_provider_combo_box, GTK_TYPE_COMBO_BOX);
22 #define PROVIDER_COMBO_BOX_GET_PRIVATE(o) \
23 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_PROVIDER_COMBO_BOX, EasysetupProviderComboBoxPrivate))
25 typedef struct _EasysetupProviderComboBoxPrivate EasysetupProviderComboBoxPrivate;
27 struct _EasysetupProviderComboBoxPrivate
33 easysetup_provider_combo_box_get_property (GObject *object, guint property_id,
34 GValue *value, GParamSpec *pspec)
36 switch (property_id) {
38 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
43 easysetup_provider_combo_box_set_property (GObject *object, guint property_id,
44 const GValue *value, GParamSpec *pspec)
46 switch (property_id) {
48 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
53 easysetup_provider_combo_box_dispose (GObject *object)
55 if (G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose)
56 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->dispose (object);
60 easysetup_provider_combo_box_finalize (GObject *object)
62 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (object);
64 g_object_unref (G_OBJECT (priv->model));
66 G_OBJECT_CLASS (easysetup_provider_combo_box_parent_class)->finalize (object);
70 easysetup_provider_combo_box_class_init (EasysetupProviderComboBoxClass *klass)
72 GObjectClass *object_class = G_OBJECT_CLASS (klass);
74 g_type_class_add_private (klass, sizeof (EasysetupProviderComboBoxPrivate));
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;
84 MODEL_COL_ID = 1 /* a string, not an int. */
90 easysetup_provider_combo_box_init (EasysetupProviderComboBox *self)
92 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (self);
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.
98 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING));
100 /* Setup the combo box: */
101 GtkComboBox *combobox = GTK_COMBO_BOX (self);
102 gtk_combo_box_set_model (combobox, priv->model);
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);
111 /* The application should call easysetup_provider_combo_box_fill()
112 * to actually add some rows. */
115 EasysetupProviderComboBox*
116 easysetup_provider_combo_box_new (void)
118 return g_object_new (EASYSETUP_TYPE_PROVIDER_COMBO_BOX, NULL);
121 void easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets, gint country_id)
123 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
125 /* Remove any existing rows: */
126 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
127 gtk_list_store_clear (liststore);
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);
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)
138 const gchar* provider_name = *iter_provider_names;
142 const gchar* provider_id = *iter_provider_ids;
146 /* printf("debug: provider_name=%s\n", provider_name); */
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);
153 ++iter_provider_names;
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);
162 /* Add the "Other" item: */
163 /* Note that ID 0 means "Other" for us: */
164 /* TODO: We need a Logical ID for this text. */
166 gtk_list_store_append (liststore, &iter);
167 gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("Other..."), -1);
169 /* Select the "Other" item: */
170 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
174 * Returns the MCC number of the selected provider,
175 * or NULL if no provider was selected, or "Other" was selected.
178 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
181 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
183 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
186 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
190 return NULL; /* Failed. */