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);
112 if (filename == NULL)
113 return -1;
115 /* destroy completion support */
116 wrln_completion_callback_data = NULL;
117 wrln_pre_completion_callback = NULL;
118 wrln_post_completion_callback = NULL;
119 g_completion_free(gcmp);
120 list = string_list_free(list);
121 filename = g_strstrip(filename);
122 } else
123 #endif
124 filename=g_strdup(name);
126 /* send save command to mpd */
128 connection = mpdclient_get_connection(c);
129 if (connection == NULL) {
130 g_free(filename);
131 return -1;
132 }
134 char *filename_utf8 = locale_to_utf8(filename);
135 if (!mpd_run_save(connection, filename_utf8)) {
136 if (mpd_connection_get_error(connection) == MPD_ERROR_SERVER &&
137 mpd_connection_get_server_error(connection) == MPD_SERVER_ERROR_EXIST &&
138 mpd_connection_clear_error(connection)) {
139 char *buf = g_strdup_printf(_("Replace %s?"), filename);
140 bool replace = screen_get_yesno(buf, false);
141 g_free(buf);
143 if (!replace) {
144 g_free(filename_utf8);
145 g_free(filename);
146 screen_status_printf(_("Aborted"));
147 return -1;
148 }
150 if (!mpd_run_rm(connection, filename_utf8) ||
151 !mpd_run_save(connection, filename_utf8)) {
152 mpdclient_handle_error(c);
153 g_free(filename_utf8);
154 g_free(filename);
155 return -1;
156 }
157 } else {
158 mpdclient_handle_error(c);
159 g_free(filename_utf8);
160 g_free(filename);
161 return -1;
162 }
163 }
165 c->events |= MPD_IDLE_STORED_PLAYLIST;
167 g_free(filename_utf8);
169 /* success */
170 screen_status_printf(_("Saved %s"), filename);
171 g_free(filename);
172 return 0;
173 }