PlaylistSave.cxx 2.75 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 32
#include "fs/io/FileOutputStream.hxx"
#include "fs/io/BufferedOutputStream.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "util/UriUtil.hxx"
34

35
#include <exception>
36

37 38 39
static void
playlist_print_path(BufferedOutputStream &os, const Path path)
{
40 41 42 43 44 45 46 47
#ifdef _UNICODE
	/* on Windows, playlists always contain UTF-8, because its
	   "narrow" charset (i.e. CP_ACP) is incapable of storing all
	   Unicode paths */
	os.Format("%s\n", path.ToUTF8().c_str());
#else
	os.Format("%s\n", path.c_str());
#endif
48 49
}

50
void
51
playlist_print_song(BufferedOutputStream &os, const DetachedSong &song)
52
{
53 54 55
	const char *uri_utf8 = playlist_saveAbsolutePaths
		? song.GetRealURI()
		: song.GetURI();
56

57
	try {
58
		const auto uri_fs = AllocatedPath::FromUTF8Throw(uri_utf8);
59
		playlist_print_path(os, uri_fs);
60
	} catch (...) {
61
	}
62 63 64
}

void
65
playlist_print_uri(BufferedOutputStream &os, const char *uri)
66
{
67 68
	try {
		auto path =
69
#ifdef ENABLE_DATABASE
70 71 72 73
			playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
			!PathTraitsUTF8::IsAbsolute(uri)
			? map_uri_fs(uri)
			:
74
#endif
75
			AllocatedPath::FromUTF8Throw(uri);
76

77
		if (!path.IsNull())
78
			playlist_print_path(os, path);
79
	} catch (...) {
80
	}
81
}
82

83 84
void
spl_save_queue(const char *name_utf8, const Queue &queue)
85
{
86 87
	const auto path_fs = spl_map_to_fs(name_utf8);
	assert(!path_fs.IsNull());
88

89 90 91
	if (FileExists(path_fs))
		throw PlaylistError(PlaylistResult::LIST_EXISTS,
				    "Playlist already exists");
92

93
	FileOutputStream fos(path_fs);
94 95
	BufferedOutputStream bos(fos);

96
	for (unsigned i = 0; i < queue.GetLength(); i++)
97
		playlist_print_song(bos, queue.Get(i));
98

99
	bos.Flush();
100 101
	fos.Commit();

102 103 104
	idle_add(IDLE_STORED_PLAYLIST);
}

105 106
void
spl_save_playlist(const char *name_utf8, const playlist &playlist)
107
{
108
	spl_save_queue(name_utf8, playlist.queue);
109
}