Code

screen_queue: move playlist_save() to save_playlist.c
[ncmpc.git] / src / screen_queue.c
index 0d08bbcb63ce67f3eb8a5dec83681fbc1b74a77a..3d20da3bc999ad10f62e13dfb3a9aa9c7e0471f5 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2010 The Music Player Daemon Project
+ * (c) 2004-2017 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
@@ -22,6 +22,7 @@
 #include "screen_file.h"
 #include "screen_status.h"
 #include "screen_find.h"
+#include "save_playlist.h"
 #include "config.h"
 #include "i18n.h"
 #include "charset.h"
@@ -180,32 +181,6 @@ screen_queue_song_change(const struct mpd_status *status)
        return true;
 }
 
-#ifndef NCMPC_MINI
-static void
-save_pre_completion_cb(GCompletion *gcmp, gcc_unused gchar *line,
-                      void *data)
-{
-       completion_callback_data_t *tmp = (completion_callback_data_t *)data;
-       GList **list = tmp->list;
-       struct mpdclient *c = tmp->c;
-
-       if( *list == NULL ) {
-               /* create completion list */
-               *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
-               g_completion_add_items(gcmp, *list);
-       }
-}
-
-static void
-save_post_completion_cb(gcc_unused GCompletion *gcmp,
-                       gcc_unused gchar *line, GList *items,
-                       gcc_unused void *data)
-{
-       if (g_list_length(items) >= 1)
-               screen_display_completion_list(items);
-}
-#endif
-
 #ifndef NCMPC_MINI
 /**
  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
@@ -219,103 +194,6 @@ completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
 }
 #endif
 
-int
-playlist_save(struct mpdclient *c, char *name, char *defaultname)
-{
-       struct mpd_connection *connection;
-       gchar *filename;
-
-#ifdef NCMPC_MINI
-       (void)defaultname;
-#endif
-
-#ifndef NCMPC_MINI
-       if (name == NULL) {
-               /* initialize completion support */
-               GCompletion *gcmp = g_completion_new(NULL);
-               g_completion_set_compare(gcmp, completion_strncmp);
-               GList *list = NULL;
-               completion_callback_data_t data = {
-                       .list = &list,
-                       .dir_list = NULL,
-                       .c = c,
-               };
-               wrln_completion_callback_data = &data;
-               wrln_pre_completion_callback = save_pre_completion_cb;
-               wrln_post_completion_callback = save_post_completion_cb;
-
-
-               /* query the user for a filename */
-               filename = screen_readln(_("Save queue as"),
-                                        defaultname,
-                                        NULL,
-                                        gcmp);
-
-               /* destroy completion support */
-               wrln_completion_callback_data = NULL;
-               wrln_pre_completion_callback = NULL;
-               wrln_post_completion_callback = NULL;
-               g_completion_free(gcmp);
-               list = string_list_free(list);
-               if( filename )
-                       filename=g_strstrip(filename);
-       } else
-#endif
-               filename=g_strdup(name);
-
-       if (filename == NULL)
-               return -1;
-
-       /* send save command to mpd */
-
-       connection = mpdclient_get_connection(c);
-       if (connection == NULL) {
-               g_free(filename);
-               return -1;
-       }
-
-       char *filename_utf8 = locale_to_utf8(filename);
-       if (!mpd_run_save(connection, filename_utf8)) {
-               if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
-                   mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
-                   mpd_connection_clear_error(connection)) {
-                       char *buf = g_strdup_printf(_("Replace %s [%s/%s] ? "),
-                                                   filename, YES, NO);
-                       bool replace = screen_get_yesno(buf, false);
-                       g_free(buf);
-
-                       if (!replace) {
-                               g_free(filename_utf8);
-                               g_free(filename);
-                               screen_status_printf(_("Aborted"));
-                               return -1;
-                       }
-
-                       if (!mpd_run_rm(connection, filename_utf8) ||
-                           !mpd_run_save(connection, filename_utf8)) {
-                               mpdclient_handle_error(c);
-                               g_free(filename_utf8);
-                               g_free(filename);
-                               return -1;
-                       }
-               } else {
-                       mpdclient_handle_error(c);
-                       g_free(filename_utf8);
-                       g_free(filename);
-                       return -1;
-               }
-       }
-
-       c->events |= MPD_IDLE_STORED_PLAYLIST;
-
-       g_free(filename_utf8);
-
-       /* success */
-       screen_status_printf(_("Saved %s"), filename);
-       g_free(filename);
-       return 0;
-}
-
 #ifndef NCMPC_MINI
 static void add_dir(GCompletion *gcmp, gchar *dir, GList **dir_list,
                    GList **list, struct mpdclient *c)
