playlist_save.c 3.46 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright (C) 2003-2011 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 "playlist_save.h"
22
#include "playlist.h"
23
#include "stored_playlist.h"
24
#include "queue.h"
25
#include "song.h"
26
#include "mapper.h"
27
#include "path.h"
28
#include "uri.h"
29
#include "database.h"
30
#include "idle.h"
31
#include "glib_compat.h"
32

33 34
#include <glib.h>

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

47
		uri_fs = utf8_to_fs_charset(uri);
48 49
		g_free(uri);

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

void
playlist_print_uri(FILE *file, const char *uri)
{
58
	char *s;
59

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

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

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

78 79 80
	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

81
	if (!spl_valid_name(name_utf8))
82 83 84 85
		return PLAYLIST_RESULT_BAD_NAME;

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

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

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

114
bool
115
playlist_load_spl(struct playlist *playlist, struct player_control *pc,
116 117 118
		  const char *name_utf8,
		  unsigned start_index, unsigned end_index,
		  GError **error_r)
119 120 121
{
	GPtrArray *list;

122
	list = spl_load(name_utf8, error_r);
123
	if (list == NULL)
124
		return false;
125

126 127 128 129
	if (list->len < end_index)
		end_index = list->len;

	for (unsigned i = start_index; i < end_index; ++i) {
130
		const char *temp = g_ptr_array_index(list, i);
131
		if ((playlist_append_uri(playlist, pc, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
132 133 134 135 136 137 138 139
			/* for windows compatibility, convert slashes */
			char *temp2 = g_strdup(temp);
			char *p = temp2;
			while (*p) {
				if (*p == '\\')
					*p = '/';
				p++;
			}
140
			if ((playlist_append_uri(playlist, pc, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
141 142 143 144 145 146 147
				g_warning("can't add file \"%s\"", temp2);
			}
			g_free(temp2);
		}
	}

	spl_free(list);
148
	return true;
149
}