[network-manager-applet/aleksander/mobile-providers: 3/11] libnm-gtk, mobile-providers: import changes from gnome-shell



commit ca0b8826e10a0a53d4f7fe41f713fc09aa91362c
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Fri Nov 23 12:23:09 2012 +0100

    libnm-gtk,mobile-providers: import changes from gnome-shell
    
    The code in gnome-shell was modified to better handle several introspection
    related issues (e.g. avoid lists of objects returned as values of hash table
    elements).
    
    This commit aligns the requirements of gnome-shell directly in libnm-gtk.
    
    Bits and pieces were fixed in the mobile broadband connection creator wizard and
    in the CDMA/GSM items of the standard nm-applet.

 src/applet-device-cdma.c            |   18 ++--
 src/applet-device-gsm.c             |   20 ++--
 src/libnm-gtk/nm-mobile-providers.c |  236 +++++++++++++++++++++++++++++------
 src/libnm-gtk/nm-mobile-providers.h |   39 +++++--
 src/libnm-gtk/nm-mobile-wizard.c    |  146 +++++++++++----------
 5 files changed, 328 insertions(+), 131 deletions(-)
---
diff --git a/src/applet-device-cdma.c b/src/applet-device-cdma.c
index 09e195d..ba75351 100644
--- a/src/applet-device-cdma.c
+++ b/src/applet-device-cdma.c
@@ -61,7 +61,7 @@ typedef struct {
 	guint32 sid;
 	gboolean modem_enabled;
 
-	GHashTable *providers;
+	GHashTable *country_infos;
 	char *provider_name;
 
 	guint32 poll_id;
@@ -624,8 +624,8 @@ cdma_device_info_free (gpointer data)
 		dbus_g_connection_unref (info->bus);
 	if (info->poll_id)
 		g_source_remove (info->poll_id);
-	if (info->providers)
-		g_hash_table_destroy (info->providers);
+	if (info->country_infos)
+		g_hash_table_destroy (info->country_infos);
 	g_free (info->provider_name);
 	memset (info, 0, sizeof (CdmaDeviceInfo));
 	g_free (info);
@@ -721,10 +721,12 @@ find_provider_for_sid (GHashTable *table, guint32 sid)
 	g_hash_table_iter_init (&iter, table);
 	/* Search through each country */
 	while (g_hash_table_iter_next (&iter, NULL, &value) && !name) {
-		GSList *providers = value;
+		NMACountryInfo *country_info = value;
 
 		/* Search through each country's providers */
-		for (piter = providers; piter && !name; piter = g_slist_next (piter)) {
+		for (piter = nma_country_info_get_providers (country_info);
+		     piter && !name;
+		     piter = g_slist_next (piter)) {
 			NMAMobileProvider *provider = piter->data;
 
 			/* Search through CDMA SID list */
@@ -763,10 +765,10 @@ serving_system_reply (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_dat
 
 	if (new_sid && (new_sid != info->sid)) {
 		info->sid = new_sid;
-		if (info->providers) {
+		if (info->country_infos) {
 			g_free (info->provider_name);
 			info->provider_name = NULL;
-			info->provider_name = find_provider_for_sid (info->providers, new_sid);
+			info->provider_name = find_provider_for_sid (info->country_infos, new_sid);
 		}
 	} else if (!new_sid) {
 		info->sid = 0;
@@ -943,7 +945,7 @@ cdma_device_added (NMDevice *device, NMApplet *applet)
 	info->bus = bus;
 	info->quality_valid = FALSE;
 
-	info->providers = nma_mobile_providers_parse (NULL);
+	info->country_infos = nma_mobile_providers_parse (NULL, NULL);
 
 	info->props_proxy = dbus_g_proxy_new_for_name (bus,
 	                                               "org.freedesktop.ModemManager",
diff --git a/src/applet-device-gsm.c b/src/applet-device-gsm.c
index 819483f..500111b 100644
--- a/src/applet-device-gsm.c
+++ b/src/applet-device-gsm.c
@@ -88,7 +88,7 @@ typedef struct {
 	guint reg_state;
 	char *op_code;
 	char *op_name;
-	GHashTable *providers;
+	GHashTable *country_infos;
 
 	guint32 poll_id;
 	gboolean skip_reg_poll;
@@ -1061,8 +1061,8 @@ gsm_device_info_free (gpointer data)
 	if (info->keyring_id)
 		gnome_keyring_cancel_request (info->keyring_id);
 
-	if (info->providers)
-		g_hash_table_destroy (info->providers);
+	if (info->country_infos)
+		g_hash_table_destroy (info->country_infos);
 
 	if (info->poll_id)
 		g_source_remove (info->poll_id);
@@ -1111,10 +1111,12 @@ find_provider_for_mcc_mnc (GHashTable *table, const char *mccmnc)
 	g_hash_table_iter_init (&iter, table);
 	/* Search through each country */
 	while (g_hash_table_iter_next (&iter, NULL, &value) && !done) {
-		GSList *providers = value;
+		NMACountryInfo *country_info = value;
 
 		/* Search through each country's providers */
-		for (piter = providers; piter && !done; piter = g_slist_next (piter)) {
+		for (piter = nma_country_info_get_providers (country_info);
+		     piter && !done;
+		     piter = g_slist_next (piter)) {
 			NMAMobileProvider *provider = piter->data;
 
 			/* Search through MCC/MNC list */
@@ -1180,12 +1182,12 @@ parse_op_name (GsmDeviceInfo *info, const char *orig, const char *op_code)
 	 * probably an MCC/MNC.  Look that up.
 	 */
 
-	if (!info->providers)
-		info->providers = nma_mobile_providers_parse (NULL);
-	if (!info->providers)
+	if (!info->country_infos)
+		info->country_infos = nma_mobile_providers_parse (NULL, NULL);
+	if (!info->country_infos)
 		return strdup (orig);
 
-	return find_provider_for_mcc_mnc (info->providers, orig);
+	return find_provider_for_mcc_mnc (info->country_infos, orig);
 }
 
 static void
diff --git a/src/libnm-gtk/nm-mobile-providers.c b/src/libnm-gtk/nm-mobile-providers.c
index d351894..c1af42b 100644
--- a/src/libnm-gtk/nm-mobile-providers.c
+++ b/src/libnm-gtk/nm-mobile-providers.c
@@ -18,7 +18,7 @@
  * Copyright (C) 2009 Novell, Inc.
  * Author: Tambet Ingo (tambet gmail com).
  *
- * Copyright (C) 2009 - 2011 Red Hat, Inc.
+ * Copyright (C) 2009 - 2012 Red Hat, Inc.
  */
 
 #include "config.h"
@@ -41,6 +41,11 @@
 /******************************************************************************/
 /* GSM MCCMNC type */
 
+static NMAGsmMccMnc *mcc_mnc_copy (const NMAGsmMccMnc *other);
+static void          mcc_mnc_free (NMAGsmMccMnc *m);
+
+G_DEFINE_BOXED_TYPE (NMAGsmMccMnc, nma_gsm_mcc_mnc, mcc_mnc_copy, mcc_mnc_free)
+
 static NMAGsmMccMnc *
 mcc_mnc_new (const char *mcc, const char *mnc)
 {
@@ -52,6 +57,17 @@ mcc_mnc_new (const char *mcc, const char *mnc)
     return m;
 }
 
+static NMAGsmMccMnc *
+mcc_mnc_copy (const NMAGsmMccMnc *other)
+{
+    NMAGsmMccMnc *ret;
+
+    ret = g_slice_new (NMAGsmMccMnc);
+    ret->mcc = g_strdup (other->mcc);
+    ret->mnc = g_strdup (other->mnc);
+    return ret;
+}
+
 static void
 mcc_mnc_free (NMAGsmMccMnc *m)
 {
@@ -166,6 +182,36 @@ nma_mobile_provider_unref (NMAMobileProvider *provider)
     }
 }
 
+/**
+ * nma_mobile_provider_get_gsm_mcc_mnc:
+ * @provider: a #NMAMobileProvider
+ *
+ * Returns: (element-type NMGtk.GsmMccMnc) (transfer none): the
+ *   list of #NMAGsmMccMnc this provider exposes
+ */
+GSList *
+nma_mobile_provider_get_gsm_mcc_mnc (NMAMobileProvider *provider)
+{
+    g_return_val_if_fail (provider != NULL, NULL);
+
+    return provider->gsm_mcc_mnc;
+}
+
+/**
+ * nma_mobile_provider_get_cdma_sid:
+ * @provider: a #NMAMobileProvider
+ *
+ * Returns: (element-type guint32) (transfer none): the
+ *   list of CDMA SIDs this provider exposes
+ */
+GSList *
+nma_mobile_provider_get_cdma_sid (NMAMobileProvider *provider)
+{
+    g_return_val_if_fail (provider != NULL, NULL);
+
+    return provider->cdma_sid;
+}
+
 GType
 nma_mobile_provider_get_type (void)
 {
@@ -180,6 +226,95 @@ nma_mobile_provider_get_type (void)
 }
 
 /******************************************************************************/
+/* Country Info type */
+
+static NMACountryInfo *
+country_info_new (const char *country_code,
+                  const gchar *country_name)
+{
+    NMACountryInfo *country_info;
+
+    country_info = g_slice_new0 (NMACountryInfo);
+    country_info->refs = 1;
+    country_info->country_code = g_strdup (country_code);
+    country_info->country_name = g_strdup (country_name);
+    return country_info;
+}
+
+NMACountryInfo *
+nma_country_info_ref (NMACountryInfo *country_info)
+{
+    country_info->refs++;
+
+    return country_info;
+}
+
+void
+nma_country_info_unref (NMACountryInfo *country_info)
+{
+    if (--country_info->refs == 0) {
+        g_free (country_info->country_code);
+        g_free (country_info->country_name);
+        g_slist_free_full (country_info->providers,
+                           (GDestroyNotify) nma_mobile_provider_unref);
+        g_slice_free (NMACountryInfo, country_info);
+    }
+}
+
+/**
+ * nma_country_info_get_country_code:
+ *
+ * Returns: (transfer none): the code of the country.
+ */
+const gchar *
+nma_country_info_get_country_code (NMACountryInfo *country_info)
+{
+    g_return_val_if_fail (country_info != NULL, NULL);
+
+    return country_info->country_code;
+}
+
+/**
+ * nma_country_info_get_country_name:
+ *
+ * Returns: (transfer none): the name of the country.
+ */
+const gchar *
+nma_country_info_get_country_name (NMACountryInfo *country_info)
+{
+    g_return_val_if_fail (country_info != NULL, NULL);
+
+    return country_info->country_name;
+}
+
+/**
+ * nma_country_info_get_providers:
+ *
+ * Returns: (element-type NMGtk.MobileProvider) (transfer none): the
+ *   list of #NMAMobileProvider this country exposes.
+ */
+GSList *
+nma_country_info_get_providers (NMACountryInfo *country_info)
+{
+    g_return_val_if_fail (country_info != NULL, NULL);
+
+    return country_info->providers;
+}
+
+GType
+nma_country_info_get_type (void)
+{
+    static GType type = 0;
+
+    if (G_UNLIKELY (type == 0)) {
+        type = g_boxed_type_register_static ("NMACountryInfo",
+                                             (GBoxedCopyFunc) nma_country_info_ref,
+                                             (GBoxedFreeFunc) nma_country_info_unref);
+    }
+    return type;
+}
+
+/******************************************************************************/
 /* XML Parser for iso_3166.xml */
 
 static void
@@ -197,6 +332,8 @@ iso_3166_parser_start_element (GMarkupParseContext *context,
     GHashTable *table = (GHashTable *) data;
 
     if (!strcmp (element_name, "iso_3166_entry")) {
+        NMACountryInfo *country_info;
+
         for (i = 0; attribute_names && attribute_names[i]; i++) {
             if (!strcmp (attribute_names[i], "alpha_2_code"))
                 country_code = attribute_values[i];
@@ -216,7 +353,10 @@ iso_3166_parser_start_element (GMarkupParseContext *context,
             return;
         }
 
-        g_hash_table_insert (table, g_strdup (country_code), g_strdup (dgettext ("iso_3166", common_name ? common_name : name)));
+        country_info = country_info_new (country_code,
+                                         dgettext ("iso_3166", common_name ? common_name : name));
+
+        g_hash_table_insert (table, g_strdup (country_code), country_info);
     }
 }
 
@@ -229,7 +369,7 @@ static const GMarkupParser iso_3166_parser = {
 };
 
 static GHashTable *
-read_country_codes (void)
+read_country_codes (const gchar *country_codes_file)
 {
     GHashTable *table = NULL;
     GMarkupParseContext *ctx;
@@ -241,12 +381,15 @@ read_country_codes (void)
     bindtextdomain ("iso_3166", ISO_CODES_LOCALESDIR);
     bind_textdomain_codeset ("iso_3166", "UTF-8");
 
-    if (g_file_get_contents (ISO_3166_COUNTRY_CODES, &buf, &buf_len, &error)) {
-        table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+    if (g_file_get_contents (country_codes_file, &buf, &buf_len, &error)) {
+        table = g_hash_table_new_full (g_str_hash,
+                                       g_str_equal,
+                                       g_free,
+                                       (GDestroyNotify)nma_country_info_unref);
         ctx = g_markup_parse_context_new (&iso_3166_parser, 0, table, NULL);
 
         if (!g_markup_parse_context_parse (ctx, buf, buf_len, &error)) {
-            g_warning ("Failed to parse '%s': %s\n", ISO_3166_COUNTRY_CODES, error->message);
+            g_warning ("Failed to parse '%s': %s\n", country_codes_file, error->message);
             g_error_free (error);
             g_hash_table_destroy (table);
             table = NULL;
@@ -256,7 +399,7 @@ read_country_codes (void)
         g_free (buf);
     } else {
         g_warning ("Failed to load '%s': %s\n Consider installing 'iso-codes'\n",
-                   ISO_3166_COUNTRY_CODES, error->message);
+                   country_codes_file, error->message);
         g_error_free (error);
     }
 
@@ -277,7 +420,6 @@ typedef enum {
 } MobileContextState;
 
 typedef struct {
-    GHashTable *country_codes;
     GHashTable *table;
 
     char *current_country;
@@ -323,15 +465,17 @@ parser_toplevel_start (MobileParser *parser,
         for (i = 0; attribute_names && attribute_names[i]; i++) {
             if (!strcmp (attribute_names[i], "code")) {
                 char *country_code;
-                char *country;
+                NMACountryInfo *country_info;
 
                 country_code = g_ascii_strup (attribute_values[i], -1);
-                country = g_hash_table_lookup (parser->country_codes, country_code);
-                if (country) {
-                    parser->current_country = g_strdup (country);
-                    g_free (country_code);
-                } else
-                    parser->current_country = country_code;
+                country_info = g_hash_table_lookup (parser->table, country_code);
+                /* Ensure we have a country provider for this country code */
+                if (!country_info) {
+                    g_warning ("%s: adding providers for unknown country '%s'", __func__, country_code);
+                    country_info = country_info_new (country_code, NULL);
+                    g_hash_table_insert (parser->table, country_code, country_info);
+                }
+                parser->current_country = country_code;
 
                 parser->state = PARSER_COUNTRY;
                 break;
@@ -468,7 +612,13 @@ parser_country_end (MobileParser *parser,
                     const char *name)
 {
     if (!strcmp (name, "country")) {
-        g_hash_table_insert (parser->table, parser->current_country, parser->current_providers);
+        NMACountryInfo *country_info;
+
+        country_info = g_hash_table_lookup (parser->table, parser->current_country);
+        if (country_info)
+            /* Store providers for this country */
+            country_info->providers = parser->current_providers;
+
         parser->current_country = NULL;
         parser->current_providers = NULL;
         parser->text_buffer = NULL;
@@ -630,8 +780,18 @@ static const GMarkupParser mobile_parser = {
 /******************************************************************************/
 /* Parser interface */
 
+/**
+ * nma_mobile_providers_parse:
+ * @country_codes: (allow-none) File with the list of country codes.
+ * @service_providers: (allow-none) File with the list of service providers.
+ *
+ * Returns: (element-type utf8 NMGtk.CountryInfo) (transfer full): a
+ *   hash table where keys are country names #gchar and values are #NMACountryInfo.
+ *   Everything is destroyed with g_hash_table_destroy().
+ */
 GHashTable *
-nma_mobile_providers_parse (GHashTable **out_ccs)
+nma_mobile_providers_parse (const gchar *country_codes,
+                            const gchar *service_providers)
 {
     GMarkupParseContext *ctx;
     GIOChannel *channel;
@@ -641,24 +801,29 @@ nma_mobile_providers_parse (GHashTable **out_ccs)
     GIOStatus status;
     gsize len = 0;
 
+    /* Use default paths if none given */
+    if (!country_codes)
+        country_codes = ISO_3166_COUNTRY_CODES;
+    if (!service_providers)
+        service_providers = MOBILE_BROADBAND_PROVIDER_INFO;
+
     memset (&parser, 0, sizeof (MobileParser));
 
-    parser.country_codes = read_country_codes ();
-    if (!parser.country_codes)
+    parser.table = read_country_codes (country_codes);
+    if (!parser.table)
         goto out;
 
-    channel = g_io_channel_new_file (MOBILE_BROADBAND_PROVIDER_INFO, "r", &error);
+    channel = g_io_channel_new_file (service_providers, "r", &error);
     if (!channel) {
         if (error) {
-            g_warning ("Could not read " MOBILE_BROADBAND_PROVIDER_INFO ": %s", error->message);
+            g_warning ("Could not read %s: %s", service_providers, error->message);
             g_error_free (error);
         } else
-            g_warning ("Could not read " MOBILE_BROADBAND_PROVIDER_INFO ": Unknown error");
+            g_warning ("Could not read %s: Unknown error", service_providers);
 
         goto out;
     }
 
-    parser.table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, provider_list_free);
     parser.state = PARSER_TOPLEVEL;
 
     ctx = g_markup_parse_context_new (&mobile_parser, 0, &parser, NULL);
@@ -703,13 +868,7 @@ nma_mobile_providers_parse (GHashTable **out_ccs)
     g_free (parser.current_country);
     g_free (parser.text_buffer);
 
- out:
-    if (parser.country_codes) {
-        if (out_ccs)
-            *out_ccs = parser.country_codes;
-        else
-            g_hash_table_destroy (parser.country_codes);
-    }
+out:
 
     return parser.table;
 }
@@ -751,12 +910,17 @@ dump_gsm (NMAMobileAccessMethod *method)
 static void
 dump_country (gpointer key, gpointer value, gpointer user_data)
 {
-    GSList *citer, *miter;
+    GSList *miter, *citer;
+    NMACountryInfo *country_info = value;
+
+    g_print ("Country: %s (%s)\n",
+             country_info->country_code,
+             country_info->country_name);
 
-    for (citer = value; citer; citer = g_slist_next (citer)) {
+    for (citer = country_info->providers; citer; citer = g_slist_next (citer)) {
         NMAMobileProvider *provider = citer->data;
 
-        g_print ("Provider: %s (%s)\n", provider->name, (const char *) key);
+        g_print ("    Provider: %s (%s)\n", provider->name, (const char *) key);
         for (miter = provider->methods; miter; miter = g_slist_next (miter)) {
             NMAMobileAccessMethod *method = miter->data;
             GSList *liter;
@@ -786,8 +950,8 @@ dump_country (gpointer key, gpointer value, gpointer user_data)
 }
 
 void
-nma_mobile_providers_dump (GHashTable *providers)
+nma_mobile_providers_dump (GHashTable *country_infos)
 {
-    g_return_if_fail (providers != NULL);
-    g_hash_table_foreach (providers, dump_country, NULL);
+    g_return_if_fail (country_infos != NULL);
+    g_hash_table_foreach (country_infos, dump_country, NULL);
 }
diff --git a/src/libnm-gtk/nm-mobile-providers.h b/src/libnm-gtk/nm-mobile-providers.h
index d2dbb15..fbe4bb9 100644
--- a/src/libnm-gtk/nm-mobile-providers.h
+++ b/src/libnm-gtk/nm-mobile-providers.h
@@ -18,7 +18,7 @@
  * Copyright (C) 2009 Novell, Inc.
  * Author: Tambet Ingo (tambet gmail com).
  *
- * Copyright (C) 2009 - 2010 Red Hat, Inc.
+ * Copyright (C) 2009 - 2012 Red Hat, Inc.
  */
 
 /* WARNING: this file is private API between nm-applet and various GNOME
@@ -34,11 +34,15 @@
 /******************************************************************************/
 /* GSM MCCMNC type */
 
+#define NMA_TYPE_GSM_MCC_MNC (nma_gsm_mcc_mnc_get_type ())
+
 typedef struct {
     char *mcc;
     char *mnc;
 } NMAGsmMccMnc;
 
+GType nma_gsm_mcc_mnc_get_type (void);
+
 /******************************************************************************/
 /* Access method type */
 
@@ -93,17 +97,36 @@ typedef struct {
 GType              nma_mobile_provider_get_type        (void);
 NMAMobileProvider *nma_mobile_provider_ref             (NMAMobileProvider *provider);
 void               nma_mobile_provider_unref           (NMAMobileProvider *provider);
-
+GSList            *nma_mobile_provider_get_gsm_mcc_mnc (NMAMobileProvider *provider);
+GSList            *nma_mobile_provider_get_cdma_sid    (NMAMobileProvider *provider);
 
 /******************************************************************************/
-/* Utils */
+/* Country Info type */
+
+#define NMA_TYPE_COUNTRY_INFO (nma_country_info_get_type ())
 
-/* Returns a hash table where keys are country names 'char *',
-   values are a 'GSList *' of 'NmaMobileProvider *'.
-   Everything is destroyed with g_hash_table_destroy (). */
+typedef struct {
+    char *country_code;
+    char *country_name;
+    GSList *providers;
 
-GHashTable *nma_mobile_providers_parse (GHashTable **out_ccs);
+    gint refs;
+} NMACountryInfo;
+
+GType           nma_country_info_get_type         (void);
+NMACountryInfo *nma_country_info_ref              (NMACountryInfo *country_info);
+void            nma_country_info_unref            (NMACountryInfo *country_info);
+const gchar    *nma_country_info_get_country_code (NMACountryInfo *country_info);
+const gchar    *nma_country_info_get_country_name (NMACountryInfo *country_info);
+GSList         *nma_country_info_get_providers    (NMACountryInfo *country_info);
+
+/******************************************************************************/
+/* Utils */
 
-void nma_mobile_providers_dump (GHashTable *providers);
+/* Returns a table where keys are country codes and values are NMACountryInfo
+ * values */
+GHashTable *nma_mobile_providers_parse (const gchar *country_codes,
+                                        const gchar *service_providers);
+void        nma_mobile_providers_dump  (GHashTable *country_infos);
 
 #endif /* NM_MOBILE_PROVIDERS_H */
diff --git a/src/libnm-gtk/nm-mobile-wizard.c b/src/libnm-gtk/nm-mobile-wizard.c
index 147270c..e247b38 100644
--- a/src/libnm-gtk/nm-mobile-wizard.c
+++ b/src/libnm-gtk/nm-mobile-wizard.c
@@ -43,7 +43,7 @@
 #define DEVICE_TAG "device"
 #define TYPE_TAG "setting-type"
 
-static char *get_selected_country (NMAMobileWizard *self, gboolean *unlisted);
+static NMACountryInfo *get_selected_country (NMAMobileWizard *self);
 static NMAMobileProvider *get_selected_provider (NMAMobileWizard *self);
 static NMAMobileAccessMethodType get_provider_unlisted_type (NMAMobileWizard *self);
 static NMAMobileAccessMethod *get_selected_method (NMAMobileWizard *self, gboolean *manual);
@@ -52,8 +52,7 @@ struct NMAMobileWizard {
 	GtkWidget *assistant;
 	NMAMobileWizardCallback callback;
 	gpointer user_data;
-	GHashTable *providers;
-	GHashTable *country_codes;
+	GHashTable *country_infos;
 	NMAMobileAccessMethodType method_type;
 	gboolean initial_method_type;
 	gboolean will_connect_after;
@@ -66,7 +65,7 @@ struct NMAMobileWizard {
 
 	/* Country page */
 	guint32 country_idx;
-	const char *country;
+	NMACountryInfo *country;
 	GtkWidget *country_page;
 	GtkWidget *country_view;
 	GtkTreeStore *country_store;
@@ -283,11 +282,11 @@ confirm_prepare (NMAMobileWizard *self)
 {
 	NMAMobileProvider *provider = NULL;
 	NMAMobileAccessMethod *method = NULL;
-	char *country = NULL;
+	NMACountryInfo *country_info;
 	gboolean manual = FALSE;
 	GString *str;
 
-	country = get_selected_country (self, NULL);
+	country_info = get_selected_country (self);
 	provider = get_selected_provider (self);
 	if (provider)
 		method = get_selected_method (self, &manual);
@@ -304,9 +303,9 @@ confirm_prepare (NMAMobileWizard *self)
 		g_string_append (str, unlisted_provider);
 	}
 
-	if (country) {
-		g_string_append_printf (str, ", %s", country);
-		g_free (country);
+	if (country_info) {
+		g_string_append_printf (str, ", %s", nma_country_info_get_country_name (country_info));
+		nma_country_info_unref (country_info);
 	}
 	gtk_label_set_text (GTK_LABEL (self->confirm_provider), str->str);
 	g_string_free (str, TRUE);
@@ -851,13 +850,13 @@ static void
 providers_prepare (NMAMobileWizard *self)
 {
 	GtkTreeSelection *selection;
-	GSList *providers, *piter;
-	char *country;
+	NMACountryInfo *country_info;
+	GSList *piter;
 
 	gtk_tree_store_clear (self->providers_store);
 
-	country = get_selected_country (self, NULL);
-	if (!country) {
+	country_info = get_selected_country (self);
+	if (!country_info) {
 		/* Unlisted country */
 		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (self->provider_unlisted_radio), TRUE);
 		gtk_widget_set_sensitive (GTK_WIDGET (self->providers_view_radio), FALSE);
@@ -865,10 +864,9 @@ providers_prepare (NMAMobileWizard *self)
 	}
 	gtk_widget_set_sensitive (GTK_WIDGET (self->providers_view_radio), TRUE);
 
-	providers = g_hash_table_lookup (self->providers, country);
-	g_free (country);
-
-	for (piter = providers; piter; piter = g_slist_next (piter)) {
+	for (piter = nma_country_info_get_providers (country_info);
+	     piter;
+	     piter = g_slist_next (piter)) {
 		NMAMobileProvider *provider = piter->data;
 		GtkTreeIter provider_iter;
 
@@ -898,6 +896,8 @@ providers_prepare (NMAMobileWizard *self)
 		                    -1);
 	}
 
+	nma_country_info_unref (country_info);
+
 	g_object_set (G_OBJECT (self->providers_view), "enable-search", TRUE, NULL);
 
 	gtk_tree_view_set_search_column (GTK_TREE_VIEW (self->providers_view), PROVIDER_COL_NAME);
@@ -939,6 +939,9 @@ done:
 /* Country page */
 /**********************************************************/
 
+#define COUNTRIES_COL_NAME 0
+#define COUNTRIES_COL_INFO 1
+
 static gboolean
 country_search_func (GtkTreeModel *model,
                      gint column,
@@ -965,18 +968,25 @@ static void
 add_one_country (gpointer key, gpointer value, gpointer user_data)
 {
 	NMAMobileWizard *self = user_data;
+	NMACountryInfo *country_info = value;
 	GtkTreeIter country_iter;
 	GtkTreePath *country_path, *country_view_path;
 
 	g_assert (key);
 
 	gtk_tree_store_append (GTK_TREE_STORE (self->country_store), &country_iter, NULL);
-	gtk_tree_store_set (GTK_TREE_STORE (self->country_store), &country_iter, 0, key, -1);
+	gtk_tree_store_set (GTK_TREE_STORE (self->country_store),
+	                    &country_iter,
+	                    COUNTRIES_COL_NAME,
+	                    nma_country_info_get_country_name (country_info),
+	                    COUNTRIES_COL_INFO,
+	                    country_info,
+	                    -1);
 
 	/* If this country is the same country as the user's current locale,
 	 * select it by default.
 	 */
-	if (!self->country || g_ascii_strcasecmp (self->country, key))
+	if (self->country != country_info)
 		return;
 
 	country_path = gtk_tree_model_get_path (GTK_TREE_MODEL (self->country_store), &country_iter);
@@ -999,45 +1009,35 @@ add_one_country (gpointer key, gpointer value, gpointer user_data)
 	gtk_tree_path_free (country_path);
 }
 
-static char *
-get_selected_country (NMAMobileWizard *self, gboolean *out_unlisted)
+NMACountryInfo *
+get_selected_country (NMAMobileWizard *self)
 {
 	GtkTreeSelection *selection;
 	GtkTreeModel *model = NULL;
 	GtkTreeIter iter;
-	char *country = NULL;
-	gboolean unlisted = FALSE;
-
-	if (!self->country_codes)
-		return NULL;
+	NMACountryInfo *country_info = NULL;
 
 	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (self->country_view));
 	g_assert (selection);
 
-	if (gtk_tree_selection_get_selected (GTK_TREE_SELECTION (selection), &model, &iter)) {
-		gtk_tree_model_get (model, &iter, 0, &country, 1, &unlisted, -1);
-		if (unlisted) {
-			g_free (country);
-			country = NULL;
-			if (out_unlisted)
-				*out_unlisted = unlisted;
-		}
-	}
+	if (!gtk_tree_selection_get_selected (GTK_TREE_SELECTION (selection), &model, &iter))
+		return NULL;
 
-	return country;
+	gtk_tree_model_get (model, &iter, COUNTRIES_COL_INFO, &country_info, -1);
+	return country_info;
 }
 
 static void
 country_update_complete (NMAMobileWizard *self)
 {
-	char *country = NULL;
-	gboolean unlisted = FALSE;
+	NMACountryInfo *country_info;
 
-	country = get_selected_country (self, &unlisted);
+	country_info = get_selected_country (self);
 	gtk_assistant_set_page_complete (GTK_ASSISTANT (self->assistant),
 	                                 self->country_page,
-	                                 (!!country || unlisted));
-	g_free (country);
+	                                 (!!country_info));
+	if (country_info)
+		nma_country_info_unref (country_info);
 }
 
 static gint
@@ -1047,16 +1047,16 @@ country_sort_func (GtkTreeModel *model,
                    gpointer user_data)
 {
 	char *a_str = NULL, *b_str = NULL;
-	gboolean a_def = FALSE, b_def = FALSE;
+	NMACountryInfo *a_country_info = NULL, *b_country_info = NULL;
 	gint ret = 0;
 
-	gtk_tree_model_get (model, a, 0, &a_str, 1, &a_def, -1);
-	gtk_tree_model_get (model, b, 0, &b_str, 1, &b_def, -1);
+	gtk_tree_model_get (model, a, COUNTRIES_COL_NAME, &a_str, COUNTRIES_COL_INFO, &a_country_info, -1);
+	gtk_tree_model_get (model, b, COUNTRIES_COL_NAME, &b_str, COUNTRIES_COL_INFO, &b_country_info, -1);
 
-	if (a_def) {
+	if (!a_country_info) {
 		ret = -1;
 		goto out;
-	} else if (b_def) {
+	} else if (!b_country_info) {
 		ret = 1;
 		goto out;
 	}
@@ -1071,6 +1071,10 @@ country_sort_func (GtkTreeModel *model,
 		ret = g_utf8_collate (a_str, b_str);
 
 out:
+	if (a_country_info)
+		nma_country_info_unref (a_country_info);
+	if (b_country_info)
+		nma_country_info_unref (b_country_info);
 	g_free (a_str);
 	g_free (b_str);
 	return ret;
@@ -1095,33 +1099,37 @@ country_setup (NMAMobileWizard *self)
 	gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);
 	gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, TRUE, 0);
 
-	self->country_store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_BOOLEAN);
+	self->country_store = gtk_tree_store_new (2, G_TYPE_STRING, NMA_TYPE_COUNTRY_INFO);
 
 	self->country_sort = GTK_TREE_MODEL_SORT (gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (self->country_store)));
-	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self->country_sort), 0, GTK_SORT_ASCENDING);
+	gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (self->country_sort),
+	                                      COUNTRIES_COL_NAME, GTK_SORT_ASCENDING);
 
 	self->country_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (self->country_sort));
 
 	renderer = gtk_cell_renderer_text_new ();
-	column = gtk_tree_view_column_new_with_attributes (_("Country or region"), renderer, "text", 0, NULL);
+	column = gtk_tree_view_column_new_with_attributes (_("Country or region"),
+	                                                   renderer,
+	                                                   "text", COUNTRIES_COL_NAME,
+	                                                   NULL);
 	gtk_tree_view_append_column (GTK_TREE_VIEW (self->country_view), column);
 	gtk_tree_view_column_set_clickable (column, TRUE);
 
 	/* My country is not listed... */
 	gtk_tree_store_append (GTK_TREE_STORE (self->country_store), &unlisted_iter, NULL);
 	gtk_tree_store_set (GTK_TREE_STORE (self->country_store), &unlisted_iter,
-	                    0, _("My country is not listed"),
-	                    1, TRUE,
+	                    PROVIDER_COL_NAME, _("My country is not listed"),
+	                    PROVIDER_COL_PROVIDER, NULL,
 	                    -1);
 	gtk_tree_sortable_set_sort_func (GTK_TREE_SORTABLE (self->country_sort),
-	                                 0,
+	                                 COUNTRIES_COL_NAME,
 	                                 country_sort_func,
 	                                 NULL,
 	                                 NULL);
 
 	/* Add the rest of the providers */
-	if (self->providers)
-		g_hash_table_foreach (self->providers, add_one_country, self);
+	if (self->country_infos)
+		g_hash_table_foreach (self->country_infos, add_one_country, self);
 	g_object_set (G_OBJECT (self->country_view), "enable-search", TRUE, NULL);
 
 	/* If no row has focus yet, focus the first row so that the user can start
@@ -1180,7 +1188,7 @@ focus_country_view (gpointer user_data)
 static void
 country_prepare (NMAMobileWizard *self)
 {
-	gtk_tree_view_set_search_column (GTK_TREE_VIEW (self->country_view), 0);
+	gtk_tree_view_set_search_column (GTK_TREE_VIEW (self->country_view), COUNTRIES_COL_NAME);
 	gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (self->country_view), country_search_func, self, NULL);
 
 	if (!self->country_focus_id)
@@ -1604,11 +1612,11 @@ get_country_from_locale (void)
  */
 NMAMobileWizard *
 nma_mobile_wizard_new (GtkWindow *parent,
-                   GtkWindowGroup *window_group,
-                   NMDeviceModemCapabilities modem_caps,
-                   gboolean will_connect_after,
-                   NMAMobileWizardCallback cb,
-                   gpointer user_data)
+                       GtkWindowGroup *window_group,
+                       NMDeviceModemCapabilities modem_caps,
+                       gboolean will_connect_after,
+                       NMAMobileWizardCallback cb,
+                       gpointer user_data)
 {
 	NMAMobileWizard *self;
 	char *cc;
@@ -1616,16 +1624,17 @@ nma_mobile_wizard_new (GtkWindow *parent,
 	self = g_malloc0 (sizeof (NMAMobileWizard));
 	g_return_val_if_fail (self != NULL, NULL);
 
-	self->providers = nma_mobile_providers_parse (&(self->country_codes));
-	if (!self->providers || !self->country_codes) {
+	self->country_infos = nma_mobile_providers_parse (NULL, NULL);
+	if (!self->country_infos) {
 		nma_mobile_wizard_destroy (self);
 		return NULL;
 	}
 
 	cc = get_country_from_locale ();
-	if (cc)
-		self->country = g_hash_table_lookup (self->country_codes, cc);
-	g_free (cc);
+	if (cc) {
+		self->country = g_hash_table_lookup (self->country_infos, cc);
+		g_free (cc);
+	}
 
 	self->will_connect_after = will_connect_after;
 	self->callback = cb;
@@ -1693,11 +1702,8 @@ nma_mobile_wizard_destroy (NMAMobileWizard *self)
 	remove_provider_focus_idle (self);
 	remove_country_focus_idle (self);
 
-	if (self->providers)
-		g_hash_table_destroy (self->providers);
-
-	if (self->country_codes)
-		g_hash_table_destroy (self->country_codes);
+	if (self->country_infos)
+		g_hash_table_destroy (self->country_infos);
 
 	g_free (self);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]