[network-manager-applet/aleksander/mm1-applet: 7/8] applet: consolidate mobile operator name retrieval for 'gsm' and 'cdma' devices



commit de5f0156db432af77cc1538733e3080d9e7809d3
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Mon Dec 17 17:38:31 2012 +0100

    applet: consolidate mobile operator name retrieval for 'gsm' and 'cdma' devices
    
    Include as helper methods the 3GPP and 3GPP operator name retrieval logic.

 src/applet-device-cdma.c |   22 +-----------
 src/applet-device-gsm.c  |   55 +++----------------------------
 src/mobile-helpers.c     |   79 ++++++++++++++++++++++++++++++++++++++++++++++
 src/mobile-helpers.h     |   10 ++++++
 4 files changed, 97 insertions(+), 69 deletions(-)
---
diff --git a/src/applet-device-cdma.c b/src/applet-device-cdma.c
index 263039e..ed9ac56 100644
--- a/src/applet-device-cdma.c
+++ b/src/applet-device-cdma.c
@@ -504,22 +504,10 @@ serving_system_reply (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_dat
 		g_value_array_free (array);
 	}
 
-	if (new_sid && (new_sid != info->sid)) {
+	if (new_sid != info->sid) {
 		info->sid = new_sid;
-		if (info->mobile_providers_database) {
-			NMAMobileProvider *provider;
-
-			g_free (info->provider_name);
-
-			provider = nma_mobile_providers_database_lookup_cdma_sid (info->mobile_providers_database, new_sid);
-			info->provider_name = (provider ?
-			                       g_strdup (nma_mobile_provider_get_name (provider)) :
-			                       NULL);
-		}
-	} else if (!new_sid) {
-		info->sid = 0;
 		g_free (info->provider_name);
-		info->provider_name = NULL;
+		info->provider_name = mobile_helper_parse_3gpp2_operator_name (&(info->mobile_providers_database), info->sid);
 	}
 
 	g_clear_error (&error);
@@ -691,12 +679,6 @@ cdma_device_added (NMDevice *device, NMApplet *applet)
 	info->bus = bus;
 	info->quality_valid = FALSE;
 
-	info->mobile_providers_database = nma_mobile_providers_database_new_sync (NULL, NULL, NULL, &error);
-	if (!info->mobile_providers_database) {
-		g_warning ("Couldn't read database: %s", error->message);
-		g_clear_error (&error);
-	}
-
 	info->props_proxy = dbus_g_proxy_new_for_name (bus,
 	                                               "org.freedesktop.ModemManager",
 	                                               udi,
diff --git a/src/applet-device-gsm.c b/src/applet-device-gsm.c
index 22a8a68..22e3f7a 100644
--- a/src/applet-device-gsm.c
+++ b/src/applet-device-gsm.c
@@ -666,53 +666,6 @@ signal_reply (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
 	g_clear_error (&error);
 }
 
-static char *
-parse_op_name (GsmDeviceInfo *info, const char *orig, const char *op_code)
-{
-	NMAMobileProvider *provider;
-	guint i, orig_len;
-
-	/* Some devices return the MCC/MNC if they haven't fully initialized
-	 * or gotten all the info from the network yet.  Handle that.
-	 */
-
-	orig_len = orig ? strlen (orig) : 0;
-	if (orig_len == 0) {
-		/* If the operator name isn't valid, maybe we can look up the MCC/MNC
-		 * from the operator code instead.
-		 */
-		if (op_code && strlen (op_code)) {
-			orig = op_code;
-			orig_len = strlen (orig);
-		} else
-			return NULL;
-	} else if (orig_len < 5 || orig_len > 6)
-		return g_strdup (orig);  /* not an MCC/MNC */
-
-	for (i = 0; i < orig_len; i++) {
-		if (!isdigit (orig[i]))
-			return strdup (orig);
-	}
-
-	/* At this point we have a 5 or 6 character all-digit string; that's
-	 * probably an MCC/MNC.  Look that up.
-	 */
-
-	if (!info->mobile_providers_database) {
-		GError *error = NULL;
-
-		info->mobile_providers_database = nma_mobile_providers_database_new_sync (NULL, NULL, NULL, &error);
-		if (!info->mobile_providers_database) {
-			g_warning ("Couldn't read database: %s", error->message);
-			g_error_free (error);
-			return strdup (orig);
-		}
-	}
-
-	provider = nma_mobile_providers_database_lookup_3gpp_mcc_mnc (info->mobile_providers_database, orig);
-	return (provider ? g_strdup (nma_mobile_provider_get_name (provider)) : NULL);
-}
-
 static void
 notify_user_of_gsm_reg_change (GsmDeviceInfo *info)
 {
@@ -764,7 +717,9 @@ reg_info_reply (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data)
 
 			value = g_value_array_get_nth (array, 2);
 			if (G_VALUE_HOLDS_STRING (value))
-				new_op_name = parse_op_name (info, g_value_get_string (value), new_op_code);
+				new_op_name = mobile_helper_parse_3gpp_operator_name (&(info->mobile_providers_database),
+				                                                      g_value_get_string (value),
+				                                                      new_op_code);
 		}
 
 		g_value_array_free (array);
@@ -1083,7 +1038,9 @@ reg_info_changed_cb (DBusGProxy *proxy,
 	g_free (info->op_code);
 	info->op_code = strlen (op_code) ? g_strdup (op_code) : NULL;
 	g_free (info->op_name);
-	info->op_name = parse_op_name (info, op_name, info->op_code);
+	info->op_name = mobile_helper_parse_3gpp_operator_name (&(info->mobile_providers_database),
+	                                                        op_name,
+	                                                        info->op_code);
 	info->skip_reg_poll = TRUE;
 }
 
diff --git a/src/mobile-helpers.c b/src/mobile-helpers.c
index 80e70ca..ddeead7 100644
--- a/src/mobile-helpers.c
+++ b/src/mobile-helpers.c
@@ -636,3 +636,82 @@ mobile_helper_get_icon (NMDevice *device,
 
 	return pixbuf;
 }
+
+/********************************************************************/
+
+char *
+mobile_helper_parse_3gpp_operator_name (NMAMobileProvidersDatabase **mpd, /* I/O */
+                                        const char *orig,
+                                        const char *op_code)
+{
+	NMAMobileProvider *provider;
+	guint i, orig_len;
+
+	g_assert (mpd != NULL);
+
+	/* Some devices return the MCC/MNC if they haven't fully initialized
+	 * or gotten all the info from the network yet.  Handle that.
+	 */
+
+	orig_len = orig ? strlen (orig) : 0;
+	if (orig_len == 0) {
+		/* If the operator name isn't valid, maybe we can look up the MCC/MNC
+		 * from the operator code instead.
+		 */
+		if (op_code && strlen (op_code)) {
+			orig = op_code;
+			orig_len = strlen (orig);
+		} else
+			return NULL;
+	} else if (orig_len < 5 || orig_len > 6)
+		return g_strdup (orig);  /* not an MCC/MNC */
+
+	for (i = 0; i < orig_len; i++) {
+		if (!isdigit (orig[i]))
+			return strdup (orig);
+	}
+
+	/* At this point we have a 5 or 6 character all-digit string; that's
+	 * probably an MCC/MNC.  Look that up.
+	 */
+
+	if (*mpd == NULL) {
+		GError *error = NULL;
+
+		*mpd = nma_mobile_providers_database_new_sync (NULL, NULL, NULL, &error);
+		if (*mpd == NULL) {
+			g_warning ("Couldn't read database: %s", error->message);
+			g_error_free (error);
+			return strdup (orig);
+		}
+	}
+
+	provider = nma_mobile_providers_database_lookup_3gpp_mcc_mnc (*mpd, orig);
+	return (provider ? g_strdup (nma_mobile_provider_get_name (provider)) : NULL);
+}
+
+char *
+mobile_helper_parse_3gpp2_operator_name (NMAMobileProvidersDatabase **mpd, /* I/O */
+                                         guint32 sid)
+{
+	NMAMobileProvider *provider;
+
+	g_assert (mpd != NULL);
+
+	if (!sid)
+		return NULL;
+
+	if (*mpd == NULL) {
+		GError *error = NULL;
+
+		*mpd = nma_mobile_providers_database_new_sync (NULL, NULL, NULL, &error);
+		if (*mpd == NULL) {
+			g_warning ("Couldn't read database: %s", error->message);
+			g_error_free (error);
+			return NULL;
+		}
+	}
+
+	provider = nma_mobile_providers_database_lookup_cdma_sid (*mpd, sid);
+	return (provider ? g_strdup (nma_mobile_provider_get_name (provider)) : NULL);
+}
diff --git a/src/mobile-helpers.h b/src/mobile-helpers.h
index 9626eec..d1057c6 100644
--- a/src/mobile-helpers.h
+++ b/src/mobile-helpers.h
@@ -26,6 +26,7 @@
 #include <gtk/gtk.h>
 #include "applet.h"
 #include "nm-mobile-wizard.h"
+#include "nm-mobile-providers.h"
 
 enum {
 	MB_STATE_UNKNOWN = 0,
@@ -101,4 +102,13 @@ GdkPixbuf *mobile_helper_get_icon (NMDevice *device,
                                    guint32 quality,
                                    gboolean quality_valid);
 
+/********************************************************************/
+
+char *mobile_helper_parse_3gpp_operator_name (NMAMobileProvidersDatabase **mpd,
+                                              const char *orig,
+                                              const char *op_code);
+
+char *mobile_helper_parse_3gpp2_operator_name (NMAMobileProvidersDatabase **mpd,
+                                               guint32 sid);
+
 #endif  /* APPLET_MOBILE_HELPERS_H */



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