[nautilus] desktop-metadata: use a keyfile instead of GConf to store it



commit 632bf39a23e5457d76540063eff69b36e36459d0
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Mon Jan 31 12:21:47 2011 -0500

    desktop-metadata: use a keyfile instead of GConf to store it

 libnautilus-private/Makefile.am                    |    2 +
 .../nautilus-desktop-directory-file.c              |  145 +------------
 .../nautilus-desktop-directory-file.h              |   10 -
 libnautilus-private/nautilus-desktop-icon-file.c   |    5 +-
 libnautilus-private/nautilus-desktop-metadata.c    |  230 ++++++++++++++++++++
 libnautilus-private/nautilus-desktop-metadata.h    |   45 ++++
 libnautilus-private/nautilus-metadata.h            |    3 -
 7 files changed, 283 insertions(+), 157 deletions(-)
---
diff --git a/libnautilus-private/Makefile.am b/libnautilus-private/Makefile.am
index d46891a..cee6fd2 100644
--- a/libnautilus-private/Makefile.am
+++ b/libnautilus-private/Makefile.am
@@ -73,6 +73,8 @@ libnautilus_private_la_SOURCES = \
 	nautilus-desktop-link-monitor.h \
 	nautilus-desktop-link.c \
 	nautilus-desktop-link.h \
+	nautilus-desktop-metadata.c \
+	nautilus-desktop-metadata.h \
 	nautilus-directory-async.c \
 	nautilus-directory-notify.h \
 	nautilus-directory-private.h \
diff --git a/libnautilus-private/nautilus-desktop-directory-file.c b/libnautilus-private/nautilus-desktop-directory-file.c
index add656b..f7025b4 100644
--- a/libnautilus-private/nautilus-desktop-directory-file.c
+++ b/libnautilus-private/nautilus-desktop-directory-file.c
@@ -26,14 +26,13 @@
 #include <config.h>
 #include "nautilus-desktop-directory-file.h"
 
+#include "nautilus-desktop-metadata.h"
 #include "nautilus-directory-notify.h"
 #include "nautilus-directory-private.h"
 #include "nautilus-file-attributes.h"
 #include "nautilus-file-private.h"
 #include "nautilus-file-utilities.h"
 #include <eel/eel-glib-extensions.h>
-#include <gconf/gconf-client.h>
-#include <gconf/gconf-value.h>
 #include "nautilus-desktop-directory.h"
 #include "nautilus-metadata.h"
 #include <gtk/gtk.h>
@@ -444,144 +443,6 @@ monitor_destroy (gpointer data)
 	g_free (monitor);
 }
 
