f8493c3ebfd0acf595b031853b4e4207f68de522
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 int i = filelist_find_song(fl, song);
60 struct filelist_entry *entry;
62 if (i < 0)
63 return;
65 entry = filelist_get(fl, i);
66 if (highlight)
67 entry->flags |= HIGHLIGHT;
68 else
69 entry->flags &= ~HIGHLIGHT;
70 }
72 /* sync highlight flags with playlist */
73 void
74 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
75 {
76 guint i;
78 for (i = 0; i < filelist_length(fl); ++i) {
79 struct filelist_entry *entry = filelist_get(fl, i);
80 mpd_InfoEntity *entity = entry->entity;
82 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
83 mpd_Song *song = entity->info.song;
85 if( playlist_get_index_from_file(c, song->file) >= 0 )
86 entry->flags |= HIGHLIGHT;
87 else
88 entry->flags &= ~HIGHLIGHT;
89 }
90 }
91 }
93 /* the playlist have been updated -> fix highlights */
94 void
95 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
96 int event, gpointer data)
97 {
98 if (browser->filelist == NULL)
99 return;
101 switch(event) {
102 case PLAYLIST_EVENT_CLEAR:
103 clear_highlights(browser->filelist);
104 break;
105 case PLAYLIST_EVENT_ADD:
106 set_highlight(browser->filelist, (mpd_Song *) data, 1);
107 break;
108 case PLAYLIST_EVENT_DELETE:
109 set_highlight(browser->filelist, (mpd_Song *) data, 0);
110 break;
111 case PLAYLIST_EVENT_MOVE:
112 break;
113 default:
114 sync_highlights(c, browser->filelist);
115 break;
116 }
117 }
119 #endif
121 /* list_window callback */
122 const char *
123 browser_lw_callback(unsigned idx, int *highlight, void *data)
124 {
125 static char buf[BUFSIZE];
126 mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
127 filelist_entry_t *entry;
128 mpd_InfoEntity *entity;
130 if (idx >= filelist_length(fl))
131 return NULL;
133 entry = filelist_get(fl, idx);
134 assert(entry != NULL);
136 entity = entry->entity;
137 #ifndef NCMPC_MINI
138 *highlight = (entry->flags & HIGHLIGHT);
139 #else
140 *highlight = false;
141 #endif
143 if( entity == NULL )
144 return "[..]";
146 if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
147 mpd_Directory *dir = entity->info.directory;
148 char *directory = utf8_to_locale(g_basename(dir->path));
150 g_snprintf(buf, BUFSIZE, "[%s]", directory);
151 g_free(directory);
152 return buf;
153 } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
154 mpd_Song *song = entity->info.song;
156 strfsong(buf, BUFSIZE, options.list_format, song);
157 return buf;
158 } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
159 mpd_PlaylistFile *plf = entity->info.playlistFile;
160 char *filename = utf8_to_locale(g_basename(plf->path));
162 g_snprintf(buf, BUFSIZE, playlist_format, filename);
163 g_free(filename);
164 return buf;
165 }
167 return "Error: Unknown entry!";
168 }
170 /* chdir */
171 bool
172 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
173 filelist_entry_t *entry, const char *new_path)
174 {
175 mpd_InfoEntity *entity = NULL;
176 gchar *path = NULL;
177 char *old_path;
178 int idx;
180 if( entry!=NULL )
181 entity = entry->entity;
182 else if( new_path==NULL )
183 return false;
185 if( entity==NULL ) {
186 if( entry || 0==strcmp(new_path, "..") ) {
187 /* return to parent */
188 char *parent = g_path_get_dirname(browser->filelist->path);
189 if( strcmp(parent, ".") == 0 )
190 parent[0] = '\0';
191 path = g_strdup(parent);
192 g_free(parent);
193 } else {
194 /* entry==NULL, then new_path ("" is root) */
195 path = g_strdup(new_path);
196 }
197 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
198 /* enter sub */
199 mpd_Directory *dir = entity->info.directory;
200 path = g_strdup(dir->path);
201 } else
202 return false;
204 if (browser->filelist != NULL) {
205 old_path = g_strdup(browser->filelist->path);
206 filelist_free(browser->filelist);
207 } else
208 old_path = NULL;
210 browser->filelist = mpdclient_filelist_get(c, path);
211 #ifndef NCMPC_MINI
212 sync_highlights(c, browser->filelist);
213 #endif
215 idx = old_path != NULL
216 ? filelist_find_directory(browser->filelist, old_path)
217 : -1;
218 g_free(old_path);
220 list_window_reset(browser->lw);
221 if (idx >= 0) {
222 list_window_set_selected(browser->lw, idx);
223 list_window_center(browser->lw,
224 filelist_length(browser->filelist), idx);
225 }
227 g_free(path);
228 return true;
229 }
231 static bool
232 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
233 {
234 mpd_InfoEntity *entity = entry->entity;
235 mpd_PlaylistFile *plf = entity->info.playlistFile;
236 char *filename = utf8_to_locale(plf->path);
238 if (mpdclient_cmd_load_playlist(c, plf->path) == 0)
239 screen_status_printf(_("Loading playlist %s..."),
240 g_basename(filename));
241 g_free(filename);
242 return true;
243 }
245 static bool
246 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
247 {
248 int idx;
249 mpd_InfoEntity *entity = entry->entity;
250 mpd_Song *song = entity->info.song;
252 #ifndef NCMPC_MINI
253 if (!(entry->flags & HIGHLIGHT)) {
254 #endif
255 if (mpdclient_cmd_add(c, song) == 0) {
256 char buf[BUFSIZE];
258 #ifndef NCMPC_MINI
259 entry->flags |= HIGHLIGHT;
260 #endif
261 strfsong(buf, BUFSIZE, options.list_format, song);
262 screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
263 mpdclient_update(c); /* get song id */
264 } else
265 return false;
266 #ifndef NCMPC_MINI
267 }
268 #endif
270 idx = playlist_get_index_from_file(c, song->file);
271 mpdclient_cmd_play(c, idx);
272 return true;
273 }
275 static struct filelist_entry *
276 browser_get_selected(const struct screen_browser *browser)
277 {
278 if (browser->filelist == NULL ||
279 browser->lw->selected >= filelist_length(browser->filelist))
280 return NULL;
282 return filelist_get(browser->filelist, browser->lw->selected);
283 }
285 static bool
286 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
287 {
288 struct filelist_entry *entry = browser_get_selected(browser);
289 mpd_InfoEntity *entity;
291 if( entry==NULL )
292 return false;
294 entity = entry->entity;
295 if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
296 return browser_change_directory(browser, c, entry, NULL);
297 else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
298 return load_playlist(c, entry);
299 else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
300 return enqueue_and_play(c, entry);
301 return false;
302 }
305 #ifdef USE_OLD_ADD
307 static int
308 add_directory(mpdclient_t *c, char *dir)
309 {
310 mpd_InfoEntity *entity;
311 GList *subdir_list = NULL;
312 GList *list = NULL;
313 char *dirname;
315 dirname = utf8_to_locale(dir);
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;
350 }
351 #endif
353 static bool
354 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
355 mpd_unused gboolean toggle)
356 {
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;
413 }
415 static bool
416 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
417 {
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);
424 }
426 static bool
427 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
428 {
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);
435 }
437 static void
438 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
439 {
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 }
451 }
453 #ifdef HAVE_GETMOUSE
454 static int
455 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
456 {
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;
483 }
484 #endif
486 bool
487 browser_cmd(struct screen_browser *browser,
488 struct mpdclient *c, command_t cmd)
489 {
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;
575 }