Code

b5ce64171721baec8d445eb660313eb5ea8a41cb
[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"
26 #include "gcc.h"
28 #include <string.h>
30 #undef  USE_OLD_ADD
32 #define BUFSIZE 1024
34 #ifndef NCMPC_MINI
35 #define HIGHLIGHT  (0x01)
36 #endif
38 static const char playlist_format[] = "*%s*";
40 #ifndef NCMPC_MINI
42 /* clear the highlight flag for all items in the filelist */
43 static void
44 clear_highlights(mpdclient_filelist_t *fl)
45 {
46         guint i;
48         for (i = 0; i < filelist_length(fl); ++i) {
49                 struct filelist_entry *entry = filelist_get(fl, i);
51                 entry->flags &= ~HIGHLIGHT;
52         }
53 }
55 /* change the highlight flag for a song */
56 static void
57 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
58 {
59         struct filelist_entry *entry = filelist_find_song(fl, song);
61         if (entry == NULL)
62                 return;
64         if (highlight)
65                 entry->flags |= HIGHLIGHT;
66         else
67                 entry->flags &= ~HIGHLIGHT;
68 }
70 /* sync highlight flags with playlist */
71 void
72 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
73 {
74         guint i;
76         for (i = 0; i < filelist_length(fl); ++i) {
77                 struct filelist_entry *entry = filelist_get(fl, i);
78                 mpd_InfoEntity *entity = entry->entity;
80                 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
81                         mpd_Song *song = entity->info.song;
83                         if( playlist_get_index_from_file(c, song->file) >= 0 )
84                                 entry->flags |= HIGHLIGHT;
85                         else
86                                 entry->flags &= ~HIGHLIGHT;
87                 }
88         }
89 }
91 /* the playlist have been updated -> fix highlights */
92 void
93 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
94                          int event, gpointer data)
95 {
96         if (browser->filelist == NULL)
97                 return;
99         switch(event) {
100         case PLAYLIST_EVENT_CLEAR:
101                 clear_highlights(browser->filelist);
102                 break;
103         case PLAYLIST_EVENT_ADD:
104                 set_highlight(browser->filelist, (mpd_Song *) data, 1);
105                 break;
106         case PLAYLIST_EVENT_DELETE:
107                 set_highlight(browser->filelist, (mpd_Song *) data, 0);
108                 break;
109         case PLAYLIST_EVENT_MOVE:
110                 break;
111         default:
112                 sync_highlights(c, browser->filelist);
113                 break;
114         }
117 #endif
119 /* list_window callback */
120 const char *
121 browser_lw_callback(unsigned idx, int *highlight, void *data)
123         static char buf[BUFSIZE];
124         mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
125         filelist_entry_t *entry;
126         mpd_InfoEntity *entity;
128         if (idx >= filelist_length(fl))
129                 return NULL;
131         entry = filelist_get(fl, idx);
132         assert(entry != NULL);
134         entity = entry->entity;
135 #ifndef NCMPC_MINI
136         *highlight = (entry->flags & HIGHLIGHT);
137 #else
138         *highlight = false;
139 #endif
141         if( entity == NULL )
142                 return "[..]";
144         if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
145                 mpd_Directory *dir = entity->info.directory;
146                 char *directory = utf8_to_locale(g_basename(dir->path));
148                 g_snprintf(buf, BUFSIZE, "[%s]", directory);
149                 g_free(directory);
150                 return buf;
151         } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
152                 mpd_Song *song = entity->info.song;
154                 strfsong(buf, BUFSIZE, options.list_format, song);
155                 return buf;
156         } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
157                 mpd_PlaylistFile *plf = entity->info.playlistFile;
158                 char *filename = utf8_to_locale(g_basename(plf->path));
160                 g_snprintf(buf, BUFSIZE, playlist_format, filename);
161                 g_free(filename);
162                 return buf;
163         }
165         return "Error: Unknown entry!";
168 /* chdir */
169 bool
170 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
171                          filelist_entry_t *entry, const char *new_path)
173         mpd_InfoEntity *entity = NULL;
174         gchar *path = NULL;
175         char *old_path;
176         int idx;
178         if( entry!=NULL )
179                 entity = entry->entity;
180         else if( new_path==NULL )
181                 return false;
183         if( entity==NULL ) {
184                 if( entry || 0==strcmp(new_path, "..") ) {
185                         /* return to parent */
186                         char *parent = g_path_get_dirname(browser->filelist->path);
187                         if( strcmp(parent, ".") == 0 )
188                                 parent[0] = '\0';
189                         path = g_strdup(parent);
190                         g_free(parent);
191                 } else {
192                         /* entry==NULL, then new_path ("" is root) */
193                         path = g_strdup(new_path);
194                 }
195         } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
196                 /* enter sub */
197                 mpd_Directory *dir = entity->info.directory;
198                 path = g_strdup(dir->path);
199         } else
200                 return false;
202         if (browser->filelist != NULL) {
203                 old_path = g_strdup(browser->filelist->path);
204                 filelist_free(browser->filelist);
205         } else
206                 old_path = NULL;
208         browser->filelist = mpdclient_filelist_get(c, path);
209 #ifndef NCMPC_MINI
210         sync_highlights(c, browser->filelist);
211 #endif
213         idx = old_path != NULL
214                 ? filelist_find_directory(browser->filelist, old_path)
215                 : -1;
216         g_free(old_path);
218         list_window_reset(browser->lw);
219         if (idx >= 0) {
220                 list_window_set_selected(browser->lw, idx);
221                 list_window_center(browser->lw,
222                                    filelist_length(browser->filelist), idx);
223         }
225         g_free(path);
226         return true;
229 static bool
230 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
232         mpd_InfoEntity *entity = entry->entity;
233         mpd_PlaylistFile *plf = entity->info.playlistFile;
234         char *filename = utf8_to_locale(plf->path);
236         if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
237                 screen_status_printf(_("Loading playlist %s..."),
238                                      g_basename(filename));
239         g_free(filename);
240         return true;
243 static bool
244 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
246         int idx;
247         mpd_InfoEntity *entity = entry->entity;
248         mpd_Song *song = entity->info.song;
250 #ifndef NCMPC_MINI
251         if (!(entry->flags & HIGHLIGHT)) {
252 #endif
253                 if (mpdclient_cmd_add(c, song) == 0) {
254                         char buf[BUFSIZE];
256 #ifndef NCMPC_MINI
257                         entry->flags |= HIGHLIGHT;
258 #endif
259                         strfsong(buf, BUFSIZE, options.list_format, song);
260                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
261                         mpdclient_update(c); /* get song id */
262                 } else
263                         return false;
264 #ifndef NCMPC_MINI
265         }
266 #endif
268         idx = playlist_get_index_from_file(c, song->file);
269         mpdclient_cmd_play(c, idx);
270         return true;
273 static struct filelist_entry *
274 browser_get_selected(const struct screen_browser *browser)
276         if (browser->filelist == NULL ||
277             browser->lw->selected >= filelist_length(browser->filelist))
278                 return NULL;
280         return filelist_get(browser->filelist, browser->lw->selected);
283 static bool
284 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
286         struct filelist_entry *entry = browser_get_selected(browser);
287         mpd_InfoEntity *entity;
289         if( entry==NULL )
290                 return false;
292         entity = entry->entity;
293         if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
294                 return browser_change_directory(browser, c, entry, NULL);
295         else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
296                 return load_playlist(c, entry);
297         else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
298                 return enqueue_and_play(c, entry);
299         return false;
303 #ifdef USE_OLD_ADD
305 static int
306 add_directory(mpdclient_t *c, char *dir)
308         mpd_InfoEntity *entity;
309         GList *subdir_list = NULL;
310         GList *list = NULL;
311         char *dirname;
313         dirname = utf8_to_locale(dir);
314         screen_status_printf(_("Adding directory %s...\n"), dirname);
315         doupdate();
316         g_free(dirname);
317         dirname = NULL;
319         mpd_sendLsInfoCommand(c->connection, dir);
320         mpd_sendCommandListBegin(c->connection);
321         while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
322                 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
323                         mpd_Song *song = entity->info.song;
324                         mpd_sendAddCommand(c->connection, song->file);
325                         mpd_freeInfoEntity(entity);
326                 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
327                         subdir_list = g_list_append(subdir_list, (gpointer) entity);
328                 } else
329                         mpd_freeInfoEntity(entity);
330         }
331         mpd_sendCommandListEnd(c->connection);
332         mpdclient_finish_command(c);
333         c->need_update = TRUE;
335         list = g_list_first(subdir_list);
336         while( list!=NULL ) {
337                 mpd_Directory *dir;
339                 entity = list->data;
340                 dir = entity->info.directory;
341                 add_directory(c, dir->path);
342                 mpd_freeInfoEntity(entity);
343                 list->data=NULL;
344                 list=list->next;
345         }
346         g_list_free(subdir_list);
347         return 0;
349 #endif
351 static bool
352 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
353                      mpd_unused gboolean toggle)
355         assert(entry != NULL);
356         assert(entry->entity != NULL);
358         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
359                 return load_playlist(c, entry);
361         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
362                 mpd_Directory *dir = entry->entity->info.directory;
363 #ifdef USE_OLD_ADD
364                 add_directory(c, tmp);
365 #else
366                 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
367                         char *tmp = utf8_to_locale(dir->path);
369                         screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
370                         g_free(tmp);
371                 }
372 #endif
373                 return true;
374         }
376         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
377                 return false;
379         assert(entry->entity->info.song != NULL);
381 #ifndef NCMPC_MINI
382         if (!toggle || (entry->flags & HIGHLIGHT) == 0)
383 #endif
384         {
385                 mpd_Song *song = entry->entity->info.song;
387 #ifndef NCMPC_MINI
388                 entry->flags |= HIGHLIGHT;
389 #endif
391                 if (mpdclient_cmd_add(c, song) == 0) {
392                         char buf[BUFSIZE];
394                         strfsong(buf, BUFSIZE, options.list_format, song);
395                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
396                 }
397 #ifndef NCMPC_MINI
398         } else {
399                 /* remove song from playlist */
400                 mpd_Song *song = entry->entity->info.song;
401                 int idx;
403                 entry->flags &= ~HIGHLIGHT;
405                 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
406                         mpdclient_cmd_delete(c, idx);
407 #endif
408         }
410         return true;
413 static bool
414 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
416         struct filelist_entry *entry = browser_get_selected(browser);
418         if (entry == NULL || entry->entity == NULL)
419                 return false;
421         return browser_select_entry(c, entry, TRUE);
424 static bool
425 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
427         struct filelist_entry *entry = browser_get_selected(browser);
429         if (entry == NULL || entry->entity == NULL)
430                 return false;
432         return browser_select_entry(c, entry, FALSE);
435 static void
436 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
438         guint i;
440         if (browser->filelist == NULL)
441                 return;
443         for (i = 0; i < filelist_length(browser->filelist); ++i) {
444                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
446                 if (entry != NULL && entry->entity != NULL)
447                         browser_select_entry(c, entry, FALSE);
448         }
451 #ifdef HAVE_GETMOUSE
452 static int
453 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
455         int row;
456         unsigned prev_selected = browser->lw->selected;
457         unsigned long bstate;
458         int length;
460         if (browser->filelist)
461                 length = filelist_length(browser->filelist);
462         else
463                 length = 0;
465         if (screen_get_mouse_event(c, &bstate, &row) ||
466             list_window_mouse(browser->lw, length, bstate, row))
467                 return 1;
469         browser->lw->selected = browser->lw->start + row;
470         list_window_check_selected(browser->lw, length);
472         if( bstate & BUTTON1_CLICKED ) {
473                 if (prev_selected == browser->lw->selected)
474                         browser_handle_enter(browser, c);
475         } else if (bstate & BUTTON3_CLICKED) {
476                 if (prev_selected == browser->lw->selected)
477                         browser_handle_select(browser, c);
478         }
480         return 1;
482 #endif
484 bool
485 browser_cmd(struct screen_browser *browser,
486             struct mpdclient *c, command_t cmd)
488 #ifdef ENABLE_LYRICS_SCREEN
489         struct filelist_entry *entry;
490 #endif
492         switch (cmd) {
493         case CMD_PLAY:
494                 browser_handle_enter(browser, c);
495                 return true;
497         case CMD_SELECT:
498                 if (browser_handle_select(browser, c))
499                         /* continue and select next item... */
500                         cmd = CMD_LIST_NEXT;
502                 /* call list_window_cmd to go to the next item */
503                 break;
505         case CMD_ADD:
506                 if (browser_handle_add(browser, c))
507                         /* continue and select next item... */
508                         cmd = CMD_LIST_NEXT;
510                 /* call list_window_cmd to go to the next item */
511                 break;
513         case CMD_SELECT_ALL:
514                 browser_handle_select_all(browser, c);
515                 return true;
517         case CMD_LIST_FIND:
518         case CMD_LIST_RFIND:
519         case CMD_LIST_FIND_NEXT:
520         case CMD_LIST_RFIND_NEXT:
521                 screen_find(browser->lw, filelist_length(browser->filelist),
522                             cmd, browser_lw_callback,
523                             browser->filelist);
524                 return true;
526 #ifdef HAVE_GETMOUSE
527         case CMD_MOUSE_EVENT:
528                 browser_handle_mouse_event(browser, c);
529                 return true;
530 #endif
532 #ifdef ENABLE_LYRICS_SCREEN
533         case CMD_SCREEN_LYRICS:
534                 entry = browser_get_selected(browser);
535                 if (entry == NULL)
536                         return false;
538                 if (entry->entity == NULL ||
539                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
540                         return true;
542                 screen_lyrics_switch(c, entry->entity->info.song);
543                 return true;
544 #endif
546         default:
547                 break;
548         }
550         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
551                             cmd))
552                 return true;
554         return false;