configure.ac: Add subheaders to Input/Sticker/Autodiscovery/Metadata and Misc.
[mpd-mk.git] / src / playlist_save.c
blob8ddc93ec9ddcaecdf52f2f8dfbad819bd886dc9f
1 /*
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.
20 #include "config.h"
21 #include "playlist_save.h"
22 #include "stored_playlist.h"
23 #include "song.h"
24 #include "mapper.h"
25 #include "path.h"
26 #include "uri.h"
27 #include "database.h"
28 #include "idle.h"
30 #include <glib.h>
32 void
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);
37 if (path != NULL) {
38 fprintf(file, "%s\n", path);
39 g_free(path);
41 } else {
42 char *uri = song_get_uri(song), *uri_fs;
44 uri_fs = utf8_to_fs_charset(uri);
45 g_free(uri);
47 fprintf(file, "%s\n", uri_fs);
48 g_free(uri_fs);
52 void
53 playlist_print_uri(FILE *file, const char *uri)
55 char *s;
57 if (playlist_saveAbsolutePaths && !uri_has_scheme(uri) &&
58 !g_path_is_absolute(uri))
59 s = map_uri_fs(uri);
60 else
61 s = utf8_to_fs_charset(uri);
63 if (s != NULL) {
64 fprintf(file, "%s\n", s);
65 g_free(s);
69 enum playlist_result
70 spl_save_queue(const char *name_utf8, const struct queue *queue)
72 char *path_fs;
73 FILE *file;
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);
82 if (path_fs == NULL)
83 return PLAYLIST_RESULT_BAD_NAME;
85 if (g_file_test(path_fs, G_FILE_TEST_EXISTS)) {
86 g_free(path_fs);
87 return PLAYLIST_RESULT_LIST_EXISTS;
90 file = fopen(path_fs, "w");
91 g_free(path_fs);
93 if (file == NULL)
94 return PLAYLIST_RESULT_ERRNO;
96 for (unsigned i = 0; i < queue_length(queue); i++)
97 playlist_print_song(file, queue_get(queue, i));
99 fclose(file);
101 idle_add(IDLE_STORED_PLAYLIST);
102 return PLAYLIST_RESULT_SUCCESS;
105 enum playlist_result
106 spl_save_playlist(const char *name_utf8, const struct playlist *playlist)
108 return spl_save_queue(name_utf8, &playlist->queue);
111 enum playlist_result
112 playlist_load_spl(struct playlist *playlist, const char *name_utf8)
114 GPtrArray *list;
116 list = spl_load(name_utf8);
117 if (list == NULL)
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);
125 char *p = temp2;
126 while (*p) {
127 if (*p == '\\')
128 *p = '/';
129 p++;
131 if ((playlist_append_uri(playlist, temp, NULL)) != PLAYLIST_RESULT_SUCCESS) {
132 g_warning("can't add file \"%s\"", temp2);
134 g_free(temp2);
138 spl_free(list);
139 return PLAYLIST_RESULT_SUCCESS;