QueueSave.cxx 3.04 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "DetachedSong.hxx"
25
#include "SongSave.hxx"
26
#include "playlist/PlaylistSong.hxx"
27 28
#include "fs/io/TextFile.hxx"
#include "fs/io/BufferedOutputStream.hxx"
29
#include "util/StringCompare.hxx"
30
#include "Log.hxx"
31 32 33

#include <stdlib.h>

34 35
#define PRIO_LABEL "Prio: "

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

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

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

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

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

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

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

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

92
	DetachedSong *song;
93

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

97 98 99 100
		try {
			song = song_load(file, uri);
		} catch (const std::runtime_error &e) {
			LogError(e);
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 115 116 117 118 119
		song = new DetachedSong(uri);
	}

	if (!playlist_check_translate_song(*song, nullptr, loader)) {
		delete song;
		return;
120
	}
121

122 123
	queue.Append(std::move(*song), priority);
	delete song;
124
}