* Fixes NB#86700 select the default region in the accounts wizard dialog by default
authorSergio Villar Senin <svillar@igalia.com>
Thu, 3 Jul 2008 18:08:49 +0000 (18:08 +0000)
committerSergio Villar Senin <svillar@igalia.com>
Thu, 3 Jul 2008 18:08:49 +0000 (18:08 +0000)
pmo-trunk-r4888

src/maemo/easysetup/modest-easysetup-country-combo-box.c
src/maemo/easysetup/modest-easysetup-country-combo-box.h
src/maemo/easysetup/modest-easysetup-wizard-dialog.c

index 9630810..6d64626 100644 (file)
@@ -41,7 +41,8 @@
 
 #include <stdlib.h>
 #include <string.h> /* For memcpy() */
-
+#include <langinfo.h>
+#include <locale.h>
 #include <libintl.h> /* For dgettext(). */
 
 /* Include config.h so that _() works: */
@@ -55,7 +56,8 @@ G_DEFINE_TYPE (EasysetupCountryComboBox, easysetup_country_combo_box, GTK_TYPE_C
 
 typedef struct
 {
-       GtkTreeModel *model;
+       gint locale_mcc;
+/*     GtkTreeModel *model; */
 } ModestEasysetupCountryComboBoxPrivate;
 
 #define MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
@@ -98,9 +100,6 @@ enum MODEL_COLS {
 static void
 easysetup_country_combo_box_finalize (GObject *object)
 {
-       ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (object);
-
-       g_object_unref (G_OBJECT (priv->model));
        G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->finalize (object);
 }
 
@@ -191,14 +190,26 @@ parse_mcc_mapping_line (const char* line,  char** country)
  * by the operator-wizard-settings package.
  */
 static void
-load_from_file (EasysetupCountryComboBox *self)
+load_from_file (EasysetupCountryComboBox *self, GtkListStore *liststore)
 {
        ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
-       GtkListStore *liststore = GTK_LIST_STORE (priv->model);
        
        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");
@@ -209,6 +220,8 @@ load_from_file (EasysetupCountryComboBox *self)
 
                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);
@@ -220,16 +233,14 @@ load_from_file (EasysetupCountryComboBox *self)
                        continue;
                }
                previous_mcc = mcc;
-               
-               /* Get the translation for the country name:
-                * Note that the osso_countries_1.0 translation domain files are installed 
-                * by the operator-wizard-settings package. */
-               /* For post-Bora, there is a separate (meta)package osso-countries-l10n-mr0 */
-                               
-               /* Note: Even when the untranslated names are different, there may still be 
-                * duplicate translated names. They would be translation bugs.
-                */
-               const gchar *name_translated = dgettext ("osso-countries", country);
+
+               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;
@@ -237,29 +248,31 @@ load_from_file (EasysetupCountryComboBox *self)
                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)
 {
        ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
-       priv->model = NULL;
+       priv->locale_mcc = 0;
 }
 
 void
 easysetup_country_combo_box_load_data(EasysetupCountryComboBox *self)
 {
-       ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
+       GtkListStore *model;
 
        /* 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.
         */
-       priv->model = GTK_TREE_MODEL (gtk_list_store_new (2,  G_TYPE_STRING, G_TYPE_INT));
+       model = gtk_list_store_new (2,  G_TYPE_STRING, G_TYPE_INT);
        
        /* Setup the combo box: */
        GtkComboBox *combobox = GTK_COMBO_BOX (self);
@@ -272,14 +285,11 @@ easysetup_country_combo_box_load_data(EasysetupCountryComboBox *self)
        gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer, 
        "text", MODEL_COL_NAME, NULL);
 
-       
-       g_assert (GTK_IS_LIST_STORE(priv->model));
-
-       
        /* Fill the model with rows: */
-       load_from_file (self);
+       load_from_file (self, model);
+
        /* Set this _after_ loading from file, it makes loading faster */
-       gtk_combo_box_set_model (combobox, priv->model);
+       gtk_combo_box_set_model (combobox, GTK_TREE_MODEL (model));
 }
 
 EasysetupCountryComboBox*
@@ -297,9 +307,9 @@ easysetup_country_combo_box_get_active_country_mcc (EasysetupCountryComboBox *se
        GtkTreeIter active;
        const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &active);
        if (found) {
-               ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
                gint mcc = 0;
-               gtk_tree_model_get (priv->model, &active, MODEL_COL_MCC, &mcc, -1); 
+               gtk_tree_model_get (gtk_combo_box_get_model (GTK_COMBO_BOX (self)), 
+                                   &active, MODEL_COL_MCC, &mcc, -1);
                return mcc;     
        }
        return 0; /* Failed. */
