utils: move gcmp_list_from_path() to db_completion.c
[ncmpc.git] / src / save_playlist.c
blobed2134d280e69215af7e63b2c5289dd756ce830c
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://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 "save_playlist.h"
21 #include "db_completion.h"
22 #include "screen_status.h"
23 #include "config.h"
24 #include "i18n.h"
25 #include "charset.h"
26 #include "mpdclient.h"
27 #include "utils.h"
28 #include "wreadln.h"
29 #include "screen_utils.h"
30 #include "Compiler.h"
32 #include <mpd/client.h>
34 #include <glib.h>
36 #include <string.h>
38 #ifndef NCMPC_MINI
40 typedef struct
42 GList **list;
43 struct mpdclient *c;
44 } completion_callback_data_t;
46 /**
47 * Wrapper for strncmp(). We are not allowed to pass &strncmp to
48 * g_completion_set_compare(), because strncmp() takes size_t where
49 * g_completion_set_compare passes a gsize value.
51 static gint
52 completion_strncmp(const gchar *s1, const gchar *s2, gsize n)
54 return strncmp(s1, s2, n);
57 static void
58 save_pre_completion_cb(GCompletion *gcmp, gcc_unused gchar *line,
59 void *data)
61 completion_callback_data_t *tmp = (completion_callback_data_t *)data;
62 GList **list = tmp->list;
63 struct mpdclient *c = tmp->c;
65 if( *list == NULL ) {
66 /* create completion list */
67 *list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
68 g_completion_add_items(gcmp, *list);
72 static void
73 save_post_completion_cb(gcc_unused GCompletion *gcmp,
74 gcc_unused gchar *line, GList *items,
75 gcc_unused void *data)
77 if (g_list_length(items) >= 1)
78 screen_display_completion_list(items);
81 #endif
83 int
84 playlist_save(struct mpdclient *c, char *name, char *defaultname)
86 struct mpd_connection *connection;
87 gchar *filename;
89 #ifdef NCMPC_MINI
90 (void)defaultname;
91 #endif
93 #ifndef NCMPC_MINI
94 if (name == NULL) {
95 /* initialize completion support */
96 GCompletion *gcmp = g_completion_new(NULL);
97 g_completion_set_compare(gcmp, completion_strncmp);
98 GList *list = NULL;
99 completion_callback_data_t data = {
100 .list = &list,
101 .c = c,
103 wrln_completion_callback_data = &data;
104 wrln_pre_completion_callback = save_pre_completion_cb;
105 wrln_post_completion_callback = save_post_completion_cb;
108 /* query the user for a filename */
109 filename = screen_readln(_("Save queue as"),
110 defaultname,
111 NULL,
112 gcmp);
113 if (filename == NULL)
114 return -1;
116 /* destroy completion support */
117 wrln_completion_callback_data = NULL;
118 wrln_pre_completion_callback = NULL;
119 wrln_post_completion_callback = NULL;
120 g_completion_free(gcmp);
121 list = string_list_free(list);
122 filename = g_strstrip(filename);
123 } else
124 #endif
125 filename=g_strdup(name);
127 /* send save command to mpd */
129 connection = mpdclient_get_connection(c);
130 if (connection == NULL) {
131 g_free(filename);
132 return -1;
135 char *filename_utf8 = locale_to_utf8(filename);
136 if (!mpd_run_save(connection, filename_utf8)) {
137 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
138 mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
139 mpd_connection_clear_error(connection)) {
140 char *buf = g_strdup_printf(_("Replace %s?"), filename);
141 bool replace = screen_get_yesno(buf, false);
142 g_free(buf);
144 if (!replace) {
145 g_free(filename_utf8);
146 g_free(filename);
147 screen_status_printf(_("Aborted"));
148 return -1;
151 if (!mpd_run_rm(connection, filename_utf8) ||
152 !mpd_run_save(connection, filename_utf8)) {
153 mpdclient_handle_error(c);
154 g_free(filename_utf8);
155 g_free(filename);
156 return -1;
158 } else {
159 mpdclient_handle_error(c);
160 g_free(filename_utf8);
161 g_free(filename);
162 return -1;
166 c->events |= MPD_IDLE_STORED_PLAYLIST;
168 g_free(filename_utf8);
170 /* success */
171 screen_status_printf(_("Saved %s"), filename);
172 g_free(filename);
173 return 0;