示例#1
0
/**
 * tracker_media_art_get_path:
 * @artist: the artist
 * @title: the title
 * @prefix: For example "album"
 * @uri: NULL or the uri of the file
 * @path: the location to store the local path
 * @local_uri: the location to store the local uri or NULL
 *
 * Get the path to media art for a given resource. Newly allocated data in
 * @path and @local_uri must be freed with g_free.
 *
 * Since: 0.10.14
 */
void
tracker_media_art_get_path (const gchar  *artist,
                            const gchar  *title,
                            const gchar  *prefix,
                            const gchar  *uri,
                            gchar       **path,
                            gchar       **local_uri)
{
    const gchar *space_checksum = "7215ee9c7d9dc229d2921a40e899ec5f";
    const gchar *a, *b;

    gchar *art_filename;
    gchar *dir;
    gchar *artist_down, *title_down;
    gchar *artist_stripped, *title_stripped;
    gchar *artist_norm, *title_norm;
    gchar *artist_checksum = NULL, *title_checksum = NULL;

    /* http://live.gnome.org/MediaArtStorageSpec */

    if (path) {
        *path = NULL;
    }

    if (local_uri) {
        *local_uri = NULL;
    }

    if (!artist && !title) {
        return;
    }

    if (artist) {
        artist_stripped = tracker_media_art_strip_invalid_entities (artist);
        artist_norm = g_utf8_normalize (artist_stripped, -1, G_NORMALIZE_NFKD);
        artist_down = g_utf8_strdown (artist_norm, -1);
        artist_checksum = media_art_checksum_for_data (G_CHECKSUM_MD5,
                          (const guchar *) artist_down,
                          strlen (artist_down));
    }

    if (title) {
        title_stripped = tracker_media_art_strip_invalid_entities (title);
        title_norm = g_utf8_normalize (title_stripped, -1, G_NORMALIZE_NFKD);
        title_down = g_utf8_strdown (title_norm, -1);
        title_checksum = media_art_checksum_for_data (G_CHECKSUM_MD5,
                         (const guchar *) title_down,
                         strlen (title_down));
    }

    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);
    }

    if (artist) {
        a = artist_checksum;
        b = title ? title_checksum : space_checksum;
    } else {
        a = title_checksum;
        b = space_checksum;
    }

    art_filename = g_strdup_printf ("%s-%s-%s.jpeg", prefix ? prefix : "album", a, b);

    if (artist) {
        g_free (artist_checksum);
        g_free (artist_stripped);
        g_free (artist_down);
        g_free (artist_norm);
    }

    if (title) {
        g_free (title_checksum);
        g_free (title_stripped);
        g_free (title_down);
        g_free (title_norm);
    }

    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);
}
示例#2
0
文件: cache.c 项目: GNOME/libmediaart
/**
 * media_art_get_file:
 * @artist: (allow-none): the artist
 * @title: (allow-none): the title
 * @prefix: (allow-none): the prefix for cache files, for example "album"
 * @cache_file: (out) (transfer full) (allow-none): a pointer to a
 * #GFile which represents the cached file for media art, or %NULL
 * a #GFile representing the user's cache path, or %NULL
 * #GFile representing the location of the local media art
 *
 * Gets the files pointing to cache files suitable for storing the media
 * art provided by the @artist, @title and @file arguments. @cache_file
 * will point to a location in the XDG user cache directory..
 *
 * The @cache_file relates to a symlink stored in XDG cache directories
 * for the user. A @cache_file would be expected to look like
 * <filename>file:///home/martyn/.cache/media-art/...</filename>. This
 * is normally the location that is most useful (assuming the cache
 * has been extracted in the first place).
 *
 * When done, both #GFile<!-- -->s must be freed with g_object_unref() if
 * non-%NULL.
 *
 * This operation should not use i/o, but it depends on the backend
 * GFile implementation.
 *
 * Returns: %TRUE if @cache_file was returned, otherwise %FALSE.
 *
 * Since: 0.2.0
 */
gboolean
media_art_get_file (const gchar  *artist,
                    const gchar  *title,
                    const gchar  *prefix,
                    GFile       **cache_file)
{
	const gchar *space_checksum = "7215ee9c7d9dc229d2921a40e899ec5f";
	const gchar *a, *b;

	gchar *art_filename;
	gchar *dir, *filename;
	gchar *artist_down, *title_down;
	gchar *artist_stripped, *title_stripped;
	gchar *artist_norm, *title_norm;
	gchar *artist_checksum = NULL, *title_checksum = NULL;

	/* http://live.gnome.org/MediaArtStorageSpec */

	if (cache_file) {
		*cache_file = NULL;
	}

	/* Rules:
	 * 1. artist OR title must be non-NULL.
	 * 2. cache_file must be non-NULL
	 */
	g_return_val_if_fail (artist != NULL || title != NULL, FALSE);
	g_return_val_if_fail (!G_IS_FILE (cache_file), FALSE);

	if (artist) {
		artist_stripped = media_art_strip_invalid_entities (artist);
		artist_norm = g_utf8_normalize (artist_stripped, -1, G_NORMALIZE_NFKD);
		artist_down = g_utf8_strdown (artist_norm, -1);
		artist_checksum = media_art_checksum_for_data (G_CHECKSUM_MD5,
		                                               (const guchar *) artist_down,
		                                               strlen (artist_down));
	}

	if (title) {
		title_stripped = media_art_strip_invalid_entities (title);
		title_norm = g_utf8_normalize (title_stripped, -1, G_NORMALIZE_NFKD);
		title_down = g_utf8_strdown (title_norm, -1);
		title_checksum = media_art_checksum_for_data (G_CHECKSUM_MD5,
		                                              (const guchar *) title_down,
		                                              strlen (title_down));
	}

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

	if (artist) {
		a = artist_checksum;
		b = title ? title_checksum : space_checksum;
	} else {
		a = title_checksum;
		b = space_checksum;
	}

	art_filename = g_strdup_printf ("%s-%s-%s.jpeg", prefix ? prefix : "album", a, b);

	if (artist) {
		g_free (artist_checksum);
		g_free (artist_stripped);
		g_free (artist_down);
		g_free (artist_norm);
	}

	if (title) {
		g_free (title_checksum);
		g_free (title_stripped);
		g_free (title_down);
		g_free (title_norm);
	}

	if (cache_file) {
		filename = g_build_filename (dir, art_filename, NULL);
		*cache_file = g_file_new_for_path (filename);
		g_free (filename);
	}

	g_free (dir);
	g_free (art_filename);

	return TRUE;
}