2009-01-23 16:35:04 +01:00
|
|
|
/*
|
2011-01-29 10:13:54 +01:00
|
|
|
* Copyright (C) 2003-2011 The Music Player Daemon Project
|
2009-01-23 16:35:04 +01:00
|
|
|
* 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.
|
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.
|
2009-01-23 16:35:04 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2009-01-23 16:35:04 +01:00
|
|
|
#include "queue_save.h"
|
|
|
|
#include "queue.h"
|
|
|
|
#include "song.h"
|
2009-02-25 16:44:06 +01:00
|
|
|
#include "uri.h"
|
2009-01-23 16:35:04 +01:00
|
|
|
#include "database.h"
|
2010-07-25 12:21:47 +02:00
|
|
|
#include "song_save.h"
|
2012-08-21 19:17:14 +02:00
|
|
|
#include "text_file.h"
|
2009-01-23 16:35:04 +01:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-08-21 19:17:14 +02:00
|
|
|
#define PRIO_LABEL "Prio: "
|
|
|
|
|
2010-07-25 12:16:15 +02:00
|
|
|
static void
|
2010-07-25 12:21:47 +02:00
|
|
|
queue_save_database_song(FILE *fp, int idx, const struct song *song)
|
2010-07-25 12:16:15 +02:00
|
|
|
{
|
|
|
|
char *uri = song_get_uri(song);
|
|
|
|
|
|
|
|
fprintf(fp, "%i:%s\n", idx, uri);
|
|
|
|
g_free(uri);
|
|
|
|
}
|
|
|
|
|
2010-07-25 12:21:47 +02:00
|
|
|
static void
|
|
|
|
queue_save_full_song(FILE *fp, const struct song *song)
|
|
|
|
{
|
|
|
|
song_save(fp, song);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
queue_save_song(FILE *fp, int idx, const struct song *song)
|
|
|
|
{
|
|
|
|
if (song_in_database(song))
|
|
|
|
queue_save_database_song(fp, idx, song);
|
|
|
|
else
|
|
|
|
queue_save_full_song(fp, song);
|
|
|
|
}
|
|
|
|
|
2009-01-23 16:35:04 +01:00
|
|
|
void
|
|
|
|
queue_save(FILE *fp, const struct queue *queue)
|
|
|
|
{
|
2012-08-21 19:17:14 +02:00
|
|
|
for (unsigned i = 0; i < queue_length(queue); i++) {
|
|
|
|
uint8_t prio = queue_get_priority_at_position(queue, i);
|
|
|
|
if (prio != 0)
|
|
|
|
fprintf(fp, PRIO_LABEL "%u\n", prio);
|
|
|
|
|
2010-07-25 12:16:15 +02:00
|
|
|
queue_save_song(fp, i, queue_get(queue, i));
|
2012-08-21 19:17:14 +02:00
|
|
|
}
|
2009-01-23 16:35:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct song *
|
|
|
|
get_song(const char *uri)
|
|
|
|
{
|
2010-07-25 12:52:36 +02:00
|
|
|
return uri_has_scheme(uri)
|
|
|
|
? song_remote_new(uri)
|
|
|
|
: db_get_song(uri);
|
2009-01-23 16:35:04 +01:00
|
|
|
}
|
|
|
|
|
2010-07-25 12:43:05 +02:00
|
|
|
void
|
2010-07-25 12:21:47 +02:00
|
|
|
queue_load_song(FILE *fp, GString *buffer, const char *line,
|
|
|
|
struct queue *queue)
|
2009-01-23 16:35:04 +01:00
|
|
|
{
|
|
|
|
struct song *song;
|
|
|
|
|
|
|
|
if (queue_is_full(queue))
|
2010-07-25 12:43:05 +02:00
|
|
|
return;
|
2009-01-23 16:35:04 +01:00
|
|
|
|
2012-08-21 19:17:14 +02:00
|
|
|
uint8_t priority = 0;
|
|
|
|
if (g_str_has_prefix(line, PRIO_LABEL)) {
|
|
|
|
priority = strtoul(line + sizeof(PRIO_LABEL) - 1, NULL, 10);
|
|
|
|
|
|
|
|
line = read_text_line(fp, buffer);
|
|
|
|
if (line == NULL)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-07-25 12:21:47 +02:00
|
|
|
if (g_str_has_prefix(line, SONG_BEGIN)) {
|
|
|
|
const char *uri = line + sizeof(SONG_BEGIN) - 1;
|
|
|
|
if (!uri_has_scheme(uri) && !g_path_is_absolute(uri))
|
|
|
|
return;
|
2009-01-23 16:35:04 +01:00
|
|
|
|
2010-07-25 12:21:47 +02:00
|
|
|
GError *error = NULL;
|
|
|
|
song = song_load(fp, NULL, uri, buffer, &error);
|
|
|
|
if (song == NULL) {
|
|
|
|
g_warning("%s", error->message);
|
|
|
|
g_error_free(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
char *endptr;
|
|
|
|
long ret = strtol(line, &endptr, 10);
|
|
|
|
if (ret < 0 || *endptr != ':' || endptr[1] == 0) {
|
|
|
|
g_warning("Malformed playlist line in state file");
|
|
|
|
return;
|
|
|
|
}
|
2009-01-23 16:35:04 +01:00
|
|
|
|
2010-07-25 12:21:47 +02:00
|
|
|
line = endptr + 1;
|
|
|
|
|
|
|
|
song = get_song(line);
|
|
|
|
if (song == NULL)
|
|
|
|
return;
|
|
|
|
}
|
2009-01-23 16:35:04 +01:00
|
|
|
|
2012-08-21 19:17:14 +02:00
|
|
|
queue_append(queue, song, priority);
|
2012-08-15 23:28:19 +02:00
|
|
|
|
|
|
|
if (song_in_database(song))
|
|
|
|
db_return_song(song);
|
2009-01-23 16:35:04 +01:00
|
|
|
}
|