2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2007-04-05 05:22:33 +02:00
|
|
|
* Copyright (C) 2003-2007 by Warren Dukes (warren.dukes@gmail.com)
|
2004-02-24 00:41:20 +01:00
|
|
|
* This project's homepage is: http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "song.h"
|
|
|
|
#include "ls.h"
|
|
|
|
#include "directory.h"
|
|
|
|
#include "utils.h"
|
|
|
|
#include "tag.h"
|
|
|
|
#include "log.h"
|
2004-03-10 03:38:31 +01:00
|
|
|
#include "path.h"
|
2004-03-10 10:55:54 +01:00
|
|
|
#include "playlist.h"
|
2004-05-31 03:21:17 +02:00
|
|
|
#include "inputPlugin.h"
|
2006-08-26 08:25:57 +02:00
|
|
|
#include "myfprintf.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#define SONG_KEY "key: "
|
2004-11-11 05:34:26 +01:00
|
|
|
#define SONG_MTIME "mtime: "
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *newNullSong(void)
|
|
|
|
{
|
2006-08-26 08:25:57 +02:00
|
|
|
Song *song = xmalloc(sizeof(Song));
|
2004-03-10 10:55:54 +01:00
|
|
|
|
|
|
|
song->tag = NULL;
|
2004-11-11 03:36:25 +01:00
|
|
|
song->url = NULL;
|
2004-05-13 20:16:03 +02:00
|
|
|
song->type = SONG_TYPE_FILE;
|
2004-11-11 01:41:28 +01:00
|
|
|
song->parentDir = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2004-03-10 10:55:54 +01:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
Song *newSong(const char *url, int type, Directory * parentDir)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-03-26 11:37:36 +01:00
|
|
|
Song *song;
|
2004-06-02 04:07:07 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (strchr(url, '\n')) {
|
|
|
|
DEBUG("newSong: '%s' is not a valid uri\n", url);
|
2005-09-08 23:08:02 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
2004-06-02 04:07:07 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
song = newNullSong();
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2006-08-26 08:25:57 +02:00
|
|
|
song->url = xstrdup(url);
|
2004-05-13 20:16:03 +02:00
|
|
|
song->type = type;
|
2004-11-11 01:41:28 +01:00
|
|
|
song->parentDir = parentDir;
|
|
|
|
|
|
|
|
assert(type == SONG_TYPE_URL || parentDir);
|
2004-03-10 03:38:31 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->type == SONG_TYPE_FILE) {
|
|
|
|
InputPlugin *plugin;
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int next = 0;
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
|
|
|
char *abs_path = rmp2amp_r(path_max_tmp,
|
|
|
|
get_song_url(path_max_tmp, song));
|
|
|
|
|
|
|
|
while (!song->tag && (plugin = isMusic(abs_path,
|
2006-07-20 18:02:40 +02:00
|
|
|
&(song->mtime),
|
|
|
|
next++))) {
|
|
|
|
song->tag = plugin->tagDupFunc(abs_path);
|
|
|
|
}
|
|
|
|
if (!song->tag || song->tag->time < 0) {
|
2004-05-13 20:16:03 +02:00
|
|
|
freeSong(song);
|
|
|
|
song = NULL;
|
|
|
|
}
|
2004-03-10 03:38:31 +01:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freeSong(Song * song)
|
|
|
|
{
|
2004-03-10 10:55:54 +01:00
|
|
|
deleteASongFromPlaylist(song);
|
2004-11-11 03:36:25 +01:00
|
|
|
freeJustSong(song);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freeJustSong(Song * song)
|
|
|
|
{
|
2004-11-11 03:36:25 +01:00
|
|
|
free(song->url);
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->tag)
|
|
|
|
freeMpdTag(song->tag);
|
2004-04-10 19:56:01 +02:00
|
|
|
free(song);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
SongList *newSongList(void)
|
|
|
|
{
|
|
|
|
return makeList((ListFreeDataFunc *) freeSong, 0);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-02-05 11:17:33 +01:00
|
|
|
Song *addSongToList(SongList * list, const char *url, const char *utf8path,
|
2006-07-20 18:02:40 +02:00
|
|
|
int songType, Directory * parentDirectory)
|
2004-05-13 20:16:03 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *song = NULL;
|
2004-05-13 20:16:03 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
switch (songType) {
|
2004-05-13 20:16:03 +02:00
|
|
|
case SONG_TYPE_FILE:
|
2006-07-20 18:02:40 +02:00
|
|
|
if (isMusic(utf8path, NULL, 0)) {
|
2004-11-11 03:36:25 +01:00
|
|
|
song = newSong(url, songType, parentDirectory);
|
2004-05-13 20:16:03 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SONG_TYPE_URL:
|
2004-11-11 03:36:25 +01:00
|
|
|
song = newSong(url, songType, parentDirectory);
|
2004-05-13 20:16:03 +02:00
|
|
|
break;
|
2005-09-08 23:08:02 +02:00
|
|
|
default:
|
2006-07-20 18:02:40 +02:00
|
|
|
DEBUG("addSongToList: Trying to add an invalid song type\n");
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-03-10 03:38:31 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song == NULL)
|
|
|
|
return NULL;
|
2006-07-15 07:04:16 +02:00
|
|
|
|
2004-11-11 03:59:16 +01:00
|
|
|
insertInList(list, song->url, (void *)song);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void freeSongList(SongList * list)
|
|
|
|
{
|
2004-02-24 00:41:20 +01:00
|
|
|
freeList(list);
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
void printSongUrl(int fd, Song * song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (song->parentDir && song->parentDir->path) {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "%s%s/%s\n", SONG_FILE,
|
2006-07-20 18:02:40 +02:00
|
|
|
getDirectoryPath(song->parentDir), song->url);
|
|
|
|
} else {
|
2006-07-30 05:43:38 +02:00
|
|
|
fdprintf(fd, "%s%s\n", SONG_FILE, song->url);
|
2004-11-11 01:41:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int printSongInfo(int fd, Song * song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2006-07-30 05:43:38 +02:00
|
|
|
printSongUrl(fd, song);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->tag)
|
2006-07-30 05:43:38 +02:00
|
|
|
printMpdTag(fd, song->tag);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-30 05:43:38 +02:00
|
|
|
int printSongInfoFromList(int fd, SongList * list)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
ListNode *tempNode = list->firstNode;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (tempNode != NULL) {
|
2006-07-30 05:43:38 +02:00
|
|
|
printSongInfo(fd, (Song *) tempNode->data);
|
2004-02-24 00:41:20 +01:00
|
|
|
tempNode = tempNode->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void writeSongInfoFromList(FILE * fp, SongList * list)
|
|
|
|
{
|
|
|
|
ListNode *tempNode = list->firstNode;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "%s\n", SONG_BEGIN);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (tempNode != NULL) {
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "%s%s\n", SONG_KEY, tempNode->key);
|
|
|
|
fflush(fp);
|
2006-07-30 05:43:38 +02:00
|
|
|
printSongInfo(fileno(fp), (Song *) tempNode->data);
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "%s%li\n", SONG_MTIME,
|
2006-07-20 18:02:40 +02:00
|
|
|
(long)((Song *) tempNode->data)->mtime);
|
2004-02-24 00:41:20 +01:00
|
|
|
tempNode = tempNode->nextNode;
|
|
|
|
}
|
|
|
|
|
2006-07-31 01:32:39 +02:00
|
|
|
fprintf(fp, "%s\n", SONG_END);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static void insertSongIntoList(SongList * list, ListNode ** nextSongNode,
|
|
|
|
char *key, Song * song)
|
2004-04-10 19:56:01 +02:00
|
|
|
{
|
2006-07-20 18:02:40 +02:00
|
|
|
ListNode *nodeTemp;
|
|
|
|
int cmpRet = 0;
|
2004-04-10 19:56:01 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (*nextSongNode
|
|
|
|
&& (cmpRet = strcmp(key, (*nextSongNode)->key)) > 0) {
|
2004-04-10 19:56:01 +02:00
|
|
|
nodeTemp = (*nextSongNode)->nextNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
deleteNodeFromList(list, *nextSongNode);
|
2004-04-10 19:56:01 +02:00
|
|
|
*nextSongNode = nodeTemp;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!(*nextSongNode)) {
|
2004-11-11 03:59:16 +01:00
|
|
|
insertInList(list, song->url, (void *)song);
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (cmpRet == 0) {
|
|
|
|
Song *tempSong = (Song *) ((*nextSongNode)->data);
|
|
|
|
if (tempSong->mtime != song->mtime) {
|
2004-04-10 19:56:01 +02:00
|
|
|
freeMpdTag(tempSong->tag);
|
|
|
|
tempSong->tag = song->tag;
|
|
|
|
tempSong->mtime = song->mtime;
|
|
|
|
song->tag = NULL;
|
|
|
|
}
|
2004-04-11 02:52:05 +02:00
|
|
|
freeJustSong(song);
|
2004-04-10 19:56:01 +02:00
|
|
|
*nextSongNode = (*nextSongNode)->nextNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else {
|
|
|
|
insertInListBeforeNode(list, *nextSongNode, -1, song->url,
|
|
|
|
(void *)song);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
static int matchesAnMpdTagItemKey(char *buffer, int *itemType)
|
|
|
|
{
|
2004-11-10 22:58:27 +01:00
|
|
|
int i;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if (0 == strncmp(mpdTagItemKeys[i], buffer,
|
|
|
|
strlen(mpdTagItemKeys[i]))) {
|
2004-11-10 22:58:27 +01:00
|
|
|
*itemType = i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir)
|
|
|
|
{
|
2007-12-28 03:56:25 +01:00
|
|
|
char buffer[MPD_PATH_MAX + 1024];
|
|
|
|
int bufferSize = MPD_PATH_MAX + 1024;
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *song = NULL;
|
|
|
|
ListNode *nextSongNode = list->firstNode;
|
|
|
|
ListNode *nodeTemp;
|
2004-11-10 22:58:27 +01:00
|
|
|
int itemType;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (myFgets(buffer, bufferSize, fp) && 0 != strcmp(SONG_END, buffer)) {
|
|
|
|
if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) {
|
2008-03-26 11:37:36 +01:00
|
|
|
if (song)
|
2006-07-20 18:02:40 +02:00
|
|
|
insertSongIntoList(list, &nextSongNode,
|
|
|
|
song->url, song);
|
2004-04-10 19:56:01 +02:00
|
|
|
|
2004-03-10 10:55:54 +01:00
|
|
|
song = newNullSong();
|
2006-08-26 08:25:57 +02:00
|
|
|
song->url = xstrdup(buffer + strlen(SONG_KEY));
|
2004-05-13 20:16:03 +02:00
|
|
|
song->type = SONG_TYPE_FILE;
|
2004-11-11 03:36:25 +01:00
|
|
|
song->parentDir = parentDir;
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
|
2007-05-26 20:15:54 +02:00
|
|
|
if (!song)
|
|
|
|
FATAL("Problems reading song info\n");
|
2004-11-11 03:36:25 +01:00
|
|
|
/* we don't need this info anymore
|
2006-08-26 08:25:57 +02:00
|
|
|
song->url = xstrdup(&(buffer[strlen(SONG_FILE)]));
|
2006-07-20 18:02:40 +02:00
|
|
|
*/
|
|
|
|
} else if (matchesAnMpdTagItemKey(buffer, &itemType)) {
|
|
|
|
if (!song->tag)
|
|
|
|
song->tag = newMpdTag();
|
2004-11-10 22:58:27 +01:00
|
|
|
addItemToMpdTag(song->tag, itemType,
|
2006-07-20 18:02:40 +02:00
|
|
|
&(buffer
|
|
|
|
[strlen(mpdTagItemKeys[itemType]) +
|
|
|
|
2]));
|
|
|
|
} else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) {
|
|
|
|
if (!song->tag)
|
|
|
|
song->tag = newMpdTag();
|
2004-03-11 01:16:49 +01:00
|
|
|
song->tag->time = atoi(&(buffer[strlen(SONG_TIME)]));
|
2006-07-20 18:02:40 +02:00
|
|
|
} else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) {
|
2004-11-10 22:58:27 +01:00
|
|
|
song->mtime = atoi(&(buffer[strlen(SONG_MTIME)]));
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-05-08 10:34:14 +02:00
|
|
|
/* ignore empty lines (starting with '\0') */
|
2007-05-26 20:15:54 +02:00
|
|
|
else if (*buffer)
|
|
|
|
FATAL("songinfo: unknown line in db: %s\n", buffer);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2006-07-15 07:04:16 +02:00
|
|
|
|
2008-03-26 11:37:36 +01:00
|
|
|
if (song)
|
2004-11-11 03:36:25 +01:00
|
|
|
insertSongIntoList(list, &nextSongNode, song->url, song);
|
2004-04-10 19:56:01 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (nextSongNode) {
|
2004-04-10 19:56:01 +02:00
|
|
|
nodeTemp = nextSongNode->nextNode;
|
2006-07-20 18:02:40 +02:00
|
|
|
deleteNodeFromList(list, nextSongNode);
|
2004-04-10 19:56:01 +02:00
|
|
|
nextSongNode = nodeTemp;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int updateSongInfo(Song * song)
|
|
|
|
{
|
|
|
|
if (song->type == SONG_TYPE_FILE) {
|
|
|
|
InputPlugin *plugin;
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int next = 0;
|
2007-12-28 03:56:25 +01:00
|
|
|
char path_max_tmp[MPD_PATH_MAX];
|
2008-01-01 11:10:08 +01:00
|
|
|
char abs_path[MPD_PATH_MAX];
|
|
|
|
|
|
|
|
utf8_to_fs_charset(abs_path, get_song_url(path_max_tmp, song));
|
|
|
|
rmp2amp_r(abs_path, abs_path);
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (song->tag)
|
|
|
|
freeMpdTag(song->tag);
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2004-05-13 20:16:03 +02:00
|
|
|
song->tag = NULL;
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
while (!song->tag && (plugin = isMusic(abs_path,
|
2006-07-20 18:02:40 +02:00
|
|
|
&(song->mtime),
|
|
|
|
next++))) {
|
2006-07-15 07:04:16 +02:00
|
|
|
song->tag = plugin->tagDupFunc(abs_path);
|
|
|
|
}
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!song->tag || song->tag->time < 0)
|
|
|
|
return -1;
|
2004-05-13 20:16:03 +02:00
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2007-12-28 03:56:25 +01:00
|
|
|
char *get_song_url(char *path_max_tmp, Song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2007-12-28 03:56:25 +01:00
|
|
|
if (!song)
|
2004-11-11 05:34:26 +01:00
|
|
|
return NULL;
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!song->parentDir || !song->parentDir->path)
|
2007-12-28 03:56:25 +01:00
|
|
|
strcpy(path_max_tmp, song->url);
|
|
|
|
else
|
|
|
|
pfx_dir(path_max_tmp, song->url, strlen(song->url),
|
|
|
|
getDirectoryPath(song->parentDir),
|
|
|
|
strlen(getDirectoryPath(song->parentDir)));
|
|
|
|
return path_max_tmp;
|
2004-11-11 03:36:25 +01:00
|
|
|
}
|