4457e7e5954628dc03d15588b7f32a75a30cc71e
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 #define HIGHLIGHT (0x01)
36 static const char playlist_format[] = "*%s*";
38 /* clear the highlight flag for all items in the filelist */
39 static void
40 clear_highlights(mpdclient_filelist_t *fl)
41 {
42 guint i;
44 for (i = 0; i < filelist_length(fl); ++i) {
45 struct filelist_entry *entry = filelist_get(fl, i);
47 entry->flags &= ~HIGHLIGHT;
48 }
49 }
51 /* change the highlight flag for a song */
52 static void
53 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
54 {
55 struct filelist_entry *entry = filelist_find_song(fl, song);
56 mpd_InfoEntity *entity;
58 if (entry == NULL)
59 return;
61 entity = entry->entity;
62 if (highlight)
63 entry->flags |= HIGHLIGHT;
64 else
65 entry->flags &= ~HIGHLIGHT;
66 }
68 /* sync highlight flags with playlist */
69 void
70 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
71 {
72 guint i;
74 for (i = 0; i < filelist_length(fl); ++i) {
75 struct filelist_entry *entry = filelist_get(fl, i);
76 mpd_InfoEntity *entity = entry->entity;
78 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
79 mpd_Song *song = entity->info.song;
81 if( playlist_get_index_from_file(c, song->file) >= 0 )
82 entry->flags |= HIGHLIGHT;
83 else
84 entry->flags &= ~HIGHLIGHT;
85 }
86 }
87 }
89 /* the playlist have been updated -> fix highlights */
90 void
91 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
92 int event, gpointer data)
93 {
94 if (browser->filelist == NULL)
95 return;
97 switch(event) {
98 case PLAYLIST_EVENT_CLEAR:
99 clear_highlights(browser->filelist);
100 break;
101 case PLAYLIST_EVENT_ADD:
102 set_highlight(browser->filelist, (mpd_Song *) data, 1);
103 break;
104 case PLAYLIST_EVENT_DELETE:
105 set_highlight(browser->filelist, (mpd_Song *) data, 0);
106 break;
107 case PLAYLIST_EVENT_MOVE:
108 break;
109 default:
110 sync_highlights(c, browser->filelist);
111 break;
112 }
113 }
115 /* list_window callback */
116 const char *
117 browser_lw_callback(unsigned idx, int *highlight, void *data)
118 {
119 static char buf[BUFSIZE];
120 mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
121 filelist_entry_t *entry;
122 mpd_InfoEntity *entity;
124 if (idx >= filelist_length(fl))
125 return NULL;
127 entry = filelist_get(fl, idx);
128 assert(entry != NULL);
130 entity = entry->entity;
131 *highlight = (entry->flags & HIGHLIGHT);
133 if( entity == NULL )
134 return "[..]";
136 if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
137 mpd_Directory *dir = entity->info.directory;
138 char *directory = utf8_to_locale(g_basename(dir->path));
140 g_snprintf(buf, BUFSIZE, "[%s]", directory);
141 g_free(directory);
142 return buf;
143 } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
144 mpd_Song *song = entity->info.song;
146 strfsong(buf, BUFSIZE, options.list_format, song);
147 return buf;
148 } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
149 mpd_PlaylistFile *plf = entity->info.playlistFile;
150 char *filename = utf8_to_locale(g_basename(plf->path));
152 g_snprintf(buf, BUFSIZE, playlist_format, filename);
153 g_free(filename);
154 return buf;
155 }
157 return "Error: Unknown entry!";
158 }
160 /* chdir */
161 int
162 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
163 filelist_entry_t *entry, const char *new_path)
164 {
165 mpd_InfoEntity *entity = NULL;
166 gchar *path = NULL;
167 char *old_path;
168 int idx;
170 if( entry!=NULL )
171 entity = entry->entity;
172 else if( new_path==NULL )
173 return -1;
175 if( entity==NULL ) {
176 if( entry || 0==strcmp(new_path, "..") ) {
177 /* return to parent */
178 char *parent = g_path_get_dirname(browser->filelist->path);
179 if( strcmp(parent, ".") == 0 )
180 parent[0] = '\0';
181 path = g_strdup(parent);
182 } else {
183 /* entry==NULL, then new_path ("" is root) */
184 path = g_strdup(new_path);
185 }
186 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
187 /* enter sub */
188 mpd_Directory *dir = entity->info.directory;
189 path = utf8_to_locale(dir->path);
190 } else
191 return -1;
193 old_path = g_strdup(browser->filelist->path);
195 filelist_free(browser->filelist);
196 browser->filelist = mpdclient_filelist_get(c, path);
197 sync_highlights(c, browser->filelist);
199 idx = filelist_find_directory(browser->filelist, old_path);
200 g_free(old_path);
202 list_window_reset(browser->lw);
203 if (idx >= 0) {
204 list_window_set_selected(browser->lw, idx);
205 list_window_center(browser->lw,
206 filelist_length(browser->filelist), idx);
207 }
209 g_free(path);
210 return 0;
211 }
213 static int
214 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
215 {
216 mpd_InfoEntity *entity = entry->entity;
217 mpd_PlaylistFile *plf = entity->info.playlistFile;
218 char *filename = utf8_to_locale(plf->path);
220 if (mpdclient_cmd_load_playlist_utf8(c, plf->path) == 0)
221 screen_status_printf(_("Loading playlist %s..."),
222 g_basename(filename));
223 g_free(filename);
224 return 0;
225 }
227 static int
228 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
229 {
230 int idx;
231 mpd_InfoEntity *entity = entry->entity;
232 mpd_Song *song = entity->info.song;
234 if (!(entry->flags & HIGHLIGHT)) {
235 if (mpdclient_cmd_add(c, song) == 0) {
236 char buf[BUFSIZE];
238 entry->flags |= HIGHLIGHT;
239 strfsong(buf, BUFSIZE, options.list_format, song);
240 screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
241 mpdclient_update(c); /* get song id */
242 } else
243 return -1;
244 }
246 idx = playlist_get_index_from_file(c, song->file);
247 mpdclient_cmd_play(c, idx);
248 return 0;
249 }
251 static struct filelist_entry *
252 browser_get_selected(const struct screen_browser *browser)
253 {
254 if (browser->filelist == NULL ||
255 browser->lw->selected >= filelist_length(browser->filelist))
256 return NULL;
258 return filelist_get(browser->filelist, browser->lw->selected);
259 }
261 static int
262 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
263 {
264 struct filelist_entry *entry = browser_get_selected(browser);
265 mpd_InfoEntity *entity;
267 if( entry==NULL )
268 return -1;
270 entity = entry->entity;
271 if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
272 return browser_change_directory(browser, c, entry, NULL);
273 else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
274 return load_playlist(c, entry);
275 else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
276 return enqueue_and_play(c, entry);
277 return -1;
278 }
281 #ifdef USE_OLD_ADD
282 /* NOTE - The add_directory functions should move to mpdclient.c */
283 extern gint mpdclient_finish_command(mpdclient_t *c);
285 static int
286 add_directory(mpdclient_t *c, char *dir)
287 {
288 mpd_InfoEntity *entity;
289 GList *subdir_list = NULL;
290 GList *list = NULL;
291 char *dirname;
293 dirname = utf8_to_locale(dir);
294 screen_status_printf(_("Adding directory %s...\n"), dirname);
295 doupdate();
296 g_free(dirname);
297 dirname = NULL;
299 mpd_sendLsInfoCommand(c->connection, dir);
300 mpd_sendCommandListBegin(c->connection);
301 while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
302 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
303 mpd_Song *song = entity->info.song;
304 mpd_sendAddCommand(c->connection, song->file);
305 mpd_freeInfoEntity(entity);
306 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
307 subdir_list = g_list_append(subdir_list, (gpointer) entity);
308 } else
309 mpd_freeInfoEntity(entity);
310 }
311 mpd_sendCommandListEnd(c->connection);
312 mpdclient_finish_command(c);
313 c->need_update = TRUE;
315 list = g_list_first(subdir_list);
316 while( list!=NULL ) {
317 mpd_Directory *dir;
319 entity = list->data;
320 dir = entity->info.directory;
321 add_directory(c, dir->path);
322 mpd_freeInfoEntity(entity);
323 list->data=NULL;
324 list=list->next;
325 }
326 g_list_free(subdir_list);
327 return 0;
328 }
329 #endif
331 static int
332 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
333 gboolean toggle)
334 {
335 assert(entry != NULL);
336 assert(entry->entity != NULL);
338 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
339 return load_playlist(c, entry);
341 if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
342 mpd_Directory *dir = entry->entity->info.directory;
343 #ifdef USE_OLD_ADD
344 add_directory(c, tmp);
345 #else
346 if (mpdclient_cmd_add_path_utf8(c, dir->path) == 0) {
347 char *tmp = utf8_to_locale(dir->path);
349 screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
350 g_free(tmp);
351 }
352 #endif
353 return 0;
354 }
356 if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
357 return -1;
359 assert(entry->entity->info.song != NULL);
361 if (!toggle || (entry->flags & HIGHLIGHT) == 0) {
362 mpd_Song *song = entry->entity->info.song;
364 entry->flags |= HIGHLIGHT;
366 if (mpdclient_cmd_add(c, song) == 0) {
367 char buf[BUFSIZE];
369 strfsong(buf, BUFSIZE, options.list_format, song);
370 screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
371 }
372 } else {
373 /* remove song from playlist */
374 mpd_Song *song = entry->entity->info.song;
375 int idx;
377 entry->flags &= ~HIGHLIGHT;
379 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
380 mpdclient_cmd_delete(c, idx);
381 }
383 return 0;
384 }
386 static int
387 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
388 {
389 struct filelist_entry *entry = browser_get_selected(browser);
391 if (entry == NULL || entry->entity == NULL)
392 return -1;
394 return browser_select_entry(c, entry, TRUE);
395 }
397 static int
398 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
399 {
400 struct filelist_entry *entry = browser_get_selected(browser);
402 if (entry == NULL || entry->entity == NULL)
403 return -1;
405 return browser_select_entry(c, entry, FALSE);
406 }
408 static void
409 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
410 {
411 guint i;
413 if (browser->filelist == NULL)
414 return;
416 for (i = 0; i < filelist_length(browser->filelist); ++i) {
417 struct filelist_entry *entry = filelist_get(browser->filelist, i);
419 if (entry != NULL && entry->entity != NULL)
420 browser_select_entry(c, entry, FALSE);
421 }
422 }
424 #ifdef HAVE_GETMOUSE
425 static int
426 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
427 {
428 int row;
429 unsigned prev_selected = browser->lw->selected;
430 unsigned long bstate;
431 int length;
433 if (browser->filelist)
434 length = filelist_length(browser->filelist);
435 else
436 length = 0;
438 if (screen_get_mouse_event(c, &bstate, &row) ||
439 list_window_mouse(browser->lw, length, bstate, row))
440 return 1;
442 browser->lw->selected = browser->lw->start + row;
443 list_window_check_selected(browser->lw, length);
445 if( bstate & BUTTON1_CLICKED ) {
446 if (prev_selected == browser->lw->selected)
447 browser_handle_enter(browser, c);
448 } else if (bstate & BUTTON3_CLICKED) {
449 if (prev_selected == browser->lw->selected)
450 browser_handle_select(browser, c);
451 }
453 return 1;
454 }
455 #endif
457 bool
458 browser_cmd(struct screen_browser *browser, struct screen *screen,
459 struct mpdclient *c, command_t cmd)
460 {
461 struct filelist_entry *entry;
463 switch (cmd) {
464 case CMD_PLAY:
465 browser_handle_enter(browser, c);
466 return true;
468 case CMD_SELECT:
469 if (browser_handle_select(browser, c) == 0)
470 /* continue and select next item... */
471 cmd = CMD_LIST_NEXT;
473 /* call list_window_cmd to go to the next item */
474 break;
476 case CMD_ADD:
477 if (browser_handle_add(browser, c) == 0)
478 /* continue and select next item... */
479 cmd = CMD_LIST_NEXT;
481 /* call list_window_cmd to go to the next item */
482 break;
484 case CMD_SELECT_ALL:
485 browser_handle_select_all(browser, c);
486 return true;
488 case CMD_LIST_FIND:
489 case CMD_LIST_RFIND:
490 case CMD_LIST_FIND_NEXT:
491 case CMD_LIST_RFIND_NEXT:
492 screen_find(screen,
493 browser->lw, filelist_length(browser->filelist),
494 cmd, browser_lw_callback,
495 browser->filelist);
496 return true;
498 #ifdef HAVE_GETMOUSE
499 case CMD_MOUSE_EVENT:
500 browser_handle_mouse_event(browser, c);
501 return true;
502 #endif
504 #ifdef ENABLE_LYRICS_SCREEN
505 case CMD_SCREEN_LYRICS:
506 entry = browser_get_selected(browser);
507 if (entry == NULL)
508 return false;
510 if (entry->entity == NULL ||
511 entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
512 return true;
514 screen_lyrics_switch(c, entry->entity->info.song);
515 return true;
516 #endif
518 default:
519 break;
520 }
522 if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
523 cmd))
524 return true;
526 return false;
527 }