Code

playlist: addded playlist_get_index_from_same_song()
[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, filelist_entry_t *entry)
231         mpd_InfoEntity *entity = entry->entity;
232         mpd_PlaylistFile *plf = entity->info.playlistFile;
233         char *filename = utf8_to_locale(plf->path);
235         if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
236                 screen_status_printf(_("Loading playlist %s..."),
237                                      g_basename(filename));
238         g_free(filename);
239         return true;
242 static bool
243 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
245         int idx;
246         mpd_InfoEntity *entity = entry->entity;
247         mpd_Song *song = entity->info.song;
249 #ifndef NCMPC_MINI
250         if (!(entry->flags & HIGHLIGHT)) {
251 #endif
252                 if (mpdclient_cmd_add(c, song) == 0) {
253                         char buf[BUFSIZE];
255 #ifndef NCMPC_MINI
256                         entry->flags |= HIGHLIGHT;
257 #endif
258                         strfsong(buf, BUFSIZE, options.list_format, song);
259                         screen_status_printf(_("Adding \'%s\' to playlist"), buf);
260                         mpdclient_update(c); /* get song id */
261                 } else
262                         return false;
263 #ifndef NCMPC_MINI
264         }
265 #endif
267         idx = playlist_get_index_from_same_song(c, song);
268         mpdclient_cmd_play(c, idx);
269         return true;
272 static struct filelist_entry *
273 browser_get_selected(const struct screen_browser *browser)
275         if (browser->filelist == NULL ||
276             browser->lw->selected >= filelist_length(browser->filelist))
277                 return NULL;
279         return filelist_get(browser->filelist, browser->lw->selected);
282 static struct filelist_entry *
283 browser_get_index(const struct screen_browser *browser, unsigned i)
285         if (browser->filelist == NULL ||
286             i >= filelist_length(browser->filelist))
287                 return NULL;
289         return filelist_get(browser->filelist, i);
292 static bool
293 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
295         struct filelist_entry *entry = browser_get_selected(browser);
296         mpd_InfoEntity *entity;
298         if( entry==NULL || browser->lw->selected_start < browser->lw->selected_end)
299                 return false;
301         entity = entry->entity;
302         if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
303                 return browser_change_directory(browser, c, entry, NULL);
304         else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
305                 return load_playlist(c, entry);
306         else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
307                 return enqueue_and_play(c, entry);
308         return false;
311 static bool
312 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
313                      G_GNUC_UNUSED gboolean toggle)
315         assert(entry != NULL);
316         assert(entry->entity != NULL);
318         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
319                 return load_playlist(c, entry);
321         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
322                 mpd_Directory *dir = entry->entity->info.directory;
324                 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
325                         char *tmp = utf8_to_locale(dir->path);
327                         screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
328                         g_free(tmp);
329                 }
331                 return true;
332         }
334         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
335                 return false;
337         assert(entry->entity->info.song != NULL);
339 #ifndef NCMPC_MINI
340         if (!toggle || (entry->flags & HIGHLIGHT) == 0)
341 #endif
342         {
343                 mpd_Song *song = entry->entity->info.song;
345 #ifndef NCMPC_MINI
346                 entry->flags |= HIGHLIGHT;
347 #endif
349                 if (mpdclient_cmd_add(c, song) == 0) {
350                         char buf[BUFSIZE];
352                         strfsong(buf, BUFSIZE, options.list_format, song);
353                         screen_status_printf(_("Adding \'%s\' to playlist"), buf);
354                 }
355 #ifndef NCMPC_MINI
356         } else {
357                 /* remove song from playlist */
358                 mpd_Song *song = entry->entity->info.song;
359                 int idx;
361                 entry->flags &= ~HIGHLIGHT;
363                 while ((idx = playlist_get_index_from_same_song(c, song)) >= 0)
364                         mpdclient_cmd_delete(c, idx);
365 #endif
366         }
368         return true;
371 static bool
372 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
374         struct filelist_entry *entry;
376         if (browser->lw->range_selection) {
377                 for (unsigned i = browser->lw->selected_start;
378                          i <= browser->lw->selected_end; i++) {
379                         entry = browser_get_index(browser, i);
381                         if (entry != NULL && entry->entity != NULL)
382                                 browser_select_entry(c, entry, TRUE);
383                 }
384                 return false;
385         } else {
386                 entry = browser_get_selected(browser);
388                 if (entry == NULL || entry->entity == NULL)
389                         return false;
391                 return browser_select_entry(c, entry, TRUE);
392         }
395 static bool
396 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
398         struct filelist_entry *entry;
400         if (browser->lw->range_selection) {
401                 for (unsigned i = browser->lw->selected_start;
402                          i <= browser->lw->selected_end; i++) {
403                         entry = browser_get_index(browser, i);
405                         if (entry != NULL && entry->entity != NULL)
406                                 browser_select_entry(c, entry, FALSE);
407                 }
408                 return false;
409         } else {
410                 entry = browser_get_selected(browser);
412                 if (entry == NULL || entry->entity == NULL)
413                         return false;
415                 return browser_select_entry(c, entry, FALSE);
416         }
419 static void
420 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
422         guint i;
424         if (browser->filelist == NULL)
425                 return;
427         for (i = 0; i < filelist_length(browser->filelist); ++i) {
428                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
430                 if (entry != NULL && entry->entity != NULL)
431                         browser_select_entry(c, entry, FALSE);
432         }
435 #ifdef HAVE_GETMOUSE
436 static int
437 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
439         int row;
440         unsigned prev_selected = browser->lw->selected;
441         unsigned long bstate;
442         int length;
444         if (browser->filelist)
445                 length = filelist_length(browser->filelist);
446         else
447                 length = 0;
449         if (screen_get_mouse_event(c, &bstate, &row) ||
450             list_window_mouse(browser->lw, length, bstate, row))
451                 return 1;
453         browser->lw->selected = browser->lw->start + row;
454         list_window_check_selected(browser->lw, length);
456         if( bstate & BUTTON1_CLICKED ) {
457                 if (prev_selected == browser->lw->selected)
458                         browser_handle_enter(browser, c);
459         } else if (bstate & BUTTON3_CLICKED) {
460                 if (prev_selected == browser->lw->selected)
461                         browser_handle_select(browser, c);
462         }
464         return 1;
466 #endif
468 bool
469 browser_cmd(struct screen_browser *browser,
470             struct mpdclient *c, command_t cmd)
472         struct filelist_entry *entry;
474         switch (cmd) {
475         case CMD_PLAY:
476                 browser_handle_enter(browser, c);
477                 return true;
479         case CMD_SELECT:
480                 if (browser_handle_select(browser, c))
481                         /* continue and select next item... */
482                         cmd = CMD_LIST_NEXT;
484                 /* call list_window_cmd to go to the next item */
485                 break;
487         case CMD_ADD:
488                 if (browser_handle_add(browser, c))
489                         /* continue and select next item... */
490                         cmd = CMD_LIST_NEXT;
492                 /* call list_window_cmd to go to the next item */
493                 break;
495         case CMD_SELECT_ALL:
496                 browser_handle_select_all(browser, c);
497                 return true;
499         case CMD_LIST_FIND:
500         case CMD_LIST_RFIND:
501         case CMD_LIST_FIND_NEXT:
502         case CMD_LIST_RFIND_NEXT:
503                 screen_find(browser->lw, filelist_length(browser->filelist),
504                             cmd, browser_lw_callback,
505                             browser->filelist);
506                 return true;
507         case CMD_LIST_JUMP:
508                 screen_jump(browser->lw, browser_lw_callback, browser->filelist);
509                 return true;
511 #ifdef HAVE_GETMOUSE
512         case CMD_MOUSE_EVENT:
513                 browser_handle_mouse_event(browser, c);
514                 return true;
515 #endif
517 #ifdef ENABLE_SONG_SCREEN
518         case CMD_SCREEN_SONG:
519                 entry = browser_get_selected(browser);
520                 if (entry == NULL || entry->entity == NULL ||
521                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
522                         return false;
524                 screen_song_switch(c, entry->entity->info.song);
525                 return true;
526 #endif
528         case CMD_LOCATE:
529                 entry = browser_get_selected(browser);
530                 if (entry == NULL || entry->entity == NULL ||
531                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
532                         return false;
534                 screen_file_goto_song(c, entry->entity->info.song);
535                 return true;
537 #ifdef ENABLE_LYRICS_SCREEN
538         case CMD_SCREEN_LYRICS:
539                 entry = browser_get_selected(browser);
540                 if (entry == NULL)
541                         return false;
543                 if (entry->entity == NULL ||
544                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
545                         return true;
547                 screen_lyrics_switch(c, entry->entity->info.song);
548                 return true;
549 #endif
550         case CMD_SCREEN_SWAP:
551                 entry = browser_get_selected(browser);
552                 if (entry->entity != NULL &&
553                         entry->entity->type == MPD_INFO_ENTITY_TYPE_SONG)
554                         screen_swap(c, entry->entity->info.song);
555                 else
556                         screen_swap(c, NULL);
557                 return true;
559         default:
560                 break;
561         }
563         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
564                             cmd))
565                 return true;
567         return false;