summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksander Morgado <aleksander@lanedo.com>2012-11-23 12:28:37 +0100
committerAleksander Morgado <aleksander@lanedo.com>2012-12-04 18:12:14 +0100
commitbc60e05d38bb8ab1419707c99088dd31d29c3ec2 (patch)
treeb2b7eaf793577287334c1660cb4f26601eceab16
parent02e4976bceddcf3e456fbaea6a67093d97a86695 (diff)
downloadnetwork-manager-applet-bc60e05d38bb8ab1419707c99088dd31d29c3ec2.tar.gz
libnm-gtk: make all mobile provider types opaque
All types defined in the mobile provider parsing code are now opaque, in case we need to update them in the future. 'NMAGsmMccMnc' is the only one which is left with the members public, as it is not really likely to be modified in the current format. Also, stick to use G_DEFINE_BOXED_TYPE() to define the types, and make all reference count operations thread-safe.
-rw-r--r--src/applet-device-cdma.c6
-rw-r--r--src/applet-device-gsm.c8
-rw-r--r--src/libnm-gtk/nm-mobile-providers.c223
-rw-r--r--src/libnm-gtk/nm-mobile-providers.h49
-rw-r--r--src/libnm-gtk/nm-mobile-wizard.c54
5 files changed, 232 insertions, 108 deletions
diff --git a/src/applet-device-cdma.c b/src/applet-device-cdma.c
index ba753517..f081256b 100644
--- a/src/applet-device-cdma.c
+++ b/src/applet-device-cdma.c
@@ -730,9 +730,11 @@ find_provider_for_sid (GHashTable *table, guint32 sid)
NMAMobileProvider *provider = piter->data;
/* Search through CDMA SID list */
- for (siter = provider->cdma_sid; siter; siter = g_slist_next (siter)) {
+ for (siter = nma_mobile_provider_get_cdma_sid (provider);
+ siter;
+ siter = g_slist_next (siter)) {
if (GPOINTER_TO_UINT (siter->data) == sid) {
- name = g_strdup (provider->name);
+ name = g_strdup (nma_mobile_provider_get_name (provider));
break;
}
}
diff --git a/src/applet-device-gsm.c b/src/applet-device-gsm.c
index 500111b0..0b36a322 100644
--- a/src/applet-device-gsm.c
+++ b/src/applet-device-gsm.c
@@ -1120,7 +1120,9 @@ find_provider_for_mcc_mnc (GHashTable *table, const char *mccmnc)
NMAMobileProvider *provider = piter->data;
/* Search through MCC/MNC list */
- for (siter = provider->gsm_mcc_mnc; siter; siter = g_slist_next (siter)) {
+ for (siter = nma_mobile_provider_get_gsm_mcc_mnc (provider);
+ siter;
+ siter = g_slist_next (siter)) {
NMAGsmMccMnc *mcc = siter->data;
/* Match both 2-digit and 3-digit MNC; prefer a
@@ -1132,11 +1134,11 @@ find_provider_for_mcc_mnc (GHashTable *table, const char *mccmnc)
if ( !name3
&& (strlen (mccmnc) == 6)
&& !strncmp (mccmnc + 3, mcc->mnc, 3))
- name3 = provider->name;
+ name3 = nma_mobile_provider_get_name (provider);
if ( !name2
&& !strncmp (mccmnc + 3, mcc->mnc, 2))
- name2 = provider->name;
+ name2 = nma_mobile_provider_get_name (provider);
if (name2 && name3) {
done = TRUE;
diff --git a/src/libnm-gtk/nm-mobile-providers.c b/src/libnm-gtk/nm-mobile-providers.c
index c1af42bb..10899caa 100644
--- a/src/libnm-gtk/nm-mobile-providers.c
+++ b/src/libnm-gtk/nm-mobile-providers.c
@@ -19,6 +19,7 @@
* Author: Tambet Ingo (tambet@gmail.com).
*
* Copyright (C) 2009 - 2012 Red Hat, Inc.
+ * Copyright (C) 2012 Lanedo GmbH
*/
#include "config.h"
@@ -80,6 +81,29 @@ mcc_mnc_free (NMAGsmMccMnc *m)
/******************************************************************************/
/* Access method type */
+G_DEFINE_BOXED_TYPE (NMAMobileAccessMethod,
+ nma_mobile_access_method,
+ nma_mobile_access_method_ref,
+ nma_mobile_access_method_unref)
+
+struct _NMAMobileAccessMethod {
+ volatile gint refs;
+
+ char *name;
+ /* maps lang (char *) -> name (char *) */
+ GHashTable *lcl_names;
+
+ char *username;
+ char *password;
+ char *gateway;
+ GSList *dns; /* GSList of 'char *' */
+
+ /* Only used with NMA_PROVIDER_TYPE_GSM */
+ char *gsm_apn;
+
+ NMAMobileAccessMethodType type;
+};
+
static NMAMobileAccessMethod *
access_method_new (void)
{
@@ -100,7 +124,7 @@ nma_mobile_access_method_ref (NMAMobileAccessMethod *method)
g_return_val_if_fail (method != NULL, NULL);
g_return_val_if_fail (method->refs > 0, NULL);
- method->refs++;
+ g_atomic_int_inc (&method->refs);
return method;
}
@@ -111,7 +135,7 @@ nma_mobile_access_method_unref (NMAMobileAccessMethod *method)
g_return_if_fail (method != NULL);
g_return_if_fail (method->refs > 0);
- if (--method->refs == 0) {
+ if (g_atomic_int_dec_and_test (&method->refs)) {
g_free (method->name);
g_hash_table_destroy (method->lcl_names);
g_free (method->username);
@@ -125,22 +149,118 @@ nma_mobile_access_method_unref (NMAMobileAccessMethod *method)
}
}
-GType
-nma_mobile_access_method_get_type (void)
+/**
+ * nma_mobile_access_method_get_name:
+ *
+ * Returns: (transfer none): the name of the method.
+ */
+const gchar *
+nma_mobile_access_method_get_name (NMAMobileAccessMethod *method)
{
- static GType type = 0;
+ g_return_val_if_fail (method != NULL, NULL);
- if (G_UNLIKELY (type == 0)) {
- type = g_boxed_type_register_static ("NMAMobileAccessMethod",
- (GBoxedCopyFunc) nma_mobile_access_method_ref,
- (GBoxedFreeFunc) nma_mobile_access_method_unref);
- }
- return type;
+ return method->name;
+}
+
+/**
+ * nma_mobile_access_method_get_username:
+ *
+ * Returns: (transfer none): the username.
+ */
+const gchar *
+nma_mobile_access_method_get_username (NMAMobileAccessMethod *method)
+{
+ g_return_val_if_fail (method != NULL, NULL);
+
+ return method->username;
+}
+
+/**
+ * nma_mobile_access_method_get_password:
+ *
+ * Returns: (transfer none): the password.
+ */
+const gchar *
+nma_mobile_access_method_get_password (NMAMobileAccessMethod *method)
+{
+ g_return_val_if_fail (method != NULL, NULL);
+
+ return method->password;
+}
+
+/**
+ * nma_mobile_access_method_get_gateway:
+ *
+ * Returns: (transfer none): the gateway.
+ */
+const gchar *
+nma_mobile_access_method_get_gateway (NMAMobileAccessMethod *method)
+{
+ g_return_val_if_fail (method != NULL, NULL);
+
+ return method->gateway;
+}
+
+/**
+ * nma_mobile_access_method_get_dns:
+ *
+ * Returns: (element-type utf8) (transfer none): the list of DNS.
+ */
+const GSList *
+nma_mobile_access_method_get_dns (NMAMobileAccessMethod *method)
+{
+ g_return_val_if_fail (method != NULL, NULL);
+
+ return method->dns;
+}
+
+/**
+ * nma_mobile_access_method_get_gsm_apn:
+ *
+ * Returns: (transfer none): the GSM APN.
+ */
+const gchar *
+nma_mobile_access_method_get_gsm_apn (NMAMobileAccessMethod *method)
+{
+ g_return_val_if_fail (method != NULL, NULL);
+
+ return method->gsm_apn;
+}
+
+/**
+ * nma_mobile_access_method_get_method_type:
+ *
+ * Returns: a #NMAMobileAccessMethodType.
+ */
+NMAMobileAccessMethodType
+nma_mobile_access_method_get_method_type (NMAMobileAccessMethod *method)
+{
+ g_return_val_if_fail (method != NULL, NMA_MOBILE_ACCESS_METHOD_TYPE_UNKNOWN);
+
+ return method->type;
}
/******************************************************************************/
/* Mobile provider type */
+G_DEFINE_BOXED_TYPE (NMAMobileProvider,
+ nma_mobile_provider,
+ nma_mobile_provider_ref,
+ nma_mobile_provider_unref)
+
+struct _NMAMobileProvider {
+ volatile gint refs;
+
+ char *name;
+ /* maps lang (char *) -> name (char *) */
+ GHashTable *lcl_names;
+
+ GSList *methods; /* GSList of NmaMobileAccessMethod */
+
+ GSList *gsm_mcc_mnc; /* GSList of NmaGsmMccMnc */
+ GSList *cdma_sid; /* GSList of guint32 */
+};
+
static NMAMobileProvider *
provider_new (void)
{
@@ -158,7 +278,10 @@ provider_new (void)
NMAMobileProvider *
nma_mobile_provider_ref (NMAMobileProvider *provider)
{
- provider->refs++;
+ g_return_val_if_fail (provider != NULL, NULL);
+ g_return_val_if_fail (provider->refs > 0, NULL);
+
+ g_atomic_int_inc (&provider->refs);
return provider;
}
@@ -166,7 +289,7 @@ nma_mobile_provider_ref (NMAMobileProvider *provider)
void
nma_mobile_provider_unref (NMAMobileProvider *provider)
{
- if (--provider->refs == 0) {
+ if (g_atomic_int_dec_and_test (&provider->refs)) {
g_free (provider->name);
g_hash_table_destroy (provider->lcl_names);
@@ -183,6 +306,34 @@ nma_mobile_provider_unref (NMAMobileProvider *provider)
}
/**
+ * nma_mobile_provider_get_name:
+ *
+ * Returns: (transfer none): the name of the provider.
+ */
+const gchar *
+nma_mobile_provider_get_name (NMAMobileProvider *provider)
+{
+ g_return_val_if_fail (provider != NULL, NULL);
+
+ return provider->name;
+}
+
+/**
+ * nma_mobile_provider_get_methods:
+ * @provider: a #NMAMobileProvider
+ *
+ * Returns: (element-type NMGtk.MobileAccessMethod) (transfer none): the
+ * list of #NMAMobileAccessMethod this provider exposes.
+ */
+GSList *
+nma_mobile_provider_get_methods (NMAMobileProvider *provider)
+{
+ g_return_val_if_fail (provider != NULL, NULL);
+
+ return provider->methods;
+}
+
+/**
* nma_mobile_provider_get_gsm_mcc_mnc:
* @provider: a #NMAMobileProvider
*
@@ -212,22 +363,22 @@ nma_mobile_provider_get_cdma_sid (NMAMobileProvider *provider)
return provider->cdma_sid;
}
-GType
-nma_mobile_provider_get_type (void)
-{
- static GType type = 0;
-
- if (G_UNLIKELY (type == 0)) {
- type = g_boxed_type_register_static ("NMAMobileProvider",
- (GBoxedCopyFunc) nma_mobile_provider_ref,
- (GBoxedFreeFunc) nma_mobile_provider_unref);
- }
- return type;
-}
-
/******************************************************************************/
/* Country Info type */
+G_DEFINE_BOXED_TYPE (NMACountryInfo,
+ nma_country_info,
+ nma_country_info_ref,
+ nma_country_info_unref)
+
+struct _NMACountryInfo {
+ volatile gint refs;
+
+ char *country_code;
+ char *country_name;
+ GSList *providers;
+};
+
static NMACountryInfo *
country_info_new (const char *country_code,
const gchar *country_name)
@@ -244,7 +395,10 @@ country_info_new (const char *country_code,
NMACountryInfo *
nma_country_info_ref (NMACountryInfo *country_info)
{
- country_info->refs++;
+ g_return_val_if_fail (country_info != NULL, NULL);
+ g_return_val_if_fail (country_info->refs > 0, NULL);
+
+ g_atomic_int_inc (&country_info->refs);
return country_info;
}
@@ -252,7 +406,7 @@ nma_country_info_ref (NMACountryInfo *country_info)
void
nma_country_info_unref (NMACountryInfo *country_info)
{
- if (--country_info->refs == 0) {
+ if (g_atomic_int_dec_and_test (&country_info->refs)) {
g_free (country_info->country_code);
g_free (country_info->country_name);
g_slist_free_full (country_info->providers,
@@ -301,19 +455,6 @@ nma_country_info_get_providers (NMACountryInfo *country_info)
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 */
diff --git a/src/libnm-gtk/nm-mobile-providers.h b/src/libnm-gtk/nm-mobile-providers.h
index fbe4bb97..0e9d9e46 100644
--- a/src/libnm-gtk/nm-mobile-providers.h
+++ b/src/libnm-gtk/nm-mobile-providers.h
@@ -19,6 +19,7 @@
* Author: Tambet Ingo (tambet@gmail.com).
*
* Copyright (C) 2009 - 2012 Red Hat, Inc.
+ * Copyright (C) 2012 Lanedo GmbH.
*/
/* WARNING: this file is private API between nm-applet and various GNOME
@@ -54,49 +55,31 @@ typedef enum {
#define NMA_TYPE_MOBILE_ACCESS_METHOD (nma_mobile_access_method_get_type ())
-typedef struct {
- char *name;
- /* maps lang (char *) -> name (char *) */
- GHashTable *lcl_names;
-
- char *username;
- char *password;
- char *gateway;
- GSList *dns; /* GSList of 'char *' */
-
- /* Only used with NMA_PROVIDER_TYPE_GSM */
- char *gsm_apn;
-
- NMAMobileAccessMethodType type;
-
- gint refs;
-} NMAMobileAccessMethod;
+typedef struct _NMAMobileAccessMethod 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);
+const gchar *nma_mobile_access_method_get_name (NMAMobileAccessMethod *method);
+const gchar *nma_mobile_access_method_get_username (NMAMobileAccessMethod *method);
+const gchar *nma_mobile_access_method_get_password (NMAMobileAccessMethod *method);
+const gchar *nma_mobile_access_method_get_gateway (NMAMobileAccessMethod *method);
+const GSList *nma_mobile_access_method_get_dns (NMAMobileAccessMethod *method);
+const gchar *nma_mobile_access_method_get_gsm_apn (NMAMobileAccessMethod *method);
+NMAMobileAccessMethodType nma_mobile_access_method_get_method_type (NMAMobileAccessMethod *method);
/******************************************************************************/
/* Mobile provider type */
#define NMA_TYPE_MOBILE_PROVIDER (nma_mobile_provider_get_type ())
-typedef struct {
- char *name;
- /* maps lang (char *) -> name (char *) */
- GHashTable *lcl_names;
-
- GSList *methods; /* GSList of NmaMobileAccessMethod */
-
- GSList *gsm_mcc_mnc; /* GSList of NmaGsmMccMnc */
- GSList *cdma_sid; /* GSList of guint32 */
-
- gint refs;
-} NMAMobileProvider;
+typedef struct _NMAMobileProvider NMAMobileProvider;
GType nma_mobile_provider_get_type (void);
NMAMobileProvider *nma_mobile_provider_ref (NMAMobileProvider *provider);
void nma_mobile_provider_unref (NMAMobileProvider *provider);
+const gchar *nma_mobile_provider_get_name (NMAMobileProvider *provider);
+GSList *nma_mobile_provider_get_methods (NMAMobileProvider *provider);
GSList *nma_mobile_provider_get_gsm_mcc_mnc (NMAMobileProvider *provider);
GSList *nma_mobile_provider_get_cdma_sid (NMAMobileProvider *provider);
@@ -105,13 +88,7 @@ GSList *nma_mobile_provider_get_cdma_sid (NMAMobileProvider *provi
#define NMA_TYPE_COUNTRY_INFO (nma_country_info_get_type ())
-typedef struct {
- char *country_code;
- char *country_name;
- GSList *providers;
-
- gint refs;
-} NMACountryInfo;
+typedef struct _NMACountryInfo NMACountryInfo;
GType nma_country_info_get_type (void);
NMACountryInfo *nma_country_info_ref (NMACountryInfo *country_info);
diff --git a/src/libnm-gtk/nm-mobile-wizard.c b/src/libnm-gtk/nm-mobile-wizard.c
index e247b38b..9fbfa140 100644
--- a/src/libnm-gtk/nm-mobile-wizard.c
+++ b/src/libnm-gtk/nm-mobile-wizard.c
@@ -129,27 +129,29 @@ assistant_closed (GtkButton *button, gpointer user_data)
} else {
gboolean manual = FALSE;
- wiz_method->provider_name = g_strdup (provider->name);
+ wiz_method->provider_name = g_strdup (nma_mobile_provider_get_name (provider));
method = get_selected_method (self, &manual);
if (method) {
- if (method->name)
- wiz_method->plan_name = g_strdup (method->name);
- method_type = method->type;
+ method_type = nma_mobile_access_method_get_method_type (method);
+ wiz_method->plan_name = g_strdup (nma_mobile_access_method_get_name (method));
+ wiz_method->username = g_strdup (nma_mobile_access_method_get_username (method));
+ wiz_method->password = g_strdup (nma_mobile_access_method_get_password (method));
if (method_type == NMA_MOBILE_ACCESS_METHOD_TYPE_GSM)
- wiz_method->gsm_apn = g_strdup (method->gsm_apn);
- wiz_method->username = method->username ? g_strdup (method->username) : NULL;
- wiz_method->password = method->password ? g_strdup (method->password) : NULL;
+ wiz_method->gsm_apn = g_strdup (nma_mobile_access_method_get_gsm_apn (method));
} else {
if (self->provider_only_cdma) {
+ GSList *methods;
+
method_type = NMA_MOBILE_ACCESS_METHOD_TYPE_CDMA;
+
+ methods = nma_mobile_provider_get_methods (provider);
/* Take username and password from the first (only) method for CDMA only provider */
- if (provider->methods) {
- method = provider->methods->data;
- wiz_method->username = method->username ? g_strdup (method->username) : NULL;
- wiz_method->password = method->password ? g_strdup (method->password) : NULL;
+ if (methods) {
+ method = methods->data;
+ wiz_method->username = g_strdup (nma_mobile_access_method_get_username (method));
+ wiz_method->password = g_strdup (nma_mobile_access_method_get_password (method));
}
- }
- else {
+ } else {
method_type = NMA_MOBILE_ACCESS_METHOD_TYPE_GSM;
wiz_method->gsm_apn = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->plan_unlisted_entry)));
}
@@ -294,7 +296,7 @@ confirm_prepare (NMAMobileWizard *self)
/* Provider */
str = g_string_new (NULL);
if (provider) {
- g_string_append (str, provider->name);
+ g_string_append (str, nma_mobile_provider_get_name (provider));
nma_mobile_provider_unref (provider);
} else {
const char *unlisted_provider;
@@ -330,8 +332,8 @@ confirm_prepare (NMAMobileWizard *self)
gtk_widget_show (self->confirm_apn);
if (method) {
- gtk_label_set_text (GTK_LABEL (self->confirm_plan), method->name);
- apn = method->gsm_apn;
+ gtk_label_set_text (GTK_LABEL (self->confirm_plan), nma_mobile_access_method_get_name (method));
+ apn = nma_mobile_access_method_get_gsm_apn (method);
} else {
gtk_label_set_text (GTK_LABEL (self->confirm_plan), _("Unlisted"));
apn = gtk_entry_get_text (GTK_ENTRY (self->plan_unlisted_entry));
@@ -410,7 +412,7 @@ plan_combo_changed (NMAMobileWizard *self)
method = get_selected_method (self, &is_manual);
if (method) {
- gtk_entry_set_text (GTK_ENTRY (self->plan_unlisted_entry), method->gsm_apn);
+ gtk_entry_set_text (GTK_ENTRY (self->plan_unlisted_entry), nma_mobile_access_method_get_gsm_apn (method));
gtk_widget_set_sensitive (self->plan_unlisted_entry, FALSE);
} else {
gtk_entry_set_text (GTK_ENTRY (self->plan_unlisted_entry), "");
@@ -563,18 +565,18 @@ plan_prepare (NMAMobileWizard *self)
GSList *iter;
guint32 count = 0;
- for (iter = provider->methods; iter; iter = g_slist_next (iter)) {
+ for (iter = nma_mobile_provider_get_methods (provider); iter; iter = g_slist_next (iter)) {
NMAMobileAccessMethod *method = iter->data;
if ( (self->method_type != NMA_MOBILE_ACCESS_METHOD_TYPE_UNKNOWN)
- && (method->type != self->method_type))
+ && (nma_mobile_access_method_get_method_type (method) != self->method_type))
continue;
gtk_tree_store_append (GTK_TREE_STORE (self->plan_store), &method_iter, NULL);
gtk_tree_store_set (GTK_TREE_STORE (self->plan_store),
&method_iter,
PLAN_COL_NAME,
- method->name,
+ nma_mobile_access_method_get_name (method),
PLAN_COL_METHOD,
method,
-1);
@@ -875,10 +877,10 @@ providers_prepare (NMAMobileWizard *self)
GSList *miter;
guint32 count = 0;
- for (miter = provider->methods; miter; miter = g_slist_next (miter)) {
+ for (miter = nma_mobile_provider_get_methods (provider); miter; miter = g_slist_next (miter)) {
NMAMobileAccessMethod *method = miter->data;
- if (self->method_type == method->type)
+ if (self->method_type == nma_mobile_access_method_get_method_type (method))
count++;
}
@@ -890,7 +892,7 @@ providers_prepare (NMAMobileWizard *self)
gtk_tree_store_set (GTK_TREE_STORE (self->providers_store),
&provider_iter,
PROVIDER_COL_NAME,
- provider->name,
+ nma_mobile_provider_get_name (provider),
PROVIDER_COL_PROVIDER,
provider,
-1);
@@ -1550,12 +1552,12 @@ forward_func (gint current_page, gpointer user_data)
provider = get_selected_provider (self);
if (provider) {
- for (iter = provider->methods; iter; iter = g_slist_next (iter)) {
+ for (iter = nma_mobile_provider_get_methods (provider); iter; iter = g_slist_next (iter)) {
NMAMobileAccessMethod *method = iter->data;
- if (method->type == NMA_MOBILE_ACCESS_METHOD_TYPE_CDMA)
+ if (nma_mobile_access_method_get_method_type (method) == NMA_MOBILE_ACCESS_METHOD_TYPE_CDMA)
cdma = TRUE;
- else if (method->type == NMA_MOBILE_ACCESS_METHOD_TYPE_GSM)
+ else if (nma_mobile_access_method_get_method_type (method) == NMA_MOBILE_ACCESS_METHOD_TYPE_GSM)
gsm = TRUE;
}
nma_mobile_provider_unref (provider);