playlist_save.c 3.17 KB
Newer Older
1 2 3
/*
 * Copyright (C) 2003-2009 The Music Player Daemon Project
 * 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 "playlist_save.h"
21
#include "stored_playlist.h"
22
#include "song.h"
23
#include "mapper.h"
24
#include "path.h"
25
#include "uri.h"
26
#include "database.h"
27
#include "idle.h"
28

29 30
#include <glib.h>

31 32 33
void
playlist_print_song(FILE *file, const struct song *song)
{
34
	if (playlist_saveAbsolutePaths && song_in_database(song)) {
35 36
		char *path = map_song_fs(song);
		if (path != NULL) {
37
			fprintf(file, "%s\n", path);
38 39
			g_free(path);
		}
40
	} else {
41
		char *uri = song_get_uri(song), *uri_fs;
42

43
		uri_fs = utf8_to_fs_charset(uri);
44 45
		g_free(uri);

46 47
		fprintf(file, "%s\n", uri_fs);
		g_free(uri_fs);
48
	}
49 50 51 52 53
}

void
playlist_print_uri(FILE *file, const char *uri)
{
54
	char *s;
55

56
	if (playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
57
	    uri[0] != '/')
58
		s = map_uri_fs(uri);
59
	else
60
		s = utf8_to_fs_charset(uri);
61

62
	if (s != NULL) {
63
		fprintf(file, "%s\n", s);
64 65
		g_free(s);
	}
66
}
67 68 69 70 71 72 73

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

74
	if (!spl_valid_name(name_utf8))
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
		return PLAYLIST_RESULT_BAD_NAME;

	path_fs = map_spl_utf8_to_fs(name_utf8);
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;

	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;
}

101 102 103 104 105 106
enum playlist_result
spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
{
	return spl_save_queue(name_utf8, &playlist->queue);
}

107
enum playlist_result
108
playlist_load_spl(struct playlist *playlist, const char *name_utf8)
109 110 111 112 113 114 115 116 117
{
	GPtrArray *list;

	list = spl_load(name_utf8);
	if (list == NULL)
		return PLAYLIST_RESULT_NO_SUCH_LIST;

	for (unsigned i = 0; i < list->len; ++i) {
		const char *temp = g_ptr_array_index(list, i);
118
		if ((addToPlaylist(playlist, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
119 120 121 122 123 124 125 126
			/* for windows compatibility, convert slashes */
			char *temp2 = g_strdup(temp);
			char *p = temp2;
			while (*p) {
				if (*p == '\\')
					*p = '/';
				p++;
			}
127
			if ((addToPlaylist(playlist, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
128 129 130 131 132 133 134 135 136
				g_warning("can't add file \"%s\"", temp2);
			}
			g_free(temp2);
		}
	}

	spl_free(list);
	return PLAYLIST_RESULT_SUCCESS;
}