[network-manager-applet/lr/jtojnar-happy: 27/35] mobile-providers: collect providers from unrecognized countries together



commit 486030365e8baf24f73cb85ae9814aa3036d1a99
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Mon Aug 27 14:47:08 2018 +0200

    mobile-providers: collect providers from unrecognized countries together
    
    Move the "My country is not listed" entry to the providers database,
    with an empty country code. Add the providers from unrecognized
    countries to this.
    
    This is essentially a robustness improvement. It also makes broken
    installations (no iso-codes, etc.) less hopeless.

 src/libnma/nma-mobile-providers.c | 75 ++++++++++++++++++---------------------
 src/libnma/nma-mobile-wizard.c    | 39 ++++++++------------
 2 files changed, 49 insertions(+), 65 deletions(-)
---
diff --git a/src/libnma/nma-mobile-providers.c b/src/libnma/nma-mobile-providers.c
index dffcdf61..1d51ae35 100644
--- a/src/libnma/nma-mobile-providers.c
+++ b/src/libnma/nma-mobile-providers.c
@@ -383,13 +383,16 @@ nma_country_info_unref (NMACountryInfo *country_info)
  * nma_country_info_get_country_code:
  * @country_info: a #NMACountryInfo
  *
- * Returns: (transfer none): the code of the country.
+ * Returns: (transfer none): the code of the country or %NULL for "Unknown".
  */
 const gchar *
 nma_country_info_get_country_code (NMACountryInfo *country_info)
 {
        g_return_val_if_fail (country_info != NULL, NULL);
 
+       if (country_info->country_code[0] == '\0')
+               return NULL;
+
        return country_info->country_code;
 }
 
