2009-03-13 18:43:16 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2003-2009 The Music Player Daemon Project
|
|
|
|
* http://www.musicpd.org
|
2008-09-07 13:35:01 +02: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.
|
2008-09-07 13:35:01 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "song_save.h"
|
2008-10-08 10:49:11 +02:00
|
|
|
#include "song.h"
|
2008-09-07 13:36:05 +02:00
|
|
|
#include "tag_save.h"
|
2008-09-07 13:35:01 +02:00
|
|
|
#include "directory.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "tag.h"
|
2009-03-15 18:23:00 +01:00
|
|
|
|
|
|
|
#include <glib.h>
|
2008-09-07 13:35:01 +02:00
|
|
|
|
2009-01-03 14:52:56 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-03-15 18:23:00 +01:00
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
#define G_LOG_DOMAIN "song"
|
|
|
|
|
2008-09-07 13:35:01 +02:00
|
|
|
#define SONG_KEY "key: "
|
|
|
|
#define SONG_MTIME "mtime: "
|
|
|
|
|
2009-07-05 08:29:52 +02:00
|
|
|
static GQuark
|
|
|
|
song_save_quark(void)
|
|
|
|
{
|
|
|
|
return g_quark_from_static_string("song_save");
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static void
|
|
|
|
song_save_url(FILE *fp, struct song *song)
|
2008-09-07 13:36:05 +02:00
|
|
|
{
|
2008-10-08 11:05:34 +02:00
|
|
|
if (song->parent != NULL && song->parent->path != NULL)
|
2008-09-07 13:36:05 +02:00
|
|
|
fprintf(fp, SONG_FILE "%s/%s\n",
|
2008-10-08 11:07:58 +02:00
|
|
|
directory_get_path(song->parent), song->url);
|
2008-09-07 13:36:05 +02:00
|
|
|
else
|
|
|
|
fprintf(fp, SONG_FILE "%s\n",
|
|
|
|
song->url);
|
|
|
|
}
|
|
|
|
|
2008-10-07 22:10:42 +02:00
|
|
|
static int
|
2008-10-08 10:49:11 +02:00
|
|
|
song_save(struct song *song, void *data)
|
2008-09-07 13:36:05 +02:00
|
|
|
{
|
2008-10-07 22:10:42 +02:00
|
|
|
FILE *fp = data;
|
|
|
|
|
|
|
|
fprintf(fp, SONG_KEY "%s\n", song->url);
|
|
|
|
|
2008-09-07 13:36:05 +02:00
|
|
|
song_save_url(fp, song);
|
|
|
|
|
|
|
|
if (song->tag != NULL)
|
|
|
|
tag_save(fp, song->tag);
|
2008-10-07 22:10:42 +02:00
|
|
|
|
|
|
|
fprintf(fp, SONG_MTIME "%li\n", (long)song->mtime);
|
|
|
|
|
|
|
|
return 0;
|
2008-09-07 13:36:05 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
void songvec_save(FILE *fp, struct songvec *sv)
|
2008-09-07 13:35:01 +02:00
|
|
|
{
|
|
|
|
fprintf(fp, "%s\n", SONG_BEGIN);
|
2008-10-07 22:10:42 +02:00
|
|
|
songvec_for_each(sv, song_save, fp);
|
2008-09-07 13:35:01 +02:00
|
|
|
fprintf(fp, "%s\n", SONG_END);
|
|
|
|
}
|
|
|
|
|
2008-10-08 10:49:11 +02:00
|
|
|
static void
|
2009-07-05 08:29:50 +02:00
|
|
|
commit_song(struct songvec *sv, struct song *newsong)
|
2008-09-07 13:35:01 +02:00
|
|
|
{
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *existing = songvec_find(sv, newsong->url);
|
2008-09-23 20:48:39 +02:00
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
songvec_add(sv, newsong);
|
|
|
|
if (newsong->tag)
|
|
|
|
tag_end_add(newsong->tag);
|
|
|
|
} else { /* prevent dupes, just update the existing song info */
|
|
|
|
if (existing->mtime != newsong->mtime) {
|
2009-04-28 19:57:49 +02:00
|
|
|
if (existing->tag != NULL)
|
|
|
|
tag_free(existing->tag);
|
2008-09-23 20:48:39 +02:00
|
|
|
if (newsong->tag)
|
|
|
|
tag_end_add(newsong->tag);
|
|
|
|
existing->tag = newsong->tag;
|
|
|
|
existing->mtime = newsong->mtime;
|
|
|
|
newsong->tag = NULL;
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|
2008-10-08 11:05:34 +02:00
|
|
|
song_free(newsong);
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 13:43:57 +01:00
|
|
|
static char *
|
2009-07-05 08:29:50 +02:00
|
|
|
parse_tag_value(char *buffer, enum tag_type *type_r)
|
2008-09-07 13:35:01 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < TAG_NUM_OF_ITEM_TYPES; i++) {
|
2009-02-27 09:01:55 +01:00
|
|
|
size_t len = strlen(tag_item_names[i]);
|
2009-01-13 23:43:16 +01:00
|
|
|
|
2009-02-27 09:01:55 +01:00
|
|
|
if (0 == strncmp(tag_item_names[i], buffer, len) &&
|
2009-01-14 13:44:14 +01:00
|
|
|
buffer[len] == ':') {
|
2009-07-05 08:29:50 +02:00
|
|
|
*type_r = i;
|
2009-01-14 13:44:14 +01:00
|
|
|
return g_strchug(buffer + len + 1);
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-14 13:43:57 +01:00
|
|
|
return NULL;
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|
|
|
|
|
2009-07-05 08:29:52 +02:00
|
|
|
bool
|
|
|
|
songvec_load(FILE *fp, struct songvec *sv, struct directory *parent,
|
|
|
|
GError **error_r)
|
2008-09-07 13:35:01 +02:00
|
|
|
{
|
|
|
|
char buffer[MPD_PATH_MAX + 1024];
|
2008-10-08 10:49:11 +02:00
|
|
|
struct song *song = NULL;
|
2009-07-05 08:29:50 +02:00
|
|
|
enum tag_type type;
|
2009-01-14 13:43:57 +01:00
|
|
|
const char *value;
|
2008-09-07 13:35:01 +02:00
|
|
|
|
2008-12-28 19:54:49 +01:00
|
|
|
while (fgets(buffer, sizeof(buffer), fp) &&
|
|
|
|
!g_str_has_prefix(buffer, SONG_END)) {
|
|
|
|
g_strchomp(buffer);
|
|
|
|
|
2008-09-07 13:35:01 +02:00
|
|
|
if (0 == strncmp(SONG_KEY, buffer, strlen(SONG_KEY))) {
|
2008-09-23 20:48:39 +02:00
|
|
|
if (song)
|
2009-07-05 08:29:50 +02:00
|
|
|
commit_song(sv, song);
|
2008-09-07 13:35:01 +02:00
|
|
|
|
2008-10-08 11:05:25 +02:00
|
|
|
song = song_file_new(buffer + strlen(SONG_KEY),
|
2008-10-08 11:05:34 +02:00
|
|
|
parent);
|
2008-09-07 13:35:01 +02:00
|
|
|
} else if (*buffer == 0) {
|
|
|
|
/* ignore empty lines (starting with '\0') */
|
|
|
|
} else if (song == NULL) {
|
2009-07-05 08:29:52 +02:00
|
|
|
g_set_error(error_r, song_save_quark(), 0,
|
|
|
|
"Problems reading song info");
|
|
|
|
return false;
|
2008-09-07 13:35:01 +02:00
|
|
|
} else if (0 == strncmp(SONG_FILE, buffer, strlen(SONG_FILE))) {
|
2008-10-06 18:46:52 +02:00
|
|
|
/* we don't need this info anymore */
|
2009-07-05 08:29:50 +02:00
|
|
|
} else if ((value = parse_tag_value(buffer,
|
|
|
|
&type)) != NULL) {
|
2008-09-07 13:35:01 +02:00
|
|
|
if (!song->tag) {
|
|
|
|
song->tag = tag_new();
|
|
|
|
tag_begin_add(song->tag);
|
|
|
|
}
|
|
|
|
|
2009-07-05 08:29:50 +02:00
|
|
|
tag_add_item(song->tag, type, value);
|
2008-09-07 13:35:01 +02:00
|
|
|
} else if (0 == strncmp(SONG_TIME, buffer, strlen(SONG_TIME))) {
|
|
|
|
if (!song->tag) {
|
|
|
|
song->tag = tag_new();
|
|
|
|
tag_begin_add(song->tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
song->tag->time = atoi(&(buffer[strlen(SONG_TIME)]));
|
|
|
|
} else if (0 == strncmp(SONG_MTIME, buffer, strlen(SONG_MTIME))) {
|
|
|
|
song->mtime = atoi(&(buffer[strlen(SONG_MTIME)]));
|
2009-07-05 08:29:52 +02:00
|
|
|
} else {
|
|
|
|
g_set_error(error_r, song_save_quark(), 0,
|
|
|
|
"unknown line in db: %s", buffer);
|
|
|
|
return false;
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 20:48:39 +02:00
|
|
|
if (song)
|
2009-07-05 08:29:50 +02:00
|
|
|
commit_song(sv, song);
|
2009-07-05 08:29:52 +02:00
|
|
|
|
|
|
|
return true;
|
2008-09-07 13:35:01 +02:00
|
|
|
}
|