1 /* the Music Player Daemon (MPD)
2 * Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
3 * This project's homepage is: 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.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "directory.h"
27 #include "inputPlugin.h"
28 #include "myfprintf.h"
30 #define SONG_KEY "key: "
31 #define SONG_MTIME "mtime: "
37 Song *newNullSong(void)
39 Song *song = xmalloc(sizeof(Song));
43 song->type = SONG_TYPE_FILE;
44 song->parentDir = NULL;
49 Song *newSong(char *url, int type, Directory * parentDir)
53 if (strchr(url, '\n')) {
54 DEBUG("newSong: '%s' is not a valid uri\n", url);
60 song->url = xstrdup(url);
62 song->parentDir = parentDir;
64 assert(type == SONG_TYPE_URL || parentDir);
66 if (song->type == SONG_TYPE_FILE) {
68 unsigned int next = 0;
69 char *song_url = getSongUrl(song);
70 char *abs_path = rmp2amp(utf8ToFsCharset(song_url));
71 while (!song->tag && (plugin = isMusic(song_url,
74 song->tag = plugin->tagDupFunc(abs_path);
76 if (!song->tag || song->tag->time < 0) {
85 void freeSong(Song * song)
87 deleteASongFromPlaylist(song);
91 void freeJustSong(Song * song)
95 freeMpdTag(song->tag);
100 SongList *newSongList(void)
102 return makeList((ListFreeDataFunc *) freeSong, 0);
105 Song *addSongToList(SongList * list, char *url, char *utf8path,
106 int songType, Directory * parentDirectory)
112 if (isMusic(utf8path, NULL, 0)) {
113 song = newSong(url, songType, parentDirectory);
117 song = newSong(url, songType, parentDirectory);
120 DEBUG("addSongToList: Trying to add an invalid song type\n");
126 insertInList(list, song->url, (void *)song);
131 void freeSongList(SongList * list)
136 void printSongUrl(int fd, Song * song)
138 if (song->parentDir && song->parentDir->path) {
139 fdprintf(fd, "%s%s/%s\n", SONG_FILE,
140 getDirectoryPath(song->parentDir), song->url);
142 fdprintf(fd, "%s%s\n", SONG_FILE, song->url);
146 int printSongInfo(int fd, Song * song)
148 printSongUrl(fd, song);
151 printMpdTag(fd, song->tag);
156 int printSongInfoFromList(int fd, SongList * list)
158 ListNode *tempNode = list->firstNode;
160 while (tempNode != NULL) {
161 printSongInfo(fd, (Song *) tempNode->data);
162 tempNode = tempNode->nextNode;
168 void writeSongInfoFromList(FILE * fp, SongList * list)
170 ListNode *tempNode = list->firstNode;
172 fprintf(fp, "%s\n", SONG_BEGIN);
174 while (tempNode != NULL) {
175 fprintf(fp, "%s%s\n", SONG_KEY, tempNode->key);
177 printSongInfo(fileno(fp), (Song *) tempNode->data);
178 fprintf(fp, "%s%li\n", SONG_MTIME,
179 (long)((Song *) tempNode->data)->mtime);
180 tempNode = tempNode->nextNode;
183 fprintf(fp, "%s\n", SONG_END);
186 static void insertSongIntoList(SongList * list, ListNode ** nextSongNode,
187 char *key, Song * song)
193 && (cmpRet = strcmp(key, (*nextSongNode)->key)) > 0) {
194 nodeTemp = (*nextSongNode)->nextNode;
195 deleteNodeFromList(list, *nextSongNode);
196 *nextSongNode = nodeTemp;
199 if (!(*nextSongNode)) {
200 insertInList(list, song->url, (void *)song);
201 } else if (cmpRet == 0) {
202 Song *tempSong = (Song *) ((*nextSongNode)->data);
203 if (tempSong->mtime != song->mtime) {
204 freeMpdTag(tempSong->tag);
205 tempSong->tag = song->tag;
206 tempSong->mtime = song->mtime;
210 *nextSongNode = (*nextSongNode)->nextNode;
212 insertInListBeforeNode(list, *nextSongNode, -1, song->url,
217 static int matchesAnMpdTagItemKey(char *buffer, int *itemType)
221 for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
222 if (0 == strncmp(mpdTagItemKeys[i], buffer,
223 strlen(mpdTagItemKeys[i]))) {
232 void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir)
234 char buffer[MAXPATHLEN + 1024];
235 int bufferSize = MAXPATHLEN + 1024;
237 ListNode *nextSongNode = list->firstNode;
241 while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) {
242 if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) {
244 insertSongIntoList(list, &nextSongNode,
249 song = newNullSong();
250 song->url = xstrdup(buffer + strlen(SONG_KEY));
251 song->type = SONG_TYPE_FILE;
252 song->parentDir = parentDir;
253 } else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
255 FATAL("Problems reading song info\n");
256 /* we don't need this info anymore
257 song->url = xstrdup(&(buffer[strlen(SONG_FILE)]));
259 } else if (matchesAnMpdTagItemKey(buffer, &itemType)) {
261 song->tag = newMpdTag();
262 addItemToMpdTag(song->tag, itemType,
264 [strlen(mpdTagItemKeys[itemType]) +
266 } else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) {
268 song->tag = newMpdTag();
269 song->tag->time = atoi(&(buffer[strlen(SONG_TIME)]));
270 } else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) {
271 song->mtime = atoi(&(buffer[strlen(SONG_MTIME)]));
273 /* ignore empty lines (starting with '\0') */
275 FATAL("songinfo: unknown line in db: %s\n", buffer);
279 insertSongIntoList(list, &nextSongNode, song->url, song);
283 while (nextSongNode) {
284 nodeTemp = nextSongNode->nextNode;
285 deleteNodeFromList(list, nextSongNode);
286 nextSongNode = nodeTemp;
290 int updateSongInfo(Song * song)
292 if (song->type == SONG_TYPE_FILE) {
294 unsigned int next = 0;
295 char *song_url = getSongUrl(song);
296 char *abs_path = rmp2amp(song_url);
299 freeMpdTag(song->tag);
303 while (!song->tag && (plugin = isMusic(song_url,
306 song->tag = plugin->tagDupFunc(abs_path);
308 if (!song->tag || song->tag->time < 0)
315 /* pass song = NULL to reset, we do this freeJustSong(), so that if
316 * we free and recreate this memory we make sure to print it correctly*/
317 char *getSongUrl(Song * song)
320 static int bufferSize;
321 static Song *lastSong;
331 if (!song->parentDir || !song->parentDir->path)
334 /* be careful with this! */
335 if (song == lastSong)
338 slen = strlen(song->url);
339 dlen = strlen(getDirectoryPath(song->parentDir));
341 size = slen + dlen + 2;
343 if (size > bufferSize) {
344 buffer = xrealloc(buffer, size);
348 strcpy(buffer, getDirectoryPath(song->parentDir));
350 strcpy(buffer + dlen + 1, song->url);