@@ -311,21 +321,23 @@ easysetup_country_combo_box_get_active_country_mcc (EasysetupCountryComboBox *se
  * Specify 0 to select no country. 
  */
 gboolean
-easysetup_country_combo_box_set_active_country_mcc (EasysetupCountryComboBox *self, guint mcc)
+easysetup_country_combo_box_set_active_country_locale (EasysetupCountryComboBox *self)
 {
        ModestEasysetupCountryComboBoxPrivate *priv = MODEST_EASYSETUP_COUNTRY_COMBO_BOX_GET_PRIVATE (self);
        GtkTreeIter iter;
+       gint current_mcc;
+       GtkTreeModel *model;
 
-       if (!gtk_tree_model_get_iter_first (priv->model, &iter)) 
+       model = gtk_combo_box_get_model (GTK_COMBO_BOX (self));
+       if (!gtk_tree_model_get_iter_first (model, &iter))
                return FALSE;
        do {
-               gint current_mcc = 0;
-               gtk_tree_model_get (priv->model, &iter, MODEL_COL_MCC, &current_mcc, -1);
-               if (current_mcc == mcc) {
+               gtk_tree_model_get (model, &iter, MODEL_COL_MCC, &current_mcc, -1);
+               if (priv->locale_mcc == current_mcc) {
                        gtk_combo_box_set_active_iter (GTK_COMBO_BOX (self), &iter);
                        return TRUE;
                }
-       } while (gtk_tree_model_iter_next (priv->model, &iter));
+       } while (gtk_tree_model_iter_next (model, &iter));
        
        return FALSE; /* not found */
 }
index a34eb5b..f8aa457 100644 (file)
@@ -73,7 +73,7 @@ void easysetup_country_combo_box_load_data(EasysetupCountryComboBox *self);
 gint easysetup_country_combo_box_get_active_country_mcc (EasysetupCountryComboBox *self);
 
 
-gboolean easysetup_country_combo_box_set_active_country_mcc (EasysetupCountryComboBox *self, guint mcc);
+gboolean easysetup_country_combo_box_set_active_country_locale (EasysetupCountryComboBox *self);
 
 G_END_DECLS
 
index 36871f3..eee0903 100644 (file)
@@ -40,6 +40,7 @@
 #include <gtk/gtkcheckbutton.h>
 #include <gtk/gtkmessagedialog.h>
 #include <gtk/gtkseparator.h>
+#include <string.h> /* For strlen(). */
 #include "maemo/easysetup/modest-easysetup-country-combo-box.h"
 #include "maemo/easysetup/modest-easysetup-provider-combo-box.h"
 #include "maemo/easysetup/modest-easysetup-servertype-combo-box.h"
@@ -47,6 +48,7 @@
 #include "widgets/modest-secureauth-combo-box.h"
 #include "widgets/modest-validating-entry.h"
 #include "modest-text-utils.h"
+#include "modest-conf.h"
 #include "modest-account-mgr.h"
 #include "modest-account-mgr-helpers.h"
 #include "modest-runtime.h" /* For modest_runtime_get_account_mgr(). */
@@ -55,8 +57,6 @@
 #include "widgets/modest-account-settings-dialog.h"
 #include "maemo/modest-maemo-utils.h"
 #include "modest-utils.h"
-#include <gconf/gconf-client.h>
-#include <string.h> /* For strlen(). */
 #include "maemo/modest-hildon-includes.h"
 
 /* Include config.h so that _() works: */
@@ -459,44 +459,6 @@ on_entry_max (ModestValidatingEntry *self, gpointer user_data)
        show_error (GTK_WIDGET (self), _CS("ckdg_ib_maximum_characters_reached"));
 }
 
-static gint
-get_default_country_code(void)
-{
-       /* TODO: Default to the current country somehow.
-        * But I don't know how to get the information that is specified in the
-        * "Language and region" control panel. It does not seem be anywhere in gconf. murrayc.
-        *
-        * This is probably not the best choice of gconf key:
-        * This is the  "mcc used in the last pairing", ie. the last connection you made.
-        * set by the osso-operator-wizard package, suggested by Dirk-Jan Binnema.
-        *
-        */
-       GError *error = NULL;
-       const gchar* key = "/apps/osso/operator-wizard/last_mcc";
-       gint mcc_id = modest_conf_get_int(modest_runtime_get_conf (), key, &error);
-
-       if(mcc_id < 0)
-               mcc_id = 0;
-
-       if (error) {
-               g_warning ("Error getting gconf key %s:\n%s", key, error->message);
-               g_error_free (error);
-               error = NULL;
-
-               mcc_id = 0;
-       }
-
-       /* Note that modest_conf_get_int() seems to return 0 without an error if the key is not there
-        * This might just be a Maemo bug.
-        */
-       if (mcc_id == 0)
-       {
-               /* For now, we default to Finland when there is nothing better: */
-               mcc_id = 244;
-       }
-       return mcc_id;
-}
-
 static GtkWidget*
 create_page_account_details (ModestEasysetupWizardDialog *self)
 {
@@ -1151,12 +1113,17 @@ presets_idle (gpointer userdata)
        priv->presets = idle_data->presets;
 
        if (MODEST_EASYSETUP_IS_COUNTRY_COMBO_BOX (priv->combo_account_country)) {
-               gint mcc = get_default_country_code();
+/*             gint mcc = get_default_country_code(); */
+               gint mcc;
                /* Fill the combo in an idle call, as it takes a lot of time */
                easysetup_country_combo_box_load_data(
                        MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
-               easysetup_country_combo_box_set_active_country_mcc (
-                       MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country), mcc);
+/*             easysetup_country_combo_box_set_active_country_mcc ( */
+/*                     MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country), mcc); */
+               easysetup_country_combo_box_set_active_country_locale (
+                       MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
+               mcc = easysetup_country_combo_box_get_active_country_mcc (
+                       MODEST_EASYSETUP_COUNTRY_COMBO_BOX (priv->combo_account_country));
                easysetup_provider_combo_box_fill (
                        EASYSETUP_PROVIDER_COMBO_BOX (priv->combo_account_serviceprovider),
                        priv->presets, mcc);