[PATCH] Easy patch (help out!): preserve wireless-enabled and networking-enabled



On Friday 23 October 2009 19:19:14 Dan Williams wrote:
> Anyone want to do a really simple patch?  It would close some bugs and
> help out a lot:
> 

Hello Dan,

I've done the patch. Please, review it.

NetworkingEnabled and WirelessEnabled are read from config file. If not present, 
the values are regarded as true; if case of mangled values (or something), 
false is used.
On changing values in the applet, the keys are changed (created)
in the cofiguration file.

Jirka
diff --git a/src/NetworkManager.c b/src/NetworkManager.c
index f14c6cb..f91308d 100644
--- a/src/NetworkManager.c
+++ b/src/NetworkManager.c
@@ -246,7 +246,7 @@ write_pidfile (const char *pidfile)
 }
 
 static gboolean
-parse_config_file (const char *filename, char **plugins, GError **error)
+parse_config_file (const char *filename, NMConfigSettings *config_values, GError **error)
 {
 	GKeyFile *config;
 
@@ -261,10 +261,20 @@ parse_config_file (const char *filename, char **plugins, GError **error)
 	if (!g_key_file_load_from_file (config, filename, G_KEY_FILE_NONE, error))
 		return FALSE;
 
-	*plugins = g_key_file_get_value (config, "main", "plugins", error);
+	config_values->plugins = g_key_file_get_value (config, "main", "plugins", error);
 	if (*error)
 		return FALSE;
 
+	config_values->networking_enabled = g_key_file_get_boolean (config, "main", "NetworkingEnabled", error);
+	if (*error && (*error)->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND)
+		config_values->networking_enabled = TRUE;
+	g_clear_error (error);
+
+	config_values->wireless_enabled = g_key_file_get_boolean (config, "main", "WirelessEnabled", error);
+	if (*error && (*error)->code == G_KEY_FILE_ERROR_KEY_NOT_FOUND)
+		config_values->wireless_enabled = TRUE;
+	g_clear_error (error);
+
 	g_key_file_free (config);
 	return TRUE;
 }
@@ -280,7 +290,8 @@ main (int argc, char *argv[])
 	gboolean become_daemon = FALSE;
 	gboolean g_fatal_warnings = FALSE;
 	char *pidfile = NULL, *user_pidfile = NULL;
-	char *config = NULL, *plugins = NULL;
+	char *config = NULL;
+	NMConfigSettings cfg_values = {NULL, TRUE, TRUE};
 	gboolean success;
 	NMPolicy *policy = NULL;
 	NMVPNManager *vpn_manager = NULL;
@@ -295,7 +306,7 @@ main (int argc, char *argv[])
 		{ "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &g_fatal_warnings, "Make all warnings fatal", NULL },
 		{ "pid-file", 0, 0, G_OPTION_ARG_FILENAME, &user_pidfile, "Specify the location of a PID file", "filename" },
 		{ "config", 0, 0, G_OPTION_ARG_FILENAME, &config, "Config file location", "/path/to/config.file" },
-		{ "plugins", 0, 0, G_OPTION_ARG_STRING, &plugins, "List of plugins separated by ,", "plugin1,plugin2" },
+		{ "plugins", 0, 0, G_OPTION_ARG_STRING, &cfg_values.plugins, "List of plugins separated by ,", "plugin1,plugin2" },
 		{NULL}
 	};
 
