[tracker/cleanup-media-art] tmp



commit d8de8f0c9edc89aff08d2a90ef60fa971fb91307
Author: Philip Van Hoof <philip codeminded be>
Date:   Tue May 17 13:37:02 2011 +0200

    tmp

 src/libtracker-common/Makefile.am        |    6 +-
 src/libtracker-common/tracker-albumart.c |  293 ++++++++++++++++++++++++++++
 src/libtracker-common/tracker-albumart.h |   42 ++++
 src/libtracker-miner/Makefile.am         |    6 +-
 src/libtracker-miner/tracker-albumart.c  |  182 ++++++++++++++++++
 src/libtracker-miner/tracker-albumart.h  |   36 ++++
 src/libtracker-miner/tracker-miner-fs.c  |    2 +
 src/libtracker-miner/tracker-miner.h     |    1 +
 src/tracker-extract/tracker-albumart.c   |  304 +++---------------------------
 9 files changed, 590 insertions(+), 282 deletions(-)
---
diff --git a/src/libtracker-common/Makefile.am b/src/libtracker-common/Makefile.am
index c75deba..5bcd345 100644
--- a/src/libtracker-common/Makefile.am
+++ b/src/libtracker-common/Makefile.am
@@ -28,7 +28,8 @@ libtracker_common_la_SOURCES = \
 	tracker-type-utils.c \
 	tracker-utils.c \
 	tracker-crc32.c \
-	tracker-locale.c
+	tracker-locale.c \
+	tracker-albumart.c
 
 noinst_HEADERS = \
 	tracker-dbus.h \
@@ -45,7 +46,8 @@ noinst_HEADERS = \
 	tracker-type-utils.h \
 	tracker-utils.h \
 	tracker-crc32.h \
-	tracker-locale.h
+	tracker-locale.h \
+	tracker-albumart.h
 
 if HAVE_TRACKER_FTS
 libtracker_common_la_SOURCES += tracker-language.c
