[tracker] Added Move and Remove functionality for thumbnails



commit af68dfeefb9e772853efdca7fdc47b81c2404b60
Author: Philip Van Hoof <philip codeminded be>
Date:   Mon Feb 1 12:20:05 2010 +0100

    Added Move and Remove functionality for thumbnails

 src/libtracker-miner/Makefile.am           |    6 +-
 src/libtracker-miner/tracker-thumbnailer.c |  407 ++++++++++++++++++++++++++++
 src/libtracker-miner/tracker-thumbnailer.h |   39 +++
 src/tracker-miner-fs/tracker-main.c        |    6 +
 4 files changed, 456 insertions(+), 2 deletions(-)
---
diff --git a/src/libtracker-miner/Makefile.am b/src/libtracker-miner/Makefile.am
index ab17b4b..89502fa 100644
--- a/src/libtracker-miner/Makefile.am
+++ b/src/libtracker-miner/Makefile.am
@@ -39,12 +39,14 @@ libtracker_miner_ TRACKER_API_VERSION@_la_SOURCES = 	\
 	tracker-monitor.c				\
 	tracker-monitor.h				\
 	tracker-utils.c					\
-	tracker-utils.h
+	tracker-utils.h					\
+	tracker-thumbnailer.c
 
 libtracker_minerinclude_HEADERS = 			\
 	tracker-miner.h 				\
 	tracker-miner-dbus.h 				\
-	tracker-miner-fs.h
+	tracker-miner-fs.h				\
+	tracker-thumbnailer.h
 
 libtracker_miner_ TRACKER_API_VERSION@_la_LDFLAGS = 	\
 	-version-info $(LT_CURRENT):$(LT_REVISION):$(LT_AGE)
