Code

6e1637db4e5b2401409eb6e0d11fb3106222b4f9
[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, bool *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) != 0;
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         /* translators: a directory is being added the to playlist */
316         screen_status_printf(_("Adding directory %s...\n"), dirname);
317         doupdate();
318         g_free(dirname);
319         dirname = NULL;
321         mpd_sendLsInfoCommand(c->connection, dir);
322         mpd_sendCommandListBegin(c->connection);
323         while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
324                 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
325                         mpd_Song *song = entity->info.song;
326                         mpd_sendAddCommand(c->connection, song->file);
327                         mpd_freeInfoEntity(entity);
328                 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
329                         subdir_list = g_list_append(subdir_list, (gpointer) entity);
330                 } else
331                         mpd_freeInfoEntity(entity);
332         }
333         mpd_sendCommandListEnd(c->connection);
334         mpdclient_finish_command(c);
335         c->need_update = TRUE;
337         list = g_list_first(subdir_list);
338         while( list!=NULL ) {
339                 mpd_Directory *dir;
341                 entity = list->data;
342                 dir = entity->info.directory;
343                 add_directory(c, dir->path);
344                 mpd_freeInfoEntity(entity);
345                 list->data=NULL;
346                 list=list->next;
347         }
348         g_list_free(subdir_list);
349         return 0;
351 #endif
353 static bool
354 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
355                      G_GNUC_UNUSED gboolean toggle)
357         assert(entry != NULL);
358         assert(entry->entity != NULL);
360         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
361                 return load_playlist(c, entry);
363         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
364                 mpd_Directory *dir = entry->entity->info.directory;
365 #ifdef USE_OLD_ADD
366                 add_directory(c, tmp);
367 #else
368                 if (mpdclient_cmd_add_path(c, dir->path) == 0) {
369                         char *tmp = utf8_to_locale(dir->path);
371                         screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
372                         g_free(tmp);
373                 }
374 #endif
375                 return true;
376         }
378         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
379                 return false;
381         assert(entry->entity->info.song != NULL);
383 #ifndef NCMPC_MINI
384         if (!toggle || (entry->flags & HIGHLIGHT) == 0)
385 #endif
386         {
387                 mpd_Song *song = entry->entity->info.song;
389 #ifndef NCMPC_MINI
390                 entry->flags |= HIGHLIGHT;
391 #endif
393                 if (mpdclient_cmd_add(c, song) == 0) {
394                         char buf[BUFSIZE];
396                         strfsong(buf, BUFSIZE, options.list_format, song);
397                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
398                 }
399 #ifndef NCMPC_MINI
400         } else {
401                 /* remove song from playlist */
402                 mpd_Song *song = entry->entity->info.song;
403                 int idx;
405                 entry->flags &= ~HIGHLIGHT;
407                 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
408                         mpdclient_cmd_delete(c, idx);
409 #endif
410         }
412         return true;
415 static bool
416 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
418         struct filelist_entry *entry = browser_get_selected(browser);
420         if (entry == NULL || entry->entity == NULL)
421                 return false;
423         return browser_select_entry(c, entry, TRUE);
426 static bool
427 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
429         struct filelist_entry *entry = browser_get_selected(browser);
431         if (entry == NULL || entry->entity == NULL)
432                 return false;
434         return browser_select_entry(c, entry, FALSE);
437 static void
438 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
440         guint i;
442         if (browser->filelist == NULL)
443                 return;
445         for (i = 0; i < filelist_length(browser->filelist); ++i) {
446                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
448                 if (entry != NULL && entry->entity != NULL)
449                         browser_select_entry(c, entry, FALSE);
450         }
453 #ifdef HAVE_GETMOUSE
454 static int
455 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
457         int row;
458         unsigned prev_selected = browser->lw->selected;
459         unsigned long bstate;
460         int length;
462         if (browser->filelist)
463                 length = filelist_length(browser->filelist);
464         else
465                 length = 0;
467         if (screen_get_mouse_event(c, &bstate, &row) ||
468             list_window_mouse(browser->lw, length, bstate, row))
469                 return 1;
471         browser->lw->selected = browser->lw->start + row;
472         list_window_check_selected(browser->lw, length);
474         if( bstate & BUTTON1_CLICKED ) {
475                 if (prev_selected == browser->lw->selected)
476                         browser_handle_enter(browser, c);
477         } else if (bstate & BUTTON3_CLICKED) {
478                 if (prev_selected == browser->lw->selected)
479                         browser_handle_select(browser, c);
480         }
482         return 1;
484 #endif
486 bool
487 browser_cmd(struct screen_browser *browser,
488             struct mpdclient *c, command_t cmd)
490         struct filelist_entry *entry;
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_SONG_SCREEN
533         case CMD_VIEW:
534                 entry = browser_get_selected(browser);
535                 if (entry == NULL || entry->entity == NULL ||
536                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
537                         return false;
539                 screen_song_switch(c, entry->entity->info.song);
540                 return true;
541 #endif
543         case CMD_LOCATE:
544                 entry = browser_get_selected(browser);
545                 if (entry == NULL || entry->entity == NULL ||
546                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
547                         return false;
549                 screen_file_goto_song(c, entry->entity->info.song);
550                 return true;
552 #ifdef ENABLE_LYRICS_SCREEN
553         case CMD_SCREEN_LYRICS:
554                 entry = browser_get_selected(browser);
555                 if (entry == NULL)
556                         return false;
558                 if (entry->entity == NULL ||
559                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
560                         return true;
562                 screen_lyrics_switch(c, entry->entity->info.song);
563                 return true;
564 #endif
566         default:
567                 break;
568         }
570         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
571                             cmd))
572                 return true;
574         return false;