PlaylistSave.cxx 2.59 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2014 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"
26
#include "SongLoader.hxx"
Max Kellermann's avatar
Max Kellermann committed
27
#include "Mapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
28
#include "Idle.hxx"
29
#include "fs/AllocatedPath.hxx"
30
#include "fs/Traits.hxx"
31
#include "fs/FileSystem.hxx"
32
#include "util/Alloc.hxx"
Max Kellermann's avatar
Max Kellermann committed
33
#include "util/UriUtil.hxx"
34
#include "util/Error.hxx"
35
#include "Log.hxx"
36

37 38
#include <string.h>

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

46 47 48
	const auto uri_fs = AllocatedPath::FromUTF8(uri_utf8);
	if (!uri_fs.IsNull())
		fprintf(file, "%s\n", uri_fs.c_str());
49 50 51 52 53
}

void
playlist_print_uri(FILE *file, const char *uri)
{
54 55 56
	auto path =
#ifdef ENABLE_DATABASE
		playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
57
		!PathTraitsUTF8::IsAbsolute(uri)
58
		? map_uri_fs(uri)
59 60 61
		:
#endif
		AllocatedPath::FromUTF8(uri);
62

63 64
	if (!path.IsNull())
		fprintf(file, "%s\n", path.c_str());
65
}
66

67
PlaylistResult
68
spl_save_queue(const char *name_utf8, const Queue &queue)
69
{
70
	if (map_spl_path().IsNull())
71
		return PlaylistResult::DISABLED;
72

73
	if (!spl_valid_name(name_utf8))
74
		return PlaylistResult::BAD_NAME;
75

76
	const auto path_fs = map_spl_utf8_to_fs(name_utf8);
77
	if (path_fs.IsNull())
78
		return PlaylistResult::BAD_NAME;
79

80
	if (FileExists(path_fs))
81
		return PlaylistResult::LIST_EXISTS;
82

83
	FILE *file = FOpen(path_fs, FOpenMode::WriteText);
84

85
	if (file == nullptr)
86
		return PlaylistResult::ERRNO;
87

88 89
	for (unsigned i = 0; i < queue.GetLength(); i++)
		playlist_print_song(file, queue.Get(i));
90 91 92 93

	fclose(file);

	idle_add(IDLE_STORED_PLAYLIST);
94
	return PlaylistResult::SUCCESS;
95 96
}

97
PlaylistResult
98
spl_save_playlist(const char *name_utf8, const playlist &playlist)
99
{
100
	return spl_save_queue(name_utf8, playlist.queue);
101
}