2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2004-02-24 00:41:20 +01:00
|
|
|
*
|
|
|
|
* 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.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* 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.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2004-02-24 00:41:20 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "song.h"
|
2009-02-25 16:44:06 +01:00
|
|
|
#include "uri.h"
|
2004-02-24 00:41:20 +01:00
|
|
|
#include "directory.h"
|
2008-10-14 11:10:49 +02:00
|
|
|
#include "mapper.h"
|
2008-08-26 08:27:08 +02:00
|
|
|
#include "decoder_list.h"
|
2009-02-15 17:48:37 +01:00
|
|
|
#include "decoder_plugin.h"
|
2009-02-28 16:44:41 +01:00
|
|
|
#include "tag_ape.h"
|
2009-01-17 13:23:42 +01:00
|
|
|
#include "tag_id3.h"
|
2009-02-15 17:48:37 +01:00
|
|
|
#include "tag.h"
|
2008-09-23 20:48:39 +02:00
|
|
|
|
2009-01-03 14:51:30 +01:00
|
|
|
#include <glib.h>
|
|
|
|
|
2008-12-29 17:28:32 +01:00
|
|
|
#include <assert.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
2009-02-15 17:48:37 +01:00
|
|
|
#include <string.h>
|
2008-12-29 17:28:32 +01:00
|
|
|
|
2008-10-08 11:05:25 +02:00
|
|
|
static struct song *
|
2008-10-08 10:49:05 +02:00
|
|
|
song_alloc(const char *url, struct directory *parent)
|
2008-10-06 18:46:52 +02:00
|
|
|
{
|
2008-10-07 21:20:34 +02:00
|
|
|
size_t urllen;
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song;
|
2008-10-07 21:20:34 +02:00
|
|
|
|
|
|
|
assert(url);
|
|
|
|
urllen = strlen(url);
|
|
|
|
assert(urllen);
|
2009-01-03 14:51:30 +01:00
|
|
|
song = g_malloc(sizeof(*song) - sizeof(song->url) + urllen + 1);
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
song->tag = NULL;
|
|
|
|
memcpy(song->url, url, urllen + 1);
|
2008-10-08 11:05:34 +02:00
|
|
|
song->parent = parent;
|
2009-07-06 11:32:31 +02:00
|
|
|
song->mtime = 0;
|
2008-10-06 18:46:52 +02:00
|
|
|
|
|
|
|
return song;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *
|
2008-10-08 11:05:25 +02:00
|
|
|
song_remote_new(const char *url)
|
|
|
|
{
|
|
|
|
return song_alloc(url, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
|
|
|
song_file_new(const char *path, struct directory *parent)
|
|
|
|
{
|
2008-10-15 22:35:13 +02:00
|
|
|
assert((parent == NULL) == (*path == '/'));
|
2008-10-08 11:05:25 +02:00
|
|
|
|
|
|
|
return song_alloc(path, parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct song *
|
|
|
|
song_file_load(const char *path, struct directory *parent)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song;
|
2008-10-08 11:06:27 +02:00
|
|
|
bool ret;
|
2004-06-02 04:07:07 +02:00
|
|
|
|
2008-10-15 22:35:13 +02:00
|
|
|
assert((parent == NULL) == (*path == '/'));
|
2009-01-04 17:26:15 +01:00
|
|
|
assert(!uri_has_scheme(path));
|
|
|
|
assert(strchr(path, '\n') == NULL);
|
2004-06-02 04:07:07 +02:00
|
|
|
|
2008-10-08 11:05:25 +02:00
|
|
|
song = song_file_new(path, parent);
|
|
|
|
|
2008-12-16 21:42:42 +01:00
|
|
|
//in archive ?
|
2009-01-17 19:56:36 +01:00
|
|
|
if (parent != NULL && parent->device == DEVICE_INARCHIVE) {
|
2008-12-16 21:42:42 +01:00
|
|
|
ret = song_file_update_inarchive(song);
|
|
|
|
} else {
|
|
|
|
ret = song_file_update(song);
|
|
|
|
}
|
2008-10-08 11:06:27 +02:00
|
|
|
if (!ret) {
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(song);
|
2008-10-08 11:05:25 +02:00
|
|
|
return NULL;
|
2004-03-10 03:38:31 +01:00
|
|
|
}
|
|
|
|
|
2004-02-24 00:41:20 +01:00
|
|
|
return song;
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
void
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
|
|
|
if (song->tag)
|
2008-08-29 09:38:21 +02:00
|
|
|
tag_free(song->tag);
|
2009-01-03 14:51:30 +01:00
|
|
|
g_free(song);
|
2004-04-10 19:56:01 +02:00
|
|
|
}
|
|
|
|
|
2009-01-17 13:23:42 +01:00
|
|
|
/**
|
|
|
|
* Attempts to load APE or ID3 tags from the specified file.
|
|
|
|
*/
|
|
|
|
static struct tag *
|
|
|
|
tag_load_fallback(const char *path)
|
|
|
|
{
|
|
|
|
struct tag *tag = tag_ape_load(path);
|
|
|
|
if (tag == NULL)
|
|
|
|
tag = tag_id3_load(path);
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The decoder plugin failed to load any tags: fall back to the APE or
|
|
|
|
* ID3 tag loader.
|
|
|
|
*/
|
|
|
|
static struct tag *
|
|
|
|
tag_fallback(const char *path, struct tag *tag)
|
|
|
|
{
|
|
|
|
struct tag *fallback = tag_load_fallback(path);
|
|
|
|
|
|
|
|
if (fallback != NULL) {
|
|
|
|
/* tag was successfully loaded: copy the song
|
|
|
|
duration, and destroy the old (empty) tag */
|
|
|
|
fallback->time = tag->time;
|
|
|
|
tag_free(tag);
|
|
|
|
return fallback;
|
|
|
|
} else
|
|
|
|
/* no APE/ID3 tag found: return the empty tag */
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2008-10-08 11:06:26 +02:00
|
|
|
bool
|
2008-10-08 11:05:34 +02:00
|
|
|
song_file_update(struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2009-01-04 17:26:22 +01:00
|
|
|
const char *suffix;
|
2009-01-02 10:48:55 +01:00
|
|
|
char *path_fs;
|
2008-11-01 14:51:41 +01:00
|
|
|
const struct decoder_plugin *plugin;
|
2008-10-13 16:56:00 +02:00
|
|
|
struct stat st;
|
2008-01-01 11:10:08 +01:00
|
|
|
|
2008-10-08 11:05:38 +02:00
|
|
|
assert(song_is_file(song));
|
2004-05-31 03:21:17 +02:00
|
|
|
|
2009-01-04 17:26:22 +01:00
|
|
|
/* check if there's a suffix and a plugin */
|
|
|
|
|
2009-01-04 17:46:42 +01:00
|
|
|
suffix = uri_get_suffix(song->url);
|
2009-01-04 17:26:22 +01:00
|
|
|
if (suffix == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
plugin = decoder_plugin_from_suffix(suffix, false);
|
|
|
|
if (plugin == NULL)
|
|
|
|
return false;
|
|
|
|
|
2009-01-02 10:48:55 +01:00
|
|
|
path_fs = map_song_fs(song);
|
2008-10-31 16:47:21 +01:00
|
|
|
if (path_fs == NULL)
|
|
|
|
return false;
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2008-10-08 11:05:38 +02:00
|
|
|
if (song->tag != NULL) {
|
|
|
|
tag_free(song->tag);
|
2004-05-13 20:16:03 +02:00
|
|
|
song->tag = NULL;
|
|
|
|
}
|
2004-03-10 10:55:54 +01:00
|
|
|
|
2009-01-02 10:48:55 +01:00
|
|
|
if (stat(path_fs, &st) < 0 || !S_ISREG(st.st_mode)) {
|
|
|
|
g_free(path_fs);
|
2008-10-13 16:56:00 +02:00
|
|
|
return false;
|
2009-01-02 10:48:55 +01:00
|
|
|
}
|
2008-10-13 16:56:00 +02:00
|
|
|
|
|
|
|
song->mtime = st.st_mtime;
|
|
|
|
|
2009-01-04 17:26:22 +01:00
|
|
|
do {
|
2008-10-31 16:47:21 +01:00
|
|
|
song->tag = plugin->tag_dup(path_fs);
|
2009-01-04 17:26:22 +01:00
|
|
|
if (song->tag != NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
plugin = decoder_plugin_from_suffix(suffix, true);
|
|
|
|
} while (plugin != NULL);
|
2008-10-08 11:05:38 +02:00
|
|
|
|
2009-01-17 13:23:42 +01:00
|
|
|
if (song->tag != NULL && tag_is_empty(song->tag))
|
|
|
|
song->tag = tag_fallback(path_fs, song->tag);
|
|
|
|
|
2009-01-02 10:48:55 +01:00
|
|
|
g_free(path_fs);
|
2008-10-13 16:56:00 +02:00
|
|
|
return song->tag != NULL;
|
2004-02-24 00:41:20 +01:00
|
|
|
}
|
|
|
|
|
2008-12-16 21:42:42 +01:00
|
|
|
bool
|
|
|
|
song_file_update_inarchive(struct song *song)
|
|
|
|
{
|
2009-01-04 17:26:22 +01:00
|
|
|
const char *suffix;
|
2008-12-16 21:42:42 +01:00
|
|
|
const struct decoder_plugin *plugin;
|
|
|
|
|
|
|
|
assert(song_is_file(song));
|
|
|
|
|
2009-01-04 17:26:22 +01:00
|
|
|
/* check if there's a suffix and a plugin */
|
|
|
|
|
2009-01-04 17:46:42 +01:00
|
|
|
suffix = uri_get_suffix(song->url);
|
2009-01-04 17:26:22 +01:00
|
|
|
if (suffix == NULL)
|
2008-12-16 21:42:42 +01:00
|
|
|
return false;
|
|
|
|
|
2009-01-04 17:26:22 +01:00
|
|
|
plugin = decoder_plugin_from_suffix(suffix, false);
|
|
|
|
if (plugin == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (song->tag != NULL)
|
2008-12-16 21:42:42 +01:00
|
|
|
tag_free(song->tag);
|
2009-01-04 17:26:22 +01:00
|
|
|
|
2008-12-16 21:42:42 +01:00
|
|
|
//accept every file that has music suffix
|
|
|
|
//because we dont support tag reading throught
|
|
|
|
//input streams
|
2009-01-04 17:26:22 +01:00
|
|
|
song->tag = tag_new();
|
|
|
|
|
|
|
|
return true;
|
2008-12-16 21:42:42 +01:00
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
char *
|
2009-01-04 19:09:34 +01:00
|
|
|
song_get_uri(const struct song *song)
|
2006-07-20 18:02:40 +02:00
|
|
|
{
|
2008-10-08 11:06:37 +02:00
|
|
|
assert(song != NULL);
|
2008-10-06 18:46:52 +02:00
|
|
|
assert(*song->url);
|
2008-08-26 08:27:11 +02:00
|
|
|
|
2009-01-04 19:08:52 +01:00
|
|
|
if (!song_in_database(song) || directory_is_root(song->parent))
|
2009-01-04 19:09:34 +01:00
|
|
|
return g_strdup(song->url);
|
2007-12-28 03:56:25 +01:00
|
|
|
else
|
2009-01-04 19:09:34 +01:00
|
|
|
return g_strconcat(directory_get_path(song->parent),
|
|
|
|
"/", song->url, NULL);
|
2004-11-11 03:36:25 +01:00
|
|
|
}
|