[network-manager-applet/aleksander/mm1-applet: 1/13] applet: consolidate mobile wizard launching for 'gsm' and 'cdma' devices



commit b22713b02cd5fa914ddac39bcb34cc371cee3045
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Thu Dec 13 17:18:52 2012 +0100

    applet: consolidate mobile wizard launching for 'gsm' and 'cdma' devices
    
    The new mobile_helper_wizard() method accepts a 'NMDeviceModemCapabilities'
    value specifying the capabilities of the device for which we are launching the
    wizard.

 src/applet-device-cdma.c |  110 +++-------------------------------
 src/applet-device-gsm.c  |  112 ++++--------------------------------
 src/mobile-helpers.c     |  145 ++++++++++++++++++++++++++++++++++++++++++++++
 src/mobile-helpers.h     |    6 ++-
 4 files changed, 171 insertions(+), 202 deletions(-)
---
diff --git a/src/applet-device-cdma.c b/src/applet-device-cdma.c
index f9710b0..ce675a1 100644
--- a/src/applet-device-cdma.c
+++ b/src/applet-device-cdma.c
@@ -39,7 +39,6 @@
 #include "applet.h"
 #include "applet-device-cdma.h"
 #include "utils.h"
-#include "nm-mobile-wizard.h"
 #include "applet-dialogs.h"
 #include "nma-marshal.h"
 #include "nm-mobile-providers.h"
@@ -89,110 +88,15 @@ cdma_menu_item_info_destroy (gpointer data)
 	g_slice_free (CdmaMenuItemInfo, data);
 }
 
-typedef struct {
-	AppletNewAutoConnectionCallback callback;
-	gpointer callback_data;
-} AutoCdmaWizardInfo;
-
-static void
-mobile_wizard_done (NMAMobileWizard *wizard,
-                    gboolean canceled,
-                    NMAMobileWizardAccessMethod *method,
-                    gpointer user_data)
-{
-	AutoCdmaWizardInfo *info = user_data;
-	NMConnection *connection = NULL;
-
-	if (!canceled && method) {
-		NMSetting *setting;
-		char *uuid, *id;
-
-		if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
-			g_warning ("Unexpected device type (not CDMA).");
-			canceled = TRUE;
-			goto done;
-		}
-
-		connection = nm_connection_new ();
-
-		setting = nm_setting_cdma_new ();
-		g_object_set (setting,
-		              NM_SETTING_CDMA_NUMBER, "#777",
-		              NM_SETTING_CDMA_USERNAME, method->username,
-		              NM_SETTING_CDMA_PASSWORD, method->password,
-		              NULL);
-		nm_connection_add_setting (connection, setting);
-
-		/* Serial setting */
-		setting = nm_setting_serial_new ();
-		g_object_set (setting,
-		              NM_SETTING_SERIAL_BAUD, 115200,
-		              NM_SETTING_SERIAL_BITS, 8,
-		              NM_SETTING_SERIAL_PARITY, 'n',
-		              NM_SETTING_SERIAL_STOPBITS, 1,
-		              NULL);
-		nm_connection_add_setting (connection, setting);
-
-		nm_connection_add_setting (connection, nm_setting_ppp_new ());
-
-		setting = nm_setting_connection_new ();
-		id = utils_create_mobile_connection_id (method->provider_name, method->plan_name);
-		uuid = nm_utils_uuid_generate ();
-		g_object_set (setting,
-		              NM_SETTING_CONNECTION_ID, id,
-		              NM_SETTING_CONNECTION_TYPE, NM_SETTING_CDMA_SETTING_NAME,
-		              NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
-		              NM_SETTING_CONNECTION_UUID, uuid,
-		              NULL);
-		g_free (uuid);
-		g_free (id);
-		nm_connection_add_setting (connection, setting);
-	}
-
-done:
-	(*(info->callback)) (connection, TRUE, canceled, info->callback_data);
-
-	if (wizard)
-		nma_mobile_wizard_destroy (wizard);
-	g_free (info);
-}
-
-static gboolean
-do_mobile_wizard (AppletNewAutoConnectionCallback callback,
-                  gpointer callback_data)
-{
-	NMAMobileWizard *wizard;
-	AutoCdmaWizardInfo *info;
-	NMAMobileWizardAccessMethod *method;
-
-	info = g_malloc0 (sizeof (AutoCdmaWizardInfo));
-	info->callback = callback;
-	info->callback_data = callback_data;
-
-	wizard = nma_mobile_wizard_new (NULL, NULL, NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO, FALSE,
-									mobile_wizard_done, info);
-	if (wizard) {
-		nma_mobile_wizard_present (wizard);
-		return TRUE;
-	}
-
-	/* Fall back to something */
-	method = g_malloc0 (sizeof (NMAMobileWizardAccessMethod));
-	method->devtype = NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO;
-	method->provider_name = _("CDMA");
-	mobile_wizard_done (NULL, FALSE, method, info);
-	g_free (method);
-
-	return TRUE;
-}
-
 static gboolean
 cdma_new_auto_connection (NMDevice *device,
                           gpointer dclass_data,
                           AppletNewAutoConnectionCallback callback,
                           gpointer callback_data)
 {
-	return do_mobile_wizard (callback, callback_data);
+	return mobile_helper_wizard (NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO,
+	                             callback,
+	                             callback_data);
 }
 
 static void
