PlaylistSave.cxx 2.53 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 The Music Player Daemon Project
3
 * http://www.musicpd.org
4 5 6 7 8 9 10 11 12 13
 *
 * 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 "PlaylistSave.hxx"
Max Kellermann's avatar
Max Kellermann committed
22
#include "PlaylistFile.hxx"
23
#include "PlaylistError.hxx"
24
#include "queue/Playlist.hxx"
25
#include "DetachedSong.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "Mapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "Idle.hxx"
28
#include "fs/AllocatedPath.hxx"
29
#include "fs/Traits.hxx"
30
#include "fs/FileSystem.hxx"
31
#include "fs/NarrowPath.hxx"
32 33
#include "fs/io/FileOutputStream.hxx"
#include "fs/io/BufferedOutputStream.hxx"
Max Kellermann's avatar
Max Kellermann committed
34
#include "util/UriUtil.hxx"
35

36 37
#include <stdexcept>

38
void
39
playlist_print_song(BufferedOutputStream &os, const DetachedSong &song)
40
{
41 42 43
	const char *uri_utf8 = playlist_saveAbsolutePaths
		? song.GetRealURI()
		: song.GetURI();
44

45
	try {
46 47
		const auto uri_fs = AllocatedPath::FromUTF8Throw(uri_utf8);
		os.Format("%s\n", NarrowPath(uri_fs).c_str());
48 49
	} catch (const std::runtime_error &) {
	}
50 51 52
}

void
53
playlist_print_uri(BufferedOutputStream &os, const char *uri)
54
{
55 56
	try {
		auto path =
57
#ifdef ENABLE_DATABASE
58 59 60 61
			playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
			!PathTraitsUTF8::IsAbsolute(uri)
			? map_uri_fs(uri)
			:
62
#endif
63
			AllocatedPath::FromUTF8Throw(uri);
64

65 66 67 68
		if (!path.IsNull())
			os.Format("%s\n", NarrowPath(path).c_str());
	} catch (const std::runtime_error &) {
	}
69
}
70

71 72
void
spl_save_queue(const char *name_utf8, const Queue &queue)
73
{
74 75
	const auto path_fs = spl_map_to_fs(name_utf8);
	assert(!path_fs.IsNull());
76

77 78 79
	if (FileExists(path_fs))
		throw PlaylistError(PlaylistResult::LIST_EXISTS,
				    "Playlist already exists");
80

81
	FileOutputStream fos(path_fs);
82 83
	BufferedOutputStream bos(fos);

84
	for (unsigned i = 0; i < queue.GetLength(); i++)
85
		playlist_print_song(bos, queue.Get(i));
86

87
	bos.Flush();
88 89
	fos.Commit();

90 91 92
	idle_add(IDLE_STORED_PLAYLIST);
}

93 94
void
spl_save_playlist(const char *name_utf8, const playlist &playlist)
95
{
96
	spl_save_queue(name_utf8, playlist.queue);
97
}