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);
60 mpd_InfoEntity *entity;
62 if (entry == NULL)
63 return;
65 entity = entry->entity;
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 int
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 -1;
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 } 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 = utf8_to_locale(dir->path);
200 } else
201 return -1;
203 old_path = g_strdup(browser->filelist->path);
205 filelist_free(browser->filelist);
206 browser->filelist = mpdclient_filelist_get(c, path);
207 #ifndef NCMPC_MINI
208 sync_highlights(c, browser->filelist);
209 #endif
211 idx = filelist_find_directory(browser->filelist, old_path);
212 g_free(old_path);
214 list_window_reset(browser->lw);
215 if (idx >= 0) {
216 list_window_set_selected(browser->lw, idx);
217 list_window_center(browser->lw,
218 filelist_length(browser->filelist), idx);
219 }
221 g_free(path);
222 return 0;
223 }
225 static int
226 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
227 {
228 mpd_InfoEntity *entity = entry->entity;
229 mpd_PlaylistFile *plf = entity->info.playlistFile;
230 char *filename = utf8_to_locale(plf->path);
232 if (mpdclient_cmd_load_playlist_utf8(c, plf->path) == 0)
233 screen_status_printf(_("Loading playlist %s..."),
234 g_basename(filename));
235 g_free(filename);
236 return 0;
237 }
239 static int
240 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
241 {
242 int idx;
243 mpd_InfoEntity *entity = entry->entity;
244 mpd_Song *song = entity->info.song;
246 #ifndef NCMPC_MINI
247 if (!(entry->flags & HIGHLIGHT)) {
248 #endif
249 if (mpdclient_cmd_add(c, song) == 0) {
250 char buf[BUFSIZE];
252 #ifndef NCMPC_MINI
253 entry->flags |= HIGHLIGHT;
254 #endif
255 strfsong(buf, BUFSIZE, options.list_format, song);
256 screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
257 mpdclient_update(c); /* get song id */
258 } else
259 return -1;
260 #ifndef NCMPC_MINI
261 }
262 #endif
264 idx = playlist_get_index_from_file(c, song->file);
265 mpdclient_cmd_play(c, idx);
266 return 0;
267 }
269 static struct filelist_entry *
270 browser_get_selected(const struct screen_browser *browser)
271 {
272 if (browser->filelist == NULL ||
273 browser->lw->selected >= filelist_length(browser->filelist))
274 return NULL;
276 return filelist_get(browser->filelist, browser->lw->selected);
277 }
279 static int
280 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
281 {
282 struct filelist_entry *entry = browser_get_selected(browser);
283 mpd_InfoEntity *entity;
285 if( entry==NULL )
286 return -1;
288 entity = entry->entity;
289 if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
290 return browser_change_directory(browser, c, entry, NULL);
291 else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
292 return load_playlist(c, entry);
293 else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
294 return enqueue_and_play(c, entry);
295 return -1;
296 }
299 #ifdef USE_OLD_ADD
301 static int
302 add_directory(mpdclient_t *c, char *dir)
303 {
304 mpd_InfoEntity *entity;
305 GList *subdir_list = NULL;
306 GList *list = NULL;
307 char *dirname;
309 dirname = utf8_to_locale(dir);
310 screen_status_printf(_("Adding directory %s...\n"), dirname);
311 doupdate();
312 g_free(dirname);
313 dirname = NULL;
315 mpd_sendLsInfoCommand(c->connection, dir);
316 mpd_sendCommandListBegin(c->connection);
317 while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
318 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
319 mpd_Song *song = entity->info.song;
320 mpd_sendAddCommand(c->connection, song->file);
321 mpd_freeInfoEntity(entity);
322 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
323 subdir_list = g_list_append(subdir_list, (gpointer) entity);
324 } else
325 mpd_freeInfoEntity(entity);
326 }
327 mpd_sendCommandListEnd(c->connection);
328 mpdclient_finish_command(c);
329 c->need_update = TRUE;
331 list = g_list_first(subdir_list);
332 while( list!=NULL ) {
333 mpd_Directory *dir;
335 entity = list->data;
336 dir = entity->info.directory;
337 add_directory(c, dir->path);
338 mpd_freeInfoEntity(entity);
339 list->data=NULL;
340 list=list->next;
341 }
342 g_list_free(subdir_list);
343 return 0;
344 }
345 #endif
347 static int
348 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
349 mpd_unused gboolean toggle)
350 {
351 assert(entry != NULL);
352 assert(entry->entity != NULL);
354 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
355 return load_playlist(c, entry);
357 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
358 mpd_Directory *dir = entry->entity->info.directory;
359 #ifdef USE_OLD_ADD
360 add_directory(c, tmp);
361 #else
362 if (mpdclient_cmd_add_path_utf8(c, dir->path) == 0) {
363 char *tmp = utf8_to_locale(dir->path);
365 screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
366 g_free(tmp);
367 }
368 #endif
369 return 0;
370 }
372 if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
373 return -1;
375 assert(entry->entity->info.song != NULL);
377 #ifndef NCMPC_MINI
378 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
379 #endif
380 {
381 mpd_Song *song = entry->entity->info.song;
383 #ifndef NCMPC_MINI
384 entry->flags |= HIGHLIGHT;
385 #endif
387 if (mpdclient_cmd_add(c, song) == 0) {
388 char buf[BUFSIZE];
390 strfsong(buf, BUFSIZE, options.list_format, song);
391 screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
392 }
393 #ifndef NCMPC_MINI
394 } else {
395 /* remove song from playlist */
396 mpd_Song *song = entry->entity->info.song;
397 int idx;
399 entry->flags &= ~HIGHLIGHT;
401 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
402 mpdclient_cmd_delete(c, idx);
403 #endif
404 }
406 return 0;
407 }
409 static int
410 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
411 {
412 struct filelist_entry *entry = browser_get_selected(browser);
414 if (entry == NULL || entry->entity == NULL)
415 return -1;
417 return browser_select_entry(c, entry, TRUE);
418 }
420 static int
421 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
422 {
423 struct filelist_entry *entry = browser_get_selected(browser);
425 if (entry == NULL || entry->entity == NULL)
426 return -1;
428 return browser_select_entry(c, entry, FALSE);
429 }
431 static void
432 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
433 {
434 guint i;
436 if (browser->filelist == NULL)
437 return;
439 for (i = 0; i < filelist_length(browser->filelist); ++i) {
440 struct filelist_entry *entry = filelist_get(browser->filelist, i);
442 if (entry != NULL && entry->entity != NULL)
443 browser_select_entry(c, entry, FALSE);
444 }
445 }
447 #ifdef HAVE_GETMOUSE
448 static int
449 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
450 {
451 int row;
452 unsigned prev_selected = browser->lw->selected;
453 unsigned long bstate;
454 int length;
456 if (browser->filelist)
457 length = filelist_length(browser->filelist);
458 else
459 length = 0;
461 if (screen_get_mouse_event(c, &bstate, &row) ||
462 list_window_mouse(browser->lw, length, bstate, row))
463 return 1;
465 browser->lw->selected = browser->lw->start + row;
466 list_window_check_selected(browser->lw, length);
468 if( bstate & BUTTON1_CLICKED ) {
469 if (prev_selected == browser->lw->selected)
470 browser_handle_enter(browser, c);
471 } else if (bstate & BUTTON3_CLICKED) {
472 if (prev_selected == browser->lw->selected)
473 browser_handle_select(browser, c);
474 }
476 return 1;
477 }
478 #endif
480 bool
481 browser_cmd(struct screen_browser *browser,
482 struct mpdclient *c, command_t cmd)
483 {
484 #ifdef ENABLE_LYRICS_SCREEN
485 struct filelist_entry *entry;
486 #endif
488 switch (cmd) {
489 case CMD_PLAY:
490 browser_handle_enter(browser, c);
491 return true;
493 case CMD_SELECT:
494 if (browser_handle_select(browser, c) == 0)
495 /* continue and select next item... */
496 cmd = CMD_LIST_NEXT;
498 /* call list_window_cmd to go to the next item */
499 break;
501 case CMD_ADD:
502 if (browser_handle_add(browser, c) == 0)
503 /* continue and select next item... */
504 cmd = CMD_LIST_NEXT;
506 /* call list_window_cmd to go to the next item */
507 break;
509 case CMD_SELECT_ALL:
510 browser_handle_select_all(browser, c);
511 return true;
513 case CMD_LIST_FIND:
514 case CMD_LIST_RFIND:
515 case CMD_LIST_FIND_NEXT:
516 case CMD_LIST_RFIND_NEXT:
517 screen_find(browser->lw, filelist_length(browser->filelist),
518 cmd, browser_lw_callback,
519 browser->filelist);
520 return true;
522 #ifdef HAVE_GETMOUSE
523 case CMD_MOUSE_EVENT:
524 browser_handle_mouse_event(browser, c);
525 return true;
526 #endif
528 #ifdef ENABLE_LYRICS_SCREEN
529 case CMD_SCREEN_LYRICS:
530 entry = browser_get_selected(browser);
531 if (entry == NULL)
532 return false;
534 if (entry->entity == NULL ||
535 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
536 return true;
538 screen_lyrics_switch(c, entry->entity->info.song);
539 return true;
540 #endif
542 default:
543 break;
544 }
546 if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
547 cmd))
548 return true;
550 return false;
551 }