Code

827f16b059129ee48a5ba312046a7021d2d3fde8
[ncmpc.git] / src / screen_file.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
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;
86         if (browser.lw->selected >= filelist_length(browser.filelist))
87                 return -1;
89         entry = filelist_get(browser.filelist, browser.lw->selected);
90         if( entry && entry->entity ) {
91                 mpd_InfoEntity *entity = entry->entity;
92                 if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
93                         mpd_PlaylistFile *plf = entity->info.playlistFile;
94                         defaultname = plf->path;
95                 }
96         }
98         defaultname = utf8_to_locale(defaultname);
99         ret = playlist_save(c, NULL, defaultname);
100         g_free(defaultname);
102         return ret;
105 static int
106 handle_delete(mpdclient_t *c)
108         filelist_entry_t *entry;
109         mpd_InfoEntity *entity;
110         mpd_PlaylistFile *plf;
111         char *str, *buf;
112         int key;
114         if (browser.lw->selected >= filelist_length(browser.filelist))
115                 return -1;
117         entry = filelist_get(browser.filelist, browser.lw->selected);
118         if( entry==NULL || entry->entity==NULL )
119                 return -1;
121         entity = entry->entity;
123         if( entity->type!=MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
124                 /* translators: the "delete" command is only possible
125                    for playlists; the user attempted to delete a song
126                    or a directory or something else */
127                 screen_status_printf(_("Deleting this item is not possible"));
128                 screen_bell();
129                 return -1;
130         }
132         plf = entity->info.playlistFile;
133         str = utf8_to_locale(g_basename(plf->path));
134         buf = g_strdup_printf(_("Delete playlist %s [%s/%s] ? "), str, YES, NO);
135         g_free(str);
136         key = tolower(screen_getch(screen.status_window.w, buf));
137         g_free(buf);
138         if( key != YES[0] ) {
139                 /* translators: a dialog was aborted by the user */
140                 screen_status_printf(_("Aborted"));
141                 return 0;
142         }
144         if( mpdclient_cmd_delete_playlist(c, plf->path) )
145                 return -1;
147         /* translators: MPD deleted the playlist, as requested by the
148            user */
149         screen_status_printf(_("Playlist deleted"));
150         return 0;
153 static void
154 browse_init(WINDOW *w, int cols, int rows)
156         browser.lw = list_window_init(w, cols, rows);
159 static void
160 browse_resize(int cols, int rows)
162         browser.lw->cols = cols;
163         browser.lw->rows = rows;
166 static void
167 browse_exit(void)
169         if (browser.filelist)
170                 filelist_free(browser.filelist);
171         list_window_free(browser.lw);
174 static void
175 browse_open(G_GNUC_UNUSED mpdclient_t *c)
177         if (browser.filelist == NULL) {
178                 browser.filelist = mpdclient_filelist_get(c, "");
179 #ifndef NCMPC_MINI
180                 mpdclient_install_playlist_callback(c, playlist_changed_callback);
181 #endif
182                 mpdclient_install_browse_callback(c, file_changed_callback);
183         }
186 static const char *
187 browse_title(char *str, size_t size)
189         const char *path = NULL, *prev = NULL, *slash = browser.filelist->path;
190         char *path_locale;
192         /* determine the last 2 parts of the path */
193         while ((slash = strchr(slash, '/')) != NULL) {
194                 path = prev;
195                 prev = ++slash;
196         }
198         if (path == NULL)
199                 /* fall back to full path */
200                 path = browser.filelist->path;
202         path_locale = utf8_to_locale(path);
203         g_snprintf(str, size, "%s: %s",
204                    /* translators: caption of the browser screen */
205                    _("Browse"), path_locale);
206         g_free(path_locale);
207         return str;
210 static void
211 browse_paint(void)
213         list_window_paint(browser.lw, browser_lw_callback, browser.filelist);
216 static bool
217 browse_cmd(mpdclient_t *c, command_t cmd)
219         switch(cmd) {
220         case CMD_GO_ROOT_DIRECTORY:
221                 browser_change_directory(&browser, c, NULL, "");
222                 file_repaint();
223                 return true;
224         case CMD_GO_PARENT_DIRECTORY:
225                 browser_change_directory(&browser, c, NULL, "..");
226                 file_repaint();
227                 return true;
229         case CMD_LOCATE:
230                 /* don't let browser_cmd() evaluate the locate command
231                    - it's a no-op, and by the way, leads to a
232                    segmentation fault in the current implementation */
233                 return false;
235         case CMD_DELETE:
236                 handle_delete(c);
237                 file_repaint();
238                 break;
239         case CMD_SAVE_PLAYLIST:
240                 handle_save(c);
241                 break;
242         case CMD_SCREEN_UPDATE:
243                 browser.filelist = mpdclient_filelist_update(c, browser.filelist);
244 #ifndef NCMPC_MINI
245                 sync_highlights(c, browser.filelist);
246 #endif
247                 list_window_check_selected(browser.lw,
248                                            filelist_length(browser.filelist));
249                 file_repaint();
250                 return false;
252         case CMD_DB_UPDATE:
253                 if (c->status == NULL)
254                         return true;
256                 if (!c->status->updatingDb) {
257                         if (mpdclient_cmd_db_update(c, browser.filelist->path) == 0) {
258                                 if (strcmp(browser.filelist->path, "")) {
259                                         char *path_locale =
260                                                 utf8_to_locale(browser.filelist->path);
261                                         screen_status_printf(_("Database update of %s started"),
262                                                              path_locale);
263                                         g_free(path_locale);
264                                 } else
265                                         screen_status_printf(_("Database update started"));
267                                 /* set updatingDb to make shure the browse callback gets called
268                                  * even if the updated has finished before status is updated */
269                                 c->status->updatingDb = 1;
270                         }
271                 } else
272                         screen_status_printf(_("Database update running..."));
273                 return true;
275         default:
276                 break;
277         }
279         if (browser_cmd(&browser, c, cmd)) {
280                 if (screen_is_visible(&screen_browse))
281                         file_repaint();
282                 return true;
283         }
285         return false;
288 const struct screen_functions screen_browse = {
289         .init = browse_init,
290         .exit = browse_exit,
291         .open = browse_open,
292         .resize = browse_resize,
293         .paint = browse_paint,
294         .cmd = browse_cmd,
295         .get_title = browse_title,
296 };
298 bool
299 screen_file_goto_song(struct mpdclient *c, const struct mpd_song *song)
301         const char *slash, *parent;
302         char *allocated = NULL;
303         bool ret;
304         int i;
306         assert(song != NULL);
307         assert(song->file != NULL);
309         if (strstr(song->file, "//") != NULL)
310                 /* an URL? */
311                 return false;
313         /* determine the song's parent directory and go there */
315         slash = strrchr(song->file, '/');
316         if (slash != NULL)
317                 parent = allocated = g_strndup(song->file, slash - song->file);
318         else
319                 parent = "";
321         ret = browser_change_directory(&browser, c, NULL, parent);
322         g_free(allocated);
323         if (!ret)
324                 return false;
326         /* select the specified song */
328         i = filelist_find_song(browser.filelist, song);
329         if (i < 0)
330                 i = 0;
332         list_window_set_selected(browser.lw, i);
334         /* finally, switch to the file screen */
335         screen_switch(&screen_browse, c);
336         return true;