-static char *
-get_metadata_gconf_path (const char *name,
-			 const char *key)
-{
-	char *res, *escaped_name;
-
-	escaped_name = gconf_escape_key (name, -1);
-	res = g_build_filename (NAUTILUS_DESKTOP_METADATA_GCONF_PATH, escaped_name, key, NULL);
-	g_free (escaped_name);
-
-	return res;
-}
-
-void
-nautilus_desktop_set_metadata_string (NautilusFile *file,
-				      const char *name,
-				      const char *key,
-				      const char *string)
-{
-	GConfClient *client;
-	char *gconf_key;
-
-	client = gconf_client_get_default ();
-	gconf_key = get_metadata_gconf_path (name, key);
-
-	if (string) {
-		gconf_client_set_string (client, gconf_key, string, NULL);
-	} else {
-		gconf_client_unset (client, gconf_key, NULL);
-	}
-
-	g_free (gconf_key);
-	g_object_unref (client);
-
-	if (nautilus_desktop_update_metadata_from_gconf (file, name)) {
-		nautilus_file_changed (file);
-	}
-}
-
-void
-nautilus_desktop_set_metadata_stringv (NautilusFile *file,
-				       const char *name,
-				       const char *key,
-				       char **stringv)
-{
-	GConfClient *client;
-	char *gconf_key;
-	GSList *list;
-	int i;
-
-	client = gconf_client_get_default ();
-	gconf_key = get_metadata_gconf_path (name, key);
-
-	list = NULL;
-	for (i = 0; stringv[i] != NULL; i++) {
-		list = g_slist_prepend (list, stringv[i]);
-	}
-	list = g_slist_reverse (list);
-
-	gconf_client_set_list (client, gconf_key,
-			       GCONF_VALUE_STRING,
-			       list, NULL);
-
-	g_slist_free (list);
-	g_free (gconf_key);
-	g_object_unref (client);
-
-	if (nautilus_desktop_update_metadata_from_gconf (file, name)) {
-		nautilus_file_changed (file);
-	}
-}
-
-gboolean
-nautilus_desktop_update_metadata_from_gconf (NautilusFile *file,
-					     const char *name)
-{
-	GConfClient *client;
-	GSList *entries, *l;
-	char *dir;
-	const char *key;
-	GConfEntry *entry;
-	GConfValue *value;
-	GFileInfo *info;
-	gboolean changed;
-	char *gio_key;
-	GSList *value_list;
-	char **strv;
-	int i;
-
-	client = gconf_client_get_default ();
-
-	dir = get_metadata_gconf_path (name, NULL);
-	entries = gconf_client_all_entries (client, dir, NULL);
-	g_free (dir);
-
-	info = g_file_info_new ();
-
-	for (l = entries; l != NULL; l = l->next) {
-		entry = l->data;
-
-		key = gconf_entry_get_key (entry);
-		value = gconf_entry_get_value (entry);
-
-                if (value == NULL) {
-			continue;
-		}
-		key = strrchr (key, '/') + 1;
-
-		gio_key = g_strconcat ("metadata::", key, NULL);
-		if (value->type == GCONF_VALUE_STRING) {
-			g_file_info_set_attribute_string (info, gio_key,
-							  gconf_value_get_string (value));
-		} else if (value->type == GCONF_VALUE_LIST &&
-			   gconf_value_get_list_type (value) == GCONF_VALUE_STRING) {
-			value_list = gconf_value_get_list (value);
-			strv = g_new (char *, g_slist_length (value_list) + 1);
-			for (i = 0; value_list != NULL; i++, value_list = value_list->next) {
-				strv[i] = l->data;
-			}
-			strv[i] = NULL;
-			g_file_info_set_attribute_stringv (info, gio_key, strv);
-			g_free (strv);
-		}
-
-		g_free (gio_key);
-
-		gconf_entry_unref (entry);
-	}
-	g_slist_free (entries);
-
-	changed = nautilus_file_update_metadata_from_info (file, info);
-
-	g_object_unref (info);
-	g_object_unref (client);
-
-	return changed;
-}
-
 static void
 nautilus_desktop_directory_file_set_metadata (NautilusFile           *file,
 					      const char             *key,
@@ -595,7 +456,7 @@ nautilus_desktop_directory_file_set_metadata_as_list (NautilusFile           *fi
 						      const char             *key,
 						      char                  **value)
 {
-	nautilus_desktop_set_metadata_stringv (file, "directory", key, value);
+	nautilus_desktop_set_metadata_stringv (file, "directory", key, (const gchar **) value);
 }
 
 static void
@@ -623,7 +484,7 @@ nautilus_desktop_directory_file_init (NautilusDesktopDirectoryFile *desktop_file
 	
 	desktop_file->details->real_dir_file = real_dir_file;
 
-	nautilus_desktop_update_metadata_from_gconf (NAUTILUS_FILE (desktop_file), "directory");
+	nautilus_desktop_update_metadata_from_keyfile (NAUTILUS_FILE (desktop_file), "directory");
 
 	g_signal_connect_object (real_dir_file, "changed",
 				 G_CALLBACK (real_file_changed_callback), desktop_file, 0);
diff --git a/libnautilus-private/nautilus-desktop-directory-file.h b/libnautilus-private/nautilus-desktop-directory-file.h
index 20d4b74..033320e 100644
--- a/libnautilus-private/nautilus-desktop-directory-file.h
+++ b/libnautilus-private/nautilus-desktop-directory-file.h
@@ -52,15 +52,5 @@ typedef struct {
 } NautilusDesktopDirectoryFileClass;
 
 GType    nautilus_desktop_directory_file_get_type    (void);
-gboolean nautilus_desktop_update_metadata_from_gconf (NautilusFile  *file,
-						      const char    *name);
-void     nautilus_desktop_set_metadata_string        (NautilusFile  *file,
-						      const char    *name,
-						      const char    *key,
-						      const char    *string);
-void     nautilus_desktop_set_metadata_stringv       (NautilusFile  *file,
-						      const char    *name,
-						      const char    *key,
-						      char         **stringv);
 
 #endif /* NAUTILUS_DESKTOP_DIRECTORY_FILE_H */
diff --git a/libnautilus-private/nautilus-desktop-icon-file.c b/libnautilus-private/nautilus-desktop-icon-file.c
index 9af342a..fbcaf53 100644
--- a/libnautilus-private/nautilus-desktop-icon-file.c
+++ b/libnautilus-private/nautilus-desktop-icon-file.c
@@ -26,6 +26,7 @@
 #include <config.h>
 #include "nautilus-desktop-icon-file.h"
 
+#include "nautilus-desktop-metadata.h"
 #include "nautilus-desktop-directory-file.h"
 #include "nautilus-directory-notify.h"
 #include "nautilus-directory-private.h"
@@ -283,7 +284,7 @@ nautilus_desktop_icon_file_new (NautilusDesktopLink *link)
 
 	update_info_from_link (icon_file);
 
-	nautilus_desktop_update_metadata_from_gconf (file, file->details->name);
+	nautilus_desktop_update_metadata_from_keyfile (file, file->details->name);
 
 	nautilus_directory_add_file (directory, file);
 
@@ -357,7 +358,7 @@ nautilus_desktop_icon_file_set_metadata_as_list (NautilusFile           *file,
 						 const char             *key,
 						 char                  **value)
 {
-	nautilus_desktop_set_metadata_stringv (file, file->details->name, key, value);
+	nautilus_desktop_set_metadata_stringv (file, file->details->name, key, (const gchar **) value);
 }
 
 static void
diff --git a/libnautilus-private/nautilus-desktop-metadata.c b/libnautilus-private/nautilus-desktop-metadata.c
new file mode 100644
index 0000000..b9c0d4d
--- /dev/null
+++ b/libnautilus-private/nautilus-desktop-metadata.c
@@ -0,0 +1,230 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ * Nautilus
+ *
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * Nautilus is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Nautilus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; see the file COPYING.  If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Cosimo Cecchi <cosimoc redhat com>
+ */
+
+#include <config.h>
+
+#include "nautilus-desktop-metadata.h"
+
+#include "nautilus-directory-notify.h"
+#include "nautilus-file-private.h"
+#include "nautilus-file-utilities.h"
+
+#include <glib/gstdio.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+static gchar *
+get_keyfile_path (void)
+{
+	gchar *xdg_dir, *retval;
+
+	xdg_dir = nautilus_get_user_directory ();
+	retval = g_build_filename (xdg_dir, "desktop-metadata", NULL);
+
+	g_free (xdg_dir);
+	
+	return retval;
+}
+
+static gboolean
+save_in_idle_cb (gpointer data)
+{
+	GKeyFile *keyfile = data;
+	gchar *contents, *filename;
+	gsize length;
+	GError *error = NULL;
+
+	contents = g_key_file_to_data (keyfile, &length, NULL);
+	filename = get_keyfile_path ();
+
+	if (contents != NULL) {
+		g_file_set_contents (filename,
+				     contents, length,
+				     &error);
+	}
+
+	if (error != NULL) {
+		g_warning ("Couldn't save the desktop metadata keyfile to disk: %s",
+			   error->message);
+		g_error_free (error);
+	}
+
+	return FALSE;
+}
+
+static void
+save_in_idle (GKeyFile *keyfile)
+{
+	g_idle_add (save_in_idle_cb, keyfile);
+}
+
+static GKeyFile *
+load_metadata_keyfile (void)
+{
+  	GKeyFile *retval;
+	GError *error = NULL;
+	gchar *filename;
+
+	retval = g_key_file_new ();
+	filename = get_keyfile_path ();
+
+	g_key_file_load_from_file (retval,
+				   filename,
+				   G_KEY_FILE_NONE,
+				   &error);
+
+	if (error != NULL) {
+		g_print ("Unable to open the desktop metadata keyfile: %s\n",
+			 error->message);
+
+		g_error_free (error);
+	}
+
+	g_free (filename);
+
+	return retval;
+}
+
+static GKeyFile *
+get_keyfile (void)
+{
+	static gboolean keyfile_loaded = FALSE;
+	static GKeyFile *keyfile = NULL;
+
+	if (!keyfile_loaded) {
+		keyfile = load_metadata_keyfile ();
+		keyfile_loaded = TRUE;
+	}
+
+	return keyfile;
+}
+
+void
+nautilus_desktop_set_metadata_string (NautilusFile *file,
+                                      const gchar *name,
+                                      const gchar *key,
+                                      const gchar *string)
+{
+	GKeyFile *keyfile;
+
+	keyfile = get_keyfile ();
+
+	g_key_file_set_string (keyfile,
+			       name,
+			       key,
+			       string);
+
+	save_in_idle (keyfile);
+
+	if (nautilus_desktop_update_metadata_from_keyfile (file, name)) {
+		nautilus_file_changed (file);
+	}	
+}
+
+void
+nautilus_desktop_set_metadata_stringv (NautilusFile *file,
+                                       const char *name,
+                                       const char *key,
+                                       const char * const *stringv)
+{
+	GKeyFile *keyfile;
+
+	g_print ("setting desktop metadata\n");
+
+	keyfile = get_keyfile ();
+
+	g_key_file_set_string_list (keyfile,
+				    name,
+				    key,
+				    stringv,
+				    g_strv_length ((gchar **) stringv));
+
+	save_in_idle (keyfile);
+
+	if (nautilus_desktop_update_metadata_from_keyfile (file, name)) {
+		nautilus_file_changed (file);
+	}
+}
+
+gboolean
+nautilus_desktop_update_metadata_from_keyfile (NautilusFile *file,
+					       const gchar *name)
+{
+	gchar **keys, **values;
+	const gchar *key;
+	gchar *gio_key;
+	gsize length, values_length;
+	GKeyFile *keyfile;
+	GFileInfo *info;
+	gint idx;
+	gboolean res;
+
+	keyfile = get_keyfile ();
+
+	keys = g_key_file_get_keys (keyfile,
+				    name,
+				    &length,
+				    NULL);
+
+	if (keys == NULL) {
+		return FALSE;
+	}
+
+	info = g_file_info_new ();
+
+	for (idx = 0; idx < length; idx++) {
+		key = keys[idx];
+		values = g_key_file_get_string_list (keyfile,
+						     name,
+						     key,
+						     &values_length,
+						     NULL);
+
+		gio_key = g_strconcat ("metadata::", key, NULL);
+
+		if (values_length < 1) {
+			continue;
+		} else if (values_length == 1) {
+			g_file_info_set_attribute_string (info,
+							  gio_key,
+							  values[0]);
+		} else {
+			g_file_info_set_attribute_stringv (info,
+							   gio_key,
+							   values);
+		}
+
+		g_free (gio_key);
+		g_strfreev (values);
+	}
+
+	res = nautilus_file_update_metadata_from_info (file, info);
+
+	g_strfreev (keys);
+	g_object_unref (info);
+
+	return res;
+}
diff --git a/libnautilus-private/nautilus-desktop-metadata.h b/libnautilus-private/nautilus-desktop-metadata.h
new file mode 100644
index 0000000..a578ea0
--- /dev/null
+++ b/libnautilus-private/nautilus-desktop-metadata.h
@@ -0,0 +1,45 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/*
+ * Nautilus
+ *
+ * Copyright (C) 2011 Red Hat, Inc.
+ *
+ * Nautilus is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Nautilus is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with this program; see the file COPYING.  If not,
+ * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ *
+ * Authors: Cosimo Cecchi <cosimoc redhat com>
+ */
+
+#ifndef __NAUTILUS_DESKTOP_METADATA_H__
+#define __NAUTILUS_DESKTOP_METADATA_H__
+
+#include <glib.h>
+
+#include <libnautilus-private/nautilus-file.h>
+
+void nautilus_desktop_set_metadata_string (NautilusFile *file,
+                                           const gchar *name,
+                                           const gchar *key,
+                                           const gchar *string);
+
+void nautilus_desktop_set_metadata_stringv (NautilusFile *file,
+                                            const char *name,
+                                            const char *key,
+                                            const char * const *stringv);
+
+gboolean nautilus_desktop_update_metadata_from_keyfile (NautilusFile *file,
+                                                        const gchar *name);
+
+#endif /* __NAUTILUS_DESKTOP_METADATA_H__ */
diff --git a/libnautilus-private/nautilus-metadata.h b/libnautilus-private/nautilus-metadata.h
index 830a826..ff77c21 100644
--- a/libnautilus-private/nautilus-metadata.h
+++ b/libnautilus-private/nautilus-metadata.h
@@ -74,9 +74,6 @@
 #define NAUTILUS_METADATA_KEY_SCREEN				"screen"
 #define NAUTILUS_METADATA_KEY_EMBLEMS				"emblems"
 
-/* This is where desktop item metadata are stored in gconf */
-#define NAUTILUS_DESKTOP_METADATA_GCONF_PATH "/apps/nautilus/desktop-metadata"
-
 guint nautilus_metadata_get_id (const char *metadata);
 
 #endif /* NAUTILUS_METADATA_H */



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