[gnome-pilot-conduits] migrate email conduit from gnome_config to g_key_file, and avoid including gnome.h



commit 8bee0c342b085541058432db0432a3d1f73676b6
Author: Matt Davey <mcdavey mrao cam ac uk>
Date:   Thu Sep 30 00:37:17 2010 +0100

    migrate email conduit from gnome_config to g_key_file, and avoid including gnome.h

 email/email_conduit.c |  216 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 186 insertions(+), 30 deletions(-)
---
diff --git a/email/email_conduit.c b/email/email_conduit.c
index b6426b2..3e63f8d 100644
--- a/email/email_conduit.c
+++ b/email/email_conduit.c
@@ -5,8 +5,9 @@
 #endif
 
 #include <glib.h>
-#include <gnome.h>
+#include <glib/gi18n.h>
 #include <gtk/gtk.h>
+/*#include <gnome.h>*/
 
 #include <pi-source.h>
 #include <pi-socket.h>
@@ -37,54 +38,96 @@
 #define LOG(format,args...)
 #endif
 
+#define EC_PAD 8
+
 GnomePilotConduit *conduit_get_gpilot_conduit( guint32 pilotId ) ;
 void conduit_destroy_gpilot_conduit( GnomePilotConduit *c );
+static gboolean save_config (GKeyFile    *kfile, const gchar *conf);
+static GKeyFile* get_kfile (const gchar *conf);
+static void migrate_conf (const gchar *old, const gchar *new);
+
 
 static void 
 load_configuration(ConduitCfg **c,guint32 pilotId) 
 {
-	gchar *prefix;
+	gchar *iPilot;
+	GKeyFile *kfile;
+	GError *error = NULL;
 
 	g_assert(c!=NULL);
 	*c = g_new0(ConduitCfg,1);
 	(*c)->child = -1;
 
-	prefix = g_strdup_printf("/gnome-pilot.d/email-conduit/Pilot_%u/",pilotId);
-  
-	gnome_config_push_prefix(prefix);
-	(*c)->sendmail = gnome_config_get_string( "sendmail=/usr/lib/sendmail -t -i");
-	(*c)->fromAddr = gnome_config_get_string( "from_address" );
-	(*c)->sendAction = gnome_config_get_string( "send_action=file");
-	(*c)->mhDirectory = gnome_config_get_string( "mh_directory" );
-	(*c)->mboxFile = gnome_config_get_string ( "mbox_file" );
-	(*c)->receiveAction = gnome_config_get_string( "receive_action=copy" );
-	gnome_config_pop_prefix();
+ 	kfile = get_kfile("email-conduit");
+	iPilot = g_strdup_printf ("Pilot_%u", pilotId);
+
+	(*c)->sendmail = g_key_file_get_string (kfile, iPilot, "sendmail", &error);
+	if (error) {
+	    g_warning (_("Unable load key email-conduit/%s/sendmail: %s"), iPilot, error->message);
+	    g_error_free (error);
+	    error = NULL;
+	    (*c)->sendmail = g_strdup ("/usr/lib/sendmail -t -i");
+	}
+	(*c)->fromAddr = g_key_file_get_string (kfile, iPilot, "from_address", NULL);
+	(*c)->sendAction = g_key_file_get_string (kfile, iPilot, "send_action", &error);
+	if (error) {
+	    g_warning (_("Unable load key email-conduit/%s/send_action: %s"), iPilot,
+		error->message);
+	    g_error_free (error);
+	    error = NULL;
+	    (*c)->sendAction = g_strdup ("file");
+	}
+	(*c)->mhDirectory = g_key_file_get_string (kfile, iPilot, "mh_directory", NULL);
+	(*c)->mboxFile = g_key_file_get_string (kfile, iPilot, "mbox_file", NULL);
+	(*c)->receiveAction = g_key_file_get_string (kfile, iPilot,
+	    "receive_action", &error);
+	if (error) {
+	    g_warning (_("Unable load key email-conduit/%s/receive_action: %s"), iPilot,
+		error->message);
+	    g_error_free (error);
+	    error = NULL;
+	    (*c)->receiveAction = g_strdup ("copy");
+	}
+
 
 	(*c)->pilotId = pilotId;
-	g_free(prefix);
+
+	g_free (iPilot);
+	g_key_file_free (kfile);
 }
 
 static void
 save_configuration(ConduitCfg *c)
 {
-	gchar *prefix;
+	gchar *iPilot;
+	GKeyFile *kfile;
 
 	g_assert(c!=NULL);
 
-	prefix = g_strdup_printf("/gnome-pilot.d/email-conduit/Pilot_%u/",c->pilotId);
-
-	gnome_config_push_prefix(prefix);
-	gnome_config_set_string("sendmail", c->sendmail);
-	gnome_config_set_string("from_address", c->fromAddr);
-	gnome_config_set_string("send_action", c->sendAction);
-	gnome_config_set_string("mh_directory", c->mhDirectory);
-	gnome_config_set_string("mbox_file", c->mboxFile);
-	gnome_config_set_string("receive_action", c->receiveAction);
-	gnome_config_pop_prefix();
-	gnome_config_sync();
-	gnome_config_drop_all();
-
-	g_free(prefix);
+ 	kfile = get_kfile("email-conduit");
+	iPilot = g_strdup_printf ("Pilot_%u", c->pilotId);
+
+	if (c->sendmail != NULL)
+	    g_key_file_set_string (kfile, iPilot, "sendmail", c->sendmail);
+	else
+	    g_key_file_remove_key (kfile, iPilot, "sendmail", NULL);
+	if (c->fromAddr != NULL)
+	    g_key_file_set_string (kfile, iPilot, "from_address", c->fromAddr);
+	else
+	    g_key_file_remove_key (kfile, iPilot, "from_addres", NULL);
+	g_key_file_set_string (kfile, iPilot, "send_action", c->sendAction);
+	if (c->mhDirectory != NULL)
+	    g_key_file_set_string (kfile, iPilot, "mh_directory", c->mhDirectory);
+	else
+	    g_key_file_remove_key (kfile, iPilot, "mh_directory", NULL);
+	if (c->mboxFile != NULL)
+	    g_key_file_set_string (kfile, iPilot, "mbox_file", c->mboxFile);
+	else
+	    g_key_file_remove_key (kfile, iPilot, "mbox_file", NULL);
+	g_key_file_set_string (kfile, iPilot, "receive_action", c->receiveAction);
+
+	g_free(iPilot);
+	save_config (kfile, "email-conduit");
 }
 
 static void 
