2 * Copyright (C) 2003-2010 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "playlist_save.h"
22 #include "stored_playlist.h"
33 playlist_print_song(FILE *file
, const struct song
*song
)
35 if (playlist_saveAbsolutePaths
&& song_in_database(song
)) {
36 char *path
= map_song_fs(song
);
38 fprintf(file
, "%s\n", path
);
42 char *uri
= song_get_uri(song
), *uri_fs
;
44 uri_fs
= utf8_to_fs_charset(uri
);
47 fprintf(file
, "%s\n", uri_fs
);
53 playlist_print_uri(FILE *file
, const char *uri
)
57 if (playlist_saveAbsolutePaths
&& !uri_has_scheme(uri
) &&
58 !g_path_is_absolute(uri
))
61 s
= utf8_to_fs_charset(uri
);
64 fprintf(file
, "%s\n", s
);
70 spl_save_queue(const char *name_utf8
, const struct queue
*queue
)
75 if (map_spl_path() == NULL
)
76 return PLAYLIST_RESULT_DISABLED
;
78 if (!spl_valid_name(name_utf8
))
79 return PLAYLIST_RESULT_BAD_NAME
;
81 path_fs
= map_spl_utf8_to_fs(name_utf8
);
83 return PLAYLIST_RESULT_BAD_NAME
;
85 if (g_file_test(path_fs
, G_FILE_TEST_EXISTS
)) {
87 return PLAYLIST_RESULT_LIST_EXISTS
;
90 file
= fopen(path_fs
, "w");
94 return PLAYLIST_RESULT_ERRNO
;
96 for (unsigned i
= 0; i
< queue_length(queue
); i
++)
97 playlist_print_song(file
, queue_get(queue
, i
));
101 idle_add(IDLE_STORED_PLAYLIST
);
102 return PLAYLIST_RESULT_SUCCESS
;
106 spl_save_playlist(const char *name_utf8
, const struct playlist
*playlist
)
108 return spl_save_queue(name_utf8
, &playlist
->queue
);
112 playlist_load_spl(struct playlist
*playlist
, const char *name_utf8
)
116 list
= spl_load(name_utf8
);
118 return PLAYLIST_RESULT_NO_SUCH_LIST
;
120 for (unsigned i
= 0; i
< list
->len
; ++i
) {
121 const char *temp
= g_ptr_array_index(list
, i
);
122 if ((playlist_append_uri(playlist
, temp
, NULL
)) != PLAYLIST_RESULT_SUCCESS
) {
123 /* for windows compatibility, convert slashes */
124 char *temp2
= g_strdup(temp
);
131 if ((playlist_append_uri(playlist
, temp
, NULL
)) != PLAYLIST_RESULT_SUCCESS
) {
132 g_warning("can't add file \"%s\"", temp2
);
139 return PLAYLIST_RESULT_SUCCESS
;