2004-02-24 00:41:20 +01:00
|
|
|
/* the Music Player Daemon (MPD)
|
|
|
|
* (c)2003-2004 by Warren Dukes (shank@mercury.chem.pitt.edu)
|
|
|
|
* 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
|
|
|
|
2004-03-10 10:55:54 +01:00
|
|
|
Song * newNullSong() {
|
2004-02-24 00:41:20 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-11-11 01:41:28 +01:00
|
|
|
Song * newSong(char * url, int type, Directory * parentDir) {
|
2004-06-02 04:07:07 +02:00
|
|
|
Song * song = NULL;
|
|
|
|
|
2005-09-08 23:08:02 +02:00
|
|
|
if(strchr(url, '\n')) {
|
2005-10-25 22:35:16 +02:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2004-05-13 20:16:03 +02:00
|
|
|
if(song->type == SONG_TYPE_FILE) {
|
2004-05-31 03:21:17 +02:00
|
|
|
InputPlugin * plugin;
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int next = 0;
|
|
|
|
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);
|
2004-05-31 03:21:17 +02:00
|
|
|
}
|
2004-05-13 20:16:03 +02:00
|
|
|
if(!song->tag || song->tag->time<0) {
|
|
|
|
freeSong(song);
|
|
|
|
song = NULL;
|
|
|
|
}
|
2004-03-10 03:38:31 +01:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-04-10 19:56:01 +02:00
|
|
|
void freeJustSong(Song * song) {
|
2004-11-11 03:36:25 +01:00
|
|
|
free(song->url);
|
2004-04-10 19:56:01 +02:00
|
|
|
if(song->tag) freeMpdTag(song->tag);
|
|
|
|
free(song);
|
2004-11-11 05:34:26 +01:00
|
|
|
getSongUrl(NULL);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
SongList * newSongList() {
|
2004-11-11 03:59:16 +01:00
|
|
|
return makeList((ListFreeDataFunc *)freeSong, 0);
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2004-11-11 03:36:25 +01:00
|
|
|
Song * addSongToList(SongList * list, char * url, char * utf8path,
|
|
|
|
int songType, Directory * parentDirectory)
|
2004-05-13 20:16:03 +02:00
|
|
|
{
|
2004-03-10 03:38:31 +01:00
|
|
|
Song * song = NULL;
|
2004-05-13 20:16:03 +02:00
|
|
|
|
2004-11-11 03:36:25 +01:00
|
|
|
switch(songType) {
|
2004-05-13 20:16:03 +02:00
|
|
|
case SONG_TYPE_FILE:
|
2006-03-16 07:52:46 +01: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:
|
|
|
|
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
|
|
|
|
|
|
|
if(song==NULL) return NULL;
|
2004-02-24 00:41:20 +01: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeSongList(SongList * list) {
|
|
|
|
freeList(list);
|
|
|
|
}
|
|
|
|
|
2004-11-11 01:41:28 +01:00
|
|
|
void printSongUrl(FILE * fp, Song * song) {
|
2004-11-12 19:02:26 +01:00
|
|
|
if(song->parentDir && song->parentDir->path) {
|
2004-11-11 05:34:26 +01:00
|
|
|
myfprintf(fp, "%s%s/%s\n", SONG_FILE,
|
|
|
|
getDirectoryPath(song->parentDir), song->url);
|
2004-11-11 01:41:28 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
myfprintf(fp, "%s%s\n", SONG_FILE, song->url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
int printSongInfo(FILE * fp, Song * song) {
|
2004-11-11 01:41:28 +01:00
|
|
|
printSongUrl(fp, song);
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
if(song->tag) printMpdTag(fp,song->tag);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int printSongInfoFromList(FILE * fp, SongList * list) {
|
|
|
|
ListNode * tempNode = list->firstNode;
|
|
|
|
|
|
|
|
while(tempNode!=NULL) {
|
|
|
|
printSongInfo(fp,(Song *)tempNode->data);
|
|
|
|
tempNode = tempNode->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void writeSongInfoFromList(FILE * fp, SongList * list) {
|
|
|
|
ListNode * tempNode = list->firstNode;
|
|
|
|
|
|
|
|
myfprintf(fp,"%s\n",SONG_BEGIN);
|
|
|
|
|
|
|
|
while(tempNode!=NULL) {
|
|
|
|
myfprintf(fp,"%s%s\n",SONG_KEY,tempNode->key);
|
|
|
|
printSongInfo(fp,(Song *)tempNode->data);
|
|
|
|
myfprintf(fp,"%s%li\n",SONG_MTIME,(long)((Song *)tempNode->data)->mtime);
|
|
|
|
tempNode = tempNode->nextNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
myfprintf(fp,"%s\n",SONG_END);
|
|
|
|
}
|
|
|
|
|
2004-04-10 19:56:01 +02:00
|
|
|
void insertSongIntoList(SongList * list, ListNode ** nextSongNode, char * key,
|
|
|
|
Song * song)
|
|
|
|
{
|
|
|
|
ListNode * nodeTemp;
|
|
|
|
int cmpRet= 0;
|
|
|
|
|
|
|
|
while(*nextSongNode && (cmpRet = strcmp(key,(*nextSongNode)->key)) > 0)
|
|
|
|
{
|
|
|
|
nodeTemp = (*nextSongNode)->nextNode;
|
|
|
|
deleteNodeFromList(list,*nextSongNode);
|
|
|
|
*nextSongNode = nodeTemp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!(*nextSongNode)) {
|
2004-11-11 03:59:16 +01:00
|
|
|
insertInList(list, song->url, (void *)song);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
else if(cmpRet == 0) {
|
|
|
|
Song * tempSong = (Song *)((*nextSongNode)->data);
|
|
|
|
if(tempSong->mtime != song->mtime) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else {
|
2004-11-15 20:29:20 +01:00
|
|
|
insertInListBeforeNode(list, *nextSongNode, -1, song->url,
|
2004-11-11 03:59:16 +01:00
|
|
|
(void *)song);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-10 22:58:27 +01:00
|
|
|
static int matchesAnMpdTagItemKey(char * buffer, int * itemType) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for(i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
|
|
|
if( 0 == strncmp(mpdTagItemKeys[i], buffer,
|
|
|
|
strlen(mpdTagItemKeys[i])))
|
|
|
|
{
|
|
|
|
*itemType = i;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-11-11 03:36:25 +01:00
|
|
|
void readSongInfoIntoList(FILE * fp, SongList * list, Directory * parentDir) {
|
2004-02-24 00:41:20 +01:00
|
|
|
char buffer[MAXPATHLEN+1024];
|
|
|
|
int bufferSize = MAXPATHLEN+1024;
|
|
|
|
Song * song = NULL;
|
2004-04-10 19:56:01 +02:00
|
|
|
ListNode * nextSongNode = list->firstNode;
|
|
|
|
ListNode * nodeTemp;
|
2004-11-10 22:58:27 +01:00
|
|
|
int itemType;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
while(myFgets(buffer,bufferSize,fp) && 0!=strcmp(SONG_END,buffer)) {
|
|
|
|
if(0==strncmp(SONG_KEY,buffer,strlen(SONG_KEY))) {
|
|
|
|
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
|
|
|
|
2004-03-10 10:55:54 +01:00
|
|
|
song = newNullSong();
|
2004-11-11 03:36:25 +01: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;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
else if(0==strncmp(SONG_FILE,buffer,strlen(SONG_FILE))) {
|
2004-11-11 03:36:25 +01:00
|
|
|
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
|
|
|
|
song->url = strdup(&(buffer[strlen(SONG_FILE)]));
|
|
|
|
*/
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
2004-11-10 22:58:27 +01:00
|
|
|
else if(matchesAnMpdTagItemKey(buffer, &itemType)) {
|
2004-06-06 18:42:14 +02:00
|
|
|
if(!song->tag) song->tag = newMpdTag();
|
2004-11-10 22:58:27 +01:00
|
|
|
addItemToMpdTag(song->tag, itemType,
|
|
|
|
&(buffer[strlen(mpdTagItemKeys[itemType])+2]));
|
2004-06-06 18:42:14 +02:00
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
else if(0==strncmp(SONG_TIME,buffer,strlen(SONG_TIME))) {
|
2004-03-11 01:16:49 +01:00
|
|
|
if(!song->tag) song->tag = newMpdTag();
|
|
|
|
song->tag->time = atoi(&(buffer[strlen(SONG_TIME)]));
|
2004-02-24 00:41:20 +01: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
|
|
|
}
|
|
|
|
else {
|
2004-03-09 21:55:51 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
while(nextSongNode) {
|
|
|
|
nodeTemp = nextSongNode->nextNode;
|
|
|
|
deleteNodeFromList(list,nextSongNode);
|
|
|
|
nextSongNode = nodeTemp;
|
|
|
|
}
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int updateSongInfo(Song * song) {
|
2004-05-13 20:16:03 +02:00
|
|
|
if(song->type == SONG_TYPE_FILE) {
|
2004-05-31 03:21:17 +02:00
|
|
|
InputPlugin * plugin;
|
2006-03-16 07:52:46 +01:00
|
|
|
unsigned int next = 0;
|
|
|
|
char * song_url = getSongUrl(song);
|
|
|
|
char * abs_path = rmp2amp(song_url);
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2004-05-13 20:16:03 +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-03-16 07:52:46 +01:00
|
|
|
while(!song->tag && (plugin = isMusic(song_url,
|
|
|
|
&(song->mtime), next++))) {
|
|
|
|
song->tag = plugin->tagDupFunc(abs_path);
|
2004-05-31 03:21:17 +02:00
|
|
|
}
|
2004-05-13 20:16:03 +02:00
|
|
|
if(!song->tag || song->tag->time<0) return -1;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
|
|
|
return 0;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Song * songDup(Song * song) {
|
|
|
|
Song * ret = malloc(sizeof(Song));
|
|
|
|
|
2004-11-11 03:36:25 +01:00
|
|
|
ret->url = strdup(song->url);
|
2004-02-24 00:41:20 +01:00
|
|
|
ret->mtime = song->mtime;
|
|
|
|
ret->tag = mpdTagDup(song->tag);
|
2004-05-13 20:16:03 +02:00
|
|
|
ret->type = song->type;
|
2004-11-11 03:36:25 +01:00
|
|
|
ret->parentDir = song->parentDir;
|
2004-02-24 00:41:20 +01:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2004-11-11 03:36:25 +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*/
|
2004-11-11 03:36:25 +01:00
|
|
|
char * getSongUrl(Song * song) {
|
|
|
|
static char * buffer = NULL;
|
|
|
|
static int bufferSize = 0;
|
|
|
|
static Song * lastSong = NULL;
|
|
|
|
int slen;
|
|
|
|
int dlen;
|
|
|
|
int size;
|
|
|
|
|
2004-11-11 05:34:26 +01:00
|
|
|
if(!song) {
|
|
|
|
lastSong = song;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-11-12 18:38:52 +01:00
|
|
|
if(!song->parentDir || !song->parentDir->path) return song->url;
|
2004-11-11 03:36:25 +01:00
|
|
|
|
2004-11-11 05:34:26 +01:00
|
|
|
/* be careful with this!*/
|
2004-11-11 03:36:25 +01:00
|
|
|
if(song == lastSong) return buffer;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
size = slen+dlen+2;
|
|
|
|
|
|
|
|
if(size > bufferSize) {
|
|
|
|
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] = '/';
|
|
|
|
strcpy(buffer+dlen+1, song->url);
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|