Example #1
0
/**
 * media_art_remove:
 * @artist: artist the media art belongs to
 * @album: (allow-none): album the media art belongs or %NULL
 * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
 * @error: location to store the error occurring, or %NULL to ignore
 *
 * Removes media art for given album/artist provided.
 *
 * If @artist and @album are %NULL, ALL media art cache is removed.
 *
 * Returns: #TRUE on success, otherwise #FALSE where @error will be set.
 *
 * Since: 0.2.0
 */
gboolean
media_art_remove (const gchar   *artist,
                  const gchar   *album,
                  GCancellable  *cancellable,
                  GError       **error)
{
	GError *local_error = NULL;
	const gchar *name;
	GDir *dir;
	gchar *dirname;
	gboolean success = TRUE;

	g_return_val_if_fail (artist != NULL && artist[0] != '\0', FALSE);

	dirname = g_build_filename (g_get_user_cache_dir (), "media-art", NULL);

	dir = g_dir_open (dirname, 0, &local_error);
	if (!dir || local_error) {
		/* Nothing to do if there is no directory in the first place. */
		g_debug ("Removing media-art for artist:'%s', album:'%s': directory could not be opened, %s",
		         artist, album, local_error ? local_error->message : "no error given");

		g_clear_error (&local_error);

		if (dir) {
			g_dir_close (dir);
		}
		g_free (dirname);

		/* We wanted to remove media art, so if there is no
		 * media art, the caller has achieved what they wanted.
		 */
		return TRUE;
	}

	/* NOTE: We expect to not find some of these paths for
	 * artist/album conbinations, so don't error in those
	 * cases...
	 */
	if (artist || album) {
		gchar *target = NULL;
		gint removed = 0;

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

		if (target) {
			if (g_unlink (target) != 0) {
				g_debug ("Could not delete file '%s'", target);
			} else {
				g_message ("Removed media-art for artist:'%s', album:'%s': deleting file '%s'",
				           artist, album, target);
				removed++;
			}

			g_free (target);
		}

		/* Add the album path also (to which the symlinks are made) */
		if (album) {
			media_art_get_path (NULL, album, "album", &target);
			if (target) {
				if (g_unlink (target) != 0) {
					g_debug ("Could not delete file '%s'", target);
				} else {
					g_message ("Removed media-art for album:'%s': deleting file '%s'",
					           album, target);
					removed++;
				}

				g_free (target);
			}
		}

		success = removed > 0;
	} else {
		for (name = g_dir_read_name (dir);
		     name != NULL;
		     name = g_dir_read_name (dir)) {
			gchar *target;

			target = g_build_filename (dirname, name, NULL);

			if (g_unlink (target) != 0) {
				g_warning ("Could not delete file '%s'", target);
				success = FALSE;
			} else {
				g_message ("Removing all media-art: deleted file '%s'", target);
			}

			g_free (target);
		}
	}

	if (!success) {
		g_set_error_literal (error,
		                     G_IO_ERROR,
		                     G_IO_ERROR_FAILED,
		                     _("Could not remove one or more files from media art cache"));
	}

	g_dir_close (dir);
	g_free (dirname);

	return success;
}
Example #2
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 */
		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 */
		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);
	}
}
Example #3
0
/**
 * media_art_remove:
 * @artist: artist the media art belongs to
 * @album: (allow-none): album the media art belongs or %NULL
 *
 * Removes media art for given album/artist/etc provided.
 *
 * Returns: #TRUE on success, otherwise #FALSE.
 *
 * Since: 0.2.0
 */
gboolean
media_art_remove (const gchar *artist,
                  const gchar *album)
{
	GError *error = NULL;
	GHashTable *table = NULL;
	const gchar *name;
	GDir *dir;
	gchar *dirname;
	GList *to_remove = NULL;
	gchar *target = NULL;
	gchar *album_path = NULL;

	g_return_if_fail (artist != NULL && artist[0] != '\0');

	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 */
		g_debug ("Nothing to do, media-art cache directory '%s' doesn't exist", dirname);
		g_free (dirname);
		return TRUE;
	}

	dir = g_dir_open (dirname, 0, &error);

	if (error) {
		g_critical ("Call to g_dir_open() failed, %s", error->message);

		g_error_free (error);
		g_free (dirname);

		if (dir) {
			g_dir_close (dir);
		}

		return FALSE;
	}

	table = g_hash_table_new_full (g_str_hash,
	                               g_str_equal,
	                               (GDestroyNotify) g_free,
	                               (GDestroyNotify) NULL);

	/* The get_path API does stripping itself */
	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 */
	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 with songs for this media-art cache", name);
			to_remove = g_list_prepend (to_remove, (gpointer) full);
		} else {
			g_free (full);
		}
	}

	if (dir) {
		g_dir_close (dir);
	}

	g_free (dirname);

	g_list_foreach (to_remove, (GFunc) g_unlink, NULL);
	g_list_foreach (to_remove, (GFunc) g_free, NULL);
	g_list_free (to_remove);

	if (table) {
		g_hash_table_unref (table);
	}

	return TRUE;
}