@@ -526,8 +529,8 @@ typedef enum {
 typedef struct {
        GHashTable *table;
 
-       char *current_country;
-       GSList *current_providers;
+       NMACountryInfo *current_country;
+       char *country_code;
        NMAMobileProvider *current_provider;
        NMAMobileAccessMethod *current_method;
 
@@ -535,15 +538,18 @@ typedef struct {
        MobileContextState state;
 } MobileParser;
 
-static void
-provider_list_free (gpointer data)
+static NMACountryInfo *
+lookup_country (GHashTable *table, const char *country_code)
 {
-       GSList *list = (GSList *) data;
+       NMACountryInfo *country_info;
 
-       while (list) {
-               nma_mobile_provider_unref ((NMAMobileProvider *) list->data);
-               list = g_slist_delete_link (list, list);
+       country_info = g_hash_table_lookup (table, country_code);
+       if (!country_info) {
+               g_warning ("%s: adding providers for unknown country '%s'", __func__, country_code);
+               country_info = g_hash_table_lookup (table, "");
        }
+
+       return country_info;
 }
 
 static void
@@ -568,19 +574,9 @@ parser_toplevel_start (MobileParser *parser,
        } else if (!strcmp (name, "country")) {
                for (i = 0; attribute_names && attribute_names[i]; i++) {
                        if (!strcmp (attribute_names[i], "code")) {
-                               char *country_code;
-                               NMACountryInfo *country_info;
-
-                               country_code = g_ascii_strup (attribute_values[i], -1);
-                               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;
-
+                               g_free (parser->country_code);
+                               parser->country_code = g_ascii_strup (attribute_values[i], -1);
+                               parser->current_country = lookup_country (parser->table, 
parser->country_code);
                                parser->state = PARSER_COUNTRY;
                                break;
                        }
@@ -723,17 +719,9 @@ parser_country_end (MobileParser *parser,
                     const char *name)
 {
        if (!strcmp (name, "country")) {
-               NMACountryInfo *country_info;
-
-               country_info = g_hash_table_lookup (parser->table, parser->current_country);
-               g_assert (country_info);
-
-               /* Store providers for this country */
-               country_info->providers = parser->current_providers;
-
-               g_free (parser->current_country);
                parser->current_country = NULL;
-               parser->current_providers = NULL;
+               g_free (parser->country_code);
+               parser->country_code = NULL;
                g_free (parser->text_buffer);
                parser->text_buffer = NULL;
                parser->state = PARSER_TOPLEVEL;
@@ -747,7 +735,14 @@ parser_provider_end (MobileParser *parser,
        if (!strcmp (name, "name")) {
                if (!parser->current_provider->name) {
                        /* Use the first one. */
-                       parser->current_provider->name = parser->text_buffer;
+                       if (nma_country_info_get_country_code (parser->current_country)) {
+                               parser->current_provider->name = parser->text_buffer;
+                       } else {
+                               parser->current_provider->name = g_strdup_printf ("%s (%s)",
+                                                                                 parser->text_buffer,
+                                                                                 parser->country_code);
+                               g_free (parser->text_buffer);
+                       }
                        parser->text_buffer = NULL;
                }
        } else if (!strcmp (name, "provider")) {
@@ -756,7 +751,8 @@ parser_provider_end (MobileParser *parser,
 
                parser->current_provider->methods = g_slist_reverse (parser->current_provider->methods);
 
-               parser->current_providers = g_slist_prepend (parser->current_providers, 
parser->current_provider);
+               parser->current_country->providers = g_slist_prepend (parser->current_country->providers,
+                                                                     parser->current_provider);
                parser->current_provider = NULL;
                g_free (parser->text_buffer);
                parser->text_buffer = NULL;
@@ -917,7 +913,7 @@ read_service_providers (GHashTable *countries,
        gsize len = 0;
 
        memset (&parser, 0, sizeof (MobileParser));
-    parser.table = countries;
+       parser.table = countries;
 
        channel = g_io_channel_new_file (service_providers, "r", error);
        if (!channel) {
@@ -968,12 +964,6 @@ read_service_providers (GHashTable *countries,
                nma_mobile_provider_unref (parser.current_provider);
        }
 
-       if (parser.current_providers) {
-               g_warning ("pending current providers");
-               provider_list_free (parser.current_providers);
-       }
-
-       g_free (parser.current_country);
        g_free (parser.text_buffer);
 
        return (status == G_IO_STATUS_EOF);
@@ -997,6 +987,9 @@ mobile_providers_parse_sync (const gchar *country_codes,
                                            g_free,
                                            (GDestroyNotify)nma_country_info_unref);
 
+       g_hash_table_insert (countries, g_strdup (""),
+                            country_info_new ("", _("My country is not listed")));
+
        /* Use default paths if none given */
        if (country_codes) {
                if (!read_country_codes (countries, country_codes, cancellable, error)) {
diff --git a/src/libnma/nma-mobile-wizard.c b/src/libnma/nma-mobile-wizard.c
index 169d1712..3f48d3f6 100644
--- a/src/libnma/nma-mobile-wizard.c
+++ b/src/libnma/nma-mobile-wizard.c
@@ -231,7 +231,6 @@ confirm_prepare (NMAMobileWizard *self)
        gboolean manual = FALSE;
        GString *str;
 
-       country_info = get_selected_country (self);
        provider = get_selected_provider (self);
        if (provider)
                method = get_selected_method (self, &manual);
@@ -248,10 +247,11 @@ confirm_prepare (NMAMobileWizard *self)
                g_string_append (str, unlisted_provider);
        }
 
-       if (country_info) {
+       country_info = get_selected_country (self);
+       if (nma_country_info_get_country_code (country_info))
                g_string_append_printf (str, ", %s", nma_country_info_get_country_name (country_info));
-               nma_country_info_unref (country_info);
-       }
+       nma_country_info_unref (country_info);
+
        gtk_label_set_text (GTK_LABEL (priv->confirm_provider), str->str);
        g_string_free (str, TRUE);
 
@@ -649,14 +649,6 @@ providers_prepare (NMAMobileWizard *self)
        gtk_tree_store_clear (priv->providers_store);
 
        country_info = get_selected_country (self);
-       if (!country_info) {
-               /* Unlisted country */
-               gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->provider_unlisted_radio), TRUE);
-               gtk_widget_set_sensitive (GTK_WIDGET (priv->providers_view_radio), FALSE);
-               goto done;
-       }
-       gtk_widget_set_sensitive (GTK_WIDGET (priv->providers_view_radio), TRUE);
-
        for (piter = nma_country_info_get_providers (country_info);
             piter;
             piter = g_slist_next (piter)) {
@@ -688,7 +680,6 @@ providers_prepare (NMAMobileWizard *self)
                                    provider,
                                    -1);
        }
-
        nma_country_info_unref (country_info);
 
        gtk_tree_view_set_search_column (GTK_TREE_VIEW (priv->providers_view), PROVIDER_COL_NAME);
@@ -713,7 +704,15 @@ providers_prepare (NMAMobileWizard *self)
                }
        }
 
-done:
+       if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL (priv->providers_store), NULL) == 0) {
+               /* No providers to choose from. */
+               gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->provider_unlisted_radio), TRUE);
+               gtk_widget_set_sensitive (GTK_WIDGET (priv->providers_view_radio), FALSE);
+       } else {
+               gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->providers_view_radio), TRUE);
+               gtk_widget_set_sensitive (GTK_WIDGET (priv->providers_view_radio), TRUE);
+       }
+
        providers_radio_toggled (NULL, self);
 
        /* Initial completeness state */
@@ -859,10 +858,10 @@ country_sort_func (GtkTreeModel *model,
        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_country_info) {
+       if (!a_country_info || !nma_country_info_get_country_code (a_country_info)) {
                ret = -1;
                goto out;
-       } else if (!b_country_info) {
+       } else if (!b_country_info || !nma_country_info_get_country_code (b_country_info)) {
                ret = 1;
                goto out;
        }
@@ -893,7 +892,6 @@ country_setup (NMAMobileWizard *self)
        GtkCellRenderer *renderer;
        GtkTreeViewColumn *column;
        GtkTreeSelection *selection;
-       GtkTreeIter unlisted_iter;
 
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (priv->country_sort),
                                              COUNTRIES_COL_NAME, GTK_SORT_ASCENDING);
@@ -912,13 +910,6 @@ country_setup (NMAMobileWizard *self)
        gtk_tree_view_append_column (GTK_TREE_VIEW (priv->country_view), column);
        gtk_tree_view_column_set_clickable (column, TRUE);
 
-       /* My country is not listed... */
-       gtk_tree_store_append (GTK_TREE_STORE (priv->country_store), &unlisted_iter, NULL);
-       gtk_tree_store_set (GTK_TREE_STORE (priv->country_store), &unlisted_iter,
-                           COUNTRIES_COL_NAME, _("My country is not listed"),
-                           COUNTRIES_COL_INFO, NULL,
-                           -1);
-
        /* Add the rest of the providers */
        if (priv->mobile_providers_database) {
                GHashTable *countries;


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