Commit cbb1ab58 authored by Max Kellermann's avatar Max Kellermann

queue_save: save tags and range of non-database songs

Use the functions song_save() and song_load() to use the same format as in the database file for those songs which need the tags.
parent b01235e3
......@@ -108,7 +108,7 @@ playlist_state_load(FILE *fp, GString *buffer, struct playlist *playlist)
}
while (!g_str_has_prefix(line, PLAYLIST_STATE_FILE_PLAYLIST_END)) {
queue_load_song(&playlist->queue, line);
queue_load_song(fp, buffer, line, &playlist->queue);
line = read_text_line(fp, buffer);
if (line == NULL) {
......
......@@ -23,11 +23,12 @@
#include "song.h"
#include "uri.h"
#include "database.h"
#include "song_save.h"
#include <stdlib.h>
static void
queue_save_song(FILE *fp, int idx, const struct song *song)
queue_save_database_song(FILE *fp, int idx, const struct song *song)
{
char *uri = song_get_uri(song);
......@@ -35,6 +36,21 @@ queue_save_song(FILE *fp, int idx, const struct song *song)
g_free(uri);
}
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);
}
void
queue_save(FILE *fp, const struct queue *queue)
{
......@@ -51,26 +67,40 @@ get_song(const char *uri)
}
void
queue_load_song(struct queue *queue, const char *line)
queue_load_song(FILE *fp, GString *buffer, const char *line,
struct queue *queue)
{
long ret;
char *endptr;
struct song *song;
if (queue_is_full(queue))
return;
ret = strtol(line, &endptr, 10);
if (ret < 0 || *endptr != ':' || endptr[1] == 0) {
g_warning("Malformed playlist line in state file");
return;
}
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;
line = endptr + 1;
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;
}
song = get_song(line);
if (song == NULL)
return;
line = endptr + 1;
song = get_song(line);
if (song == NULL)
return;
}
queue_append(queue, song);
}
......@@ -25,6 +25,7 @@
#ifndef QUEUE_SAVE_H
#define QUEUE_SAVE_H
#include <glib.h>
#include <stdio.h>
struct queue;
......@@ -36,6 +37,7 @@ queue_save(FILE *fp, const struct queue *queue);
* Loads one song from the state file and appends it to the queue.
*/
void
queue_load_song(struct queue *queue, const char *line);
queue_load_song(FILE *fp, GString *buffer, const char *line,
struct queue *queue);
#endif
......@@ -41,11 +41,9 @@ song_save_quark(void)
return g_quark_from_static_string("song_save");
}
static int
song_save(struct song *song, void *data)
void
song_save(FILE *fp, const struct song *song)
{
FILE *fp = data;
fprintf(fp, SONG_BEGIN "%s\n", song->uri);
if (song->end_ms > 0)
......@@ -58,20 +56,28 @@ song_save(struct song *song, void *data)
fprintf(fp, SONG_MTIME ": %li\n", (long)song->mtime);
fprintf(fp, SONG_END "\n");
}
static int
song_save_callback(struct song *song, void *data)
{
FILE *fp = data;
song_save(fp, song);
return 0;
}
void songvec_save(FILE *fp, const struct songvec *sv)
{
songvec_for_each(sv, song_save, fp);
songvec_for_each(sv, song_save_callback, fp);
}
struct song *
song_load(FILE *fp, struct directory *parent, const char *uri,
GString *buffer, GError **error_r)
{
struct song *song = song_file_new(uri, parent);
struct song *song = parent != NULL
? song_file_new(uri, parent)
: song_remote_new(uri);
char *line, *colon;
enum tag_type type;
const char *value;
......
......@@ -26,10 +26,14 @@
#define SONG_BEGIN "song_begin: "
struct song;
struct songvec;
struct directory;
void
song_save(FILE *fp, const struct song *song);
void
songvec_save(FILE *fp, const struct songvec *sv);
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment