PlaylistSave.cxx 2.77 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 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 "song/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/UriExtract.hxx"
34

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

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

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

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

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

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

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

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

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

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

103 104 105
	idle_add(IDLE_STORED_PLAYLIST);
}

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