Code

screen_browser: export browser_get_selected_entry()
[ncmpc.git] / src / screen_browser.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 */
20 #include "screen_browser.h"
21 #include "i18n.h"
22 #include "options.h"
23 #include "charset.h"
24 #include "strfsong.h"
25 #include "screen_utils.h"
27 #include <string.h>
29 #define BUFSIZE 1024
31 #ifndef NCMPC_MINI
32 #define HIGHLIGHT  (0x01)
33 #endif
35 static const char playlist_format[] = "*%s*";
37 #ifndef NCMPC_MINI
39 /* clear the highlight flag for all items in the filelist */
40 static void
41 clear_highlights(mpdclient_filelist_t *fl)
42 {
43         guint i;
45         for (i = 0; i < filelist_length(fl); ++i) {
46                 struct filelist_entry *entry = filelist_get(fl, i);
48                 entry->flags &= ~HIGHLIGHT;
49         }
50 }
52 /* change the highlight flag for a song */
53 static void
54 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
55 {
56         int i = filelist_find_song(fl, song);
57         struct filelist_entry *entry;
59         if (i < 0)
60                 return;
62         entry = filelist_get(fl, i);
63         if (highlight)
64                 entry->flags |= HIGHLIGHT;
65         else
66                 entry->flags &= ~HIGHLIGHT;
67 }
69 /* sync highlight flags with playlist */
70 void
71 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
72 {
73         guint i;
75         for (i = 0; i < filelist_length(fl); ++i) {
76                 struct filelist_entry *entry = filelist_get(fl, i);
77                 mpd_InfoEntity *entity = entry->entity;
79                 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
80                         mpd_Song *song = entity->info.song;
82                         if (playlist_get_index_from_same_song(c, song) >= 0)
83                                 entry->flags |= HIGHLIGHT;
84                         else
85                                 entry->flags &= ~HIGHLIGHT;
86                 }
87         }
88 }
90 /* the playlist has been updated -> fix highlights */
91 void
92 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
93                          int event, gpointer data)
94 {
95         if (browser->filelist == NULL)
96                 return;
98         switch(event) {
99         case PLAYLIST_EVENT_CLEAR:
100                 clear_highlights(browser->filelist);
101                 break;
102         case PLAYLIST_EVENT_ADD:
103                 set_highlight(browser->filelist, (mpd_Song *) data, 1);
104                 break;
105         case PLAYLIST_EVENT_DELETE:
106                 set_highlight(browser->filelist, (mpd_Song *) data, 0);
107                 break;
108         case PLAYLIST_EVENT_MOVE:
109                 break;
110         default:
111                 sync_highlights(c, browser->filelist);
112                 break;
113         }
116 #endif
118 /* list_window callback */
119 const char *
120 browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_column, void *data)
122         static char buf[BUFSIZE];
123         mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
124         filelist_entry_t *entry;
125         mpd_InfoEntity *entity;
127         if (idx >= filelist_length(fl))
128                 return NULL;
130         entry = filelist_get(fl, idx);
131         assert(entry != NULL);
133         entity = entry->entity;
134 #ifndef NCMPC_MINI
135         *highlight = (entry->flags & HIGHLIGHT) != 0;
136 #else
137         *highlight = false;
138 #endif
140         if( entity == NULL )
141                 return "[..]";
143         if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
144                 mpd_Directory *dir = entity->info.directory;
145                 char *directory = utf8_to_locale(g_basename(dir->path));
147                 g_snprintf(buf, BUFSIZE, "[%s]", directory);
148                 g_free(directory);
149                 return buf;
150         } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
151                 mpd_Song *song = entity->info.song;
153                 strfsong(buf, BUFSIZE, options.list_format, song);
154                 return buf;
155         } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
156                 mpd_PlaylistFile *plf = entity->info.playlistFile;
157                 char *filename = utf8_to_locale(g_basename(plf->path));
159                 g_snprintf(buf, BUFSIZE, playlist_format, filename);
160                 g_free(filename);
161                 return buf;
162         }
164         return "Error: Unknown entry!";
167 /* chdir */
168 bool
169 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
170                          filelist_entry_t *entry, const char *new_path)
172         mpd_InfoEntity *entity = NULL;
173         gchar *path = NULL;
174         char *old_path;
175         int idx;
177         if( entry!=NULL )
178                 entity = entry->entity;
179         else if( new_path==NULL )
180                 return false;
182         if( entity==NULL ) {
183                 if( entry || 0==strcmp(new_path, "..") ) {
184                         /* return to parent */
185                         char *parent = g_path_get_dirname(browser->filelist->path);
186                         if( strcmp(parent, ".") == 0 )
187                                 parent[0] = '\0';
188                         path = g_strdup(parent);
189                         g_free(parent);
190                 } else {
191                         /* entry==NULL, then new_path ("" is root) */
192                         path = g_strdup(new_path);
193                 }
194         } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
195                 /* enter sub */
196                 mpd_Directory *dir = entity->info.directory;
197                 path = g_strdup(dir->path);
198         } else
199                 return false;
201         if (browser->filelist != NULL) {
202                 old_path = g_strdup(browser->filelist->path);
203                 filelist_free(browser->filelist);
204         } else
205                 old_path = NULL;
207         browser->filelist = mpdclient_filelist_get(c, path);
208 #ifndef NCMPC_MINI
209         sync_highlights(c, browser->filelist);
210 #endif
212         idx = old_path != NULL
213                 ? filelist_find_directory(browser->filelist, old_path)
214                 : -1;
215         g_free(old_path);
217         list_window_reset(browser->lw);
218         if (idx >= 0) {
219                 list_window_set_selected(browser->lw, idx);
220                 list_window_center(browser->lw,
221                                    filelist_length(browser->filelist), idx);
222         }
224         g_free(path);
225         return true;
228 static bool
229 load_playlist(mpdclient_t *c, const mpd_PlaylistFile *plf)
231         char *filename = utf8_to_locale(plf->path);
233         if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
234                 screen_status_printf(_("Loading playlist %s..."),
235                                      g_basename(filename));
236         g_free(filename);
237         return true;
240 static bool
241 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
243         int idx;
244         mpd_InfoEntity *entity = entry->entity;
245         mpd_Song *song = entity->info.song;
247 #ifndef NCMPC_MINI
248         if (!(entry->flags & HIGHLIGHT)) {
249 #endif
250                 if (mpdclient_cmd_add(c, song) == 0) {
251                         char buf[BUFSIZE];
253 #ifndef NCMPC_MINI
254                         entry->flags |= HIGHLIGHT;
255 #endif
256                         strfsong(buf, BUFSIZE, options.list_format, song);
257                         screen_status_printf(_("Adding \'%s\' to playlist"), buf);
258                         mpdclient_update(c); /* get song id */
259                 } else
260                         return false;
261 #ifndef NCMPC_MINI
262         }
263 #endif
265         idx = playlist_get_index_from_same_song(c, song);
266         mpdclient_cmd_play(c, idx);
267         return true;
270 struct filelist_entry *
271 browser_get_selected_entry(const struct screen_browser *browser)
273         if (browser->filelist == NULL ||
274             browser->lw->selected_start < browser->lw->selected_end ||
275             browser->lw->selected >= filelist_length(browser->filelist))
276                 return NULL;
278         return filelist_get(browser->filelist, browser->lw->selected);
281 static struct mpd_InfoEntity *
282 browser_get_selected_entity(const struct screen_browser *browser)
284         struct filelist_entry *entry = browser_get_selected_entry(browser);
286         return entry != NULL
287                 ? entry->entity
288                 : NULL;
291 static struct mpd_song *
292 browser_get_selected_song(const struct screen_browser *browser)
294         struct mpd_InfoEntity *entity = browser_get_selected_entity(browser);
296         return entity != NULL && entity->type == MPD_INFO_ENTITY_TYPE_SONG
297                 ? entity->info.song
298                 : NULL;
301 static struct filelist_entry *
302 browser_get_index(const struct screen_browser *browser, unsigned i)
304         if (browser->filelist == NULL ||
305             i >= filelist_length(browser->filelist))
306                 return NULL;
308         return filelist_get(browser->filelist, i);
311 static bool
312 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
314         struct filelist_entry *entry = browser_get_selected_entry(browser);
315         mpd_InfoEntity *entity;
317         if (entry == NULL)
318                 return false;
320         entity = entry->entity;
321         if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
322                 return browser_change_directory(browser, c, entry, NULL);
323         else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
324                 return load_playlist(c, entity->info.playlistFile);
325         else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
326                 return enqueue_and_play(c, entry);
327         return false;
330 static bool
331 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
332                      G_GNUC_UNUSED gboolean toggle)
334         assert(entry != NULL);
335         assert(entry->entity != NULL);
337         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
338                 return load_playlist(c, entry->entity->info.playlistFile);
340         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
341                 mpd_Directory *dir = entry->entity->info.directory;
343                 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
344                         char *tmp = utf8_to_locale(dir->path);
346                         screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
347                         g_free(tmp);
348                 }
350                 return true;
351         }
353         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
354                 return false;
356         assert(entry->entity->info.song != NULL);
358 #ifndef NCMPC_MINI
359         if (!toggle || (entry->flags & HIGHLIGHT) == 0)
360 #endif
361         {
362                 mpd_Song *song = entry->entity->info.song;
364 #ifndef NCMPC_MINI
365                 entry->flags |= HIGHLIGHT;
366 #endif
368                 if (mpdclient_cmd_add(c, song) == 0) {
369                         char buf[BUFSIZE];
371                         strfsong(buf, BUFSIZE, options.list_format, song);
372                         screen_status_printf(_("Adding \'%s\' to playlist"), buf);
373                 }
374 #ifndef NCMPC_MINI
375         } else {
376                 /* remove song from playlist */
377                 mpd_Song *song = entry->entity->info.song;
378                 int idx;
380                 entry->flags &= ~HIGHLIGHT;
382                 while ((idx = playlist_get_index_from_same_song(c, song)) >= 0)
383                         mpdclient_cmd_delete(c, idx);
384 #endif
385         }
387         return true;
390 static bool
391 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
393         struct filelist_entry *entry;
395         if (browser->lw->range_selection) {
396                 for (unsigned i = browser->lw->selected_start;
397                          i <= browser->lw->selected_end; i++) {
398                         entry = browser_get_index(browser, i);
400                         if (entry != NULL && entry->entity != NULL)
401                                 browser_select_entry(c, entry, TRUE);
402                 }
403                 return false;
404         } else {
405                 entry = browser_get_selected_entry(browser);
407                 if (entry == NULL || entry->entity == NULL)
408                         return false;
410                 return browser_select_entry(c, entry, TRUE);
411         }
414 static bool
415 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
417         struct filelist_entry *entry;
419         if (browser->lw->range_selection) {
420                 for (unsigned i = browser->lw->selected_start;
421                          i <= browser->lw->selected_end; i++) {
422                         entry = browser_get_index(browser, i);
424                         if (entry != NULL && entry->entity != NULL)
425                                 browser_select_entry(c, entry, FALSE);
426                 }
427                 return false;
428         } else {
429                 entry = browser_get_selected_entry(browser);
431                 if (entry == NULL || entry->entity == NULL)
432                         return false;
434                 return browser_select_entry(c, entry, FALSE);
435         }
438 static void
439 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
441         guint i;
443         if (browser->filelist == NULL)
444                 return;
446         for (i = 0; i < filelist_length(browser->filelist); ++i) {
447                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
449                 if (entry != NULL && entry->entity != NULL)
450                         browser_select_entry(c, entry, FALSE);
451         }
454 #ifdef HAVE_GETMOUSE
455 static int
456 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
458         int row;
459         unsigned prev_selected = browser->lw->selected;
460         unsigned long bstate;
461         int length;
463         if (browser->filelist)
464                 length = filelist_length(browser->filelist);
465         else
466                 length = 0;
468         if (screen_get_mouse_event(c, &bstate, &row) ||
469             list_window_mouse(browser->lw, length, bstate, row))
470                 return 1;
472         browser->lw->selected = browser->lw->start + row;
473         list_window_check_selected(browser->lw, length);
475         if( bstate & BUTTON1_CLICKED ) {
476                 if (prev_selected == browser->lw->selected)
477                         browser_handle_enter(browser, c);
478         } else if (bstate & BUTTON3_CLICKED) {
479                 if (prev_selected == browser->lw->selected)
480                         browser_handle_select(browser, c);
481         }
483         return 1;
485 #endif
487 bool
488 browser_cmd(struct screen_browser *browser,
489             struct mpdclient *c, command_t cmd)
491         struct mpd_song *song;
493         switch (cmd) {
494         case CMD_PLAY:
495                 browser_handle_enter(browser, c);
496                 return true;
498         case CMD_SELECT:
499                 if (browser_handle_select(browser, c))
500                         /* continue and select next item... */
501                         cmd = CMD_LIST_NEXT;
503                 /* call list_window_cmd to go to the next item */
504                 break;
506         case CMD_ADD:
507                 if (browser_handle_add(browser, c))
508                         /* continue and select next item... */
509                         cmd = CMD_LIST_NEXT;
511                 /* call list_window_cmd to go to the next item */
512                 break;
514         case CMD_SELECT_ALL:
515                 browser_handle_select_all(browser, c);
516                 return true;
518         case CMD_LIST_FIND:
519         case CMD_LIST_RFIND:
520         case CMD_LIST_FIND_NEXT:
521         case CMD_LIST_RFIND_NEXT:
522                 screen_find(browser->lw, filelist_length(browser->filelist),
523                             cmd, browser_lw_callback,
524                             browser->filelist);
525                 return true;
526         case CMD_LIST_JUMP:
527                 screen_jump(browser->lw, browser_lw_callback, browser->filelist);
528                 return true;
530 #ifdef HAVE_GETMOUSE
531         case CMD_MOUSE_EVENT:
532                 browser_handle_mouse_event(browser, c);
533                 return true;
534 #endif
536 #ifdef ENABLE_SONG_SCREEN
537         case CMD_SCREEN_SONG:
538                 song = browser_get_selected_song(browser);
539                 if (song == NULL)
540                         return false;
542                 screen_song_switch(c, song);
543                 return true;
544 #endif
546         case CMD_LOCATE:
547                 song = browser_get_selected_song(browser);
548                 if (song == NULL)
549                         return false;
551                 screen_file_goto_song(c, song);
552                 return true;
554 #ifdef ENABLE_LYRICS_SCREEN
555         case CMD_SCREEN_LYRICS:
556                 song = browser_get_selected_song(browser);
557                 if (song == NULL)
558                         return false;
560                 screen_lyrics_switch(c, song, false);
561                 return true;
562 #endif
563         case CMD_SCREEN_SWAP:
564                 screen_swap(c, browser_get_selected_song(browser));
565                 return true;
567         default:
568                 break;
569         }
571         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
572                             cmd))
573                 return true;
575         return false;