aboutsummaryrefslogtreecommitdiff
path: root/src/PlaylistSave.cxx
blob: 6ca6740fcb5c88fe1143f8b7e63ee3a898b55103 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2003-2013 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * 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.
 *
 * 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.
 */

#include "config.h"
#include "PlaylistSave.hxx"
#include "PlaylistFile.hxx"
#include "Playlist.hxx"
#include "song.h"
#include "Mapper.hxx"
#include "Idle.hxx"
#include "fs/Path.hxx"
#include "fs/FileSystem.hxx"

extern "C" {
#include "uri.h"
}

#include <glib.h>

void
playlist_print_song(FILE *file, const struct song *song)
{
	if (playlist_saveAbsolutePaths && song_in_database(song)) {
		const Path path = map_song_fs(song);
		if (!path.IsNull())
			fprintf(file, "%s\n", path.c_str());
	} else {
		char *uri = song_get_uri(song);
		const Path uri_fs = Path::FromUTF8(uri);
		g_free(uri);

		if (!uri_fs.IsNull())
			fprintf(file, "%s\n", uri_fs.c_str());
	}
}

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

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

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

	if (!spl_valid_name(name_utf8))
		return PLAYLIST_RESULT_BAD_NAME;

	const Path path_fs = map_spl_utf8_to_fs(name_utf8);
	if (path_fs.IsNull())
		return PLAYLIST_RESULT_BAD_NAME;

	if (FileExists(path_fs))
		return PLAYLIST_RESULT_LIST_EXISTS;

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

	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;

	for (unsigned i = 0; i < queue->GetLength(); i++)
		playlist_print_song(file, queue->Get(i));

	fclose(file);

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}

enum playlist_result
spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
{
	return spl_save_queue(name_utf8, &playlist->queue);
}

bool
playlist_load_spl(struct playlist *playlist, struct player_control *pc,
		  const char *name_utf8,
		  unsigned start_index, unsigned end_index,
		  GError **error_r)
{
	GError *error = NULL;
	PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
	if (contents.empty() && error != nullptr) {
		g_propagate_error(error_r, error);
		return false;
	}

	if (end_index > contents.size())
		end_index = contents.size();

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

		if ((playlist->AppendURI(*pc, uri_utf8.c_str())) != PLAYLIST_RESULT_SUCCESS) {
			/* for windows compatibility, convert slashes */
			char *temp2 = g_strdup(uri_utf8.c_str());
			char *p = temp2;
			while (*p) {
				if (*p == '\\')
					*p = '/';
				p++;
			}

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

			g_free(temp2);
		}
	}

	return true;
}