Code

screen_queue: move playlist_save() to save_playlist.c
[ncmpc.git] / src / save_playlist.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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.
18  */
20 #include "save_playlist.h"
21 #include "screen_status.h"
22 #include "config.h"
23 #include "i18n.h"
24 #include "charset.h"
25 #include "mpdclient.h"
26 #include "utils.h"
27 #include "wreadln.h"
28 #include "screen_utils.h"
29 #include "Compiler.h"
31 #include <mpd/client.h>
33 #include <glib.h>
35 #include <string.h>
37 #ifndef NCMPC_MINI
39 typedef struct
40 {
41         GList **list;
42         struct mpdclient *c;
43 } completion_callback_data_t;
45 /**
46  * Wrapper for strncmp().  We are not allowed to pass &strncmp to
47  * g_completion_set_compare(), because strncmp() takes size_t where
48  * g_completion_set_compare passes a gsize value.
49  */
50 static gint
51 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
52 {
53         return strncmp(s1, s2, n);
54 }
56 static void
57 save_pre_completion_cb(GCompletion *gcmp, gcc_unused gchar *line,
58                        void *data)
59 {
60         completion_callback_data_t *tmp = (completion_callback_data_t *)data;
61         GList **list = tmp->list;
62         struct mpdclient *c = tmp->c;
64         if( *list == NULL ) {
65                 /* create completion list */
66                 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
67                 g_completion_add_items(gcmp, *list);
68         }
69 }
71 static void
72 save_post_completion_cb(gcc_unused GCompletion *gcmp,
73                         gcc_unused gchar *line, GList *items,
74                         gcc_unused void *data)
75 {
76         if (g_list_length(items) >= 1)
77                 screen_display_completion_list(items);
78 }
80 #endif
82 int
83 playlist_save(struct mpdclient *c, char *name, char *defaultname)
84 {
85         struct mpd_connection *connection;
86         gchar *filename;
88 #ifdef NCMPC_MINI
89         (void)defaultname;
90 #endif
92 #ifndef NCMPC_MINI
93         if (name == NULL) {
94                 /* initialize completion support */
95                 GCompletion *gcmp = g_completion_new(NULL);
96                 g_completion_set_compare(gcmp, completion_strncmp);
97                 GList *list = NULL;
98                 completion_callback_data_t data = {
99                         .list = &list,
100                         .c = c,
101                 };
102                 wrln_completion_callback_data = &data;
103                 wrln_pre_completion_callback = save_pre_completion_cb;
104                 wrln_post_completion_callback = save_post_completion_cb;
107                 /* query the user for a filename */
108                 filename = screen_readln(_("Save queue as"),
109                                          defaultname,
110                                          NULL,
111                                          gcmp);
113                 /* destroy completion support */
114                 wrln_completion_callback_data = NULL;
115                 wrln_pre_completion_callback = NULL;
116                 wrln_post_completion_callback = NULL;
117                 g_completion_free(gcmp);
118                 list = string_list_free(list);
119                 if( filename )
120                         filename=g_strstrip(filename);
121         } else
122 #endif
123                 filename=g_strdup(name);
125         if (filename == NULL)
126                 return -1;
128         /* send save command to mpd */
130         connection = mpdclient_get_connection(c);
131         if (connection == NULL) {
132                 g_free(filename);
133                 return -1;
134         }
136         char *filename_utf8 = locale_to_utf8(filename);
137         if (!mpd_run_save(connection, filename_utf8)) {
138                 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
139                     mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
140                     mpd_connection_clear_error(connection)) {
141                         char *buf = g_strdup_printf(_("Replace %s?"), filename);
142                         bool replace = screen_get_yesno(buf, false);
143                         g_free(buf);
145                         if (!replace) {
146                                 g_free(filename_utf8);
147                                 g_free(filename);
148                                 screen_status_printf(_("Aborted"));
149                                 return -1;
150                         }
152                         if (!mpd_run_rm(connection, filename_utf8) ||
153                             !mpd_run_save(connection, filename_utf8)) {
154                                 mpdclient_handle_error(c);
155                                 g_free(filename_utf8);
156                                 g_free(filename);
157                                 return -1;
158                         }
159                 } else {
160                         mpdclient_handle_error(c);
161                         g_free(filename_utf8);
162                         g_free(filename);
163                         return -1;
164                 }
165         }
167         c->events |= MPD_IDLE_STORED_PLAYLIST;
169         g_free(filename_utf8);
171         /* success */
172         screen_status_printf(_("Saved %s"), filename);
173         g_free(filename);
174         return 0;