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