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