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"
26 #include "mpdclient.h"
29 #include "screen_utils.h"
32 #include <mpd/client.h>
44 } completion_callback_data_t
;
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.
52 completion_strncmp(const gchar
*s1
, const gchar
*s2
, gsize n
)
54 return strncmp(s1
, s2
, n
);
58 save_pre_completion_cb(GCompletion
*gcmp
, gcc_unused gchar
*line
,
61 completion_callback_data_t
*tmp
= (completion_callback_data_t
*)data
;
62 GList
**list
= tmp
->list
;
63 struct mpdclient
*c
= tmp
->c
;
66 /* create completion list */
67 *list
= gcmp_list_from_path(c
, "", NULL
, GCMP_TYPE_PLAYLIST
);
68 g_completion_add_items(gcmp
, *list
);
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
);
84 playlist_save(struct mpdclient
*c
, char *name
, char *defaultname
)
86 struct mpd_connection
*connection
;
95 /* initialize completion support */
96 GCompletion
*gcmp
= g_completion_new(NULL
);
97 g_completion_set_compare(gcmp
, completion_strncmp
);
99 completion_callback_data_t data
= {
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"),
113 if (filename
== NULL
)
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
);
125 filename
=g_strdup(name
);
127 /* send save command to mpd */
129 connection
= mpdclient_get_connection(c
);
130 if (connection
== NULL
) {
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);
145 g_free(filename_utf8
);
147 screen_status_printf(_("Aborted"));
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
);
159 mpdclient_handle_error(c
);
160 g_free(filename_utf8
);
166 c
->events
|= MPD_IDLE_STORED_PLAYLIST
;
168 g_free(filename_utf8
);
171 screen_status_printf(_("Saved %s"), filename
);