QueueSave.cxx 3 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13
 * 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.
14 15 16 17
 *
 * 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.
18 19
 */

20
#include "config.h"
21
#include "QueueSave.hxx"
22 23
#include "Queue.hxx"
#include "PlaylistError.hxx"
24
#include "Song.hxx"
25
#include "SongSave.hxx"
26 27
#include "DatabasePlugin.hxx"
#include "DatabaseGlue.hxx"
28
#include "TextFile.hxx"
Max Kellermann's avatar
Max Kellermann committed
29
#include "util/UriUtil.hxx"
30
#include "util/Error.hxx"
31
#include "fs/Traits.hxx"
32
#include "Log.hxx"
33

34 35
#include <glib.h>

36 37
#include <stdlib.h>

38 39
#define PRIO_LABEL "Prio: "

40
static void
41
queue_save_database_song(FILE *fp, int idx, const Song &song)
42
{
43
	const auto uri = song.GetURI();
44
	fprintf(fp, "%i:%s\n", idx, uri.c_str());
45 46
}

47
static void
48
queue_save_full_song(FILE *fp, const Song &song)
49 50 51 52 53
{
	song_save(fp, song);
}

static void
54
queue_save_song(FILE *fp, int idx, const Song &song)
55
{
56
	if (song.IsInDatabase())
57 58 59 60 61
		queue_save_database_song(fp, idx, song);
	else
		queue_save_full_song(fp, song);
}

62
void
63
queue_save(FILE *fp, const queue &queue)
64
{
65 66
	for (unsigned i = 0; i < queue.GetLength(); i++) {
		uint8_t prio = queue.GetPriorityAtPosition(i);
67 68 69
		if (prio != 0)
			fprintf(fp, PRIO_LABEL "%u\n", prio);

70
		queue_save_song(fp, i, queue.Get(i));
71
	}
72 73
}

74
void
75
queue_load_song(TextFile &file, const char *line, queue &queue)
76
{
77
	if (queue.IsFull())
78
		return;
79

80 81
	uint8_t priority = 0;
	if (g_str_has_prefix(line, PRIO_LABEL)) {
82
		priority = strtoul(line + sizeof(PRIO_LABEL) - 1, nullptr, 10);
83

84
		line = file.ReadLine();
85
		if (line == nullptr)
86 87 88
			return;
	}

89
	const Database *db = nullptr;
90
	Song *song;
91

92 93
	if (g_str_has_prefix(line, SONG_BEGIN)) {
		const char *uri = line + sizeof(SONG_BEGIN) - 1;
94
		if (!uri_has_scheme(uri) && !PathTraits::IsAbsoluteUTF8(uri))
95
			return;
96

97
		Error error;
98 99
		song = song_load(file, nullptr, uri, error);
		if (song == nullptr) {
100
			LogError(error);
101 102 103 104 105 106
			return;
		}
	} else {
		char *endptr;
		long ret = strtol(line, &endptr, 10);
		if (ret < 0 || *endptr != ':' || endptr[1] == 0) {
107 108
			LogError(playlist_domain,
				 "Malformed playlist line in state file");
109 110
			return;
		}
111

112
		const char *uri = endptr + 1;
113

114
		if (uri_has_scheme(uri)) {
115
			song = Song::NewRemote(uri);
116
		} else {
117
			db = GetDatabase();
118 119 120
			if (db == nullptr)
				return;

121
			song = db->GetSong(uri, IgnoreError());
122 123 124
			if (song == nullptr)
				return;
		}
125
	}
126

127
	queue.Append(song, priority);
128

129 130
	if (db != nullptr)
		db->ReturnSong(song);
131 132
	else
		song->Free();
133
}