1 /* Copyright (c) 2008, Nokia Corporation
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
31 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
36 #include "modest-maemo-utils.h"
37 #include "modest-country-picker.h"
38 #include <gtk/gtkliststore.h>
39 #include <gtk/gtkcelllayout.h>
40 #include <gtk/gtkcellrenderertext.h>
43 #include <string.h> /* For memcpy() */
46 #include <libintl.h> /* For dgettext(). */
48 /* Include config.h so that _() works: */
53 #define MAX_LINE_LEN 128 /* max length of a line in MCC file */
55 G_DEFINE_TYPE (ModestCountryPicker, modest_country_picker, HILDON_TYPE_PICKER_BUTTON);
60 /* GtkTreeModel *model; */
61 } ModestCountryPickerPrivate;
63 #define MODEST_COUNTRY_PICKER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
64 MODEST_TYPE_COUNTRY_PICKER, \
65 ModestCountryPickerPrivate))
68 modest_country_picker_get_property (GObject *object, guint property_id,
69 GValue *value, GParamSpec *pspec)
71 switch (property_id) {
73 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
78 modest_country_picker_set_property (GObject *object, guint property_id,
79 const GValue *value, GParamSpec *pspec)
81 switch (property_id) {
83 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88 modest_country_picker_dispose (GObject *object)
90 if (G_OBJECT_CLASS (modest_country_picker_parent_class)->dispose)
91 G_OBJECT_CLASS (modest_country_picker_parent_class)->dispose (object);
95 MODEL_COL_NAME = 0, /* string */
96 MODEL_COL_MCC = 1 /* the 'effective mcc' for this country */
101 modest_country_picker_finalize (GObject *object)
103 G_OBJECT_CLASS (modest_country_picker_parent_class)->finalize (object);
107 modest_country_picker_class_init (ModestCountryPickerClass *klass)
109 GObjectClass *object_class = G_OBJECT_CLASS (klass);
111 g_type_class_add_private (klass, sizeof (ModestCountryPickerPrivate));
113 object_class->get_property = modest_country_picker_get_property;
114 object_class->set_property = modest_country_picker_set_property;
115 object_class->dispose = modest_country_picker_dispose;
116 object_class->finalize = modest_country_picker_finalize;
122 /* cluster mcc's, based on the list
123 * http://en.wikipedia.org/wiki/Mobile_country_code
126 effective_mcc (gint mcc)
129 case 405: return 404; /* india */
130 case 441: return 440; /* japan */
131 case 235: return 234; /* united kingdom */
137 case 316: return 310; /* united states */
143 /* each line is of the form:
146 NOTE: this function is NOT re-entrant, the out-param country
147 are static strings that should NOT be freed. and will change when
148 calling this function again
150 also note, this function will return the "effective mcc", which
151 is the normalized mcc for a country - ie. even if the there
152 are multiple entries for the United States with various mccs,
153 this function will always return 310, even if the real mcc parsed
154 would be 314. see the 'effective_mcc' function above.
157 parse_mcc_mapping_line (const char* line, char** country)
159 char mcc[4]; /* the mcc code, always 3 bytes*/
160 gchar *iter, *tab, *final;
167 /* Go to the first tab (Country separator) */
168 tab = g_utf8_strrchr (line, -1, '\t');
169 *country = g_utf8_find_next_char (tab, NULL);
171 /* Replace by end of string */
172 final = g_utf8_strrchr (tab, g_utf8_strlen (tab, 100) + 1, '\n');
176 mcc[0] = g_utf8_get_char (line);
177 iter = g_utf8_find_next_char (line, NULL);
178 mcc[1] = g_utf8_get_char (iter);
179 iter = g_utf8_find_next_char (iter, NULL);
180 mcc[2] = g_utf8_get_char (iter);
183 return effective_mcc ((int) strtol ((const char*)mcc, NULL, 10));
186 /** Note that the mcc_mapping file is installed
187 * by the operator-wizard-settings package.
190 load_from_file (ModestCountryPicker *self, GtkListStore *liststore)
192 ModestCountryPickerPrivate *priv = MODEST_COUNTRY_PICKER_GET_PRIVATE (self);
194 char line[MAX_LINE_LEN];
195 guint previous_mcc = 0;
198 FILE *file = modest_maemo_open_mcc_mapping_file (&translated);
200 g_warning("Could not open mcc_mapping file");
204 /* Get the territory specified for the current locale */
205 territory = nl_langinfo (_NL_ADDRESS_COUNTRY_NAME);
207 while (fgets (line, MAX_LINE_LEN, file) != NULL) {
210 char *country = NULL;
211 const gchar *name_translated;
213 mcc = parse_mcc_mapping_line (line, &country);
214 if (!country || mcc == 0) {
215 g_warning ("%s: error parsing line: '%s'", __FUNCTION__, line);
219 if (mcc == previous_mcc) {
220 /* g_warning ("already seen: %s", line); */
225 if (!priv->locale_mcc) {
227 if (!g_utf8_collate (country, territory))
228 priv->locale_mcc = mcc;
229 g_debug ("'%s' || '%s' || %d", territory, country, mcc);
231 gchar *translation = dgettext ("osso-countries", country);
232 if (!g_utf8_collate (translation, territory))
233 priv->locale_mcc = mcc;
234 g_debug ("'%s' || '%s' || %d", territory, translation, mcc);
237 name_translated = dgettext ("osso-countries", country);
239 /* Add the row to the model: */
241 gtk_list_store_append (liststore, &iter);
242 gtk_list_store_set(liststore, &iter, MODEL_COL_MCC, mcc, MODEL_COL_NAME, name_translated, -1);
246 /* Fallback to Finland */
247 if (!priv->locale_mcc)
248 priv->locale_mcc = 244;
250 /* Sort the items: */
251 gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (liststore),
252 MODEL_COL_NAME, GTK_SORT_ASCENDING);
256 modest_country_picker_init (ModestCountryPicker *self)
258 ModestCountryPickerPrivate *priv = MODEST_COUNTRY_PICKER_GET_PRIVATE (self);
259 priv->locale_mcc = 0;
263 modest_country_picker_load_data(ModestCountryPicker *self)
265 GtkCellRenderer *renderer;
268 HildonTouchSelectorColumn *column;
270 /* Create a tree model for the combo box,
271 * with a string for the name, and an int for the MCC ID.
272 * This must match our MODEL_COLS enum constants.
274 model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT);
277 * The ID model column in not shown in the view. */
278 renderer = gtk_cell_renderer_text_new ();
279 g_object_set (G_OBJECT (renderer), "ellipsize", PANGO_ELLIPSIZE_END, NULL);
281 selector = hildon_touch_selector_new ();
282 column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (model),
283 renderer, "text", MODEL_COL_NAME, NULL);
284 g_object_set (G_OBJECT (column), "text-column", MODEL_COL_NAME, NULL);
286 /* Fill the model with rows: */
287 load_from_file (self, model);
289 /* Set this _after_ loading from file, it makes loading faster */
290 hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector),
291 0, GTK_TREE_MODEL (model));
292 hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector));
294 g_object_unref (model);
298 modest_country_picker_new (HildonSizeType size,
299 HildonButtonArrangement arrangement)
301 return g_object_new (MODEST_TYPE_COUNTRY_PICKER,
302 "arrangement", arrangement,
308 * Returns the MCC number of the selected country, or 0 if no country was selected.
311 modest_country_picker_get_active_country_mcc (ModestCountryPicker *self)
316 found = hildon_touch_selector_get_selected (hildon_picker_button_get_selector
317 (HILDON_PICKER_BUTTON (self)), 0, &active);
320 gtk_tree_model_get (hildon_touch_selector_get_model (hildon_picker_button_get_selector
321 (HILDON_PICKER_BUTTON (self)),
323 &active, MODEL_COL_MCC, &mcc, -1);
326 return 0; /* Failed. */
331 * Selects the MCC number of the selected country.
332 * Specify 0 to select no country.
335 modest_country_picker_set_active_country_locale (ModestCountryPicker *self)
337 ModestCountryPickerPrivate *priv = MODEST_COUNTRY_PICKER_GET_PRIVATE (self);
343 selector = GTK_WIDGET (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)));
344 model = hildon_touch_selector_get_model (HILDON_TOUCH_SELECTOR (selector), 0);
345 if (!gtk_tree_model_get_iter_first (model, &iter))
348 gtk_tree_model_get (model, &iter, MODEL_COL_MCC, ¤t_mcc, -1);
349 if (priv->locale_mcc == current_mcc) {
350 hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0,
352 hildon_button_set_value (HILDON_BUTTON (self),
353 hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
356 } while (gtk_tree_model_iter_next (model, &iter));
358 return FALSE; /* not found */