diff --git a/src/libtracker-miner/tracker-thumbnailer.c b/src/libtracker-miner/tracker-thumbnailer.c
new file mode 100644
index 0000000..ddededf
--- /dev/null
+++ b/src/libtracker-miner/tracker-thumbnailer.c
@@ -0,0 +1,407 @@
+/*
+ * Copyright (C) 2008, Nokia
+ *
+ * This library 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.
+ *
+ * This library 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 library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ *
+ * Author: Philip Van Hoof <philip codeminded be>
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include <libtracker-common/tracker-dbus.h>
+
+#include "tracker-thumbnailer.h"
+
+#define THUMBCACHE_SERVICE      "org.freedesktop.thumbnails.Cache1"
+#define THUMBCACHE_PATH         "/org/freedesktop/thumbnails/Cache1"
+#define THUMBCACHE_INTERFACE    "org.freedesktop.thumbnails.Cache1"
+
+#define THUMBMAN_SERVICE        "org.freedesktop.thumbnails.Thumbnailer1"
+#define THUMBMAN_PATH           "/org/freedesktop/thumbnails/Thumbnailer1"
+#define THUMBMAN_INTERFACE      "org.freedesktop.thumbnails.Thumbnailer1"
+
+typedef struct {
+	DBusGProxy *cache_proxy;
+	DBusGProxy *manager_proxy;
+
+	GStrv supported_mime_types;
+
+	GSList *removes;
+	GSList *moves_to;
+	GSList *moves_from;
+
+	guint request_id;
+	gboolean service_is_available;
+} TrackerThumbnailerPrivate;
+
+static GStaticPrivate private_key = G_STATIC_PRIVATE_INIT;
+
+static void
+private_free (gpointer data)
+{
+	TrackerThumbnailerPrivate *private;
+
+	private = data;
+
+	if (private->cache_proxy) {
+		g_object_unref (private->cache_proxy);
+	}
+
+	if (private->manager_proxy) {
+		g_object_unref (private->manager_proxy);
+	}
+
+	g_strfreev (private->supported_mime_types);
+
+	g_slist_foreach (private->removes, (GFunc) g_free, NULL);
+	g_slist_free (private->removes);
+
+	g_slist_foreach (private->moves_to, (GFunc) g_free, NULL);
+	g_slist_free (private->moves_to);
+
+	g_slist_foreach (private->moves_from, (GFunc) g_free, NULL);
+	g_slist_free (private->moves_from);
+
+	g_free (private);
+}
+
+inline static gboolean
+should_be_thumbnailed (GStrv        list,
+                       const gchar *mime)
+{
+	gboolean should_thumbnail;
+	guint i;
+
+	if (!list) {
+		return TRUE;
+	}
+
+	for (should_thumbnail = FALSE, i = 0;
+	     should_thumbnail == FALSE && list[i] != NULL;
+	     i++) {
+		if (g_ascii_strcasecmp (list[i], mime) == 0) {
+			should_thumbnail = TRUE;
+		}
+	}
+
+	return should_thumbnail;
+}
+
+gboolean
+tracker_thumbnailer_init (void)
+{
+	TrackerThumbnailerPrivate *private;
+	DBusGConnection *connection;
+	GStrv mime_types = NULL;
+	GStrv uri_schemes = NULL;
+	GError *error = NULL;
+
+	private = g_new0 (TrackerThumbnailerPrivate, 1);
+
+	/* Don't start at 0, start at 1. */
+	private->request_id = 1;
+
+	g_static_private_set (&private_key,
+	                      private,
+	                      private_free);
+
+	g_message ("Thumbnailer connections being set up...");
+
+	connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
+
+	if (!connection) {
+		g_critical ("Could not connect to the D-Bus session bus, %s",
+		            error ? error->message : "no error given.");
+		g_clear_error (&error);
+
+		private->service_is_available = FALSE;
+
+		return FALSE;
+	}
+
+	private->cache_proxy =
+		dbus_g_proxy_new_for_name (connection,
+		                           THUMBCACHE_SERVICE,
+		                           THUMBCACHE_PATH,
+		                           THUMBCACHE_INTERFACE);
+
+	private->manager_proxy =
+		dbus_g_proxy_new_for_name (connection,
+		                           THUMBMAN_SERVICE,
+		                           THUMBMAN_PATH,
+		                           THUMBMAN_INTERFACE);
+
+	dbus_g_proxy_call (private->manager_proxy,
+	                   "GetSupported", &error,
+	                   G_TYPE_INVALID,
+	                   G_TYPE_STRV, &uri_schemes,
+	                   G_TYPE_STRV, &mime_types,
+	                   G_TYPE_INVALID);
+
+	if (error) {
+		g_message ("Thumbnailer service did not return supported mime types, %s",
+		           error->message);
+
+		g_error_free (error);
+
+		if (private->cache_proxy) {
+			g_object_unref (private->cache_proxy);
+			private->cache_proxy = NULL;
+		}
+
+		if (private->manager_proxy) {
+			g_object_unref (private->manager_proxy);
+			private->manager_proxy = NULL;
+		}
+
+		return FALSE;
+	} else if (mime_types) {
+		GHashTable *hash;
+		GHashTableIter iter;
+		gpointer key, value;
+		guint i;
+
+		/* The table that you receive may contain duplicate mime-types, because
+		 * they are grouped against the uri_schemes table */
+
+		hash = g_hash_table_new (g_str_hash, g_str_equal);
+
+		for (i = 0; mime_types[i] != NULL; i++) {
+			g_hash_table_insert (hash, mime_types[i], NULL);
+		}
+
+		i = g_hash_table_size (hash);
+		g_message ("Thumbnailer supports %d mime types", i);
+
+		g_hash_table_iter_init (&iter, hash);
+		private->supported_mime_types = (GStrv) g_new0 (gchar *, i + 1);
+
+		i = 0;
+		while (g_hash_table_iter_next (&iter, &key, &value)) {
+			private->supported_mime_types[i] = g_strdup (key);
+			i++;
+		}
+
+		g_hash_table_unref (hash);
+
+		private->service_is_available = TRUE;
+	}
+
+	if (mime_types)
+		g_strfreev (mime_types);
+
+	if (uri_schemes)
+		g_strfreev (uri_schemes);
+
+	return TRUE;
+}
+
+void
+tracker_thumbnailer_shutdown (void)
+{
+	g_static_private_set (&private_key, NULL, NULL);
+}
+
+gboolean
+tracker_thumbnailer_move_add (const gchar *from_uri,
+                              const gchar *mime_type,
+                              const gchar *to_uri)
+{
+
+	TrackerThumbnailerPrivate *private;
+	gchar *used_from_uri, *used_to_uri;
+
+	/* mime_type can be NULL */
+
+	g_return_val_if_fail (from_uri != NULL, FALSE);
+	g_return_val_if_fail (to_uri != NULL, FALSE);
+
+	private = g_static_private_get (&private_key);
+	g_return_val_if_fail (private != NULL, FALSE);
+
+	if (!private->service_is_available) {
+		return FALSE;
+	}
+
+	if (mime_type && !should_be_thumbnailed (private->supported_mime_types, mime_type)) {
+		return FALSE;
+	}
+
+	/* Add new URI (detect if we got passed a path) */
+	if (!strstr (from_uri, "://")) {
+		used_from_uri = g_filename_to_uri (from_uri, NULL, NULL);
+	} else {
+		used_from_uri = g_strdup (from_uri);
+	}
+
+	if (!strstr (to_uri, "://")) {
+		used_to_uri = g_filename_to_uri (to_uri, NULL, NULL);
+	} else {
+		used_to_uri = g_strdup (to_uri);
+	}
+
+	private->moves_from = g_slist_prepend (private->moves_from, used_from_uri);
+	private->moves_to = g_slist_prepend (private->moves_to, used_to_uri);
+
+	g_debug ("Thumbnailer request to move uri from:'%s' to:'%s' queued",
+	         used_from_uri,
+	         used_to_uri);
+
+	return TRUE;
+}
+
+gboolean
+tracker_thumbnailer_remove_add (const gchar *uri,
+                                const gchar *mime_type)
+{
+	TrackerThumbnailerPrivate *private;
+	gchar *used_uri;
+
+	/* mime_type can be NULL */
+
+	g_return_val_if_fail (uri != NULL, FALSE);
+
+	private = g_static_private_get (&private_key);
+	g_return_val_if_fail (private != NULL, FALSE);
+
+	if (!private->service_is_available) {
+		return FALSE;
+	}
+
+	if (mime_type && !should_be_thumbnailed (private->supported_mime_types, mime_type)) {
+		return FALSE;
+	}
+
+	/* Add new URI (detect if we got passed a path) */
+	if (!strstr (uri, "://")) {
+		used_uri = g_filename_to_uri (uri, NULL, NULL);
+	} else {
+		used_uri = g_strdup (uri);
+	}
+
+	private->removes = g_slist_prepend (private->removes, used_uri);
+
+	g_debug ("Thumbnailer request to remove uri:'%s', appended to queue", uri);
+
+	return TRUE;
+}
+
+gboolean
+tracker_thumbnailer_cleanup (const gchar *uri_prefix)
+{
+	TrackerThumbnailerPrivate *private;
+
+	g_return_val_if_fail (uri_prefix != NULL, FALSE);
+
+	private = g_static_private_get (&private_key);
+	g_return_val_if_fail (private != NULL, FALSE);
+
+	if (!private->service_is_available) {
+		return FALSE;
+	}
+
+	private->request_id++;
+
+	g_debug ("Thumbnailer cleaning up uri:'%s', request_id:%d...",
+	         uri_prefix,
+	         private->request_id);
+
+	dbus_g_proxy_call_no_reply (private->cache_proxy,
+	                            "Cleanup",
+	                            G_TYPE_STRING, uri_prefix,
+	                            G_TYPE_UINT, 0,
+	                            G_TYPE_INVALID,
+	                            G_TYPE_INVALID);
+
+	return TRUE;
+}
+
+void
+tracker_thumbnailer_send (void)
+{
+	TrackerThumbnailerPrivate *private;
+	guint list_len;
+
+	private = g_static_private_get (&private_key);
+	g_return_if_fail (private != NULL);
+
+	if (!private->service_is_available) {
+		return;
+	}
+
+	list_len = g_slist_length (private->removes);
+
+	if (list_len > 0) {
+		GStrv uri_strv;
+
+		uri_strv = tracker_dbus_slist_to_strv (private->removes);
+
+		dbus_g_proxy_call_no_reply (private->cache_proxy,
+		                            "Delete",
+		                            G_TYPE_STRV, uri_strv,
+		                            G_TYPE_INVALID,
+		                            G_TYPE_INVALID);
+
+		g_message ("Thumbnailer removes queue sent with %d items to thumbnailer daemon, request ID:%d...",
+		           list_len,
+		           private->request_id++);
+
+		/* Clean up newly created GStrv */
+		g_strfreev (uri_strv);
+
+		/* Clean up privately held data */
+		g_slist_foreach (private->removes, (GFunc) g_free, NULL);
+		g_slist_free (private->removes);
+		private->removes = NULL;
+	}
+
+	list_len = g_slist_length (private->moves_from);
+
+	if (list_len > 0) {
+		GStrv from_strv, to_strv;
+
+		g_assert (list_len == g_slist_length (private->moves_to));
+
+		from_strv = tracker_dbus_slist_to_strv (private->moves_from);
+		to_strv = tracker_dbus_slist_to_strv (private->moves_to);
+
+		dbus_g_proxy_call_no_reply (private->cache_proxy,
+		                            "Move",
+		                            G_TYPE_STRV, from_strv,
+		                            G_TYPE_STRV, to_strv,
+		                            G_TYPE_INVALID,
+		                            G_TYPE_INVALID);
+
+		g_message ("Thumbnailer moves queue sent with %d items to thumbnailer daemon, request ID:%d...",
+		           list_len,
+		           private->request_id++);
+
+		/* Clean up newly created GStrv */
+		g_strfreev (from_strv);
+		g_strfreev (to_strv);
+
+		/* Clean up privately held data */
+		g_slist_foreach (private->moves_from, (GFunc) g_free, NULL);
+		g_slist_free (private->moves_from);
+		private->moves_from = NULL;
+
+		g_slist_foreach (private->moves_to, (GFunc) g_free, NULL);
+		g_slist_free (private->moves_to);
+		private->moves_to = NULL;
+	}
+}
diff --git a/src/libtracker-miner/tracker-thumbnailer.h b/src/libtracker-miner/tracker-thumbnailer.h
new file mode 100644
index 0000000..86fa1b3
--- /dev/null
+++ b/src/libtracker-miner/tracker-thumbnailer.h
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2008, Nokia
+ *
+ * This library 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.
+ *
+ * This library 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 library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ *
+ * Author: Philip Van Hoof <philip codeminded be>
+ */
+
+#ifndef __TRACKER_MINER_FS_THUMBNAILER_H__
+#define __TRACKER_MINER_FS_THUMBNAILER_H__
+
+G_BEGIN_DECLS
+
+gboolean tracker_thumbnailer_init       (void);
+void     tracker_thumbnailer_shutdown   (void);
+void     tracker_thumbnailer_send       (void);
+gboolean tracker_thumbnailer_move_add   (const gchar *from_uri,
+                                         const gchar *mime_type,
+                                         const gchar *to_uri);
+gboolean tracker_thumbnailer_remove_add (const gchar *uri,
+                                         const gchar *mime_type);
+gboolean tracker_thumbnailer_cleanup    (const gchar *uri_prefix);
+
+G_END_DECLS
+
+#endif /* __TRACKER_MINER_FS_THUMBNAILER_H__ */
diff --git a/src/tracker-miner-fs/tracker-main.c b/src/tracker-miner-fs/tracker-main.c
index 20d4373..75228eb 100644
--- a/src/tracker-miner-fs/tracker-main.c
+++ b/src/tracker-miner-fs/tracker-main.c
@@ -40,6 +40,8 @@
 #include <libtracker-common/tracker-ontology.h>
 #include <libtracker-common/tracker-file-utils.h>
 
+#include <libtracker-miner/tracker-thumbnailer.h>
+
 #include <libtracker-db/tracker-db-manager.h>
 #include <libtracker-db/tracker-db-dbus.h>
 
@@ -356,6 +358,8 @@ main (gint argc, gchar *argv[])
 	                  G_CALLBACK (miner_finished_cb),
 	                  NULL);
 
+	tracker_thumbnailer_init ();
+
 	miner_handle_next ();
 
 	main_loop = g_main_loop_new (NULL, FALSE);
@@ -366,6 +370,8 @@ main (gint argc, gchar *argv[])
 	g_main_loop_unref (main_loop);
 	g_object_unref (config);
 
+	tracker_thumbnailer_shutdown ();
+
 	g_slist_foreach (miners, (GFunc) g_object_unref, NULL);
 	g_slist_free (miners);
 



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