configure.ac: Move FAAD to Decoder Plugins, add header.
[mpd-mk.git] / src / playlist_song.c
blobfede0b3a177e0d19f5baf3139b6da2918cdab1d3
1 /*
2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "config.h"
21 #include "playlist_song.h"
22 #include "database.h"
23 #include "mapper.h"
24 #include "song.h"
25 #include "uri.h"
26 #include "ls.h"
27 #include "tag.h"
29 #include <assert.h>
30 #include <string.h>
32 static void
33 merge_song_metadata(struct song *dest, const struct song *base,
34 const struct song *add)
36 dest->tag = base->tag != NULL
37 ? (add->tag != NULL
38 ? tag_merge(base->tag, add->tag)
39 : tag_dup(base->tag))
40 : (add->tag != NULL
41 ? tag_dup(add->tag)
42 : NULL);
44 dest->mtime = base->mtime;
45 dest->start_ms = add->start_ms;
46 dest->end_ms = add->end_ms;
49 static struct song *
50 apply_song_metadata(struct song *dest, const struct song *src)
52 struct song *tmp;
54 assert(dest != NULL);
55 assert(src != NULL);
57 if (src->tag == NULL && src->start_ms == 0 && src->end_ms == 0)
58 return dest;
60 if (song_in_database(dest)) {
61 char *path_fs = map_song_fs(dest);
62 if (path_fs == NULL)
63 return dest;
65 tmp = song_file_new(path_fs, NULL);
66 merge_song_metadata(tmp, dest, src);
67 } else {
68 tmp = song_file_new(dest->uri, NULL);
69 merge_song_metadata(tmp, dest, src);
70 song_free(dest);
73 return tmp;
76 struct song *
77 playlist_check_translate_song(struct song *song, const char *base_uri)
79 struct song *dest;
81 if (song_in_database(song))
82 /* already ok */
83 return song;
85 char *uri = song->uri;
87 if (uri_has_scheme(uri)) {
88 if (uri_supported_scheme(uri))
89 /* valid remote song */
90 return song;
91 else {
92 /* unsupported remote song */
93 song_free(song);
94 return NULL;
98 if (g_path_is_absolute(uri)) {
99 /* XXX fs_charset vs utf8? */
100 char *prefix = base_uri != NULL
101 ? map_uri_fs(base_uri)
102 : map_directory_fs(db_get_root());
104 if (prefix == NULL || !g_str_has_prefix(uri, prefix) ||
105 uri[strlen(prefix)] != '/') {
106 /* local files must be relative to the music
107 directory */
108 g_free(prefix);
109 song_free(song);
110 return NULL;
113 uri += strlen(prefix) + 1;
114 g_free(prefix);
117 if (base_uri != NULL)
118 uri = g_build_filename(base_uri, uri, NULL);
119 else
120 uri = g_strdup(uri);
122 if (uri_has_scheme(base_uri)) {
123 dest = song_remote_new(uri);
124 g_free(uri);
125 } else {
126 dest = db_get_song(uri);
127 g_free(uri);
128 if (dest == NULL) {
129 /* not found in database */
130 song_free(song);
131 return dest;
135 dest = apply_song_metadata(dest, song);
136 song_free(song);
138 return dest;