playlist_queue: resolve relative URIs, database lookup

Prepend the playlist's base URI to relative song URIs.  Look up songs
in the database (if the URI refers to a local song file).  Merge
existing database metadata with metadata from the playlist plugin.
This commit is contained in:
Max Kellermann 2009-12-26 12:55:24 +01:00
parent a038bca745
commit 201316cd67
2 changed files with 120 additions and 20 deletions

View File

@ -22,43 +22,139 @@
#include "playlist_list.h" #include "playlist_list.h"
#include "playlist_plugin.h" #include "playlist_plugin.h"
#include "stored_playlist.h" #include "stored_playlist.h"
#include "database.h"
#include "mapper.h" #include "mapper.h"
#include "song.h" #include "song.h"
#include "tag.h"
#include "uri.h" #include "uri.h"
#include "ls.h" #include "ls.h"
#include "input_stream.h" #include "input_stream.h"
/** static void
* Determins if it's allowed to add this song to the playlist. For merge_song_metadata(struct song *dest, const struct song *base,
* safety reasons, we disallow local files. const struct song *add)
*/
static inline bool
accept_song(const struct song *song)
{ {
return !song_is_file(song) && uri_has_scheme(song->uri) && dest->tag = base->tag != NULL
uri_supported_scheme(song->uri); ? (add->tag != NULL
? tag_merge(base->tag, add->tag)
: tag_dup(base->tag))
: (add->tag != NULL
? tag_dup(add->tag)
: NULL);
dest->mtime = base->mtime;
dest->start_ms = add->start_ms;
dest->end_ms = add->end_ms;
}
static struct song *
apply_song_metadata(struct song *dest, const struct song *src)
{
struct song *tmp;
assert(dest != NULL);
assert(src != NULL);
if (src->tag == NULL && src->start_ms == 0 && src->end_ms == 0)
return dest;
if (song_in_database(dest)) {
char *path_fs = map_song_fs(dest);
if (path_fs == NULL)
return dest;
tmp = song_file_new(path_fs, NULL);
merge_song_metadata(tmp, dest, src);
} else {
tmp = song_file_new(dest->uri, NULL);
merge_song_metadata(tmp, dest, src);
song_free(dest);
}
return tmp;
}
/**
* Verifies the song, returns NULL if it is unsafe. Translate the
* song to a new song object within the database, if it is a local
* file. The old song object is freed.
*/
static struct song *
check_translate_song(struct song *song, const char *base_uri)
{
struct song *dest;
char *uri;
if (song_in_database(song))
/* already ok */
return song;
if (uri_has_scheme(song->uri)) {
if (uri_supported_scheme(song->uri))
/* valid remote song */
return song;
else {
/* unsupported remote song */
song_free(song);
return NULL;
}
}
if (g_path_is_absolute(song->uri)) {
/* local files must be relative to the music
directory */
song_free(song);
return NULL;
}
if (base_uri != NULL)
uri = g_build_filename(base_uri, song->uri, NULL);
else
uri = g_strdup(song->uri);
if (uri_has_scheme(base_uri)) {
dest = song_remote_new(uri);
g_free(uri);
} else {
dest = db_get_song(uri);
g_free(uri);
if (dest == NULL) {
/* not found in database */
song_free(song);
return dest;
}
}
dest = apply_song_metadata(dest, song);
song_free(song);
return dest;
} }
enum playlist_result enum playlist_result
playlist_load_into_queue(struct playlist_provider *source, playlist_load_into_queue(const char *uri, struct playlist_provider *source,
struct playlist *dest) struct playlist *dest)
{ {
enum playlist_result result; enum playlist_result result;
struct song *song; struct song *song;
char *base_uri = uri != NULL ? g_path_get_dirname(uri) : NULL;
while ((song = playlist_plugin_read(source)) != NULL) { while ((song = playlist_plugin_read(source)) != NULL) {
if (!accept_song(song)) { song = check_translate_song(song, base_uri);
song_free(song); if (song == NULL)
continue; continue;
}
result = playlist_append_song(dest, song, NULL); result = playlist_append_song(dest, song, NULL);
if (result != PLAYLIST_RESULT_SUCCESS) { if (result != PLAYLIST_RESULT_SUCCESS) {
song_free(song); if (!song_in_database(song))
song_free(song);
g_free(base_uri);
return result; return result;
} }
} }
g_free(base_uri);
return PLAYLIST_RESULT_SUCCESS; return PLAYLIST_RESULT_SUCCESS;
} }
@ -93,7 +189,7 @@ playlist_open_remote_into_queue(const char *uri, struct playlist *dest)
} }
} }
result = playlist_load_into_queue(playlist, dest); result = playlist_load_into_queue(uri, playlist, dest);
playlist_plugin_close(playlist); playlist_plugin_close(playlist);
if (stream) if (stream)
@ -103,16 +199,17 @@ playlist_open_remote_into_queue(const char *uri, struct playlist *dest)
} }
static enum playlist_result static enum playlist_result
playlist_open_path_into_queue(const char *path_fs, struct playlist *dest) playlist_open_path_into_queue(const char *path_fs, const char *uri,
struct playlist *dest)
{ {
struct playlist_provider *playlist; struct playlist_provider *playlist;
struct input_stream is; struct input_stream is;
enum playlist_result result; enum playlist_result result;
if ((playlist = playlist_list_open_uri(path_fs)) != NULL) if ((playlist = playlist_list_open_uri(path_fs)) != NULL)
result = playlist_load_into_queue(playlist, dest); result = playlist_load_into_queue(uri, playlist, dest);
else if ((playlist = playlist_list_open_path(&is, path_fs)) != NULL) { else if ((playlist = playlist_list_open_path(&is, path_fs)) != NULL) {
result = playlist_load_into_queue(playlist, dest); result = playlist_load_into_queue(uri, playlist, dest);
input_stream_close(&is); input_stream_close(&is);
} else } else
return PLAYLIST_RESULT_NO_SUCH_LIST; return PLAYLIST_RESULT_NO_SUCH_LIST;
@ -139,7 +236,7 @@ playlist_open_local_into_queue(const char *uri, struct playlist *dest)
return PLAYLIST_RESULT_DISABLED; return PLAYLIST_RESULT_DISABLED;
path_fs = g_build_filename(playlist_directory_fs, uri, NULL); path_fs = g_build_filename(playlist_directory_fs, uri, NULL);
result = playlist_open_path_into_queue(path_fs, dest); result = playlist_open_path_into_queue(path_fs, NULL, dest);
g_free(path_fs); g_free(path_fs);
return result; return result;
@ -160,7 +257,7 @@ playlist_open_local_into_queue2(const char *uri, struct playlist *dest)
if (path_fs == NULL) if (path_fs == NULL)
return PLAYLIST_RESULT_NO_SUCH_LIST; return PLAYLIST_RESULT_NO_SUCH_LIST;
result = playlist_open_path_into_queue(path_fs, dest); result = playlist_open_path_into_queue(path_fs, uri, dest);
g_free(path_fs); g_free(path_fs);
return result; return result;

View File

@ -32,9 +32,12 @@ struct playlist;
/** /**
* Loads the contents of a playlist and append it to the specified * Loads the contents of a playlist and append it to the specified
* play queue. * play queue.
*
* @param uri the URI of the playlist, used to resolve relative song
* URIs
*/ */
enum playlist_result enum playlist_result
playlist_load_into_queue(struct playlist_provider *source, playlist_load_into_queue(const char *uri, struct playlist_provider *source,
struct playlist *dest); struct playlist *dest);
/** /**