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