Code

screen_browser.c: replaced mpd_unused by G_GNUC_UNUSED
[ncmpc.git] / src / screen_browser.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
4  *
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.
9  *
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.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
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 #undef  USE_OLD_ADD
31 #define BUFSIZE 1024
33 #ifndef NCMPC_MINI
34 #define HIGHLIGHT  (0x01)
35 #endif
37 static const char playlist_format[] = "*%s*";
39 #ifndef NCMPC_MINI
41 /* clear the highlight flag for all items in the filelist */
42 static void
43 clear_highlights(mpdclient_filelist_t *fl)
44 {
45         guint i;
47         for (i = 0; i < filelist_length(fl); ++i) {
48                 struct filelist_entry *entry = filelist_get(fl, i);
50                 entry->flags &= ~HIGHLIGHT;
51         }
52 }
54 /* change the highlight flag for a song */
55 static void
56 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
57 {
58         int i = filelist_find_song(fl, song);
59         struct filelist_entry *entry;
61         if (i < 0)
62                 return;
64         entry = filelist_get(fl, i);
65         if (highlight)
66                 entry->flags |= HIGHLIGHT;
67         else
68                 entry->flags &= ~HIGHLIGHT;
69 }
71 /* sync highlight flags with playlist */
72 void
73 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
74 {
75         guint i;
77         for (i = 0; i < filelist_length(fl); ++i) {
78                 struct filelist_entry *entry = filelist_get(fl, i);
79                 mpd_InfoEntity *entity = entry->entity;
81                 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
82                         mpd_Song *song = entity->info.song;
84                         if( playlist_get_index_from_file(c, song->file) >= 0 )
85                                 entry->flags |= HIGHLIGHT;
86                         else
87                                 entry->flags &= ~HIGHLIGHT;
88                 }
89         }
90 }
92 /* the playlist have been updated -> fix highlights */
93 void
94 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
95                          int event, gpointer data)
96 {
97         if (browser->filelist == NULL)
98                 return;
100         switch(event) {
101         case PLAYLIST_EVENT_CLEAR:
102                 clear_highlights(browser->filelist);
103                 break;
104         case PLAYLIST_EVENT_ADD:
105                 set_highlight(browser->filelist, (mpd_Song *) data, 1);
106                 break;
107         case PLAYLIST_EVENT_DELETE:
108                 set_highlight(browser->filelist, (mpd_Song *) data, 0);
109                 break;
110         case PLAYLIST_EVENT_MOVE:
111                 break;
112         default:
113                 sync_highlights(c, browser->filelist);
114                 break;
115         }
118 #endif
120 /* list_window callback */
121 const char *
122 browser_lw_callback(unsigned idx, int *highlight, void *data)
124         static char buf[BUFSIZE];
125         mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
126         filelist_entry_t *entry;
127         mpd_InfoEntity *entity;
129         if (idx >= filelist_length(fl))
130                 return NULL;
132         entry = filelist_get(fl, idx);
133         assert(entry != NULL);
135         entity = entry->entity;
136 #ifndef NCMPC_MINI
137         *highlight = (entry->flags & HIGHLIGHT);
138 #else
139         *highlight = false;
140 #endif
142         if( entity == NULL )
143                 return "[..]";
145         if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
146                 mpd_Directory *dir = entity->info.directory;
147                 char *directory = utf8_to_locale(g_basename(dir->path));
149                 g_snprintf(buf, BUFSIZE, "[%s]", directory);
150                 g_free(directory);
151                 return buf;
152         } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
153                 mpd_Song *song = entity->info.song;
155                 strfsong(buf, BUFSIZE, options.list_format, song);
156                 return buf;
157         } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
158                 mpd_PlaylistFile *plf = entity->info.playlistFile;
159                 char *filename = utf8_to_locale(g_basename(plf->path));
161                 g_snprintf(buf, BUFSIZE, playlist_format, filename);
162                 g_free(filename);
163                 return buf;
164         }
166         return "Error: Unknown entry!";
169 /* chdir */
170 bool
171 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
172                          filelist_entry_t *entry, const char *new_path)
174         mpd_InfoEntity *entity = NULL;
175         gchar *path = NULL;
176         char *old_path;
177         int idx;
179         if( entry!=NULL )
180                 entity = entry->entity;
181         else if( new_path==NULL )
182                 return false;
184         if( entity==NULL ) {
185                 if( entry || 0==strcmp(new_path, "..") ) {
186                         /* return to parent */
187                         char *parent = g_path_get_dirname(browser->filelist->path);
188                         if( strcmp(parent, ".") == 0 )
189                                 parent[0] = '\0';
190                         path = g_strdup(parent);
191                         g_free(parent);
192                 } else {
193                         /* entry==NULL, then new_path ("" is root) */
194                         path = g_strdup(new_path);
195                 }
196         } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
197                 /* enter sub */
198                 mpd_Directory *dir = entity->info.directory;
199                 path = g_strdup(dir->path);
200         } else
201                 return false;
203         if (browser->filelist != NULL) {
204                 old_path = g_strdup(browser->filelist->path);
205                 filelist_free(browser->filelist);
206         } else
207                 old_path = NULL;
209         browser->filelist = mpdclient_filelist_get(c, path);
210 #ifndef NCMPC_MINI
211         sync_highlights(c, browser->filelist);
212 #endif
214         idx = old_path != NULL
215                 ? filelist_find_directory(browser->filelist, old_path)
216                 : -1;
217         g_free(old_path);
219         list_window_reset(browser->lw);
220         if (idx >= 0) {
221                 list_window_set_selected(browser->lw, idx);
222                 list_window_center(browser->lw,
223                                    filelist_length(browser->filelist), idx);
224         }
226         g_free(path);
227         return true;
230 static bool
231 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
233         mpd_InfoEntity *entity = entry->entity;
234         mpd_PlaylistFile *plf = entity->info.playlistFile;
235         char *filename = utf8_to_locale(plf->path);
237         if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
238                 screen_status_printf(_("Loading playlist %s..."),
239                                      g_basename(filename));
240         g_free(filename);
241         return true;
244 static bool
245 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
247         int idx;
248         mpd_InfoEntity *entity = entry->entity;
249         mpd_Song *song = entity->info.song;
251 #ifndef NCMPC_MINI
252         if (!(entry->flags & HIGHLIGHT)) {
253 #endif
254                 if (mpdclient_cmd_add(c, song) == 0) {
255                         char buf[BUFSIZE];
257 #ifndef NCMPC_MINI
258                         entry->flags |= HIGHLIGHT;
259 #endif
260                         strfsong(buf, BUFSIZE, options.list_format, song);
261                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
262                         mpdclient_update(c); /* get song id */
263                 } else
264                         return false;
265 #ifndef NCMPC_MINI
266         }
267 #endif
269         idx = playlist_get_index_from_file(c, song->file);
270         mpdclient_cmd_play(c, idx);
271         return true;
274 static struct filelist_entry *
275 browser_get_selected(const struct screen_browser *browser)
277         if (browser->filelist == NULL ||
278             browser->lw->selected >= filelist_length(browser->filelist))
279                 return NULL;
281         return filelist_get(browser->filelist, browser->lw->selected);
284 static bool
285 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
287         struct filelist_entry *entry = browser_get_selected(browser);
288         mpd_InfoEntity *entity;
290         if( entry==NULL )
291                 return false;
293         entity = entry->entity;
294         if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
295                 return browser_change_directory(browser, c, entry, NULL);
296         else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
297                 return load_playlist(c, entry);
298         else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
299                 return enqueue_and_play(c, entry);
300         return false;
304 #ifdef USE_OLD_ADD
306 static int
307 add_directory(mpdclient_t *c, char *dir)
309         mpd_InfoEntity *entity;
310         GList *subdir_list = NULL;
311         GList *list = NULL;
312         char *dirname;
314         dirname = utf8_to_locale(dir);
315         screen_status_printf(_("Adding directory %s...\n"), dirname);
316         doupdate();
317         g_free(dirname);
318         dirname = NULL;
320         mpd_sendLsInfoCommand(c->connection, dir);
321         mpd_sendCommandListBegin(c->connection);
322         while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
323                 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
324                         mpd_Song *song = entity->info.song;
325                         mpd_sendAddCommand(c->connection, song->file);
326                         mpd_freeInfoEntity(entity);
327                 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
328                         subdir_list = g_list_append(subdir_list, (gpointer) entity);
329                 } else
330                         mpd_freeInfoEntity(entity);
331         }
332         mpd_sendCommandListEnd(c->connection);
333         mpdclient_finish_command(c);
334         c->need_update = TRUE;
336         list = g_list_first(subdir_list);
337         while( list!=NULL ) {
338                 mpd_Directory *dir;
340                 entity = list->data;
341                 dir = entity->info.directory;
342                 add_directory(c, dir->path);
343                 mpd_freeInfoEntity(entity);
344                 list->data=NULL;
345                 list=list->next;
346         }
347         g_list_free(subdir_list);
348         return 0;
350 #endif
352 static bool
353 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
354                      G_GNUC_UNUSED gboolean toggle)
356         assert(entry != NULL);
357         assert(entry->entity != NULL);
359         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
360                 return load_playlist(c, entry);
362         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
363                 mpd_Directory *dir = entry->entity->info.directory;
364 #ifdef USE_OLD_ADD
365                 add_directory(c, tmp);
366 #else
367                 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
368                         char *tmp = utf8_to_locale(dir->path);
370                         screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
371                         g_free(tmp);
372                 }
373 #endif
374                 return true;
375         }
377         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
378                 return false;
380         assert(entry->entity->info.song != NULL);
382 #ifndef NCMPC_MINI
383         if (!toggle || (entry->flags & HIGHLIGHT) == 0)
384 #endif
385         {
386                 mpd_Song *song = entry->entity->info.song;
388 #ifndef NCMPC_MINI
389                 entry->flags |= HIGHLIGHT;
390 #endif
392                 if (mpdclient_cmd_add(c, song) == 0) {
393                         char buf[BUFSIZE];
395                         strfsong(buf, BUFSIZE, options.list_format, song);
396                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
397                 }
398 #ifndef NCMPC_MINI
399         } else {
400                 /* remove song from playlist */
401                 mpd_Song *song = entry->entity->info.song;
402                 int idx;
404                 entry->flags &= ~HIGHLIGHT;
406                 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
407                         mpdclient_cmd_delete(c, idx);
408 #endif
409         }
411         return true;
414 static bool
415 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
417         struct filelist_entry *entry = browser_get_selected(browser);
419         if (entry == NULL || entry->entity == NULL)
420                 return false;
422         return browser_select_entry(c, entry, TRUE);
425 static bool
426 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
428         struct filelist_entry *entry = browser_get_selected(browser);
430         if (entry == NULL || entry->entity == NULL)
431                 return false;
433         return browser_select_entry(c, entry, FALSE);
436 static void
437 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
439         guint i;
441         if (browser->filelist == NULL)
442                 return;
444         for (i = 0; i < filelist_length(browser->filelist); ++i) {
445                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
447                 if (entry != NULL && entry->entity != NULL)
448                         browser_select_entry(c, entry, FALSE);
449         }
452 #ifdef HAVE_GETMOUSE
453 static int
454 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
456         int row;
457         unsigned prev_selected = browser->lw->selected;
458         unsigned long bstate;
459         int length;
461         if (browser->filelist)
462                 length = filelist_length(browser->filelist);
463         else
464                 length = 0;
466         if (screen_get_mouse_event(c, &bstate, &row) ||
467             list_window_mouse(browser->lw, length, bstate, row))
468                 return 1;
470         browser->lw->selected = browser->lw->start + row;
471         list_window_check_selected(browser->lw, length);
473         if( bstate & BUTTON1_CLICKED ) {
474                 if (prev_selected == browser->lw->selected)
475                         browser_handle_enter(browser, c);
476         } else if (bstate & BUTTON3_CLICKED) {
477                 if (prev_selected == browser->lw->selected)
478                         browser_handle_select(browser, c);
479         }
481         return 1;
483 #endif
485 bool
486 browser_cmd(struct screen_browser *browser,
487             struct mpdclient *c, command_t cmd)
489         struct filelist_entry *entry;
491         switch (cmd) {
492         case CMD_PLAY:
493                 browser_handle_enter(browser, c);
494                 return true;
496         case CMD_SELECT:
497                 if (browser_handle_select(browser, c))
498                         /* continue and select next item... */
499                         cmd = CMD_LIST_NEXT;
501                 /* call list_window_cmd to go to the next item */
502                 break;
504         case CMD_ADD:
505                 if (browser_handle_add(browser, c))
506                         /* continue and select next item... */
507                         cmd = CMD_LIST_NEXT;
509                 /* call list_window_cmd to go to the next item */
510                 break;
512         case CMD_SELECT_ALL:
513                 browser_handle_select_all(browser, c);
514                 return true;
516         case CMD_LIST_FIND:
517         case CMD_LIST_RFIND:
518         case CMD_LIST_FIND_NEXT:
519         case CMD_LIST_RFIND_NEXT:
520                 screen_find(browser->lw, filelist_length(browser->filelist),
521                             cmd, browser_lw_callback,
522                             browser->filelist);
523                 return true;
525 #ifdef HAVE_GETMOUSE
526         case CMD_MOUSE_EVENT:
527                 browser_handle_mouse_event(browser, c);
528                 return true;
529 #endif
531 #ifdef ENABLE_SONG_SCREEN
532         case CMD_VIEW:
533                 entry = browser_get_selected(browser);
534                 if (entry == NULL || entry->entity == NULL ||
535                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
536                         return false;
538                 screen_song_switch(c, entry->entity->info.song);
539                 return true;
540 #endif
542         case CMD_LOCATE:
543                 entry = browser_get_selected(browser);
544                 if (entry == NULL || entry->entity == NULL ||
545                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
546                         return false;
548                 screen_file_goto_song(c, entry->entity->info.song);
549                 return true;
551 #ifdef ENABLE_LYRICS_SCREEN
552         case CMD_SCREEN_LYRICS:
553                 entry = browser_get_selected(browser);
554                 if (entry == NULL)
555                         return false;
557                 if (entry->entity == NULL ||
558                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
559                         return true;
561                 screen_lyrics_switch(c, entry->entity->info.song);
562                 return true;
563 #endif
565         default:
566                 break;
567         }
569         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
570                             cmd))
571                 return true;
573         return false;