In stead of using tny_folder_refresh, use tny_folder_poke_status in case folders...
[modest] / src / maemo / easysetup / modest-easysetup-country-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 #ifndef _GNU_SOURCE
31 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
32 #endif
33
34 #include <stdio.h>
35
36 #include "modest-easysetup-country-combo-box.h"
37 #include <gtk/gtkliststore.h>
38 #include <gtk/gtkcelllayout.h>
39 #include <gtk/gtkcellrenderertext.h>
40
41 #include <stdlib.h>
42 #include <string.h> /* For memcpy() */
43
44 #include <libintl.h> /* For dgettext(). */
45
46 /* Include config.h so that _() works: */
47 #ifdef HAVE_CONFIG_H
48 #include <config.h>
49 #endif
50
51 G_DEFINE_TYPE (EasysetupCountryComboBox, easysetup_country_combo_box, GTK_TYPE_COMBO_BOX);
52
53 #define COUNTRY_COMBO_BOX_GET_PRIVATE(o) \
54         (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_COUNTRY_COMBO_BOX, EasysetupCountryComboBoxPrivate))
55
56 typedef struct _EasysetupCountryComboBoxPrivate EasysetupCountryComboBoxPrivate;
57
58 struct _EasysetupCountryComboBoxPrivate
59 {
60         GtkTreeModel *model;
61 };
62
63 static void
64 easysetup_country_combo_box_get_property (GObject *object, guint property_id,
65                                                                                                                         GValue *value, GParamSpec *pspec)
66 {
67         switch (property_id) {
68         default:
69                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
70         }
71 }
72
73 static void
74 easysetup_country_combo_box_set_property (GObject *object, guint property_id,
75                                                                                                                         const GValue *value, GParamSpec *pspec)
76 {
77         switch (property_id) {
78         default:
79                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
80         }
81 }
82
83 static void
84 easysetup_country_combo_box_dispose (GObject *object)
85 {
86         if (G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose)
87                 G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose (object);
88 }
89
90 static void
91 easysetup_country_combo_box_finalize (GObject *object)
92 {
93         EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (object);
94
95         g_object_unref (G_OBJECT (priv->model));
96
97         G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->finalize (object);
98 }
99
100 static void
101 easysetup_country_combo_box_class_init (EasysetupCountryComboBoxClass *klass)
102 {
103         GObjectClass *object_class = G_OBJECT_CLASS (klass);
104
105         g_type_class_add_private (klass, sizeof (EasysetupCountryComboBoxPrivate));
106
107         object_class->get_property = easysetup_country_combo_box_get_property;
108         object_class->set_property = easysetup_country_combo_box_set_property;
109         object_class->dispose = easysetup_country_combo_box_dispose;
110         object_class->finalize = easysetup_country_combo_box_finalize;
111 }
112
113 enum MODEL_COLS {
114         MODEL_COL_NAME = 0,
115         MODEL_COL_ID = 1
116 };
117
118 /** id and country must be freed.
119  */
120 static void parse_mcc_mapping_line (const char* line, char** id, char** country)
121 {
122         /* Initialize output parameters: */
123         *id = NULL;
124         *country = NULL;
125         
126         g_assert(line);
127         
128         const gboolean is_valid_utf8 = g_utf8_validate (line, -1, NULL);
129         if(!is_valid_utf8) {
130                 g_warning("UTF8 validation failed.");
131                 return;
132         }
133         
134         /* Look at each character, to find the whitespace between the ID and name: */
135         char* result_id = NULL;
136         char* result_country = NULL;
137         
138         const char* p = line;
139         const char* p_start_of_country = NULL;
140         while (p && *p)
141         {
142                 p = g_utf8_next_char(p);
143                 gunichar ch = g_utf8_get_char(p);
144                 if (g_unichar_isspace(ch)) { /* Note: This checks for any whitespace, not just space. */
145                         if(!result_id) {
146                                 /* The text before this must be the ID: */
147                                 const int length = p - line;
148                                 result_id = g_malloc (length + 1); /* 1 for null-termination. */
149                                 memcpy(result_id, line, length);
150                                 result_id[length] = 0; /* Null-termination. */
151                         }
152                         else if(p_start_of_country)
153                         {
154                                 /* This whitespace is probably the newline after the country. */
155                                 
156                                 /* The text after the whitespace, after the ID, must be the country: */
157                                 int length = p - p_start_of_country;
158                                 result_country = g_malloc(length + 1);
159                                 memcpy(result_country, p_start_of_country, length);
160                                 result_country[length] = 0; /* Null-termination. */
161                                 break;
162                         }
163                 }
164                 else if(result_id && !p_start_of_country) {
165                         p_start_of_country = p;
166                 }
167         }
168         
169         *id = result_id;
170         *country = result_country;
171 }
172
173 /** Note that the mcc_mapping file is installed 
174  * by the operator-wizard-settings package.
175  */
176 static void load_from_file (EasysetupCountryComboBox *self)
177 {
178         EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
179         
180         /* Load the file one line at a time: */
181 #ifndef MODEST_HILDON_VERSION_0
182         const gchar* filepath = PROVIDER_DATA_DIR "/mcc_mapping";
183 #else
184         /* this is the official version, in the 'operator-wizard-settings' package */
185         const gchar* filepath = "/usr/share/operator-wizard/mcc_mapping";
186 #endif /*MODEST_HILDON_VERSION_0*/
187         FILE *file = fopen(filepath, "r");
188         if (!file)
189         {
190                 const gchar* filepath_hack = HACK_TOP_SRCDIR "src/maemo/easysetup/mcc_mapping";
191                 g_warning ("Could not locate the official mcc_mapping countries list file from %s, "
192                         "so attempting to load it instead from %s", filepath, filepath_hack);
193                 file = fopen(filepath_hack, "r");
194         }
195         
196         if (!file) {
197                 g_warning("Could not open mcc_mapping file");
198                 return;
199         }
200
201         GtkListStore *liststore = GTK_LIST_STORE (priv->model);
202                         
203         /* We use the getline() GNU extension,
204          * because it reads per line, which simplifies our code,
205          * and it doesn't require us to hard-code a buffer length.
206          */
207         int len = 0;
208         char *line = NULL;
209         while (getline (&line, &len, file) > 0) { /* getline will realloc line if necessary. */
210                 /* printf ("DBEUG: len=%d, line: %s\n", len, line); */
211                 
212                 char *id_str = NULL;
213                 char *country = NULL;
214                 parse_mcc_mapping_line (line, &id_str, &country);
215                 //printf("DEBUG: parsed: id=%s, country=%s\n", id_str, country); 
216                 
217                 if(id_str && country) {
218                         guint id = (guint)g_ascii_strtod(id_str, NULL); /* Note that this parses locale-independent text. */
219                         
220                         /* Get the translation for the country name:
221                          * Note that the osso_countries_1.0 translation domain files are installed 
222                          * by the operator-wizard-settings package. */
223                         /* For post-Bora, there is a separate (meta)package osso-countries-l10n-mr0 */
224                         const gchar *name_translated = dgettext ("osso-countries", country);
225                         if(!name_translated)
226                           name_translated = country;
227                         
228                         /* Add the row to the model: */
229                         GtkTreeIter iter;
230                         gtk_list_store_append (liststore, &iter);
231                         gtk_list_store_set(liststore, &iter, MODEL_COL_ID, id, MODEL_COL_NAME, name_translated, -1);
232                 }
233                 
234                 g_free (id_str);
235                 g_free (country);
236         }
237
238         if (line)
239                 free (line);
240                 
241         fclose (file);
242 }
243
244 static void
245 easysetup_country_combo_box_init (EasysetupCountryComboBox *self)
246 {
247         EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
248
249         /* Create a tree model for the combo box,
250          * with a string for the name, and an int for the MCC ID.
251          * This must match our MODEL_COLS enum constants.
252          */
253         priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT));
254
255         /* Setup the combo box: */
256         GtkComboBox *combobox = GTK_COMBO_BOX (self);
257         gtk_combo_box_set_model (combobox, priv->model);
258
259         /* Country column:
260          * The ID model column in not shown in the view. */
261         GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
262         gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
263         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
264         "text", MODEL_COL_NAME, NULL);
265         
266         /* Fill the model with rows: */
267         load_from_file (self);
268 }
269
270 EasysetupCountryComboBox*
271 easysetup_country_combo_box_new (void)
272 {
273         return g_object_new (EASYSETUP_TYPE_COUNTRY_COMBO_BOX, NULL);
274 }
275
276 /**
277  * Returns the MCC number of the selected country, or 0 if no country was selected. 
278  */
279 guint
280 easysetup_country_combo_box_get_active_country_id (EasysetupCountryComboBox *self)
281 {
282         GtkTreeIter active;
283         const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &active);
284         if (found) {
285                 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
286
287                 guint id = 0;
288                 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1); 
289                 return id;      
290         }
291
292         return 0; /* Failed. */
293 }
294
295
296 /* This allows us to pass more than one piece of data to the signal handler,
297  * and get a result: */
298 typedef struct 
299 {
300                 EasysetupCountryComboBox* self;
301                 guint mcc_id;
302                 gboolean found;
303 } ForEachData;
304
305 static gboolean
306 on_model_foreach_select_id(GtkTreeModel *model, 
307         GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
308 {
309         ForEachData *state = (ForEachData*)(user_data);
310         
311         /* Select the item if it has the matching ID: */
312         guint id = 0;
313         gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1); 
314         if(id == state->mcc_id) {
315                 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
316                 
317                 state->found = TRUE;
318                 return TRUE; /* Stop walking the tree. */
319         }
320         
321         return FALSE; /* Keep walking the tree. */
322 }
323
324 /**
325  * Selects the MCC number of the selected country.
326  * Specify 0 to select no country. 
327  */
328 gboolean
329 easysetup_country_combo_box_set_active_country_id (EasysetupCountryComboBox *self, guint mcc_id)
330 {
331         EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
332         
333         /* Create a state instance so we can send two items of data to the signal handler: */
334         ForEachData *state = g_new0 (ForEachData, 1);
335         state->self = self;
336         state->mcc_id = mcc_id;
337         state->found = FALSE;
338         
339         /* Look at each item, and select the one with the correct ID: */
340         gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
341
342         const gboolean result = state->found;
343         
344         /* Free the state instance: */
345         g_free(state);
346         
347         return result;
348 }
349