PlaylistSave.cxx 2.79 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 "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/UriUtil.hxx"
34

35
#include <exception>
36

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

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

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

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

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

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

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

96
	FileOutputStream fos(path_fs);
97 98
	BufferedOutputStream bos(fos);

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

102
	bos.Flush();
103 104
	fos.Commit();

105 106 107
	idle_add(IDLE_STORED_PLAYLIST);
}

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