Code

fixed crash while saving a playlist
[ncmpc.git] / src / screen_file.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
18 */
19 #include "config.h"
20 #include "i18n.h"
21 #include "options.h"
22 #include "charset.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "screen_browser.h"
28 #include "screen_play.h"
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <glib.h>
35 static struct screen_browser browser;
37 static void
38 browse_paint(void);
40 static void
41 file_repaint(void)
42 {
43         browse_paint();
44         wrefresh(browser.lw->w);
45 }
47 static void
48 file_repaint_if_active(void)
49 {
50         if (screen_is_visible(&screen_browse))
51                 file_repaint();
52 }
54 /* the db have changed -> update the filelist */
55 static void
56 file_changed_callback(mpdclient_t *c, G_GNUC_UNUSED int event,
57                       G_GNUC_UNUSED gpointer data)
58 {
59         browser.filelist = mpdclient_filelist_update(c, browser.filelist);
60 #ifndef NCMPC_MINI
61         sync_highlights(c, browser.filelist);
62 #endif
63         list_window_check_selected(browser.lw, filelist_length(browser.filelist));
65         file_repaint_if_active();
66 }
68 #ifndef NCMPC_MINI
69 /* the playlist have been updated -> fix highlights */
70 static void
71 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
72 {
73         browser_playlist_changed(&browser, c, event, data);
75         file_repaint_if_active();
76 }
77 #endif
79 static int
80 handle_save(mpdclient_t *c)
81 {
82         filelist_entry_t *entry;
83         char *defaultname = NULL;
84         int ret;
85         unsigned selected;
87         if (browser.lw->selected >= filelist_length(browser.filelist))
88                 return -1;
90         for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
91         {
92                 entry = filelist_get(browser.filelist, selected);
93                 if( entry && entry->entity ) {
94                         mpd_InfoEntity *entity = entry->entity;
95                         if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
96                                 mpd_PlaylistFile *plf = entity->info.playlistFile;
97                                 defaultname = plf->path;
98                         }
99                 }
100         }
102         if(defaultname)
103                 defaultname = utf8_to_locale(defaultname);
104         ret = playlist_save(c, NULL, defaultname);
105         g_free(defaultname);
107         return ret;
110 static int
111 handle_delete(mpdclient_t *c)
113         filelist_entry_t *entry;
114         mpd_InfoEntity *entity;
115         mpd_PlaylistFile *plf;
116         char *str, *buf;
117         int key;
118         unsigned selected;
120         for(selected = browser.lw->selected_start; selected <= browser.lw->selected_end; ++selected)
121         {
122                 if (selected >= filelist_length(browser.filelist))
123                         return -1;
125                 entry = filelist_get(browser.filelist, selected);
126                 if( entry==NULL || entry->entity==NULL )
127                         continue;
129                 entity = entry->entity;
131                 if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
132                         /* translators: the "delete" command is only possible
133                            for playlists; the user attempted to delete a song
134                            or a directory or something else */
135                         screen_status_printf(_("Deleting this item is not possible"));
136                         screen_bell();
137                         continue;
138                 }
140                 plf = entity->info.playlistFile;
141                 str = utf8_to_locale(g_basename(plf->path));
142                 buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
143                 g_free(str);
144                 key = tolower(screen_getch(screen.status_window.w, buf));
145                 g_free(buf);
146                 if( key != YES[0] ) {
147                         /* translators: a dialog was aborted by the user */
148                         screen_status_printf(_("Aborted"));
149                         return 0;
150                 }
152                 if( mpdclient_cmd_delete_playlist(c, plf->path) )
153                         continue;
155                 /* translators: MPD deleted the playlist, as requested by the
156                    user */
157                 screen_status_printf(_("Playlist deleted"));
158         }
159         return 0;
162 static void
163 browse_init(WINDOW *w, int cols, int rows)
165         browser.lw = list_window_init(w, cols, rows);
168 static void
169 browse_resize(int cols, int rows)
171         browser.lw->cols = cols;
172         browser.lw->rows = rows;
175 static void
176 browse_exit(void)
178         if (browser.filelist)
179                 filelist_free(browser.filelist);
180         list_window_free(browser.lw);
183 static void
184 browse_open(G_GNUC_UNUSED mpdclient_t *c)
186         if (browser.filelist == NULL) {
187                 browser.filelist = mpdclient_filelist_get(c, "");
188 #ifndef NCMPC_MINI
189                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
190 #endif
191                 mpdclient_install_browse_callback(c, file_changed_callback);
192         }
195 static const char *
196 browse_title(char *str, size_t size)
198         const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
199         char *path_locale;
201         /* determine the last 2 parts of the path */
202         while ((slash = strchr(slash, '/')) != NULL) {
203                 path = prev;
204                 prev = ++slash;
205         }
207         if (path == NULL)
208                 /* fall back to full path */
209                 path = browser.filelist->path;
211         path_locale = utf8_to_locale(path);
212         g_snprintf(str, size, "%s: %s",
213                    /* translators: caption of the browser screen */
214                    _("Browse"), path_locale);
215         g_free(path_locale);
216         return str;
219 static void
220 browse_paint(void)
222         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
225 static bool
226 browse_cmd(mpdclient_t *c, command_t cmd)
228         switch(cmd) {
229         case CMD_GO_ROOT_DIRECTORY:
230                 browser_change_directory(&browser, c, NULL, "");
231                 file_repaint();
232                 return true;
233         case CMD_GO_PARENT_DIRECTORY:
234                 browser_change_directory(&browser, c, NULL, "..");
235                 file_repaint();
236                 return true;
238         case CMD_LOCATE:
239                 /* don't let browser_cmd() evaluate the locate command
240                    - it's a no-op, and by the way, leads to a
241                    segmentation fault in the current implementation */
242                 return false;
244         case CMD_DELETE:
245                 handle_delete(c);
246                 file_repaint();
247                 break;
248         case CMD_SAVE_PLAYLIST:
249                 handle_save(c);
250                 break;
251         case CMD_SCREEN_UPDATE:
252                 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
253 #ifndef NCMPC_MINI
254                 sync_highlights(c, browser.filelist);
255 #endif
256                 list_window_check_selected(browser.lw,
257                                            filelist_length(browser.filelist));
258                 file_repaint();
259                 return false;
261         case CMD_DB_UPDATE:
262                 if (c->status == NULL)
263                         return true;
265                 if (!c->status->updatingDb) {
266                         if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
267                                 if (strcmp(browser.filelist->path, "")) {
268                                         char *path_locale =
269                                                 utf8_to_locale(browser.filelist->path);
270                                         screen_status_printf(_("Database update of %s started"),
271                                                              path_locale);
272                                         g_free(path_locale);
273                                 } else
274                                         screen_status_printf(_("Database update started"));
276                                 /* set updatingDb to make shure the browse callback gets called
277                                  * even if the updated has finished before status is updated */
278                                 c->status->updatingDb = 1;
279                         }
280                 } else
281                         screen_status_printf(_("Database update running..."));
282                 return true;
284         default:
285                 break;
286         }
288         if (browser_cmd(&browser, c, cmd)) {
289                 if (screen_is_visible(&screen_browse))
290                         file_repaint();
291                 return true;
292         }
294         return false;
297 const struct screen_functions screen_browse = {
298         .init = browse_init,
299         .exit = browse_exit,
300         .open = browse_open,
301         .resize = browse_resize,
302         .paint = browse_paint,
303         .cmd = browse_cmd,
304         .get_title = browse_title,
305 };
307 bool
308 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
310         const char *slash, *parent;
311         char *allocated = NULL;
312         bool ret;
313         int i;
315         assert(song != NULL);
316         assert(song->file != NULL);
318         if (strstr(song->file, "//") != NULL)
319                 /* an URL? */
320                 return false;
322         /* determine the song's parent directory and go there */
324         slash = strrchr(song->file, '/');
325         if (slash != NULL)
326                 parent = allocated = g_strndup(song->file, slash - song->file);
327         else
328                 parent = "";
330         ret = browser_change_directory(&browser, c, NULL, parent);
331         g_free(allocated);
332         if (!ret)
333                 return false;
335         /* select the specified song */
337         i = filelist_find_song(browser.filelist, song);
338         if (i < 0)
339                 i = 0;
341         list_window_set_selected(browser.lw, i);
343         /* finally, switch to the file screen */
344         screen_switch(&screen_browse, c);
345         return true;