summaryrefslogtreecommitdiff
path: root/libmediaart/queue.c
blob: 97a889f3d4f290837f21475ce0b3baa32975d130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * 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-sparql/tracker-sparql.h>

/**
 * SECTION:tracker-media-art
 * @title: Media art management
 * @short_description: Media art request and management.
 * @include: libtracker-miner/tracker-media-art.h
 *
 * This is a convenience API using D-Bus to talk to the media management service.
 **/

static gboolean had_any = FALSE;
static guint timer_id = 0;

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)) {
		/* Ignore this and just quit the function */
		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) NULL);

	while (tracker_sparql_cursor_next (cursor, NULL, NULL)) {
		gchar *target = NULL, *album_path = NULL;
		const gchar *album, *artist;

		album = tracker_sparql_cursor_get_string (cursor, 0, NULL);
		artist = tracker_sparql_cursor_get_value_type (cursor, 1) != TRACKER_SPARQL_VALUE_TYPE_UNBOUND ? tracker_sparql_cursor_get_string (cursor, 1, NULL) : NULL;

		/* The get_path API does stripping itself */
		tracker_media_art_get_path (artist,
		                            album,
		                            "album", NULL,
		                            &target, NULL);

		g_hash_table_replace (table, target, target);

		/* Also add the file to which the symlinks are made */
		tracker_media_art_get_path (NULL,
		                            album,
		                            "album", NULL,
		                            &album_path, NULL);


		g_hash_table_replace (table, album_path, album_path);
	}

	/* Perhaps we should have an internal list of media art files that we made,
	 * instead of going over all the media art (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;
		gchar *full;

		full = g_build_filename (dirname, name, NULL);

		value = g_hash_table_lookup (table, full);

		if (!value) {
			g_message ("Removing media-art file %s: no album exists that has "
			           "any songs for this media-art cache", name);
			to_remove = g_list_prepend (to_remove, (gpointer) full);
		} else {
			g_free (full);
		}
	}

	g_list_foreach (to_remove, (GFunc) g_unlink, NULL);
	g_list_foreach (to_remove, (GFunc) g_free, 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_media_art_queue_remove:
 * @uri: URI of the file
 * @mime_type: mime-type of the file
 *
 * Adds a new request to tell the media art subsystem that @uri was removed.
 * Stored requests can be processed with tracker_media_art_queue_empty().
 *
 * Returns: #TRUE if successfully stored to be reported, #FALSE otherwise.
 *
 * Since: 0.10.4
 */
gboolean
tracker_media_art_queue_remove (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;
}

static gboolean
on_timer_callback (gpointer data)
{
	TrackerSparqlConnection *connection = data;

	tracker_sparql_connection_query_async (connection,
	                                       "SELECT ?title nmm:artistName (?artist) WHERE { "
	                                       "  ?mpiece nmm:musicAlbum ?album . "
	                                       "  ?album nmm:albumTitle ?title . "
	                                       "  OPTIONAL { ?album nmm:albumArtist ?artist } "
	                                       "}",
	                                       NULL,
	                                       on_query_finished,
	                                       NULL);

	return FALSE;
}

static void
on_timer_destroy (gpointer data)
{
	TrackerSparqlConnection *connection = data;

	g_object_unref (connection);
	timer_id = 0;
}

/**
 * tracker_media_art_queue_execute:
 *
 * Process all stored media art requests.
 *
 * Since: 0.10.4
 */
void
tracker_media_art_queue_empty (TrackerSparqlConnection *connection)
{
	if (had_any && timer_id == 0) {

		timer_id = g_timeout_add_seconds_full (G_PRIORITY_LOW,
		                                       1800 /* Half an hour worth of seconds*/,
		                                       on_timer_callback,
		                                       g_object_ref (connection),
		                                       on_timer_destroy);

		had_any = FALSE;
	}
}