diff --git a/src/libtracker-common/tracker-albumart.c b/src/libtracker-common/tracker-albumart.c
new file mode 100644
index 0000000..c9c9e03
--- /dev/null
+++ b/src/libtracker-common/tracker-albumart.c
@@ -0,0 +1,293 @@
+/*
+ * 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 <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <sys/types.h>
+#include <utime.h>
+#include <time.h>
+
+#include <glib.h>
+#include <glib/gprintf.h>
+#include <glib/gstdio.h>
+#include <gio/gio.h>
+
+#include "tracker-file-utils.h"
+#include "tracker-date-time.h"
+#include "tracker-albumart.h"
+
+static gboolean
+albumart_strip_find_next_block (const gchar    *original,
+                                const gunichar  open_char,
+                                const gunichar  close_char,
+                                gint           *open_pos,
+                                gint           *close_pos)
+{
+	const gchar *p1, *p2;
+
+	if (open_pos) {
+		*open_pos = -1;
+	}
+
+	if (close_pos) {
+		*close_pos = -1;
+	}
+
+	p1 = g_utf8_strchr (original, -1, open_char);
+	if (p1) {
+		if (open_pos) {
+			*open_pos = p1 - original;
+		}
+
+		p2 = g_utf8_strchr (g_utf8_next_char (p1), -1, close_char);
+		if (p2) {
+			if (close_pos) {
+				*close_pos = p2 - original;
+			}
+
+			return TRUE;
+		}
+	}
+
+	return FALSE;
+}
+
+gchar *
+tracker_albumart_strip_invalid_entities (const gchar *original)
+{
+	GString *str_no_blocks;
+	gchar **strv;
+	gchar *str;
+	gboolean blocks_done = FALSE;
+	const gchar *p;
+	const gchar *invalid_chars = "()[]<>{}_! #$^&*+=|\\/\"'?~";
+	const gchar *invalid_chars_delimiter = "*";
+	const gchar *convert_chars = "\t";
+	const gchar *convert_chars_delimiter = " ";
+	const gunichar blocks[5][2] = {
+		{ '(', ')' },
+		{ '{', '}' },
+		{ '[', ']' },
+		{ '<', '>' },
+		{  0,   0  }
+	};
+
+	str_no_blocks = g_string_new ("");
+
+	p = original;
+
+	while (!blocks_done) {
+		gint pos1, pos2, i;
+
+		pos1 = -1;
+		pos2 = -1;
+
+		for (i = 0; blocks[i][0] != 0; i++) {
+			gint start, end;
+
+			/* Go through blocks, find the earliest block we can */
+			if (albumart_strip_find_next_block (p, blocks[i][0], blocks[i][1], &start, &end)) {
+				if (pos1 == -1 || start < pos1) {
+					pos1 = start;
+					pos2 = end;
+				}
+			}
+		}
+
+		/* If either are -1 we didn't find any */
+		if (pos1 == -1) {
+			/* This means no blocks were found */
+			g_string_append (str_no_blocks, p);
+			blocks_done = TRUE;
+		} else {
+			/* Append the test BEFORE the block */
+			if (pos1 > 0) {
+				g_string_append_len (str_no_blocks, p, pos1);
+			}
+
+			p = g_utf8_next_char (p + pos2);
+
+			/* Do same again for position AFTER block */
+			if (*p == '\0') {
+				blocks_done = TRUE;
+			}
+		}
+	}
+
+	/* Now convert chars to lower case */
+	str = g_utf8_strdown (str_no_blocks->str, -1);
+	g_string_free (str_no_blocks, TRUE);
+
+	/* Now strip invalid chars */
+	g_strdelimit (str, invalid_chars, *invalid_chars_delimiter);
+	strv = g_strsplit (str, invalid_chars_delimiter, -1);
+	g_free (str);
+	str = g_strjoinv (NULL, strv);
+	g_strfreev (strv);
+
+	/* Now convert chars */
+	g_strdelimit (str, convert_chars, *convert_chars_delimiter);
+	strv = g_strsplit (str, convert_chars_delimiter, -1);
+	g_free (str);
+	str = g_strjoinv (convert_chars_delimiter, strv);
+	g_strfreev (strv);
+
+	while (g_strrstr (str, "  ") != NULL) {
+		/* Now remove double spaces */
+		strv = g_strsplit (str, "  ", -1);
+		g_free (str);
+		str = g_strjoinv (" ", strv);
+		g_strfreev (strv);
+	}
+
+	/* Now strip leading/trailing white space */
+	g_strstrip (str);
+
+	return str;
+}
+
+static gchar *
+albumart_checksum_for_data (GChecksumType  checksum_type,
+                            const guchar  *data,
+                            gsize          length)
+{
+	GChecksum *checksum;
+	gchar *retval;
+
+	checksum = g_checksum_new (checksum_type);
+	if (!checksum) {
+		return NULL;
+	}
+
+	g_checksum_update (checksum, data, length);
+	retval = g_strdup (g_checksum_get_string (checksum));
+	g_checksum_free (checksum);
+
+	return retval;
+}
+
+void
+tracker_albumart_get_path (const gchar  *artist,
+                           const gchar  *album,
+                           const gchar  *prefix,
+                           const gchar  *uri,
+                           gchar       **path,
+                           gchar       **local_uri)
+{
+	gchar *art_filename;
+	gchar *dir;
+	gchar *artist_down, *album_down;
+	gchar *artist_stripped, *album_stripped;
+	gchar *artist_checksum, *album_checksum;
+
+	/* http://live.gnome.org/MediaArtStorageSpec */
+
+	if (path) {
+		*path = NULL;
+	}
+
+	if (local_uri) {
+		*local_uri = NULL;
+	}
+
+	if (!artist && !album) {
+		return;
+	}
+
+	if (!artist) {
+		artist_stripped = g_strdup (" ");
+	} else {
+		artist_stripped = tracker_albumart_strip_invalid_entities (artist);
+	}
+
+	if (!album) {
+		album_stripped = g_strdup (" ");
+	} else {
+		album_stripped = tracker_albumart_strip_invalid_entities (album);
+	}
+
+	artist_down = g_utf8_strdown (artist_stripped, -1);
+	album_down = g_utf8_strdown (album_stripped, -1);
+
+	/* g_print ("[%s] [%s]\n", artist_down, album_down); */
+
+	g_free (artist_stripped);
+	g_free (album_stripped);
+
+	dir = g_build_filename (g_get_user_cache_dir (),
+	                        "media-art",
+	                        NULL);
+
+	if (!g_file_test (dir, G_FILE_TEST_EXISTS)) {
+		g_mkdir_with_parents (dir, 0770);
+	}
+
+	artist_checksum = albumart_checksum_for_data (G_CHECKSUM_MD5,
+	                                              (const guchar *) artist_down,
+	                                              strlen (artist_down));
+	album_checksum = albumart_checksum_for_data (G_CHECKSUM_MD5,
+	                                             (const guchar *) album_down,
+	                                             strlen (album_down));
+
+	g_free (artist_down);
+	g_free (album_down);
+
+	art_filename = g_strdup_printf ("%s-%s-%s.jpeg",
+	                                prefix ? prefix : "album",
+	                                artist_checksum,
+	                                album_checksum);
+
+	if (path) {
+		*path = g_build_filename (dir, art_filename, NULL);
+	}
+
+	if (local_uri) {
+		gchar *local_dir;
+		GFile *file, *parent;
+
+		if (strstr (uri, "://")) {
+			file = g_file_new_for_uri (uri);
+		} else {
+			file = g_file_new_for_path (uri);
+		}
+
+		parent = g_file_get_parent (file);
+		if (parent) {
+			local_dir = g_file_get_uri (parent);
+
+			/* This is a URI, don't use g_build_filename here */
+			*local_uri = g_strdup_printf ("%s/.mediaartlocal/%s", local_dir, art_filename);
+
+			g_free (local_dir);
+			g_object_unref (parent);
+		}
+		g_object_unref (file);
+	}
+
+	g_free (dir);
+	g_free (art_filename);
+	g_free (artist_checksum);
+	g_free (album_checksum);
+}
diff --git a/src/libtracker-common/tracker-albumart.h b/src/libtracker-common/tracker-albumart.h
new file mode 100644
index 0000000..90f461b
--- /dev/null
+++ b/src/libtracker-common/tracker-albumart.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2010 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_COMMON_ALBUMART_H__
+#define __LIBTRACKER_COMMON_ALBUMART_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+#if !defined (__LIBTRACKER_COMMON_INSIDE__) && !defined (TRACKER_COMPILATION)
+#error "only <libtracker-common/tracker-common.h> must be included directly."
+#endif
+
+gchar * tracker_albumart_strip_invalid_entities (const gchar  *original);
+void    tracker_albumart_get_path               (const gchar  *artist,
+                                                 const gchar  *album,
+                                                 const gchar  *prefix,
+                                                 const gchar  *uri,
+                                                 gchar       **path,
+                                                 gchar       **local_uri);
+
+
+G_END_DECLS
+
+#endif /* __LIBTRACKER_COMMON_LOCALE_H__ */
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..0047ea0
--- /dev/null
+++ b/src/libtracker-miner/tracker-albumart.c
@@ -0,0 +1,182 @@
+/*
+ * 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 "tracker-albumart.h"
+
+#include <libtracker-common/tracker-albumart.h>
+#include <libtracker-sparql/tracker-sparql.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_cleanup (TrackerSparqlConnection *connection)
+{
+	if (had_any) {
+		GString *query = g_string_new ("SELECT ?title WHERE { "
+		                               "   ?mpiece nmm:musicAlbum ?album . "
+		                               "   ?album nmm:albumTitle ?title "
+		                               "}");
+
+		tracker_sparql_connection_query_async (connection,
+		                                       query->str,
+		                                       NULL,
+		                                       on_query_finished,
+		                                       NULL);
+
+		g_string_free (query, TRUE);
+		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..41157bc
--- /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_cleanup    (TrackerSparqlConnection *connection);
+G_END_DECLS
+
+#endif /* __LIBTRACKER_MINER_THUMBNAILER_H__ */
diff --git a/src/libtracker-miner/tracker-miner-fs.c b/src/libtracker-miner/tracker-miner-fs.c
index bffbe79..1f2c2e1 100644
--- a/src/libtracker-miner/tracker-miner-fs.c
+++ b/src/libtracker-miner/tracker-miner-fs.c
@@ -1861,6 +1861,7 @@ item_remove (TrackerMinerFS *fs,
 	}
 
 	tracker_thumbnailer_remove_add (uri, mime);