@@ -333,7 +344,7 @@ main (int argc, char *argv[])
 
 	/* Parse the config file */
 	if (config) {
-		if (!parse_config_file (config, &plugins, &error)) {
+		if (!parse_config_file (config, &cfg_values, &error)) {
 			g_warning ("Config file %s invalid: (%d) %s.",
 			           config,
 			           error ? error->code : -1,
@@ -342,7 +353,7 @@ main (int argc, char *argv[])
 		}
 	} else {
 		config = NM_DEFAULT_SYSTEM_CONF_FILE;
-		if (!parse_config_file (config, &plugins, &error)) {
+		if (!parse_config_file (config, &cfg_values, &error)) {
 			g_warning ("Default config file %s invalid: (%d) %s.",
 			           config,
 			           error ? error->code : -1,
@@ -419,7 +430,7 @@ main (int argc, char *argv[])
 		goto done;
 	}
 
-	manager = nm_manager_get (config, plugins, &error);
+	manager = nm_manager_get (config, &cfg_values, &error);
 	if (manager == NULL) {
 		nm_error ("Failed to initialize the network manager: %s",
 		          error && error->message ? error->message : "(unknown)");
diff --git a/src/nm-device-olpc-mesh.c b/src/nm-device-olpc-mesh.c
index 743f47c..5aa6966 100644
--- a/src/nm-device-olpc-mesh.c
+++ b/src/nm-device-olpc-mesh.c
@@ -898,6 +898,7 @@ check_companion_cb (gpointer user_data)
 	NMDeviceOlpcMeshPrivate *priv = NM_DEVICE_OLPC_MESH_GET_PRIVATE (self);
 	NMManager *manager;
 	GSList *list;
+	NMConfigSettings cfg_values = {NULL, TRUE, TRUE};
 
 	if (priv->companion != NULL) {
 		nm_device_state_changed (NM_DEVICE (user_data),
@@ -909,7 +910,7 @@ check_companion_cb (gpointer user_data)
 	if (priv->device_added_cb != 0)
 		return FALSE;
 
-	manager = nm_manager_get (NULL, NULL, NULL);
+	manager = nm_manager_get (NULL, &cfg_values, NULL);
 
 	priv->device_added_cb = g_signal_connect (manager, "device-added",
 	                                          G_CALLBACK (device_added_cb), self);
diff --git a/src/nm-manager.c b/src/nm-manager.c
index e082ab0..b488d88 100644
--- a/src/nm-manager.c
+++ b/src/nm-manager.c
@@ -1105,6 +1105,9 @@ manager_set_wireless_enabled (NMManager *manager, gboolean enabled)
 {
 	NMManagerPrivate *priv = NM_MANAGER_GET_PRIVATE (manager);
 	GSList *iter;
+	GKeyFile *config;
+	char *data;
+	gsize len = 0;
 
 	if (priv->wireless_enabled == enabled)
 		return;
@@ -1117,6 +1120,21 @@ manager_set_wireless_enabled (NMManager *manager, gboolean enabled)
 
 	g_object_notify (G_OBJECT (manager), NM_MANAGER_WIRELESS_ENABLED);
 
+	/* If there was config file specified, update "WirelessEnabled" key */
+	if (priv->config_file) {
+		if ((config = g_key_file_new ()) != NULL) {
+			g_key_file_set_list_separator (config, ',');
+			g_key_file_load_from_file (config, priv->config_file, G_KEY_FILE_KEEP_COMMENTS, NULL);
+			g_key_file_set_boolean (config, "main", "WirelessEnabled", enabled);
+			data = g_key_file_to_data (config, &len, NULL);
+			if (data) {
+				g_file_set_contents (priv->config_file, data, len, NULL);
+				g_free (data);
+			}
+			g_key_file_free (config);
+		}
+	}
+
 	/* Don't touch devices if asleep/networking disabled */
 	if (priv->sleeping)
 		return;
@@ -2385,6 +2403,9 @@ impl_manager_sleep (NMManager *self, gboolean sleep, GError **error)
 {
 	NMManagerPrivate *priv;
 	GSList *iter;
+	GKeyFile *config;
+	char *data;
+	gsize len = 0;
 
 	g_return_val_if_fail (NM_IS_MANAGER (self), FALSE);
 
@@ -2399,6 +2420,21 @@ impl_manager_sleep (NMManager *self, gboolean sleep, GError **error)
 
 	priv->sleeping = sleep;
 
+	/* If there was config file specified, update "NetworkingEnabled" key */
+	if (priv->config_file) {
+		if ((config = g_key_file_new ()) != NULL) {
+			g_key_file_set_list_separator (config, ',');
+			g_key_file_load_from_file (config, priv->config_file, G_KEY_FILE_KEEP_COMMENTS, NULL);
+			g_key_file_set_boolean (config, "main", "NetworkingEnabled", !sleep);
+			data = g_key_file_to_data (config, &len, NULL);
+			if (data) {
+				g_file_set_contents (priv->config_file, data, len, NULL);
+				g_free (data);
+			}
+			g_key_file_free (config);
+		}
+	}
+
 	if (sleep) {
 		nm_info ("Sleeping...");
 
@@ -2569,9 +2605,10 @@ nm_manager_start (NMManager *self)
 		break;
 	}
 
-	nm_info ("Wireless now %s by radio killswitch",
-	         (priv->wireless_hw_enabled && we) ? "enabled" : "disabled");
-	manager_set_wireless_enabled (self, we);
+	nm_info ("Wireless %s by radio killswitch; %s by config file",
+	         (priv->wireless_hw_enabled && we) ? "enabled" : "disabled",
+	         (priv->wireless_enabled) ? "enabled" : "disabled");
+	manager_set_wireless_enabled (self, priv->wireless_enabled && we);
 
 	system_unmanaged_devices_changed_cb (priv->sys_settings, NULL, self);
 	system_hostname_changed_cb (priv->sys_settings, NULL, self);
@@ -2589,7 +2626,7 @@ nm_manager_start (NMManager *self)
 }
 
 NMManager *
-nm_manager_get (const char *config_file, const char *plugins, GError **error)
+nm_manager_get (const char *config_file, NMConfigSettings *config_values, GError **error)
 {
 	static NMManager *singleton = NULL;
 	NMManagerPrivate *priv;
@@ -2606,7 +2643,7 @@ nm_manager_get (const char *config_file, const char *plugins, GError **error)
 	bus = nm_dbus_manager_get_connection (priv->dbus_mgr);
 	g_assert (bus);
 
-	priv->sys_settings = nm_sysconfig_settings_new (config_file, plugins, bus, error);
+	priv->sys_settings = nm_sysconfig_settings_new (config_file, config_values->plugins, bus, error);
 	if (!priv->sys_settings) {
 		g_object_unref (singleton);
 		return NULL;
@@ -2615,6 +2652,10 @@ nm_manager_get (const char *config_file, const char *plugins, GError **error)
 
 	priv->config_file = g_strdup (config_file);
 
+	priv->sleeping = !config_values->networking_enabled;
+
+	priv->wireless_enabled = config_values->wireless_enabled;
+
 	g_signal_connect (priv->sys_settings, "notify::" NM_SYSCONFIG_SETTINGS_UNMANAGED_SPECS,
 	                  G_CALLBACK (system_unmanaged_devices_changed_cb), singleton);
 	g_signal_connect (priv->sys_settings, "notify::" NM_SETTINGS_SYSTEM_INTERFACE_HOSTNAME,
diff --git a/src/nm-manager.h b/src/nm-manager.h
index 8e48574..f691533 100644
--- a/src/nm-manager.h
+++ b/src/nm-manager.h
@@ -74,7 +74,15 @@ typedef struct {
 
 GType nm_manager_get_type (void);
 
-NMManager *nm_manager_get (const char *config_file, const char *plugins, GError **error);
+typedef struct {
+	const char *plugins;
+	gboolean networking_enabled;
+	gboolean wireless_enabled;
+} NMConfigSettings;
+
+NMManager *nm_manager_get (const char *config_file,
+                           NMConfigSettings *config_values,
+                           GError **error);
 
 void nm_manager_start (NMManager *manager);
 


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