@@ -781,12 +824,12 @@ static GtkWidget
     GtkWidget *menu, *menuItem;
     GtkWidget *box, *button;
 
-    vbox = gtk_vbox_new(FALSE, GNOME_PAD);
+    vbox = gtk_vbox_new(FALSE, EC_PAD);
 
     table = gtk_table_new(2, 5, FALSE);
     gtk_table_set_row_spacings(GTK_TABLE(table), 4);
     gtk_table_set_col_spacings(GTK_TABLE(table), 10);
-    gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, GNOME_PAD);
+    gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, EC_PAD);
 
     /* send_action option menu */
     label = gtk_label_new(_("Send Action:"));
@@ -1051,3 +1094,116 @@ void conduit_destroy_gpilot_conduit( GnomePilotConduit *c )
   gtk_object_destroy(GTK_OBJECT(c));
 }
 
+#define OLD_PREFIX ".gnome2/gnome-pilot.d"
+#define NEW_PREFIX ".gnome-pilot"
+
+#define IS_STR_SET(x) (x != NULL && x[0] != '\0')
+
+static void
+migrate_conf (const gchar *old, const gchar *new)
+{
+	gchar *basename = g_path_get_dirname (new);
+
+	if (!g_file_test (basename, G_FILE_TEST_EXISTS)) {
+		g_mkdir_with_parents (basename, S_IRUSR | S_IWUSR | S_IXUSR);
+	} else {
+		if (!g_file_test (basename, G_FILE_TEST_IS_DIR)) {
+			gchar *tmp = g_strdup_printf ("%s.old", basename);
+			rename (basename, tmp);
+			g_free (tmp);
+			g_mkdir_with_parents (basename, S_IRUSR | S_IWUSR | S_IXUSR);
+		}
+	}
+	g_free (basename);
+
+	if (g_file_test (new, G_FILE_TEST_IS_REGULAR)) {
+		return;
+	} else if (g_file_test (old, G_FILE_TEST_IS_REGULAR)) {
+		rename (old, new);
+	} else {
+		creat (new, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+	}
+}
+
+static GKeyFile*
+get_kfile (const gchar *conf)
+{
+	GKeyFile   *kfile = g_key_file_new ();
+	const char *homedir = g_getenv ("HOME");
+	char       *old = NULL;
+	char       *new = NULL;
+
+	if (!homedir)
+		homedir = g_get_home_dir ();
+
+	old = g_build_filename (homedir, OLD_PREFIX, conf, NULL);
+	new = g_build_filename (homedir, NEW_PREFIX, conf, NULL);
+
+	migrate_conf (old, new);
+
+	g_key_file_load_from_file (kfile, new, G_KEY_FILE_NONE, NULL);
+	g_key_file_set_list_separator (kfile, ' ');
+
+	g_free (new);
+	g_free (old);
+	return kfile;
+}
+
+static gboolean
+save_config (GKeyFile    *kfile,
+	     const gchar *conf)
+{
+	const char *homedir = g_getenv ("HOME");
+        GError     *error = NULL;
+        gchar      *data = NULL;
+        gsize       size;
+	gchar 	   *filename = NULL;
+
+	g_return_val_if_fail (kfile, FALSE);
+	g_return_val_if_fail (IS_STR_SET (conf), FALSE);
+
+	if (!homedir)
+		homedir = g_get_home_dir ();
+
+	filename = g_build_filename (homedir, NEW_PREFIX, conf, NULL);
+
+	if (! g_file_test (filename, G_FILE_TEST_IS_REGULAR)) {
+		g_free (filename);
+		g_warning ("File %s does not exsit", filename);
+		return FALSE;
+	}
+
+        g_message ("Saving config to disk...");
+
+	g_key_file_set_list_separator (kfile, ' ');
+ 	data = g_key_file_to_data (kfile, &size, &error);
+        if (error) {
+                g_warning ("Could not get config data to write to file, %s",
+                           error->message);
+                g_error_free (error);
+
+                return FALSE;
+        }
+
+        g_file_set_contents (filename, data, size, &error);
+        g_free (data);
+
+        if (error) {
+                g_warning ("Could not write %" G_GSIZE_FORMAT " bytes to file '%s', %s",
+                           size,
+                           filename,
+                           error->message);
+                g_free (filename);
+                g_error_free (error);
+
+                return FALSE;
+        }
+
+        g_message ("Wrote config to '%s' (%" G_GSIZE_FORMAT " bytes)",
+                   filename,
+                   size);
+
+	g_free (filename);
+
+	return TRUE;
+}



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