[tracker] libtracker-miner: Cleanup media-art that isn't valid anymore



commit f72421f47f28537f153c79bb916ad5225d994522
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue May 17 15:13:31 2011 +0200

    libtracker-miner: Cleanup media-art that isn't valid anymore

 src/libtracker-miner/Makefile.am        |    6 +-
 src/libtracker-miner/tracker-albumart.c |  178 +++++++++++++++++++++++++++++++
 src/libtracker-miner/tracker-albumart.h |   36 ++++++
 src/libtracker-miner/tracker-miner-fs.c |    3 +
 src/libtracker-miner/tracker-miner.h    |    1 +
 5 files changed, 222 insertions(+), 2 deletions(-)
---
diff --git a/src/libtracker-miner/Makefile.am b/src/libtracker-miner/Makefile.am
index 328f64b..ef0ffb3 100644
--- a/src/libtracker-miner/Makefile.am
+++ b/src/libtracker-miner/Makefile.am
@@ -62,7 +62,9 @@ libtracker_miner_ TRACKER_API_VERSION@_la_SOURCES =    \
 	tracker-network-provider.c                     \
 	tracker-password-provider.c                    \
 	tracker-thumbnailer.c                          \
-	tracker-thumbnailer.h                          
+	tracker-thumbnailer.h                          \
+	tracker-albumart.c                             \
+	tracker-albumart.h
 
 libtracker_minerinclude_HEADERS =                      \
 	tracker-crawler.h                              \
@@ -172,4 +174,4 @@ typelibdir = $(libdir)/girepository-1.0
 typelib_DATA = $(INTROSPECTION_GIRS:.gir=.typelib)
 
 CLEANFILES += $(gir_DATA) $(typelib_DATA)
