diff options
Diffstat (limited to '')
-rw-r--r-- | src/command.c | 5 | ||||
-rw-r--r-- | src/playlist.c | 61 | ||||
-rw-r--r-- | src/playlist.h | 2 | ||||
-rw-r--r-- | src/playlist_save.c | 65 | ||||
-rw-r--r-- | src/playlist_save.h | 15 |
5 files changed, 83 insertions, 65 deletions
diff --git a/src/command.c b/src/command.c index 7f1858a34..7118e979b 100644 --- a/src/command.c +++ b/src/command.c @@ -20,6 +20,7 @@ #include "player_control.h" #include "playlist.h" #include "playlist_print.h" +#include "playlist_save.h" #include "queue_print.h" #include "ls.h" #include "directory.h" @@ -668,7 +669,7 @@ handle_save(struct client *client, { enum playlist_result result; - result = savePlaylist(argv[1]); + result = spl_save_queue(argv[1], playlist_get_queue()); return print_playlist_result(client, result); } @@ -677,7 +678,7 @@ handle_load(struct client *client, G_GNUC_UNUSED int argc, char *argv[]) { enum playlist_result result; - result = loadPlaylist(argv[1]); + result = playlist_load_spl(argv[1]); return print_playlist_result(client, result); } diff --git a/src/playlist.c b/src/playlist.c index 57d0771dc..04cb59f3c 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -1025,38 +1025,6 @@ void shufflePlaylist(void) incrPlaylistVersion(); } -enum playlist_result savePlaylist(const char *utf8file) -{ - FILE *fp; - char *path; - - if (!is_valid_playlist_name(utf8file)) - return PLAYLIST_RESULT_BAD_NAME; - - path = map_spl_utf8_to_fs(utf8file); - if (path == NULL) - return PLAYLIST_RESULT_DISABLED; - - if (g_file_test(path, G_FILE_TEST_EXISTS)) { - g_free(path); - return PLAYLIST_RESULT_LIST_EXISTS; - } - - while (!(fp = fopen(path, "w")) && errno == EINTR); - g_free(path); - - if (fp == NULL) - return PLAYLIST_RESULT_ERRNO; - - for (unsigned i = 0; i < queue_length(&playlist.queue); i++) - playlist_print_song(fp, queue_get(&playlist.queue, i)); - - while (fclose(fp) && errno == EINTR) ; - - idle_add(IDLE_STORED_PLAYLIST); - return PLAYLIST_RESULT_SUCCESS; -} - int getPlaylistCurrentSong(void) { if (playlist.current >= 0) @@ -1124,35 +1092,6 @@ unsigned getPlaylistSongId(unsigned song) return queue_position_to_id(&playlist.queue, song); } -enum playlist_result loadPlaylist(const char *utf8file) -{ - GPtrArray *list; - - if (!(list = spl_load(utf8file))) - return PLAYLIST_RESULT_NO_SUCH_LIST; - - for (unsigned i = 0; i < list->len; ++i) { - const char *temp = g_ptr_array_index(list, i); - if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) { - /* for windows compatibility, convert slashes */ - char *temp2 = g_strdup(temp); - char *p = temp2; - while (*p) { - if (*p == '\\') - *p = '/'; - p++; - } - if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) { - g_warning("can't add file \"%s\"", temp2); - } - free(temp2); - } - } - - spl_free(list); - return PLAYLIST_RESULT_SUCCESS; -} - /* * Not supporting '/' was done out of laziness, and we should really * strive to support it in the future. diff --git a/src/playlist.h b/src/playlist.h index e257e5392..632ddfc76 100644 --- a/src/playlist.h +++ b/src/playlist.h @@ -144,8 +144,6 @@ enum playlist_result swapSongsInPlaylist(unsigned song1, unsigned song2); enum playlist_result swapSongsInPlaylistById(unsigned id1, unsigned id2); -enum playlist_result loadPlaylist(const char *utf8file); - bool getPlaylistRepeatStatus(void); void setPlaylistRepeatStatus(bool status); diff --git a/src/playlist_save.c b/src/playlist_save.c index d2fe4c9ba..d5b175071 100644 --- a/src/playlist_save.c +++ b/src/playlist_save.c @@ -23,6 +23,7 @@ #include "path.h" #include "ls.h" #include "database.h" +#include "idle.h" #include <glib.h> @@ -62,3 +63,67 @@ playlist_print_uri(FILE *file, const char *uri) g_free(s); } } + +enum playlist_result +spl_save_queue(const char *name_utf8, const struct queue *queue) +{ + char *path_fs; + FILE *file; + + if (!is_valid_playlist_name(name_utf8)) + 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; +} + +enum playlist_result +playlist_load_spl(const char *name_utf8) +{ + 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); + if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) { + /* for windows compatibility, convert slashes */ + char *temp2 = g_strdup(temp); + char *p = temp2; + while (*p) { + if (*p == '\\') + *p = '/'; + p++; + } + if ((addToPlaylist(temp, NULL)) != PLAYLIST_RESULT_SUCCESS) { + g_warning("can't add file \"%s\"", temp2); + } + g_free(temp2); + } + } + + spl_free(list); + return PLAYLIST_RESULT_SUCCESS; +} diff --git a/src/playlist_save.h b/src/playlist_save.h index b9e8169e6..151595755 100644 --- a/src/playlist_save.h +++ b/src/playlist_save.h @@ -19,6 +19,8 @@ #ifndef MPD_PLAYLIST_SAVE_H #define MPD_PLAYLIST_SAVE_H +#include "playlist.h" + #include <stdio.h> struct song; @@ -29,4 +31,17 @@ playlist_print_song(FILE *fp, const struct song *song); void playlist_print_uri(FILE *fp, const char *uri); +/** + * Saves a queue object into a stored playlist file. + */ +enum playlist_result +spl_save_queue(const char *name_utf8, const struct queue *queue); + +/** + * Loads a stored playlist file, and append all songs to the global + * playlist. + */ +enum playlist_result +playlist_load_spl(const char *name_utf8); + #endif |