QueueSave.cxx 2.9 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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"
Max Kellermann's avatar
Max Kellermann committed
26
#include "db/DatabaseSong.hxx"
27
#include "fs/TextFile.hxx"
28
#include "util/StringUtil.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 <stdlib.h>

36 37
#define PRIO_LABEL "Prio: "

38
static void
39
queue_save_database_song(FILE *fp, int idx, const DetachedSong &song)
40
{
41
	fprintf(fp, "%i:%s\n", idx, song.GetURI());
42 43
}

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

static void
51
queue_save_song(FILE *fp, int idx, const DetachedSong &song)
52
{
53
	if (song.IsInDatabase())
54 55 56 57 58
		queue_save_database_song(fp, idx, song);
	else
		queue_save_full_song(fp, song);
}

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

67
		queue_save_song(fp, i, queue.Get(i));
68
	}
69 70
}

71
void
72
queue_load_song(TextFile &file, const char *line, Queue &queue)
73
{
74
	if (queue.IsFull())
75
		return;
76

77
	uint8_t priority = 0;
78
	if (StringStartsWith(line, PRIO_LABEL)) {
79
		priority = strtoul(line + sizeof(PRIO_LABEL) - 1, nullptr, 10);
80

81
		line = file.ReadLine();
82
		if (line == nullptr)
83 84 85
			return;
	}

86
	DetachedSong *song;
87

88
	if (StringStartsWith(line, SONG_BEGIN)) {
89
		const char *uri = line + sizeof(SONG_BEGIN) - 1;
90
		if (!uri_has_scheme(uri) && !PathTraitsUTF8::IsAbsolute(uri))
91
			return;
92

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

108
		const char *uri = endptr + 1;
109

110
		if (uri_has_scheme(uri)) {
111
			song = new DetachedSong(uri);
112
		} else {
113
#ifdef ENABLE_DATABASE
114 115
			song = DatabaseDetachSong(uri, IgnoreError());
			if (song == nullptr)
116
#endif
117 118
				return;
		}
119
	}
120

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