-endif
\ No newline at end of file
+endif
diff --git a/src/libtracker-miner/tracker-albumart.c b/src/libtracker-miner/tracker-albumart.c
new file mode 100644
index 0000000..b75340a
--- /dev/null
+++ b/src/libtracker-miner/tracker-albumart.c
@@ -0,0 +1,178 @@
+/*
+ * Copyright (C) 2008, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include <glib.h>
+#include <glib/gstdio.h>
+
+#include <libtracker-common/tracker-albumart.h>
+#include <libtracker-sparql/tracker-sparql.h>
+
+#include "tracker-albumart.h"
+
+static gboolean had_any = FALSE;
+
+static void
+on_query_finished (GObject      *source_object,
+                   GAsyncResult *res,
+                   gpointer      user_data)
+{
+	GError *error = NULL;
+	TrackerSparqlCursor *cursor = NULL;
+	GDir *dir = NULL;
+	GHashTable *table = NULL;
+	const gchar *name;
+	gchar *dirname = NULL;
+	GList *to_remove = NULL;
+
+	cursor = tracker_sparql_connection_query_finish (TRACKER_SPARQL_CONNECTION (source_object),
+	                                                 res,
+	                                                 &error);
+
+	if (error) {
+		goto on_error;
+	}
+
+	dirname = g_build_filename (g_get_user_cache_dir (),
+	                            "media-art",
+	                            NULL);
+
+	if (!g_file_test (dirname, G_FILE_TEST_EXISTS)) {
+		goto on_error;
+	}
+
+	dir = g_dir_open (dirname, 0, &error);
+
+	if (error) {
+		goto on_error;
+	}
+
+	table = g_hash_table_new_full (g_str_hash,
+	                               g_str_equal,
+	                               (GDestroyNotify) g_free,
+	                               (GDestroyNotify) g_free);
+
+	while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
+		const gchar *album = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+		gchar *album_stripped, *target = NULL;
+
+		if (album) {
+			album_stripped = tracker_albumart_strip_invalid_entities (album);
+			tracker_albumart_get_path (NULL,
+			                           album_stripped,
+			                           "album", NULL,
+			                           &target, NULL);
+			g_hash_table_replace (table, target, album_stripped);
+		}
+	}
+
+	/* Perhaps we should have an internal list of albumart files that we made,
+	 * instead of going over all the albumart (which could also have been made
+	 * by other softwares) */
+
+	for (name = g_dir_read_name (dir); name != NULL; name = g_dir_read_name (dir)) {
+		gpointer value;
+
+		value = g_hash_table_lookup (table, name);
+
+		if (!value) {
+			g_message ("Removing media-art file %s: no album exists that has "
+			           "more than one song for this media-art cache", name);
+			to_remove = g_list_prepend (to_remove, (gpointer) name);
+		}
+	}
+
+	g_list_foreach (to_remove, (GFunc) g_unlink, NULL);
+	g_list_free (to_remove);
+
+on_error:
+
+	g_free (dirname);
+
+	if (table) {
+		g_hash_table_unref (table);
+	}
+
+	if (cursor) {
+		g_object_unref (cursor);
+	}
+
+	if (dir) {
+		g_dir_close (dir);
+	}
+
+	if (error) {
+		g_critical ("Error running cleanup of media-art: %s",
+		            error->message ? error->message : "No error given");
+		g_error_free (error);
+	}
+}
+/**
+ * tracker_albumart_remove_add:
+ * @connection: SPARQL connection of this miner
+ * @uri: URI of the file
+ * @mime_type: mime-type of the file
+ *
+ * Adds a new request to tell the albumart subsystem that @uri was removed.
+ * Stored requests can be processed with tracker_thumbnailer_process().
+ *
+ * Returns: #TRUE if successfully stored to be reported, #FALSE otherwise.
+ *
+ * Since: 0.8
+ */
+gboolean
+tracker_albumart_remove_add (const gchar *uri,
+                             const gchar *mime_type)
+{
+	/* mime_type can be NULL */
+
+	g_return_val_if_fail (uri != NULL, FALSE);
+
+	if (!mime_type || (g_str_has_prefix (mime_type, "video/") || g_str_has_prefix (mime_type, "audio/"))) {
+		had_any = TRUE;
+	}
+
+	return TRUE;
+}
+
+/**
+ * tracker_albumart_process:
+ *
+ * Process all stored albumart requests.
+ *
+ * Since: 0.10
+ */
+void
+tracker_albumart_check_cleanup (TrackerSparqlConnection *connection)
+{
+	if (had_any) {
+		tracker_sparql_connection_query_async (connection,
+		                                       "SELECT ?title WHERE { "
+		                                       "   ?mpiece nmm:musicAlbum ?album . "
+		                                       "   ?album nmm:albumTitle ?title "
+		                                       "}",
+		                                       NULL,
+		                                       on_query_finished,
+		                                       NULL);
+		had_any = FALSE;
+	}
+}
diff --git a/src/libtracker-miner/tracker-albumart.h b/src/libtracker-miner/tracker-albumart.h
new file mode 100644
index 0000000..49637c6
--- /dev/null
+++ b/src/libtracker-miner/tracker-albumart.h
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2008, Nokia <ivan frade nokia com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser 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
+ */
+
+#ifndef __LIBTRACKER_MINER_ALBUMART_H__
+#define __LIBTRACKER_MINER_ALBUMART_H__
+
+#include <libtracker-sparql/tracker-sparql.h>
+
+#if !defined (__LIBTRACKER_MINER_H_INSIDE__) && !defined (TRACKER_COMPILATION)
+#error "Only <libtracker-miner/tracker-miner.h> can be included directly."
+#endif
+
+G_BEGIN_DECLS
+
+gboolean tracker_albumart_remove_add    (const gchar             *uri,
+                                         const gchar             *mime_type);
+void     tracker_albumart_check_cleanup (TrackerSparqlConnection *connection);
+G_END_DECLS
+
+#endif /* __LIBTRACKER_MINER_ALBUMART_H__ */
diff --git a/src/libtracker-miner/tracker-miner-fs.c b/src/libtracker-miner/tracker-miner-fs.c
index bffbe79..40321bb 100644
--- a/src/libtracker-miner/tracker-miner-fs.c
+++ b/src/libtracker-miner/tracker-miner-fs.c
@@ -27,6 +27,7 @@
 #include "tracker-crawler.h"
 #include "tracker-marshal.h"
 #include "tracker-miner-fs.h"
+#include "tracker-albumart.h"
 #include "tracker-monitor.h"
 #include "tracker-utils.h"
 #include "tracker-thumbnailer.h"
@@ -1861,6 +1862,7 @@ item_remove (TrackerMinerFS *fs,
 	}
 
 	tracker_thumbnailer_remove_add (uri, mime);
+	tracker_albumart_remove_add (uri, mime);
 
 	g_free (mime);
 
@@ -2775,6 +2777,7 @@ item_queue_handlers_cb (gpointer user_data)
 		                                      "Queue handlers NONE");
 
 		tracker_thumbnailer_send ();
+		tracker_albumart_check_cleanup (tracker_miner_get_connection (TRACKER_MINER (fs)));
 		/* No more files left to process */
 		keep_processing = FALSE;
 		break;
diff --git a/src/libtracker-miner/tracker-miner.h b/src/libtracker-miner/tracker-miner.h
index d00be97..59775c0 100644
--- a/src/libtracker-miner/tracker-miner.h
+++ b/src/libtracker-miner/tracker-miner.h
@@ -25,6 +25,7 @@
 #include <libtracker-miner/tracker-crawler.h>
 #include <libtracker-miner/tracker-storage.h>
 #include <libtracker-miner/tracker-thumbnailer.h>
+#include <libtracker-miner/tracker-albumart.h>
 #include <libtracker-miner/tracker-network-provider.h>
 #include <libtracker-miner/tracker-password-provider.h>
 #include <libtracker-miner/tracker-miner-object.h>



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