2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
2006-07-14 21:37:45 +02:00
|
|
|
* (c)2003-2006 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"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#define SONG_KEY "key: "
|
|
|
|
#define SONG_FILE "file: "
|
|
|
|
#define SONG_TIME "Time: "
|
2004-11-11 05:34:26 +01:00
|
|
|
#define SONG_MTIME "mtime: "
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2004-11-11 03:36:25 +01:00
|
|
|
#include <assert.h>
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *newNullSong(void)
|
|
|
|
{
|
|
|
|
Song *song = malloc(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;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *newSong(char *url, int type, Directory * parentDir)
|
|
|
|
{
|
|
|
|
Song *song = NULL;
|
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
|
|
|
|
2004-11-11 03:36:25 +01:00
|
|
|
song->url = strdup(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;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *song_url = getSongUrl(song);
|
|
|
|
char *abs_path = rmp2amp(utf8ToFsCharset(song_url));
|
|
|
|
while (!song->tag && (plugin = isMusic(song_url,
|
|
|
|
&(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);
|
2004-11-11 05:34:26 +01:00
|
|
|
getSongUrl(NULL);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
SongList *newSongList(void)
|
|
|
|
{
|
|
|
|
return makeList((ListFreeDataFunc *) freeSong, 0);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
Song *addSongToList(SongList * list, char *url, char *utf8path,
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char buffer[MAXPATHLEN + 1024];
|
|
|
|
int bufferSize = MAXPATHLEN + 1024;
|
|
|
|
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))) {
|
|
|
|
if (song) {
|
|
|
|
insertSongIntoList(list, &nextSongNode,
|
|
|
|
song->url, song);
|
2004-04-10 19:56:01 +02:00
|
|
|
song = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-04-10 19:56:01 +02:00
|
|
|
|
2004-03-10 10:55:54 +01:00
|
|
|
song = newNullSong();
|
2006-07-20 18:02:40 +02:00
|
|
|
song->url = strdup(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))) {
|
|
|
|
if (!song) {
|
2004-02-24 00:41:20 +01:00
|
|
|
ERROR("Problems reading song info\n");
|
2004-04-03 01:34:16 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-11-11 03:36:25 +01:00
|
|
|
/* we don't need this info anymore
|
2006-07-20 18:02:40 +02:00
|
|
|
song->url = strdup(&(buffer[strlen(SONG_FILE)]));
|
|
|
|
*/
|
|
|
|
} 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') */
|
2006-07-20 18:02:40 +02:00
|
|
|
else if (*buffer) {
|
|
|
|
ERROR("songinfo: unknown line in db: %s\n", buffer);
|
2004-04-03 01:34:16 +02:00
|
|
|
exit(EXIT_FAILURE);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
}
|
2006-07-15 07:04:16 +02:00
|
|
|
|
2006-07-20 18:02:40 +02: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
|
|
|
song = NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
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;
|
2006-07-20 18:02:40 +02:00
|
|
|
char *song_url = getSongUrl(song);
|
|
|
|
char *abs_path = rmp2amp(song_url);
|
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
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
while (!song->tag && (plugin = isMusic(song_url,
|
|
|
|
&(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
|
|
|
}
|
|
|
|
|
2004-11-11 05:34:26 +01:00
|
|
|
/* pass song = NULL to reset, we do this freeJustSong(), so that if
|
|
|
|
* we free and recreate this memory we make sure to print it correctly*/
|
2006-07-20 18:02:40 +02:00
|
|
|
char *getSongUrl(Song * song)
|
|
|
|
{
|
|
|
|
static char *buffer = NULL;
|
2004-11-11 03:36:25 +01:00
|
|
|
static int bufferSize = 0;
|
2006-07-20 18:02:40 +02:00
|
|
|
static Song *lastSong = NULL;
|
2004-11-11 03:36:25 +01:00
|
|
|
int slen;
|
|
|
|
int dlen;
|
|
|
|
int size;
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!song) {
|
2004-11-11 05:34:26 +01:00
|
|
|
lastSong = song;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (!song->parentDir || !song->parentDir->path)
|
|
|
|
return song->url;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
/* be careful with this! */
|
|
|
|
if (song == lastSong)
|
|
|
|
return buffer;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
|
|
|
slen = strlen(song->url);
|
2004-11-11 05:34:26 +01:00
|
|
|
dlen = strlen(getDirectoryPath(song->parentDir));
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
size = slen + dlen + 2;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
if (size > bufferSize) {
|
2004-11-11 03:36:25 +01:00
|
|
|
buffer = realloc(buffer, size);
|
|
|
|
bufferSize = size;
|
|
|
|
}
|
|
|
|
|
2004-11-11 05:34:26 +01:00
|
|
|
strcpy(buffer, getDirectoryPath(song->parentDir));
|
2004-11-11 03:36:25 +01:00
|
|
|
buffer[dlen] = '/';
|
2006-07-20 18:02:40 +02:00
|
|
|
strcpy(buffer + dlen + 1, song->url);
|
2004-11-11 03:36:25 +01:00
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|