@@ -415,7 +293,7 @@ handle_add_to_playlist(struct mpdclient *c)
 }
 
 static void
-screen_queue_init(WINDOW *w, int cols, int rows)
+screen_queue_init(WINDOW *w, unsigned cols, unsigned rows)
 {
        lw = list_window_init(w, cols, rows);
 
@@ -442,8 +320,8 @@ timer_hide_cursor(gpointer data)
                lw->hide_cursor = true;
                screen_queue_repaint();
        } else
-               timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
-                                                    timer_hide_cursor, c);
+               timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
+                                                            timer_hide_cursor, c);
 
        return FALSE;
 }
@@ -456,8 +334,8 @@ screen_queue_open(struct mpdclient *c)
        assert(timer_hide_cursor_id == 0);
        if (options.hide_cursor > 0) {
                lw->hide_cursor = false;
-               timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
-                                                    timer_hide_cursor, c);
+               timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
+                                                            timer_hide_cursor, c);
        }
 
        screen_queue_restore_selection();
@@ -479,7 +357,7 @@ screen_queue_close(void)
 }
 
 static void
-screen_queue_resize(int cols, int rows)
+screen_queue_resize(unsigned cols, unsigned rows)
 {
        list_window_resize(lw, cols, rows);
 }
@@ -504,7 +382,7 @@ screen_queue_title(char *str, size_t size)
 static void
 screen_queue_paint_callback(WINDOW *w, unsigned i,
                            unsigned y, unsigned width,
-                           bool selected, gcc_unused void *data)
+                           bool selected, gcc_unused const void *data)
 {
        assert(playlist != NULL);
        assert(i < playlist_length(playlist));
@@ -519,7 +397,7 @@ screen_queue_paint_callback(WINDOW *w, unsigned i,
 
        paint_song_row(w, y, width, selected,
                       (int)mpd_song_get_id(song) == current_song_id,
-                      song, row_hscroll);
+                      song, row_hscroll, options.list_format);
 }
 
 static void
@@ -548,7 +426,7 @@ screen_queue_update(struct mpdclient *c)
            c->events & MPD_IDLE_QUEUE)
                /* the queue or the current song has changed, we must
                   paint the new version */
-               screen_queue_repaint();
+               screen_queue_paint();
 }
 
 #ifdef HAVE_GETMOUSE
@@ -559,7 +437,7 @@ handle_mouse_event(struct mpdclient *c)
        int row;
        if (screen_get_mouse_event(c, &bstate, &row) ||
            list_window_mouse(lw, bstate, row)) {
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
        }
 
@@ -593,7 +471,7 @@ handle_mouse_event(struct mpdclient *c)
        }
 
        screen_queue_save_selection();
-       screen_queue_repaint();
+       screen_queue_paint();
 
        return true;
 }
@@ -613,26 +491,26 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
        if (options.hide_cursor > 0) {
                if (timer_hide_cursor_id != 0)
                        g_source_remove(timer_hide_cursor_id);
-               timer_hide_cursor_id = g_timeout_add(options.hide_cursor * 1000,
-                                                    timer_hide_cursor, c);
+               timer_hide_cursor_id = g_timeout_add_seconds(options.hide_cursor,
+                                                            timer_hide_cursor, c);
        }
 
        if (list_window_cmd(lw, cmd)) {
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
        }
 
        switch(cmd) {
        case CMD_SCREEN_UPDATE:
                center_playing_item(c->status, prev_cmd == CMD_SCREEN_UPDATE);
-               screen_queue_repaint();
+               screen_queue_paint();
                return false;
        case CMD_SELECT_PLAYING:
                list_window_set_cursor(lw, playlist_get_index(&c->playlist,
                                                              c->song));
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
 
        case CMD_LIST_FIND:
@@ -641,12 +519,12 @@ screen_queue_cmd(struct mpdclient *c, command_t cmd)
        case CMD_LIST_RFIND_NEXT:
                screen_find(lw, cmd, screen_queue_lw_callback, NULL);
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
        case CMD_LIST_JUMP:
-               screen_jump(lw, screen_queue_lw_callback, NULL, NULL);
+               screen_jump(lw, screen_queue_lw_callback, NULL, NULL, NULL);
                screen_queue_save_selection();
-               screen_queue_repaint();
+               screen_queue_paint();
                return true;
 
 #ifdef HAVE_GETMOUSE