PlaylistSave.cxx 3.56 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"
26 27

extern "C" {
28
#include "path.h"
29
#include "uri.h"
30
#include "idle.h"
31 32
}

33
#include "glib_compat.h"
34

35 36
#include <glib.h>

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

49
		uri_fs = utf8_to_fs_charset(uri);
50 51
		g_free(uri);

52 53
		fprintf(file, "%s\n", uri_fs);
		g_free(uri_fs);
54
	}
55 56 57 58 59
}

void
playlist_print_uri(FILE *file, const char *uri)
{
60
	char *s;
61

62
	if (playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
63
	    !g_path_is_absolute(uri))
64
		s = map_uri_fs(uri);
65
	else
66
		s = utf8_to_fs_charset(uri);
67

68
	if (s != NULL) {
69
		fprintf(file, "%s\n", s);
70 71
		g_free(s);
	}
72
}
73 74 75 76 77 78 79

enum playlist_result
spl_save_queue(const char *name_utf8, const struct queue *queue)
{
	char *path_fs;
	FILE *file;

80 81 82
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

83
	if (!spl_valid_name(name_utf8))
84 85 86 87
		return PLAYLIST_RESULT_BAD_NAME;

	path_fs = map_spl_utf8_to_fs(name_utf8);
	if (path_fs == NULL)
88
		return PLAYLIST_RESULT_BAD_NAME;
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

	if (g_file_test(path_fs, G_FILE_TEST_EXISTS)) {
		g_free(path_fs);
		return PLAYLIST_RESULT_LIST_EXISTS;
	}

	file = fopen(path_fs, "w");
	g_free(path_fs);

	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;

	for (unsigned i = 0; i < queue_length(queue); i++)
		playlist_print_song(file, queue_get(queue, i));

	fclose(file);

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}

110 111 112 113 114 115
enum playlist_result
spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
{
	return spl_save_queue(name_utf8, &playlist->queue);
}

116
bool
117
playlist_load_spl(struct playlist *playlist, struct player_control *pc,
118 119 120
		  const char *name_utf8,
		  unsigned start_index, unsigned end_index,
		  GError **error_r)
121
{
122 123 124 125
	GError *error = NULL;
	PlaylistFileContents contents = LoadPlaylistFile(name_utf8, &error);
	if (contents.empty() && error != nullptr) {
		g_propagate_error(error_r, error);
126
		return false;
127
	}
128

129 130
	if (end_index > contents.size())
		end_index = contents.size();
131 132

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

		if ((playlist_append_uri(playlist, pc, uri_utf8.c_str(),
					 nullptr)) != PLAYLIST_RESULT_SUCCESS) {
137
			/* for windows compatibility, convert slashes */
138
			char *temp2 = g_strdup(uri_utf8.c_str());
139 140 141 142 143 144
			char *p = temp2;
			while (*p) {
				if (*p == '\\')
					*p = '/';
				p++;
			}
145
			if ((playlist_append_uri(playlist, pc, temp2, NULL)) != PLAYLIST_RESULT_SUCCESS) {
146 147 148 149 150 151
				g_warning("can't add file \"%s\"", temp2);
			}
			g_free(temp2);
		}
	}

152
	return true;
153
}