From 3b5dc6fc08a4bb09b660ce6c777edc2e35fdcdfa Mon Sep 17 00:00:00 2001 From: Jose Dapena Paz Date: Wed, 8 Apr 2009 15:43:00 +0000 Subject: [PATCH] Refactor mcc parsing code pmo-trunk-r8700 --- src/hildon2/modest-country-picker.c | 167 ++--------------- src/hildon2/modest-maemo-utils.c | 36 ---- src/hildon2/modest-maemo-utils.h | 9 - src/hildon2/modest-platform.c | 3 +- .../easysetup/modest-easysetup-country-combo-box.c | 166 ++--------------- src/maemo/modest-maemo-utils.c | 28 --- src/maemo/modest-maemo-utils.h | 9 - src/maemo/modest-platform.c | 3 +- src/modest-defs.h | 2 +- src/modest-utils.c | 190 ++++++++++++++++++++ src/modest-utils.h | 42 +++++ 11 files changed, 266 insertions(+), 389 deletions(-) diff --git a/src/hildon2/modest-country-picker.c b/src/hildon2/modest-country-picker.c index 26b560d..1bf9dab 100644 --- a/src/hildon2/modest-country-picker.c +++ b/src/hildon2/modest-country-picker.c @@ -33,7 +33,7 @@ #include -#include "modest-maemo-utils.h" +#include "modest-utils.h" #include "modest-country-picker.h" #include #include @@ -42,7 +42,6 @@ #include #include /* For memcpy() */ -#include #include #include /* For dgettext(). */ @@ -51,8 +50,6 @@ #include #endif -#define MAX_LINE_LEN 128 /* max length of a line in MCC file */ - G_DEFINE_TYPE (ModestCountryPicker, modest_country_picker, HILDON_TYPE_PICKER_BUTTON); typedef struct @@ -120,143 +117,6 @@ modest_country_picker_class_init (ModestCountryPickerClass *klass) -/* cluster mcc's, based on the list - * http://en.wikipedia.org/wiki/Mobile_country_code - */ -static int -effective_mcc (gint mcc) -{ - switch (mcc) { - case 405: return 404; /* india */ - case 441: return 440; /* japan */ - case 235: return 234; /* united kingdom */ - case 311: - case 312: - case 313: - case 314: - case 315: - case 316: return 310; /* united states */ - default: return mcc; - } -} - - -/* each line is of the form: - xxx logical_id - - NOTE: this function is NOT re-entrant, the out-param country - are static strings that should NOT be freed. and will change when - calling this function again - - also note, this function will return the "effective mcc", which - is the normalized mcc for a country - ie. even if the there - are multiple entries for the United States with various mccs, - this function will always return 310, even if the real mcc parsed - would be 314. see the 'effective_mcc' function above. -*/ -static int -parse_mcc_mapping_line (const char* line, char** country) -{ - char mcc[4]; /* the mcc code, always 3 bytes*/ - gchar *iter, *tab, *final; - - if (!line) { - *country = NULL; - return 0; - } - - /* Go to the first tab (Country separator) */ - tab = g_utf8_strrchr (line, -1, '\t'); - if (!tab) - return 0; - - *country = g_utf8_find_next_char (tab, NULL); - - /* Replace by end of string. We need to use strlen, because - g_utf8_strrchr expects bytes and not UTF8 characters */ - final = g_utf8_strrchr (tab, strlen (tab) + 1, '\n'); - if (G_LIKELY (final)) - *final = '\0'; - else - tab[strlen(tab) - 1] = '\0'; - - /* Get MCC code */ - mcc[0] = g_utf8_get_char (line); - iter = g_utf8_find_next_char (line, NULL); - mcc[1] = g_utf8_get_char (iter); - iter = g_utf8_find_next_char (iter, NULL); - mcc[2] = g_utf8_get_char (iter); - mcc[3] = '\0'; - - return effective_mcc ((int) strtol ((const char*)mcc, NULL, 10)); -} - -/** Note that the mcc_mapping file is installed - * by the operator-wizard-settings package. - */ -static void -load_from_file (ModestCountryPicker *self, GtkListStore *liststore) -{ - ModestCountryPickerPrivate *priv = MODEST_COUNTRY_PICKER_GET_PRIVATE (self); - gboolean translated; - char line[MAX_LINE_LEN]; - guint previous_mcc = 0; - gchar *territory; - - FILE *file = modest_maemo_open_mcc_mapping_file (&translated); - if (!file) { - g_warning("Could not open mcc_mapping file"); - return; - } - - /* Get the territory specified for the current locale */ - territory = nl_langinfo (_NL_ADDRESS_COUNTRY_NAME); - - while (fgets (line, MAX_LINE_LEN, file) != NULL) { - - int mcc; - char *country = NULL; - const gchar *name_translated; - - mcc = parse_mcc_mapping_line (line, &country); - if (!country || mcc == 0) { - g_warning ("%s: error parsing line: '%s'", __FUNCTION__, line); - continue; - } - - if (mcc == previous_mcc) { - /* g_warning ("already seen: %s", line); */ - continue; - } - previous_mcc = mcc; - - if (!priv->locale_mcc) { - if (translated) { - if (!g_utf8_collate (country, territory)) - priv->locale_mcc = mcc; - } else { - gchar *translation = dgettext ("osso-countries", country); - if (!g_utf8_collate (translation, territory)) - priv->locale_mcc = mcc; - } - } - name_translated = dgettext ("osso-countries", country); - - /* Add the row to the model: */ - GtkTreeIter iter; - gtk_list_store_append (liststore, &iter); - gtk_list_store_set(liststore, &iter, MODEL_COL_MCC, mcc, MODEL_COL_NAME, name_translated, -1); - } - fclose (file); - - /* Fallback to Finland */ - if (!priv->locale_mcc) - priv->locale_mcc = 244; - - /* Sort the items: */ - gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (liststore), - MODEL_COL_NAME, GTK_SORT_ASCENDING); -} static void modest_country_picker_init (ModestCountryPicker *self) @@ -297,14 +157,17 @@ modest_country_picker_load_data(ModestCountryPicker *self) { GtkCellRenderer *renderer; GtkWidget *selector; - GtkListStore *model; + GtkTreeModel *model; HildonTouchSelectorColumn *column; + ModestCountryPickerPrivate *priv; + + priv = MODEST_COUNTRY_PICKER_GET_PRIVATE (self); /* Create a tree model for the combo box, * with a string for the name, and an int for the MCC ID. * This must match our MODEL_COLS enum constants. */ - model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT); + model = modest_utils_create_country_model (); /* Country column: * The ID model column in not shown in the view. */ @@ -313,22 +176,22 @@ modest_country_picker_load_data(ModestCountryPicker *self) selector = hildon_touch_selector_entry_new (); hildon_touch_selector_set_print_func (HILDON_TOUCH_SELECTOR (selector), (HildonTouchSelectorPrintFunc) country_picker_print_func); - column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), GTK_TREE_MODEL (model), - renderer, "text", MODEL_COL_NAME, NULL); - g_object_set (G_OBJECT (column), "text-column", MODEL_COL_NAME, NULL); - /* Fill the model with rows: */ - load_from_file (self, model); + column = hildon_touch_selector_append_column (HILDON_TOUCH_SELECTOR (selector), model, + renderer, "text", MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, NULL); + g_object_set (G_OBJECT (column), "text-column", MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, NULL); + modest_utils_fill_country_model (model, &(priv->locale_mcc)); /* Set this _after_ loading from file, it makes loading faster */ hildon_touch_selector_set_model (HILDON_TOUCH_SELECTOR (selector), - 0, GTK_TREE_MODEL (model)); + 0, model); hildon_touch_selector_entry_set_text_column (HILDON_TOUCH_SELECTOR_ENTRY (selector), - MODEL_COL_NAME); + MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME); hildon_touch_selector_entry_set_input_mode (HILDON_TOUCH_SELECTOR_ENTRY (selector), HILDON_GTK_INPUT_MODE_ALPHA | HILDON_GTK_INPUT_MODE_AUTOCAP); + hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector)); g_object_unref (model); @@ -360,7 +223,7 @@ modest_country_picker_get_active_country_mcc (ModestCountryPicker *self) gtk_tree_model_get (hildon_touch_selector_get_model (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)), 0), - &active, MODEL_COL_MCC, &mcc, -1); + &active, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, &mcc, -1); return mcc; } return 0; /* Failed. */ @@ -385,7 +248,7 @@ modest_country_picker_set_active_country_locale (ModestCountryPicker *self) if (!gtk_tree_model_get_iter_first (model, &iter)) return FALSE; do { - gtk_tree_model_get (model, &iter, MODEL_COL_MCC, ¤t_mcc, -1); + gtk_tree_model_get (model, &iter, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, ¤t_mcc, -1); if (priv->locale_mcc == current_mcc) { hildon_touch_selector_select_iter (HILDON_TOUCH_SELECTOR (selector), 0, &iter, TRUE); diff --git a/src/hildon2/modest-maemo-utils.c b/src/hildon2/modest-maemo-utils.c index a0bad06..e703c7d 100644 --- a/src/hildon2/modest-maemo-utils.c +++ b/src/hildon2/modest-maemo-utils.c @@ -156,42 +156,6 @@ modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, #endif /* MODEST_HAVE_HILDON1_WIDGETS */ } -FILE* -modest_maemo_open_mcc_mapping_file (gboolean *translated) -{ - FILE* result = NULL; - const gchar* path; - gchar *path1 = g_strdup_printf ("%s.%s", MODEST_OPERATOR_WIZARD_MCC_MAPPING, getenv("LANG")); - const gchar* path2 = MODEST_MCC_MAPPING; - - if (translated) - *translated = TRUE; - - if (access (path1, R_OK) == 0) { - path = path1; - } else if (access (MODEST_OPERATOR_WIZARD_MCC_MAPPING, R_OK) == 0) { - path = MODEST_OPERATOR_WIZARD_MCC_MAPPING; - if (translated) - *translated = FALSE; - } else if (access (path2, R_OK) == 0) { - path = path2; - } else { - g_warning ("%s: neither '%s' nor '%s' is a readable mapping file", - __FUNCTION__, path1, path2); - goto end; - } - - result = fopen (path, "r"); - if (!result) { - g_warning ("%s: error opening mapping file '%s': %s", - __FUNCTION__, path, strerror(errno)); - goto end; - } - end: - g_free (path1); - return result; -} - GtkWidget * modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager, const gchar *item_name) diff --git a/src/hildon2/modest-maemo-utils.h b/src/hildon2/modest-maemo-utils.h index 271905f..8832fff 100644 --- a/src/hildon2/modest-maemo-utils.h +++ b/src/hildon2/modest-maemo-utils.h @@ -75,15 +75,6 @@ void modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser); osso_context_t *modest_maemo_utils_get_osso_context (void); /** - * modest_maemo_open_mcc_mapping_file: - * - * open the mcc mapping file, or NULL if it fails - * - * Returns: file ptr or NULL in case of error - */ -FILE* modest_maemo_open_mcc_mapping_file (gboolean *translated); - -/** * modest_maemo_set_thumbable_scrollbar: * @win: a scrollable window * @thumbable: set it to thumbable (TRUE) or small (FALSE) diff --git a/src/hildon2/modest-platform.c b/src/hildon2/modest-platform.c index e8fc6a8..a178366 100644 --- a/src/hildon2/modest-platform.c +++ b/src/hildon2/modest-platform.c @@ -39,6 +39,7 @@ #include "modest-widget-memory.h" #include #include +#include #include #include #include @@ -132,7 +133,7 @@ on_modest_conf_update_interval_changed (ModestConf* self, static gboolean check_required_files (void) { - FILE *mcc_file = modest_maemo_open_mcc_mapping_file (NULL); + FILE *mcc_file = modest_utils_open_mcc_mapping_file (NULL); if (!mcc_file) { g_printerr ("modest: check for mcc file failed\n"); return FALSE; diff --git a/src/maemo/easysetup/modest-easysetup-country-combo-box.c b/src/maemo/easysetup/modest-easysetup-country-combo-box.c index 0f7b3dc..fc900af 100644 --- a/src/maemo/easysetup/modest-easysetup-country-combo-box.c +++ b/src/maemo/easysetup/modest-easysetup-country-combo-box.c @@ -33,7 +33,7 @@ #include -#include "../modest-maemo-utils.h" +#include #include "modest-easysetup-country-combo-box.h" #include #include @@ -121,145 +121,6 @@ easysetup_country_combo_box_class_init (EasysetupCountryComboBoxClass *klass) } - - -/* cluster mcc's, based on the list - * http://en.wikipedia.org/wiki/Mobile_country_code - */ -static int -effective_mcc (gint mcc) -{ - switch (mcc) { - case 405: return 404; /* india */ - case 441: return 440; /* japan */ - case 235: return 234; /* united kingdom */ - case 311: - case 312: - case 313: - case 314: - case 315: - case 316: return 310; /* united states */ - default: return mcc; - } -} - - -/* each line is of the form: - xxx logical_id - - NOTE: this function is NOT re-entrant, the out-param country - are static strings that should NOT be freed. and will change when - calling this function again - - also note, this function will return the "effective mcc", which - is the normalized mcc for a country - ie. even if the there - are multiple entries for the United States with various mccs, - this function will always return 310, even if the real mcc parsed - would be 314. see the 'effective_mcc' function above. -*/ -static int -parse_mcc_mapping_line (const char* line, char** country) -{ - int i, j; - char mcc[4]; /* the mcc code, always 3 bytes*/ - static char my_country[128]; - - if (!line) { - *country = NULL; - return 0; - } - - for (i = 3, j = 0; i < 128; ++i) { - char kar = line[i]; - if (kar == '\0') - break; - else if (kar < '_') - continue; - else - my_country [j++] = kar; - } - my_country[j] = '\0'; - - mcc[0] = line[0]; - mcc[1] = line[1]; - mcc[2] = line[2]; - mcc[3] = '\0'; - - *country = my_country; - - return effective_mcc ((int) strtol ((const char*)mcc, NULL, 10)); -} - -/** Note that the mcc_mapping file is installed - * by the operator-wizard-settings package. - */ -static void -load_from_file (EasysetupCountryComboBox *self, GtkListStore *liststore) -{ - ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self); - - char line[MAX_LINE_LEN]; - guint previous_mcc = 0; - gchar *territory, *fallback = NULL; - gchar *current_locale; - - /* Get the territory specified for the current locale */ - territory = nl_langinfo (_NL_ADDRESS_COUNTRY_NAME); - - /* Tricky stuff, the translations of the OSSO countries does - not always correspond to the country names in locale - databases. Add all these cases here. sergio */ - if (!strcmp (territory, "United Kingdom")) - fallback = g_strdup ("UK"); - - current_locale = setlocale (LC_ALL ,NULL); - - FILE *file = modest_maemo_open_mcc_mapping_file (); - if (!file) { - g_warning("Could not open mcc_mapping file"); - return; - } - - while (fgets (line, MAX_LINE_LEN, file) != NULL) { - - int mcc; - char *country = NULL; - const gchar *name_translated, *english_translation; - - mcc = parse_mcc_mapping_line (line, &country); - if (!country || mcc == 0) { - g_warning ("%s: error parsing line: '%s'", __FUNCTION__, line); - continue; - } - - if (mcc == previous_mcc) { - /* g_warning ("already seen: %s", line); */ - continue; - } - previous_mcc = mcc; - - if (!priv->locale_mcc) { - english_translation = dgettext ("osso-countries", country); - if (!strcmp (english_translation, territory) || - (fallback && !strcmp (english_translation, fallback))) - priv->locale_mcc = mcc; - } - name_translated = dgettext ("osso-countries", country); - - /* Add the row to the model: */ - GtkTreeIter iter; - gtk_list_store_append (liststore, &iter); - gtk_list_store_set(liststore, &iter, MODEL_COL_MCC, mcc, MODEL_COL_NAME, name_translated, -1); - } - fclose (file); - - /* Sort the items: */ - gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (liststore), - MODEL_COL_NAME, GTK_SORT_ASCENDING); - - g_free (fallback); -} - static void easysetup_country_combo_box_init (EasysetupCountryComboBox *self) { @@ -270,13 +131,15 @@ easysetup_country_combo_box_init (EasysetupCountryComboBox *self) void easysetup_country_combo_box_load_data(EasysetupCountryComboBox *self) { - GtkListStore *model; + ModestEasysetupCountryComboBoxPrivate *priv; + GtkTreeModel *model; + priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self); /* Create a tree model for the combo box, * with a string for the name, and an int for the MCC ID. * This must match our MODEL_COLS enum constants. */ - model = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT); + model = modest_utils_create_country_model (); /* Country column: * The ID model column in not shown in the view. */ @@ -286,24 +149,23 @@ easysetup_country_combo_box_load_data(EasysetupCountryComboBox *self) #if MODEST_HILDON_API < 2 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (self), renderer, TRUE); gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (self), renderer, - "text", MODEL_COL_NAME, NULL); + "text", MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, NULL); #else GtkWidget *selector = hildon_touch_selector_new (); hildon_picker_button_set_selector (HILDON_PICKER_BUTTON (self), HILDON_TOUCH_SELECTOR (selector)); hildon_touch_selector_append_column (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)), - GTK_TREE_MODEL (model), - renderer, "text", MODEL_COL_NAME, NULL); + model, + renderer, "text", MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, NULL); #endif - /* Fill the model with rows: */ - load_from_file (self, model); + modest_utils_fill_country_model (model, &(priv->locale_mcc)); /* Set this _after_ loading from file, it makes loading faster */ #if MODEST_HILDON_API < 2 - gtk_combo_box_set_model (GTK_COMBO_BOX (self), GTK_TREE_MODEL (model)); + gtk_combo_box_set_model (GTK_COMBO_BOX (self), model); #else hildon_touch_selector_set_model (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)), - 0, GTK_TREE_MODEL (model)); + 0, model); #endif } @@ -340,12 +202,12 @@ easysetup_country_combo_box_get_active_country_mcc (EasysetupCountryComboBox *se gint mcc = 0; #if MODEST_HILDON_API < 2 gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (self)), - &active, MODEL_COL_MCC, &mcc, -1); + &active, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, &mcc, -1); #else gtk_tree_model_get (hildon_touch_selector_get_model (hildon_picker_button_get_selector (HILDON_PICKER_BUTTON (self)), 0), - &active, MODEL_COL_MCC, &mcc, -1); + &active, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, &mcc, -1); #endif return mcc; } @@ -376,7 +238,7 @@ easysetup_country_combo_box_set_active_country_locale (EasysetupCountryComboBox if (!gtk_tree_model_get_iter_first (model, &iter)) return FALSE; do { - gtk_tree_model_get (model, &iter, MODEL_COL_MCC, ¤t_mcc, -1); + gtk_tree_model_get (model, &iter, MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, ¤t_mcc, -1); if (priv->locale_mcc == current_mcc) { #if MODEST_HILDON_API < 2 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter); diff --git a/src/maemo/modest-maemo-utils.c b/src/maemo/modest-maemo-utils.c index d1a10c7..28f73e1 100644 --- a/src/maemo/modest-maemo-utils.c +++ b/src/maemo/modest-maemo-utils.c @@ -237,34 +237,6 @@ modest_maemo_set_thumbable_scrollbar (GtkScrolledWindow *win, #endif /* MODEST_HAVE_HILDON1_WIDGETS */ } -FILE* -modest_maemo_open_mcc_mapping_file (void) -{ - FILE* result; - - const gchar* path; - const gchar* path1 = MODEST_OPERATOR_WIZARD_MCC_MAPPING; - const gchar* path2 = MODEST_MCC_MAPPING; - - if (access(path1, R_OK) == 0) - path = path1; - else if (access(path2, R_OK) == 0) - path = path2; - else { - g_warning ("%s: neither '%s' nor '%s' is a readable mapping file", - __FUNCTION__, path1, path2); - return NULL; - } - - result = fopen (path, "r"); - if (!result) { - g_warning ("%s: error opening mapping file '%s': %s", - __FUNCTION__, path, strerror(errno)); - return NULL; - } - return result; -} - GtkWidget * modest_maemo_utils_get_manager_menubar_as_menu (GtkUIManager *manager, const gchar *item_name) diff --git a/src/maemo/modest-maemo-utils.h b/src/maemo/modest-maemo-utils.h index c9b889b..c845d1f 100644 --- a/src/maemo/modest-maemo-utils.h +++ b/src/maemo/modest-maemo-utils.h @@ -74,15 +74,6 @@ void modest_maemo_utils_setup_images_filechooser (GtkFileChooser *chooser); osso_context_t *modest_maemo_utils_get_osso_context (void); /** - * modest_maemo_open_mcc_mapping_file: - * - * open the mcc mapping file, or NULL if it fails - * - * Returns: file ptr or NULL in case of error - */ -FILE* modest_maemo_open_mcc_mapping_file (void); - -/** * modest_maemo_set_thumbable_scrollbar: * @win: a scrollable window * @thumbable: set it to thumbable (TRUE) or small (FALSE) diff --git a/src/maemo/modest-platform.c b/src/maemo/modest-platform.c index 344c567..83ef31e 100644 --- a/src/maemo/modest-platform.c +++ b/src/maemo/modest-platform.c @@ -37,6 +37,7 @@ #include "maemo/modest-maemo-global-settings-dialog.h" #include "modest-widget-memory.h" #include +#include #include #include #include @@ -102,7 +103,7 @@ on_modest_conf_update_interval_changed (ModestConf* self, static gboolean check_required_files (void) { - FILE *mcc_file = modest_maemo_open_mcc_mapping_file (); + FILE *mcc_file = modest_utils_open_mcc_mapping_file (NULL); if (!mcc_file) { g_printerr ("modest: check for mcc file failed\n"); return FALSE; diff --git a/src/modest-defs.h b/src/modest-defs.h index 5772952..96f75f6 100644 --- a/src/modest-defs.h +++ b/src/modest-defs.h @@ -82,7 +82,7 @@ const gchar *modest_defs_namespace (const gchar *string); /* configuration key definitions for modest */ #define MODEST_CONF_NAMESPACE (modest_defs_namespace (NULL)) -/* the mapping files, there are two possibilities; used in modest_maemo_open_mcc_mapping_file */ +/* the mapping files, there are two possibilities; used in modest_utils_open_mcc_mapping_file */ #define MODEST_MCC_MAPPING PREFIX "/share/modest/provider-data/mcc_mapping" #define MODEST_OPERATOR_WIZARD_MCC_MAPPING "/usr/share/operator-wizard/mcc_mapping" diff --git a/src/modest-utils.c b/src/modest-utils.c index 99fc120..7fe1833 100644 --- a/src/modest-utils.c +++ b/src/modest-utils.c @@ -54,6 +54,7 @@ #ifdef MODEST_TOOLKIT_HILDON2 #include "modest-header-window.h" #endif +#include GQuark modest_utils_get_supported_secure_authentication_error_quark (void) @@ -826,3 +827,192 @@ modest_utils_on_entry_invalid_character (ModestValidatingEntry *self, modest_platform_information_banner (GTK_WIDGET (self), NULL, message); g_free (message); } + +FILE* +modest_utils_open_mcc_mapping_file (gboolean *translated) +{ + FILE* result = NULL; + const gchar* path; + gchar *path1 = g_strdup_printf ("%s.%s", MODEST_OPERATOR_WIZARD_MCC_MAPPING, getenv("LANG")); + const gchar* path2 = MODEST_MCC_MAPPING; + + if (translated) + *translated = TRUE; + + if (access (path1, R_OK) == 0) { + path = path1; + } else if (access (MODEST_OPERATOR_WIZARD_MCC_MAPPING, R_OK) == 0) { + path = MODEST_OPERATOR_WIZARD_MCC_MAPPING; + if (translated) + *translated = FALSE; + } else if (access (path2, R_OK) == 0) { + path = path2; + } else { + g_warning ("%s: neither '%s' nor '%s' is a readable mapping file", + __FUNCTION__, path1, path2); + goto end; + } + + result = fopen (path, "r"); + if (!result) { + g_warning ("%s: error opening mapping file '%s': %s", + __FUNCTION__, path, strerror(errno)); + goto end; + } + end: + g_free (path1); + return result; +} + +/* cluster mcc's, based on the list + * http://en.wikipedia.org/wiki/Mobile_country_code + */ +static int +effective_mcc (gint mcc) +{ + switch (mcc) { + case 405: return 404; /* india */ + case 441: return 440; /* japan */ + case 235: return 234; /* united kingdom */ + case 311: + case 312: + case 313: + case 314: + case 315: + case 316: return 310; /* united states */ + default: return mcc; + } +} + +/* each line is of the form: + xxx logical_id + + NOTE: this function is NOT re-entrant, the out-param country + are static strings that should NOT be freed. and will change when + calling this function again + + also note, this function will return the "effective mcc", which + is the normalized mcc for a country - ie. even if the there + are multiple entries for the United States with various mccs, + this function will always return 310, even if the real mcc parsed + would be 314. see the 'effective_mcc' function above. +*/ +static int +parse_mcc_mapping_line (const char* line, char** country) +{ + char mcc[4]; /* the mcc code, always 3 bytes*/ + gchar *iter, *tab, *final; + + if (!line) { + *country = NULL; + return 0; + } + + /* Go to the first tab (Country separator) */ + tab = g_utf8_strrchr (line, -1, '\t'); + if (!tab) + return 0; + + *country = g_utf8_find_next_char (tab, NULL); + + /* Replace by end of string. We need to use strlen, because + g_utf8_strrchr expects bytes and not UTF8 characters */ + final = g_utf8_strrchr (tab, strlen (tab) + 1, '\n'); + if (G_LIKELY (final)) + *final = '\0'; + else + tab[strlen(tab) - 1] = '\0'; + + /* Get MCC code */ + mcc[0] = g_utf8_get_char (line); + iter = g_utf8_find_next_char (line, NULL); + mcc[1] = g_utf8_get_char (iter); + iter = g_utf8_find_next_char (iter, NULL); + mcc[2] = g_utf8_get_char (iter); + mcc[3] = '\0'; + + return effective_mcc ((int) strtol ((const char*)mcc, NULL, 10)); +} + +#define MCC_FILE_MAX_LINE_LEN 128 /* max length of a line in MCC file */ + +/** Note that the mcc_mapping file is installed + * by the operator-wizard-settings package. + */ +GtkTreeModel * +modest_utils_create_country_model (void) +{ + GtkTreeModel *model; + + model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_INT)); + + return model; +} + +void +modest_utils_fill_country_model (GtkTreeModel *model, gint *locale_mcc) +{ + gboolean translated; + char line[MCC_FILE_MAX_LINE_LEN]; + guint previous_mcc = 0; + gchar *territory; + + FILE *file; + + file = modest_utils_open_mcc_mapping_file (&translated); + if (!file) { + g_warning("Could not open mcc_mapping file"); + } + + /* Get the territory specified for the current locale */ + territory = nl_langinfo (_NL_ADDRESS_COUNTRY_NAME); + + while (fgets (line, MCC_FILE_MAX_LINE_LEN, file) != NULL) { + + int mcc; + char *country = NULL; + const gchar *name_translated; + + mcc = parse_mcc_mapping_line (line, &country); + if (!country || mcc == 0) { + g_warning ("%s: error parsing line: '%s'", __FUNCTION__, line); + continue; + } + + if (mcc == previous_mcc) { + /* g_warning ("already seen: %s", line); */ + continue; + } + previous_mcc = mcc; + + if (!(*locale_mcc)) { + if (translated) { + if (!g_utf8_collate (country, territory)) + *locale_mcc = mcc; + } else { + gchar *translation = dgettext ("osso-countries", country); + if (!g_utf8_collate (translation, territory)) + *locale_mcc = mcc; + } + } + name_translated = dgettext ("osso-countries", country); + + /* Add the row to the model: */ + GtkTreeIter iter; + gtk_list_store_append (GTK_LIST_STORE (model), &iter); + gtk_list_store_set(GTK_LIST_STORE (model), &iter, + MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC, mcc, + MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, name_translated, + -1); + } + fclose (file); + + /* Fallback to Finland */ + if (!(*locale_mcc)) + *locale_mcc = 244; + + /* Sort the items: */ + gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model), + MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME, GTK_SORT_ASCENDING); +} + diff --git a/src/modest-utils.h b/src/modest-utils.h index 8f88a35..1fd37ea 100644 --- a/src/modest-utils.h +++ b/src/modest-utils.h @@ -189,4 +189,46 @@ void modest_utils_on_entry_invalid_character (ModestValidatingEntry *self, const gchar* character, gpointer user_data); +/** + * modest_utils_open_mcc_mapping_file: + * @translated: a #gboolean pointer + * + * open the mcc mapping file, or %NULL if it fails. It also + * sets @translated to %TRUE if the file is translated + * + * Returns: file ptr or %NULL in case of error + */ +FILE* modest_utils_open_mcc_mapping_file (gboolean *translated); + +typedef enum { + MODEST_UTILS_COUNTRY_MODEL_COLUMN_NAME = 0, + MODEST_UTILS_COUNTRY_MODEL_COLUMN_MCC = 1, + MODEST_UTILS_COUNTRY_MODEL_N_COLUMNS +} ModestUtilsCountryModelColumns; + +/** + * modest_utils_create_country_model: + * @locale_mcc: a #gboolean + * + * creates the countries tree model used in wizard from the mcc + * files. + * + * Returns: an empty #GtkTreeModel with the columns enumerated in + * #ModestUtilsCountryModelColumns + */ +GtkTreeModel *modest_utils_create_country_model (void); + +/** + * modest_utils_fill_country_model: + * @model: a #GtkTreeModel (obtained with modest_utils_create_country_model + * @locale_mcc: a #gboolean + * + * fills the countries tree model used in wizard from the mcc + * files. + * + */ +void modest_utils_fill_country_model (GtkTreeModel *model, gint *locale_mcc); + + + #endif /*__MODEST_MAEMO_UTILS_H__*/ -- 1.7.9.5