@@ -247,7 +151,13 @@ applet_cdma_connect_network (NMApplet *applet, NMDevice *device)
 	info->applet = applet;
 	info->device = g_object_ref (device);
 
-	do_mobile_wizard (dbus_connect_3g_cb, info);
+	if (!mobile_helper_wizard (NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO,
+	                           dbus_connect_3g_cb,
+	                           info)) {
+		g_warning ("Couldn't run mobile wizard for CDMA device");
+		g_object_unref (info->device);
+		g_free (info);
+	}
 }
 
 static void
diff --git a/src/applet-device-gsm.c b/src/applet-device-gsm.c
index 00ab125..2dc686c 100644
--- a/src/applet-device-gsm.c
+++ b/src/applet-device-gsm.c
@@ -42,7 +42,6 @@
 #include "applet.h"
 #include "applet-device-gsm.h"
 #include "utils.h"
-#include "nm-mobile-wizard.h"
 #include "applet-dialogs.h"
 #include "mb-menu-item.h"
 #include "nma-marshal.h"
@@ -120,111 +119,15 @@ gsm_menu_item_info_destroy (gpointer data)
 	g_slice_free (GSMMenuItemInfo, data);
 }
 
-typedef struct {
-	AppletNewAutoConnectionCallback callback;
-	gpointer callback_data;
-} AutoGsmWizardInfo;
-
-static void
-mobile_wizard_done (NMAMobileWizard *wizard,
-                    gboolean canceled,
-                    NMAMobileWizardAccessMethod *method,
-                    gpointer user_data)
-{
-	AutoGsmWizardInfo *info = user_data;
-	NMConnection *connection = NULL;
-
-	if (!canceled && method) {
-		NMSetting *setting;
-		char *uuid, *id;
-
-		if (method->devtype != NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
-			g_warning ("Unexpected device type (not GSM).");
-			canceled = TRUE;
-			goto done;
-		}
-
-		connection = nm_connection_new ();
-
-		setting = nm_setting_gsm_new ();
-		g_object_set (setting,
-		              NM_SETTING_GSM_NUMBER, "*99#",
-		              NM_SETTING_GSM_USERNAME, method->username,
-		              NM_SETTING_GSM_PASSWORD, method->password,
-		              NM_SETTING_GSM_APN, method->gsm_apn,
-		              NULL);
-		nm_connection_add_setting (connection, setting);
-
-		/* Serial setting */
-		setting = nm_setting_serial_new ();
-		g_object_set (setting,
-		              NM_SETTING_SERIAL_BAUD, 115200,
-		              NM_SETTING_SERIAL_BITS, 8,
-		              NM_SETTING_SERIAL_PARITY, 'n',
-		              NM_SETTING_SERIAL_STOPBITS, 1,
-		              NULL);
-		nm_connection_add_setting (connection, setting);
-
-		nm_connection_add_setting (connection, nm_setting_ppp_new ());
-
-		setting = nm_setting_connection_new ();
-		id = utils_create_mobile_connection_id (method->provider_name, method->plan_name);
-		uuid = nm_utils_uuid_generate ();
-		g_object_set (setting,
-		              NM_SETTING_CONNECTION_ID, id,
-		              NM_SETTING_CONNECTION_TYPE, NM_SETTING_GSM_SETTING_NAME,
-		              NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
-		              NM_SETTING_CONNECTION_UUID, uuid,
-		              NULL);
-		g_free (uuid);
-		g_free (id);
-		nm_connection_add_setting (connection, setting);
-	}
-
-done:
-	(*(info->callback)) (connection, TRUE, canceled, info->callback_data);
-
-	if (wizard)
-		nma_mobile_wizard_destroy (wizard);
-	g_free (info);
-}
-
-static gboolean
-do_mobile_wizard (AppletNewAutoConnectionCallback callback,
-                  gpointer callback_data)
-{
-	NMAMobileWizard *wizard;
-	AutoGsmWizardInfo *info;
-	NMAMobileWizardAccessMethod *method;
-
-	info = g_malloc0 (sizeof (AutoGsmWizardInfo));
-	info->callback = callback;
-	info->callback_data = callback_data;
-
-	wizard = nma_mobile_wizard_new (NULL, NULL, NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS, FALSE,
-									mobile_wizard_done, info);
-	if (wizard) {
-		nma_mobile_wizard_present (wizard);
-		return TRUE;
-	}
-
-	/* Fall back to something */
-	method = g_malloc0 (sizeof (NMAMobileWizardAccessMethod));
-	method->devtype = NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS;
-	method->provider_name = _("GSM");
-	mobile_wizard_done (NULL, FALSE, method, info);
-	g_free (method);
-
-	return TRUE;
-}
-
 static gboolean
 gsm_new_auto_connection (NMDevice *device,
                          gpointer dclass_data,
                          AppletNewAutoConnectionCallback callback,
                          gpointer callback_data)
 {
-	return do_mobile_wizard (callback, callback_data);
+	return mobile_helper_wizard (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS,
+	                             callback,
+	                             callback_data);
 }
 
 static void
