-/* Copyright (c) 2007, Nokia Corporation
+/* Copyright (c) 2006, Nokia Corporation
* All rights reserved.
*
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
+#ifndef _GNU_SOURCE
#define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
+#endif
+
#include <stdio.h>
#include "modest-easysetup-country-combo-box.h"
G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose (object);
}
+enum MODEL_COLS {
+ MODEL_COL_NAME = 0, /* string */
+ MODEL_COL_IDS = 1 /* A GSList* of guints. */
+};
+
+static gboolean
+on_model_foreach_release (GtkTreeModel *model, GtkTreePath *path,
+ GtkTreeIter *iter, gpointer data)
+{
+ GSList *list = NULL;
+ gtk_tree_model_get (model, iter, MODEL_COL_IDS, &list, -1);
+ if (list)
+ g_slist_free (list);
+
+ return FALSE; /* keep walking. */
+}
+
static void
easysetup_country_combo_box_finalize (GObject *object)
{
EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (object);
+ gtk_tree_model_foreach (priv->model, on_model_foreach_release, NULL);
g_object_unref (G_OBJECT (priv->model));
G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->finalize (object);
object_class->finalize = easysetup_country_combo_box_finalize;
}
-enum MODEL_COLS {
- MODEL_COL_NAME = 0,
- MODEL_COL_ID = 1
-};
-
/** id and country must be freed.
*/
static void parse_mcc_mapping_line (const char* line, char** id, char** country)
#ifndef MODEST_HILDON_VERSION_0
const gchar* filepath = PROVIDER_DATA_DIR "/mcc_mapping";
#else
- /* this is the official version, in the 'operator-wizard-settings' package */
+ /* This is the official version, in the 'operator-wizard-settings' package */
const gchar* filepath = "/usr/share/operator-wizard/mcc_mapping";
#endif /*MODEST_HILDON_VERSION_0*/
+ /* printf ("DEBUG: %s: filepath=%s\n", __FUNCTION__, filepath); */
FILE *file = fopen(filepath, "r");
if (!file)
{
/* We use the getline() GNU extension,
* because it reads per line, which simplifies our code,
* and it doesn't require us to hard-code a buffer length.
+ * TODO: Could we make this faster?
*/
- int len = 0;
+ unsigned int len = 0;
char *line = NULL;
+ guint previous_id = 0;
+ gchar* previous_country = NULL;
+ GSList *list = NULL;
while (getline (&line, &len, file) > 0) { /* getline will realloc line if necessary. */
/* printf ("DBEUG: len=%d, line: %s\n", len, line); */
char *id_str = NULL;
char *country = NULL;
parse_mcc_mapping_line (line, &id_str, &country);
- //printf("DEBUG: parsed: id=%s, country=%s\n", id_str, country);
+ /* printf("DEBUG: parsed: id=%s, country=%s\n", id_str, country); */
if(id_str && country) {
- guint id = (guint)g_ascii_strtod(id_str, NULL); /* Note that this parses locale-independent text. */
- /* 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 */
- const gchar *name_translated = dgettext ("osso-countries", country);
- if(!name_translated)
- name_translated = country;
+ if (previous_country) {
+ /* printf (" debug: storing id=%d for country=%s\n", previous_id, previous_country); */
+ list = g_slist_append (list, GUINT_TO_POINTER (previous_id));
+ }
+
+ /* Group multiple MMC IDs for the same country together:
+ * This assumes that they are in sequence.
+ * We don't know why some countries, such as the USA, have several MMC IDs.
+ * If they are regions in the country, and we need to show them separately, then
+ * we would need to have that information in the file to distinguish them.
+ */
+ if (!previous_country ||
+ (previous_country && strcmp (previous_country, country) != 0)) {
+
+ /* 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", previous_country);
+ if(!name_translated)
+ name_translated = previous_country;
+
+ /* Add the row to the model: */
+ GtkTreeIter iter;
+ gtk_list_store_append (liststore, &iter);
+ gtk_list_store_set(liststore, &iter, MODEL_COL_IDS, list, MODEL_COL_NAME, name_translated, -1);
+
+ /* The list will be freed in our finalize(). */
+ list = NULL;
+ }
+
+ g_free (previous_country);
+ previous_country = g_strdup (country);
- /* Add the row to the model: */
- GtkTreeIter iter;
- gtk_list_store_append (liststore, &iter);
- gtk_list_store_set(liststore, &iter, MODEL_COL_ID, id, MODEL_COL_NAME, name_translated, -1);
+ const guint id = (guint)g_ascii_strtod(id_str, NULL); /* Note that this parses locale-independent text. */
+ previous_id = id;
}
g_free (id_str);
g_free (country);
}
+
+ /* Deal with the last country: */
+ const gchar *name_translated = dgettext ("osso-countries", previous_country);
+ if(!name_translated)
+ name_translated = previous_country;
+
+ /* Add the row to the model: */
+ GtkTreeIter iter;
+ gtk_list_store_append (liststore, &iter);
+ gtk_list_store_set(liststore, &iter, MODEL_COL_IDS, list, MODEL_COL_NAME, name_translated, -1);
+
+ if (list) {
+ g_slist_free (list);
+ list = NULL;
+ }
+
if (line)
free (line);
fclose (file);
+
+ /* Sort the items: */
+ gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (liststore),
+ MODEL_COL_NAME, GTK_SORT_ASCENDING);
}
static void
* 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_UINT));
+ priv->model = GTK_TREE_MODEL (gtk_list_store_new (2,
+ G_TYPE_STRING,
+ G_TYPE_POINTER));
/* Setup the combo box: */
GtkComboBox *combobox = GTK_COMBO_BOX (self);
/**
* Returns the MCC number of the selected country, or 0 if no country was selected.
+ * The list should not be freed.
*/
-guint
-easysetup_country_combo_box_get_active_country_id (EasysetupCountryComboBox *self)
+GSList *
+easysetup_country_combo_box_get_active_country_ids (EasysetupCountryComboBox *self)
{
GtkTreeIter active;
const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &active);
if (found) {
EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
- guint id = 0;
- gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
- return id;
+ GSList *list = NULL;
+ gtk_tree_model_get (priv->model, &active, MODEL_COL_IDS, &list, -1);
+ return list;
}
- return 0; /* Failed. */
+ return NULL; /* Failed. */
}
ForEachData *state = (ForEachData*)(user_data);
/* Select the item if it has the matching ID: */
- guint id = 0;
- gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
- if(id == state->mcc_id) {
+ GSList *list = NULL;
+ gtk_tree_model_get (model, iter, MODEL_COL_IDS, &list, -1);
+ if(list && g_slist_find (list, GUINT_TO_POINTER (state->mcc_id))) {
gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
state->found = TRUE;