+	tracker_albumart_remove_add (uri, mime);
 
 	g_free (mime);
 
@@ -2775,6 +2776,7 @@ item_queue_handlers_cb (gpointer user_data)
 		                                      "Queue handlers NONE");
 
 		tracker_thumbnailer_send ();
+		tracker_albumart_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>
diff --git a/src/tracker-extract/tracker-albumart.c b/src/tracker-extract/tracker-albumart.c
index 6b7ba24..963da3e 100644
--- a/src/tracker-extract/tracker-albumart.c
+++ b/src/tracker-extract/tracker-albumart.c
@@ -37,6 +37,7 @@
 #include <libtracker-miner/tracker-miner.h>
 #include <libtracker-common/tracker-file-utils.h>
 #include <libtracker-common/tracker-date-time.h>
+#include <libtracker-common/tracker-albumart.h>
 
 #include "tracker-albumart.h"
 #include "tracker-extract.h"
@@ -65,259 +66,6 @@ albumart_queue_cb (GObject      *source_object,
                    GAsyncResult *res,
                    gpointer      user_data);
 
-static gboolean
-albumart_strip_find_next_block (const gchar    *original,
-                                const gunichar  open_char,
-                                const gunichar  close_char,
-                                gint           *open_pos,
-                                gint           *close_pos)
-{
-	const gchar *p1, *p2;
-
-	if (open_pos) {
-		*open_pos = -1;
-	}
-
-	if (close_pos) {
-		*close_pos = -1;
-	}
-
-	p1 = g_utf8_strchr (original, -1, open_char);
-	if (p1) {
-		if (open_pos) {
-			*open_pos = p1 - original;
-		}
-
-		p2 = g_utf8_strchr (g_utf8_next_char (p1), -1, close_char);
-		if (p2) {
-			if (close_pos) {
-				*close_pos = p2 - original;
-			}
-
-			return TRUE;
-		}
-	}
-
-	return FALSE;
-}
-
-static gchar *
-albumart_strip_invalid_entities (const gchar *original)
-{
-	GString *str_no_blocks;
-	gchar **strv;
-	gchar *str;
-	gboolean blocks_done = FALSE;
-	const gchar *p;
-	const gchar *invalid_chars = "()[]<>{}_! #$^&*+=|\\/\"'?~";
-	const gchar *invalid_chars_delimiter = "*";
-	const gchar *convert_chars = "\t";
-	const gchar *convert_chars_delimiter = " ";
-	const gunichar blocks[5][2] = {
-		{ '(', ')' },
-		{ '{', '}' },
-		{ '[', ']' },
-		{ '<', '>' },
-		{  0,   0  }
-	};
-
-	str_no_blocks = g_string_new ("");
-
-	p = original;
-
-	while (!blocks_done) {
-		gint pos1, pos2, i;
-
-		pos1 = -1;
-		pos2 = -1;
-
-		for (i = 0; blocks[i][0] != 0; i++) {
-			gint start, end;
-
-			/* Go through blocks, find the earliest block we can */
-			if (albumart_strip_find_next_block (p, blocks[i][0], blocks[i][1], &start, &end)) {
-				if (pos1 == -1 || start < pos1) {
-					pos1 = start;
-					pos2 = end;
-				}
-			}
-		}
-
-		/* If either are -1 we didn't find any */
-		if (pos1 == -1) {
-			/* This means no blocks were found */
-			g_string_append (str_no_blocks, p);
-			blocks_done = TRUE;
-		} else {
-			/* Append the test BEFORE the block */
-			if (pos1 > 0) {
-				g_string_append_len (str_no_blocks, p, pos1);
-			}
-
-			p = g_utf8_next_char (p + pos2);
-
-			/* Do same again for position AFTER block */
-			if (*p == '\0') {
-				blocks_done = TRUE;
-			}
-		}
-	}
-
-	/* Now convert chars to lower case */
-	str = g_utf8_strdown (str_no_blocks->str, -1);
-	g_string_free (str_no_blocks, TRUE);
-
-	/* Now strip invalid chars */
-	g_strdelimit (str, invalid_chars, *invalid_chars_delimiter);
-	strv = g_strsplit (str, invalid_chars_delimiter, -1);
-	g_free (str);
-	str = g_strjoinv (NULL, strv);
-	g_strfreev (strv);
-
-	/* Now convert chars */
-	g_strdelimit (str, convert_chars, *convert_chars_delimiter);
-	strv = g_strsplit (str, convert_chars_delimiter, -1);
-	g_free (str);
-	str = g_strjoinv (convert_chars_delimiter, strv);
-	g_strfreev (strv);
-
-	while (g_strrstr (str, "  ") != NULL) {
-		/* Now remove double spaces */
-		strv = g_strsplit (str, "  ", -1);
-		g_free (str);
-		str = g_strjoinv (" ", strv);
-		g_strfreev (strv);
-	}
-
-	/* Now strip leading/trailing white space */
-	g_strstrip (str);
-
-	return str;
-}
-
-static gchar *
-albumart_checksum_for_data (GChecksumType  checksum_type,
-                            const guchar  *data,
-                            gsize          length)
-{
-	GChecksum *checksum;
-	gchar *retval;
-
-	checksum = g_checksum_new (checksum_type);
-	if (!checksum) {
-		return NULL;
-	}
-
-	g_checksum_update (checksum, data, length);
-	retval = g_strdup (g_checksum_get_string (checksum));
-	g_checksum_free (checksum);
-
-	return retval;
-}
-
-static void
-albumart_get_path (const gchar  *artist,
-                   const gchar  *album,
-                   const gchar  *prefix,
-                   const gchar  *uri,
-                   gchar       **path,
-                   gchar       **local_uri)
-{
-	gchar *art_filename;
-	gchar *dir;
-	gchar *artist_down, *album_down;
-	gchar *artist_stripped, *album_stripped;
-	gchar *artist_checksum, *album_checksum;
-
-	/* http://live.gnome.org/MediaArtStorageSpec */
-
-	if (path) {
-		*path = NULL;
-	}
-
-	if (local_uri) {
-		*local_uri = NULL;
-	}
-
-	if (!artist && !album) {
-		return;
-	}
-
-	if (!artist) {
-		artist_stripped = g_strdup (" ");
-	} else {
-		artist_stripped = albumart_strip_invalid_entities (artist);
-	}
-
-	if (!album) {
-		album_stripped = g_strdup (" ");
-	} else {
-		album_stripped = albumart_strip_invalid_entities (album);
-	}
-
-	artist_down = g_utf8_strdown (artist_stripped, -1);
-	album_down = g_utf8_strdown (album_stripped, -1);
-
-	/* g_print ("[%s] [%s]\n", artist_down, album_down); */
-
-	g_free (artist_stripped);
-	g_free (album_stripped);
-
-	dir = g_build_filename (g_get_user_cache_dir (),
-	                        "media-art",
-	                        NULL);
-
-	if (!g_file_test (dir, G_FILE_TEST_EXISTS)) {
-		g_mkdir_with_parents (dir, 0770);
-	}
-
-	artist_checksum = albumart_checksum_for_data (G_CHECKSUM_MD5,
-	                                              (const guchar *) artist_down,
-	                                              strlen (artist_down));
-	album_checksum = albumart_checksum_for_data (G_CHECKSUM_MD5,
-	                                             (const guchar *) album_down,
-	                                             strlen (album_down));
-
-	g_free (artist_down);
-	g_free (album_down);
-
-	art_filename = g_strdup_printf ("%s-%s-%s.jpeg",
-	                                prefix ? prefix : "album",
-	                                artist_checksum,
-	                                album_checksum);
-
-	if (path) {
-		*path = g_build_filename (dir, art_filename, NULL);
-	}
-
-	if (local_uri) {
-		gchar *local_dir;
-		GFile *file, *parent;
-
-		if (strstr (uri, "://")) {
-			file = g_file_new_for_uri (uri);
-		} else {
-			file = g_file_new_for_path (uri);
-		}
-
-		parent = g_file_get_parent (file);
-		if (parent) {
-			local_dir = g_file_get_uri (parent);
-
-			/* This is a URI, don't use g_build_filename here */
-			*local_uri = g_strdup_printf ("%s/.mediaartlocal/%s", local_dir, art_filename);
-
-			g_free (local_dir);
-			g_object_unref (parent);
-		}
-		g_object_unref (file);
-	}
-
-	g_free (dir);
-	g_free (art_filename);
-	g_free (artist_checksum);
-	g_free (album_checksum);
-}
 
 static gboolean
 albumart_heuristic (const gchar *artist,
@@ -344,11 +92,11 @@ albumart_heuristic (const gchar *artist,
 	}
 
 	if (artist) {
-		artist_stripped = albumart_strip_invalid_entities (artist);
+		artist_stripped = tracker_albumart_strip_invalid_entities (artist);
 	}
 
 	if (album) {
-		album_stripped = albumart_strip_invalid_entities (album);
+		album_stripped = tracker_albumart_strip_invalid_entities (album);
 	}
 
 	/* Copy from local album art (.mediaartlocal) to spec */
@@ -361,10 +109,10 @@ albumart_heuristic (const gchar *artist,
 			g_debug ("Album art being copied from local (.mediaartlocal) file:'%s'",
 			         local_uri);
 
-			albumart_get_path (artist_stripped,
-			                   album_stripped,
-			                   "album", NULL,
-			                   &target, NULL);
+			tracker_albumart_get_path (artist_stripped,
+			                           album_stripped,
+			                           "album", NULL,
+			                           &target, NULL);
 			if (target) {
 				file = g_file_new_for_path (target);
 
@@ -462,12 +210,12 @@ albumart_heuristic (const gchar *artist,
 				if (g_str_has_suffix (name_strdown, "jpeg") ||
 				    g_str_has_suffix (name_strdown, "jpg")) {
 					if (!target) {
-						albumart_get_path (artist_stripped,
-						                   album_stripped,
-						                   "album",
-						                   NULL,
-						                   &target,
-						                   NULL);
+						tracker_albumart_get_path (artist_stripped,
+						                           album_stripped,
+						                           "album",
+						                           NULL,
+						                           &target,
+						                           NULL);
 					}
 
 					if (!file && target) {
@@ -493,12 +241,12 @@ albumart_heuristic (const gchar *artist,
 					gchar *found;
 
 					if (!target) {
-						albumart_get_path (artist_stripped,
-						                   album_stripped,
-						                   "album",
-						                   NULL,
-						                   &target,
-						                   NULL);
+						tracker_albumart_get_path (artist_stripped,
+						                           album_stripped,
+						                           "album",
+						                           NULL,
+						                           &target,
+						                           NULL);
 					}
 
 					found = g_build_filename (dirname, name, NULL);
@@ -552,7 +300,7 @@ albumart_set (const unsigned char *buffer,
 		return FALSE;
 	}
 
-	albumart_get_path (artist, album, "album", NULL, &local_path, NULL);
+	tracker_albumart_get_path (artist, album, "album", NULL, &local_path, NULL);
 
 	retval = tracker_albumart_buffer_to_jpeg (buffer, len, mime, local_path);
 
@@ -808,12 +556,12 @@ tracker_albumart_process (const unsigned char *buffer,
 
 	mtime = tracker_file_get_mtime_uri (filename_uri);
 
-	albumart_get_path (artist,
-	                   album,
-	                   "album",
-	                   filename_uri,
-	                   &art_path,
-	                   &local_uri);
+	tracker_albumart_get_path (artist,
+	                           album,
+	                           "album",
+	                           filename_uri,
+	                           &art_path,
+	                           &local_uri);
 
 	if (!art_path) {
 		g_debug ("Album art path could not be obtained, not processing any further");



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