diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2012-11-23 12:07:56 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-12-04 18:12:14 +0100 |
commit | 063246248a3df43ed4dc1f3ac1b8e909892f3218 (patch) | |
tree | c916e65874e4ebbec545e5c800aed61f68f10570 | |
parent | 7bc0a247c939764a51248ed8077f6f11597f7082 (diff) | |
download | network-manager-applet-063246248a3df43ed4dc1f3ac1b8e909892f3218.tar.gz |
libnm-gtk: reorganize mobile providers code
Just reorganize the code to have it a bit cleaner. Will also help to review the
changes coming next.
-rw-r--r-- | src/libnm-gtk/nm-mobile-providers.c | 229 | ||||
-rw-r--r-- | src/libnm-gtk/nm-mobile-providers.h | 38 |
2 files changed, 145 insertions, 122 deletions
diff --git a/src/libnm-gtk/nm-mobile-providers.c b/src/libnm-gtk/nm-mobile-providers.c index 519b5299..d3518941 100644 --- a/src/libnm-gtk/nm-mobile-providers.c +++ b/src/libnm-gtk/nm-mobile-providers.c @@ -38,115 +38,8 @@ #define ISO_3166_COUNTRY_CODES ISO_CODES_PREFIX"/share/xml/iso-codes/iso_3166.xml" #define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX"/share/locale" - -/* XML Parser for iso_3166.xml */ - -static void -iso_3166_parser_start_element (GMarkupParseContext *context, - const gchar *element_name, - const gchar **attribute_names, - const gchar **attribute_values, - gpointer data, - GError **error) -{ - int i; - const char *country_code = NULL; - const char *common_name = NULL; - const char *name = NULL; - GHashTable *table = (GHashTable *) data; - - if (!strcmp (element_name, "iso_3166_entry")) { - for (i = 0; attribute_names && attribute_names[i]; i++) { - if (!strcmp (attribute_names[i], "alpha_2_code")) - country_code = attribute_values[i]; - else if (!strcmp (attribute_names[i], "common_name")) - common_name = attribute_values[i]; - else if (!strcmp (attribute_names[i], "name")) - name = attribute_values[i]; - } - if (!country_code) { - g_warning ("%s: missing mandatory 'alpha_2_code' atribute in '%s'" - " element.", __func__, element_name); - return; - } - if (!name) { - g_warning ("%s: missing mandatory 'name' atribute in '%s'" - " element.", __func__, element_name); - return; - } - - g_hash_table_insert (table, g_strdup (country_code), g_strdup (dgettext ("iso_3166", common_name ? common_name : name))); - } -} - -static const GMarkupParser iso_3166_parser = { - iso_3166_parser_start_element, - NULL, /* end element */ - NULL, /* text */ - NULL, /* passthrough */ - NULL /* error */ -}; - -static GHashTable * -read_country_codes (void) -{ - GHashTable *table = NULL; - GMarkupParseContext *ctx; - GError *error = NULL; - char *buf; - gsize buf_len; - - /* Set domain to iso_3166 for country name translation */ - 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); - 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_error_free (error); - g_hash_table_destroy (table); - table = NULL; - } - - g_markup_parse_context_free (ctx); - g_free (buf); - } else { - g_warning ("Failed to load '%s': %s\n Consider installing 'iso-codes'\n", - ISO_3166_COUNTRY_CODES, error->message); - g_error_free (error); - } - - return table; -} - - -/* XML Parser for serviceproviders.xml */ - -typedef enum { - PARSER_TOPLEVEL = 0, - PARSER_COUNTRY, - PARSER_PROVIDER, - PARSER_METHOD_GSM, - PARSER_METHOD_GSM_APN, - PARSER_METHOD_CDMA, - PARSER_ERROR -} MobileContextState; - -typedef struct { - GHashTable *country_codes; - GHashTable *table; - - char *current_country; - GSList *current_providers; - NMAMobileProvider *current_provider; - NMAMobileAccessMethod *current_method; - - char *text_buffer; - MobileContextState state; -} MobileParser; +/******************************************************************************/ +/* GSM MCCMNC type */ static NMAGsmMccMnc * mcc_mnc_new (const char *mcc, const char *mnc) @@ -168,6 +61,9 @@ mcc_mnc_free (NMAGsmMccMnc *m) g_slice_free (NMAGsmMccMnc, m); } +/******************************************************************************/ +/* Access method type */ + static NMAMobileAccessMethod * access_method_new (void) { @@ -226,6 +122,8 @@ nma_mobile_access_method_get_type (void) return type; } +/******************************************************************************/ +/* Mobile provider type */ static NMAMobileProvider * provider_new (void) @@ -281,6 +179,116 @@ nma_mobile_provider_get_type (void) return type; } +/******************************************************************************/ +/* XML Parser for iso_3166.xml */ + +static void +iso_3166_parser_start_element (GMarkupParseContext *context, + const gchar *element_name, + const gchar **attribute_names, + const gchar **attribute_values, + gpointer data, + GError **error) +{ + int i; + const char *country_code = NULL; + const char *common_name = NULL; + const char *name = NULL; + GHashTable *table = (GHashTable *) data; + + if (!strcmp (element_name, "iso_3166_entry")) { + for (i = 0; attribute_names && attribute_names[i]; i++) { + if (!strcmp (attribute_names[i], "alpha_2_code")) + country_code = attribute_values[i]; + else if (!strcmp (attribute_names[i], "common_name")) + common_name = attribute_values[i]; + else if (!strcmp (attribute_names[i], "name")) + name = attribute_values[i]; + } + if (!country_code) { + g_warning ("%s: missing mandatory 'alpha_2_code' atribute in '%s'" + " element.", __func__, element_name); + return; + } + if (!name) { + g_warning ("%s: missing mandatory 'name' atribute in '%s'" + " element.", __func__, element_name); + return; + } + + g_hash_table_insert (table, g_strdup (country_code), g_strdup (dgettext ("iso_3166", common_name ? common_name : name))); + } +} + +static const GMarkupParser iso_3166_parser = { + iso_3166_parser_start_element, + NULL, /* end element */ + NULL, /* text */ + NULL, /* passthrough */ + NULL /* error */ +}; + +static GHashTable * +read_country_codes (void) +{ + GHashTable *table = NULL; + GMarkupParseContext *ctx; + GError *error = NULL; + char *buf; + gsize buf_len; + + /* Set domain to iso_3166 for country name translation */ + 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); + 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_error_free (error); + g_hash_table_destroy (table); + table = NULL; + } + + g_markup_parse_context_free (ctx); + g_free (buf); + } else { + g_warning ("Failed to load '%s': %s\n Consider installing 'iso-codes'\n", + ISO_3166_COUNTRY_CODES, error->message); + g_error_free (error); + } + + return table; +} + +/******************************************************************************/ +/* XML Parser for serviceproviders.xml */ + +typedef enum { + PARSER_TOPLEVEL = 0, + PARSER_COUNTRY, + PARSER_PROVIDER, + PARSER_METHOD_GSM, + PARSER_METHOD_GSM_APN, + PARSER_METHOD_CDMA, + PARSER_ERROR +} MobileContextState; + +typedef struct { + GHashTable *country_codes; + GHashTable *table; + + char *current_country; + GSList *current_providers; + NMAMobileProvider *current_provider; + NMAMobileAccessMethod *current_method; + + char *text_buffer; + MobileContextState state; +} MobileParser; + static void provider_list_free (gpointer data) { @@ -619,6 +627,9 @@ static const GMarkupParser mobile_parser = { NULL /* error */ }; +/******************************************************************************/ +/* Parser interface */ + GHashTable * nma_mobile_providers_parse (GHashTable **out_ccs) { diff --git a/src/libnm-gtk/nm-mobile-providers.h b/src/libnm-gtk/nm-mobile-providers.h index a9536df8..d2dbb154 100644 --- a/src/libnm-gtk/nm-mobile-providers.h +++ b/src/libnm-gtk/nm-mobile-providers.h @@ -31,8 +31,16 @@ #include <glib.h> #include <glib-object.h> -#define NMA_TYPE_MOBILE_PROVIDER (nma_mobile_provider_get_type ()) -#define NMA_TYPE_MOBILE_ACCESS_METHOD (nma_mobile_access_method_get_type ()) +/******************************************************************************/ +/* GSM MCCMNC type */ + +typedef struct { + char *mcc; + char *mnc; +} NMAGsmMccMnc; + +/******************************************************************************/ +/* Access method type */ typedef enum { NMA_MOBILE_ACCESS_METHOD_TYPE_UNKNOWN = 0, @@ -40,10 +48,7 @@ typedef enum { NMA_MOBILE_ACCESS_METHOD_TYPE_CDMA } NMAMobileAccessMethodType; -typedef struct { - char *mcc; - char *mnc; -} NMAGsmMccMnc; +#define NMA_TYPE_MOBILE_ACCESS_METHOD (nma_mobile_access_method_get_type ()) typedef struct { char *name; @@ -63,6 +68,15 @@ typedef struct { gint refs; } NMAMobileAccessMethod; +GType nma_mobile_access_method_get_type (void); +NMAMobileAccessMethod *nma_mobile_access_method_ref (NMAMobileAccessMethod *method); +void nma_mobile_access_method_unref (NMAMobileAccessMethod *method); + +/******************************************************************************/ +/* Mobile provider type */ + +#define NMA_TYPE_MOBILE_PROVIDER (nma_mobile_provider_get_type ()) + typedef struct { char *name; /* maps lang (char *) -> name (char *) */ @@ -76,15 +90,13 @@ typedef struct { gint refs; } NMAMobileProvider; +GType nma_mobile_provider_get_type (void); +NMAMobileProvider *nma_mobile_provider_ref (NMAMobileProvider *provider); +void nma_mobile_provider_unref (NMAMobileProvider *provider); -GType nma_mobile_provider_get_type (void); -GType nma_mobile_access_method_get_type (void); - -NMAMobileProvider *nma_mobile_provider_ref (NMAMobileProvider *provider); -void nma_mobile_provider_unref (NMAMobileProvider *provider); -NMAMobileAccessMethod *nma_mobile_access_method_ref (NMAMobileAccessMethod *method); -void nma_mobile_access_method_unref (NMAMobileAccessMethod *method); +/******************************************************************************/ +/* Utils */ /* Returns a hash table where keys are country names 'char *', values are a 'GSList *' of 'NmaMobileProvider *'. |