QueueSave.cxx 3.04 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 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 "QueueSave.hxx"
21 22
#include "Queue.hxx"
#include "PlaylistError.hxx"
23
#include "song/DetachedSong.hxx"
24
#include "SongSave.hxx"
25
#include "playlist/PlaylistSong.hxx"
26 27
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
28
#include "util/StringCompare.hxx"
29
#include "Log.hxx"
30

31 32
#include <exception>

33 34
#include <stdlib.h>

35 36
#define PRIO_LABEL "Prio: "

37
static void
38 39
queue_save_database_song(BufferedOutputStream &os,
			 int idx, const DetachedSong &song)
40
{
41
	os.Format("%i:%s\n", idx, song.GetURI());
42 43
}

44
static void
45
queue_save_full_song(BufferedOutputStream &os, const DetachedSong &song)
46
{
47
	song_save(os, song);
48 49 50
}

static void
51
queue_save_song(BufferedOutputStream &os, int idx, const DetachedSong &song)
52
{
53
	if (song.IsInDatabase() &&
54
	    song.GetStartTime().IsZero() && song.GetEndTime().IsZero())
55 56
		/* use the brief format (just the URI) for "full"
		   database songs */
57
		queue_save_database_song(os, idx, song);
58
	else
59 60
		/* use the long format (URI, range, tags) for the
		   rest, so all metadata survives a MPD restart */
61
		queue_save_full_song(os, song);
62 63
}

64
void
65
queue_save(BufferedOutputStream &os, const Queue &queue)
66
{
67 68
	for (unsigned i = 0; i < queue.GetLength(); i++) {
		uint8_t prio = queue.GetPriorityAtPosition(i);
69
		if (prio != 0)
70
			os.Format(PRIO_LABEL "%u\n", prio);
71

72
		queue_save_song(os, i, queue.Get(i));
73
	}
74 75
}

76
void
77 78
queue_load_song(TextFile &file, const SongLoader &loader,
		const char *line, Queue &queue)
79
{
80
	if (queue.IsFull())
81
		return;
82

83
	uint8_t priority = 0;
84 85 86
	const char *p;
	if ((p = StringAfterPrefix(line, PRIO_LABEL))) {
		priority = strtoul(p, nullptr, 10);
87

88
		line = file.ReadLine();
89
		if (line == nullptr)
90 91 92
			return;
	}

93
	std::unique_ptr<DetachedSong> song;
94

95
	if ((p = StringAfterPrefix(line, SONG_BEGIN))) {
96
		const char *uri = p;
97

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

113
		const char *uri = endptr + 1;
114

115
		song = std::make_unique<DetachedSong>(uri);
116 117
	}

118
	if (!playlist_check_translate_song(*song, nullptr, loader))
119
		return;
120

121
	queue.Append(std::move(*song), priority);
122
}