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 "log.h"
|
2004-03-10 03:38:31 +01:00
|
|
|
#include "path.h"
|
2004-03-10 10:55:54 +01:00
|
|
|
#include "playlist.h"
|
2008-08-26 08:27:08 +02:00
|
|
|
#include "decoder_list.h"
|
2008-08-26 08:27:08 +02:00
|
|
|
#include "decoder_api.h"
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2008-01-03 08:29:49 +01:00
|
|
|
#include "os_compat.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
|
2008-10-06 18:46:52 +02:00
|
|
|
Song *
|
2008-10-06 18:52:13 +02:00
|
|
|
song_alloc(const char *url, struct _Directory *parent)
|
2008-10-06 18:46:52 +02:00
|
|
|
{
|
|
|
|
size_t urllen = strlen(url);
|
2008-10-06 18:48:45 +02:00
|
|
|
Song *song = xmalloc(sizeof(*song) - sizeof(song->url) + urllen + 1);
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
song->tag = NULL;
|
|
|
|
memcpy(song->url, url, urllen + 1);
|
|
|
|
song->parentDir = parent;
|
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2008-10-06 18:52:13 +02:00
|
|
|
Song *newSong(const char *url, 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
|
|
|
|
2008-10-06 18:52:13 +02:00
|
|
|
song = song_alloc(url, parentDir);
|
2004-11-11 01:41:28 +01:00
|
|
|
|
2008-10-06 18:52:13 +02:00
|
|
|
if (song_is_file(song)) {
|
2008-08-26 08:27:08 +02:00
|
|
|
struct decoder_plugin *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++))) {
|
2008-09-29 15:54:27 +02:00
|
|
|
song->tag = plugin->tag_dup(abs_path);
|
2006-07-20 18:02:40 +02:00
|
|
|
}
|
|
|
|
if (!song->tag || song->tag->time < 0) {
|
2008-10-06 18:41:41 +02:00
|
|
|
freeJustSong(song);
|
2004-05-13 20:16:03 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (song->tag)
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(song->tag);
|
2004-04-10 19:56:01 +02:00
|
|
|
free(song);
|
|
|
|
}
|
|
|
|
|
2006-07-20 18:02:40 +02:00
|
|
|
int updateSongInfo(Song * song)
|
|
|
|
{
|
2008-10-06 18:52:13 +02:00
|
|
|
if (song_is_file(song)) {
|
2008-08-26 08:27:08 +02:00
|
|
|
struct decoder_plugin *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)
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(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++))) {
|
2008-09-29 15:54:27 +02:00
|
|
|
song->tag = plugin->tag_dup(abs_path);
|
2006-07-15 07:04:16 +02:00
|
|
|
}
|
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;
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2008-10-06 18:46:52 +02:00
|
|
|
assert(*song->url);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
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
|
|
|
}
|