* fix for bug NB#57325 ("Local folders on: %s")
[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 easysetup_provider_combo_box_fill (EasysetupProviderComboBox *combobox, ModestPresets *presets, gint country_id)
146 {       
147         EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
148         
149         /* Remove any existing rows: */
150         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
151         gtk_list_store_clear (liststore);
152         
153         /* Add the appropriate rows for this country, from the presets file: */
154         gchar ** provider_ids = NULL;
155         gchar ** provider_names = modest_presets_get_providers (presets, country_id, 
156                 TRUE /* include_globals */, &provider_ids);
157         
158         gchar ** iter_provider_names = provider_names;
159         gchar ** iter_provider_ids = provider_ids;
160         while(iter_provider_names && *iter_provider_names && iter_provider_ids && *iter_provider_ids)
161         {
162                 const gchar* provider_name = *iter_provider_names;
163                 if(!provider_name)
164                         continue;
165                         
166                 const gchar* provider_id = *iter_provider_ids;
167                 if(!provider_id)
168                         continue;
169                 
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                 gtk_list_store_set(liststore, &iter, MODEL_COL_ID, provider_id, MODEL_COL_NAME, provider_name, -1);
176                 
177                 ++iter_provider_names;
178                 ++iter_provider_ids;    
179         }
180         
181         /* Free the result of modest_presets_get_providers()
182          * as specified by its documentation: */
183         g_strfreev (provider_names);
184         g_strfreev (provider_ids);
185         
186         /* Add the "Other" item: */
187         /* Note that ID 0 means "Other" for us: */
188         /* TODO: We need a Logical ID for this text. */
189         GtkTreeIter iter;
190         gtk_list_store_append (liststore, &iter);
191         gtk_list_store_set (liststore, &iter, MODEL_COL_ID, 0, MODEL_COL_NAME, _("Other..."), -1);
192         
193         /* Select the "Other" item: */
194         gtk_combo_box_set_active_iter (GTK_COMBO_BOX (combobox), &iter);
195 }
196
197 /**
198  * Returns the MCC number of the selected provider, 
199  * or NULL if no provider was selected, or "Other" was selected. 
200  */
201 gchar*
202 easysetup_provider_combo_box_get_active_provider_id (EasysetupProviderComboBox *combobox)
203 {
204         GtkTreeIter active;
205         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (combobox), &active);
206         if (found) {
207                 EasysetupProviderComboBoxPrivate *priv = PROVIDER_COMBO_BOX_GET_PRIVATE (combobox);
208
209                 gchar *id = NULL;
210                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
211                 return g_strdup(id);    
212         }
213
214         return NULL; /* Failed. */
215 }