PlaylistSave.cxx 3.44 KB
Newer Older
1
/*
2
 * Copyright (C) 2003-2013 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 "Playlist.hxx"
24
#include "song.h"
Max Kellermann's avatar
Max Kellermann committed
25
#include "Mapper.hxx"
Max Kellermann's avatar
Max Kellermann committed
26
#include "Idle.hxx"
27
#include "fs/Path.hxx"
28
#include "fs/FileSystem.hxx"
29 30

extern "C" {
31
#include "uri.h"
32 33
}

34 35
#include <glib.h>

36 37 38
void
playlist_print_song(FILE *file, const struct song *song)
{
39
	if (playlist_saveAbsolutePaths && song_in_database(song)) {
40 41 42
		const Path path = map_song_fs(song);
		if (!path.IsNull())
			fprintf(file, "%s\n", path.c_str());
43
	} else {
44 45
		char *uri = song_get_uri(song);
		const Path uri_fs = Path::FromUTF8(uri);
46 47
		g_free(uri);

48 49
		if (!uri_fs.IsNull())
			fprintf(file, "%s\n", uri_fs.c_str());
50
	}
51 52 53 54 55
}

void
playlist_print_uri(FILE *file, const char *uri)
{
56 57 58 59
	Path path = playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
		!g_path_is_absolute(uri)
		? map_uri_fs(uri)
		: Path::FromUTF8(uri);
60

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

enum playlist_result
spl_save_queue(const char *name_utf8, const struct queue *queue)
{
68
	if (map_spl_path().IsNull())
69 70
		return PLAYLIST_RESULT_DISABLED;

71
	if (!spl_valid_name(name_utf8))
72 73
		return PLAYLIST_RESULT_BAD_NAME;

74 75
	const Path path_fs = map_spl_utf8_to_fs(name_utf8);
	if (path_fs.IsNull())
76
		return PLAYLIST_RESULT_BAD_NAME;
77

78
	if (FileExists(path_fs))
79 80
		return PLAYLIST_RESULT_LIST_EXISTS;

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

	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;

86 87
	for (unsigned i = 0; i < queue->GetLength(); i++)
		playlist_print_song(file, queue->Get(i));
88 89 90 91 92 93 94

	fclose(file);

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}

95 96 97 98 99 100
enum playlist_result
spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
{
	return spl_save_queue(name_utf8, &playlist->queue);
}

101
bool
102
playlist_load_spl(struct playlist *playlist, struct player_control *pc,
103 104 105
		  const char *name_utf8,
		  unsigned start_index, unsigned end_index,
		  GError **error_r)
106
{
107 108 109 110
	GError *error = NULL;
	PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
	if (contents.empty() && error != nullptr) {
		g_propagate_error(error_r, error);
111
		return false;
112
	}
113

114 115
	if (end_index > contents.size())
		end_index = contents.size();
116 117

	for (unsigned i = start_index; i < end_index; ++i) {
118 119
		const auto &uri_utf8 = contents[i];

120
		if ((playlist->AppendURI(*pc, uri_utf8.c_str())) != PLAYLIST_RESULT_SUCCESS) {
121
			/* for windows compatibility, convert slashes */
122
			char *temp2 = g_strdup(uri_utf8.c_str());
123 124 125 126 127 128
			char *p = temp2;
			while (*p) {
				if (*p == '\\')
					*p = '/';
				p++;
			}
129 130

			if (playlist->AppendURI(*pc, temp2) != PLAYLIST_RESULT_SUCCESS)
131
				g_warning("can't add file \"%s\"", temp2);
132

133 134 135 136
			g_free(temp2);
		}
	}

137
	return true;
138
}