@@ -279,7 +182,14 @@ applet_gsm_connect_network (NMApplet *applet, NMDevice *device)
 	info->applet = applet;
 	info->device = g_object_ref (device);
 
-	do_mobile_wizard (dbus_connect_3g_cb, info);
+
+	if (!mobile_helper_wizard (NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS,
+	                           dbus_connect_3g_cb,
+	                           info)) {
+		g_warning ("Couldn't run mobile wizard for CDMA device");
+		g_object_unref (info->device);
+		g_free (info);
+	}
 }
 
 static void
diff --git a/src/mobile-helpers.c b/src/mobile-helpers.c
index cb9c33e..46aa2ef 100644
--- a/src/mobile-helpers.c
+++ b/src/mobile-helpers.c
@@ -20,6 +20,10 @@
  * (C) Copyright 2010 Red Hat, Inc.
  */
 
+#include <glib/gi18n.h>
+#include <nm-utils.h>
+
+#include "utils.h"
 #include "mobile-helpers.h"
 
 GdkPixbuf *
@@ -127,3 +131,144 @@ mobile_helper_get_tech_icon (guint32 tech, NMApplet *applet)
 	}
 }
 
+/********************************************************************/
+
+typedef struct {
+	AppletNewAutoConnectionCallback callback;
+	gpointer callback_data;
+	NMDeviceModemCapabilities requested_capability;
+} AutoWizardInfo;
+
+static void
+mobile_wizard_done (NMAMobileWizard *wizard,
+                    gboolean cancelled,
+                    NMAMobileWizardAccessMethod *method,
+                    gpointer user_data)
+{
+	AutoWizardInfo *info = user_data;
+	NMConnection *connection = NULL;
+
+	if (!cancelled && method) {
+		NMSetting *setting;
+		char *uuid, *id;
+		const char *setting_name;
+
+		if (method->devtype != info->requested_capability) {
+			g_warning ("Unexpected device type");
+			cancelled = TRUE;
+			goto done;
+		}
+
+		connection = nm_connection_new ();
+
+		if (method->devtype == NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO) {
+			setting_name = NM_SETTING_CDMA_SETTING_NAME;
+			setting = nm_setting_cdma_new ();
+			g_object_set (setting,
+			              NM_SETTING_CDMA_NUMBER, "#777",
+			              NM_SETTING_CDMA_USERNAME, method->username,
+			              NM_SETTING_CDMA_PASSWORD, method->password,
+			              NULL);
+			nm_connection_add_setting (connection, setting);
+		} else if (method->devtype == NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS) {
+			setting_name = NM_SETTING_GSM_SETTING_NAME;
+			setting = nm_setting_gsm_new ();
+			g_object_set (setting,
+			              NM_SETTING_GSM_NUMBER, "*99#",
+			              NM_SETTING_GSM_USERNAME, method->username,
+			              NM_SETTING_GSM_PASSWORD, method->password,
+			              NM_SETTING_GSM_APN, method->gsm_apn,
+			              NULL);
+			nm_connection_add_setting (connection, setting);
+		} else
+			g_assert_not_reached ();
+
+		/* Serial setting */
+		setting = nm_setting_serial_new ();
+		g_object_set (setting,
+		              NM_SETTING_SERIAL_BAUD, 115200,
+		              NM_SETTING_SERIAL_BITS, 8,
+		              NM_SETTING_SERIAL_PARITY, 'n',
+		              NM_SETTING_SERIAL_STOPBITS, 1,
+		              NULL);
+		nm_connection_add_setting (connection, setting);
+
+		nm_connection_add_setting (connection, nm_setting_ppp_new ());
+
+		setting = nm_setting_connection_new ();
+		id = utils_create_mobile_connection_id (method->provider_name, method->plan_name);
+		uuid = nm_utils_uuid_generate ();
+		g_object_set (setting,
+		              NM_SETTING_CONNECTION_ID, id,
+		              NM_SETTING_CONNECTION_TYPE, setting_name,
+		              NM_SETTING_CONNECTION_AUTOCONNECT, FALSE,
+		              NM_SETTING_CONNECTION_UUID, uuid,
+		              NULL);
+		g_free (uuid);
+		g_free (id);
+		nm_connection_add_setting (connection, setting);
+	}
+
+done:
+	(*(info->callback)) (connection, TRUE, cancelled, info->callback_data);
+
+	if (wizard)
+		nma_mobile_wizard_destroy (wizard);
+	g_free (info);
+}
+
+gboolean
+mobile_helper_wizard (NMDeviceModemCapabilities capabilities,
+                      AppletNewAutoConnectionCallback callback,
+                      gpointer callback_data)
+{
+	NMAMobileWizard *wizard;
+	AutoWizardInfo *info;
+	NMAMobileWizardAccessMethod *method;
+	NMDeviceModemCapabilities wizard_capability;
+
+	/* Convert the input capabilities mask into a single value */
+	if (capabilities & NM_DEVICE_MODEM_CAPABILITY_LTE)
+		/* All LTE modems treated as GSM/UMTS for the wizard */
+		wizard_capability = NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS;
+	else if (capabilities & NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS)
+		wizard_capability = NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS;
+	else if (capabilities & NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
+		wizard_capability = NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO;
+	else {
+		g_warning ("Unknown modem capabilities (0x%X): can't launch wizard", capabilities);
+		return FALSE;
+	}
+
+	info = g_malloc0 (sizeof (AutoWizardInfo));
+	info->callback = callback;
+	info->callback_data = callback_data;
+	info->requested_capability = wizard_capability;
+
+	wizard = nma_mobile_wizard_new (NULL,
+	                                NULL,
+	                                wizard_capability,
+	                                FALSE,
+									mobile_wizard_done,
+	                                info);
+	if (wizard) {
+		nma_mobile_wizard_present (wizard);
+		return TRUE;
+	}
+
+	/* Fall back to something */
+	method = g_malloc0 (sizeof (NMAMobileWizardAccessMethod));
+	method->devtype = wizard_capability;
+
+	if (wizard_capability == NM_DEVICE_MODEM_CAPABILITY_GSM_UMTS)
+		method->provider_name = _("GSM");
+	else if (wizard_capability == NM_DEVICE_MODEM_CAPABILITY_CDMA_EVDO)
+		method->provider_name = _("CDMA");
+	else
+		g_assert_not_reached ();
+
+	mobile_wizard_done (NULL, FALSE, method, info);
+	g_free (method);
+
+	return TRUE;
+}
diff --git a/src/mobile-helpers.h b/src/mobile-helpers.h
index 578ec13..7c5d060 100644
--- a/src/mobile-helpers.h
+++ b/src/mobile-helpers.h
@@ -25,6 +25,7 @@
 
 #include <gtk/gtk.h>
 #include "applet.h"
+#include "nm-mobile-wizard.h"
 
 enum {
 	MB_STATE_UNKNOWN = 0,
@@ -62,5 +63,8 @@ GdkPixbuf *mobile_helper_get_quality_icon (guint32 quality, NMApplet *applet);
 
 GdkPixbuf *mobile_helper_get_tech_icon (guint32 tech, NMApplet *applet);
 
-#endif  /* APPLET_MOBILE_HELPERS_H */
+gboolean   mobile_helper_wizard (NMDeviceModemCapabilities capabilities,
+                                 AppletNewAutoConnectionCallback callback,
+                                 gpointer callback_data);
 
+#endif  /* APPLET_MOBILE_HELPERS_H */



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