3b0fc00ccf2a5610880c06cde0ece8e8e93f4649
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "screen_browser.h"
21 #include "screen_file.h"
22 #include "screen_song.h"
23 #include "screen_lyrics.h"
24 #include "screen_message.h"
25 #include "screen_find.h"
26 #include "screen.h"
27 #include "i18n.h"
28 #include "options.h"
29 #include "charset.h"
30 #include "strfsong.h"
31 #include "mpdclient.h"
32 #include "filelist.h"
34 #include <mpd/client.h>
36 #include <string.h>
38 #define BUFSIZE 1024
40 #ifndef NCMPC_MINI
41 #define HIGHLIGHT (0x01)
42 #endif
44 static const char playlist_format[] = "*%s*";
46 #ifndef NCMPC_MINI
48 /* sync highlight flags with playlist */
49 void
50 screen_browser_sync_highlights(struct filelist *fl,
51 const struct mpdclient_playlist *playlist)
52 {
53 guint i;
55 for (i = 0; i < filelist_length(fl); ++i) {
56 struct filelist_entry *entry = filelist_get(fl, i);
57 const struct mpd_entity *entity = entry->entity;
59 if (entity != NULL && mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
60 const struct mpd_song *song =
61 mpd_entity_get_song(entity);
63 if (playlist_get_index_from_same_song(playlist,
64 song) >= 0)
65 entry->flags |= HIGHLIGHT;
66 else
67 entry->flags &= ~HIGHLIGHT;
68 }
69 }
70 }
72 #endif
74 /* list_window callback */
75 static const char *
76 browser_lw_callback(unsigned idx, bool *highlight, G_GNUC_UNUSED char **second_column, void *data)
77 {
78 const struct filelist *fl = (const struct filelist *) data;
79 static char buf[BUFSIZE];
80 const struct filelist_entry *entry;
81 const struct mpd_entity *entity;
83 assert(fl != NULL);
84 assert(idx < filelist_length(fl));
86 entry = filelist_get(fl, idx);
87 assert(entry != NULL);
89 entity = entry->entity;
90 #ifndef NCMPC_MINI
91 *highlight = (entry->flags & HIGHLIGHT) != 0;
92 #else
93 *highlight = false;
94 #endif
96 if( entity == NULL )
97 return "[..]";
99 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_DIRECTORY) {
100 const struct mpd_directory *dir =
101 mpd_entity_get_directory(entity);
102 char *directory = utf8_to_locale(g_basename(mpd_directory_get_path(dir)));
104 g_snprintf(buf, BUFSIZE, "[%s]", directory);
105 g_free(directory);
106 return buf;
107 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG) {
108 const struct mpd_song *song = mpd_entity_get_song(entity);
110 strfsong(buf, BUFSIZE, options.list_format, song);
111 return buf;
112 } else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST) {
113 const struct mpd_playlist *playlist =
114 mpd_entity_get_playlist(entity);
115 char *filename = utf8_to_locale(g_basename(mpd_playlist_get_path(playlist)));
117 g_snprintf(buf, BUFSIZE, playlist_format, filename);
118 g_free(filename);
119 return buf;
120 }
122 return "Error: Unknown entry!";
123 }
125 static bool
126 load_playlist(struct mpdclient *c, const struct mpd_playlist *playlist)
127 {
128 struct mpd_connection *connection = mpdclient_get_connection(c);
129 char *filename = utf8_to_locale(mpd_playlist_get_path(playlist));
131 if (mpd_run_load(connection, mpd_playlist_get_path(playlist))) {
132 screen_status_printf(_("Loading playlist %s..."),
133 g_basename(filename));
134 c->events |= MPD_IDLE_QUEUE;
135 } else
136 mpdclient_handle_error(c);
138 g_free(filename);
139 return true;
140 }
142 static bool
143 enqueue_and_play(struct mpdclient *c, struct filelist_entry *entry)
144 {
145 struct mpd_connection *connection = mpdclient_get_connection(c);
146 const struct mpd_song *song = mpd_entity_get_song(entry->entity);
147 int id;
149 #ifndef NCMPC_MINI
150 if (!(entry->flags & HIGHLIGHT))
151 id = -1;
152 else
153 #endif
154 id = playlist_get_id_from_same_song(&c->playlist, song);
156 if (id < 0) {
157 char buf[BUFSIZE];
159 id = mpd_run_add_id(connection, mpd_song_get_uri(song));
160 if (id < 0) {
161 mpdclient_handle_error(c);
162 return false;
163 }
165 #ifndef NCMPC_MINI
166 entry->flags |= HIGHLIGHT;
167 #endif
168 strfsong(buf, BUFSIZE, options.list_format, song);
169 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
170 }
172 if (!mpd_run_play_id(connection, id)) {
173 mpdclient_handle_error(c);
174 return false;
175 }
177 return true;
178 }
180 struct filelist_entry *
181 browser_get_selected_entry(const struct screen_browser *browser)
182 {
183 struct list_window_range range;
185 list_window_get_range(browser->lw, &range);
187 if (browser->filelist == NULL ||
188 range.end <= range.start ||
189 range.end > range.start + 1 ||
190 range.start >= filelist_length(browser->filelist))
191 return NULL;
193 return filelist_get(browser->filelist, range.start);
194 }
196 static const struct mpd_entity *
197 browser_get_selected_entity(const struct screen_browser *browser)
198 {
199 const struct filelist_entry *entry = browser_get_selected_entry(browser);
201 return entry != NULL
202 ? entry->entity
203 : NULL;
204 }
206 static const struct mpd_song *
207 browser_get_selected_song(const struct screen_browser *browser)
208 {
209 const struct mpd_entity *entity = browser_get_selected_entity(browser);
211 return entity != NULL &&
212 mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG
213 ? mpd_entity_get_song(entity)
214 : NULL;
215 }
217 static struct filelist_entry *
218 browser_get_index(const struct screen_browser *browser, unsigned i)
219 {
220 if (browser->filelist == NULL ||
221 i >= filelist_length(browser->filelist))
222 return NULL;
224 return filelist_get(browser->filelist, i);
225 }
227 static bool
228 browser_handle_enter(struct screen_browser *browser, struct mpdclient *c)
229 {
230 struct filelist_entry *entry = browser_get_selected_entry(browser);
231 struct mpd_entity *entity;
233 if (entry == NULL)
234 return false;
236 entity = entry->entity;
237 if (entity == NULL)
238 return false;
240 if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_PLAYLIST)
241 return load_playlist(c, mpd_entity_get_playlist(entity));
242 else if (mpd_entity_get_type(entity) == MPD_ENTITY_TYPE_SONG)
243 return enqueue_and_play(c, entry);
244 return false;
245 }
247 static bool
248 browser_select_entry(struct mpdclient *c, struct filelist_entry *entry,
249 G_GNUC_UNUSED gboolean toggle)
250 {
251 assert(entry != NULL);
252 assert(entry->entity != NULL);
254 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_PLAYLIST)
255 return load_playlist(c, mpd_entity_get_playlist(entry->entity));
257 if (mpd_entity_get_type(entry->entity) == MPD_ENTITY_TYPE_DIRECTORY) {
258 const struct mpd_directory *dir =
259 mpd_entity_get_directory(entry->entity);
261 if (mpdclient_cmd_add_path(c, mpd_directory_get_path(dir))) {
262 char *tmp = utf8_to_locale(mpd_directory_get_path(dir));
264 screen_status_printf(_("Adding \'%s\' to playlist"), tmp);
265 g_free(tmp);
266 }
268 return true;
269 }
271 if (mpd_entity_get_type(entry->entity) != MPD_ENTITY_TYPE_SONG)
272 return false;
274 #ifndef NCMPC_MINI
275 if (!toggle || (entry->flags & HIGHLIGHT) == 0)
276 #endif
277 {
278 const struct mpd_song *song =
279 mpd_entity_get_song(entry->entity);
281 #ifndef NCMPC_MINI
282 entry->flags |= HIGHLIGHT;
283 #endif
285 if (mpdclient_cmd_add(c, song)) {
286 char buf[BUFSIZE];
288 strfsong(buf, BUFSIZE, options.list_format, song);
289 screen_status_printf(_("Adding \'%s\' to playlist"), buf);
290 }
291 #ifndef NCMPC_MINI
292 } else {
293 /* remove song from playlist */
294 const struct mpd_song *song =
295 mpd_entity_get_song(entry->entity);
296 int idx;
298 entry->flags &= ~HIGHLIGHT;
300 while ((idx = playlist_get_index_from_same_song(&c->playlist,
301 song)) >= 0)
302 mpdclient_cmd_delete(c, idx);
303 #endif
304 }
306 return true;
307 }
309 static bool
310 browser_handle_select(struct screen_browser *browser, struct mpdclient *c)
311 {
312 struct list_window_range range;
313 struct filelist_entry *entry;
314 bool success;
316 list_window_get_range(browser->lw, &range);
317 for (unsigned i = range.start; i < range.end; ++i) {
318 entry = browser_get_index(browser, i);
320 if (entry != NULL && entry->entity != NULL)
321 success = browser_select_entry(c, entry, TRUE);
322 }
324 return range.end == range.start + 1 && success;
325 }
327 static bool
328 browser_handle_add(struct screen_browser *browser, struct mpdclient *c)
329 {
330 struct list_window_range range;
331 struct filelist_entry *entry;
332 bool success;
334 list_window_get_range(browser->lw, &range);
335 for (unsigned i = range.start; i < range.end; ++i) {
336 entry = browser_get_index(browser, i);
338 if (entry != NULL && entry->entity != NULL)
339 success = browser_select_entry(c, entry, FALSE);
340 }
342 return range.end == range.start + 1 && success;
343 }
345 static void
346 browser_handle_select_all(struct screen_browser *browser, struct mpdclient *c)
347 {
348 guint i;
350 if (browser->filelist == NULL)
351 return;
353 for (i = 0; i < filelist_length(browser->filelist); ++i) {
354 struct filelist_entry *entry = filelist_get(browser->filelist, i);
356 if (entry != NULL && entry->entity != NULL)
357 browser_select_entry(c, entry, FALSE);
358 }
359 }
361 #ifdef HAVE_GETMOUSE
362 static int
363 browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
364 {
365 int row;
366 unsigned prev_selected = browser->lw->selected;
367 unsigned long bstate;
369 if (screen_get_mouse_event(c, &bstate, &row) ||
370 list_window_mouse(browser->lw, bstate, row))
371 return 1;
373 list_window_set_cursor(browser->lw, browser->lw->start + row);
375 if( bstate & BUTTON1_CLICKED ) {
376 if (prev_selected == browser->lw->selected)
377 browser_handle_enter(browser, c);
378 } else if (bstate & BUTTON3_CLICKED) {
379 if (prev_selected == browser->lw->selected)
380 browser_handle_select(browser, c);
381 }
383 return 1;
384 }
385 #endif
387 bool
388 browser_cmd(struct screen_browser *browser,
389 struct mpdclient *c, command_t cmd)
390 {
391 const struct mpd_song *song;
393 if (browser->filelist == NULL)
394 return false;
396 if (list_window_cmd(browser->lw, cmd))
397 return true;
399 switch (cmd) {
400 case CMD_LIST_FIND:
401 case CMD_LIST_RFIND:
402 case CMD_LIST_FIND_NEXT:
403 case CMD_LIST_RFIND_NEXT:
404 screen_find(browser->lw, cmd, browser_lw_callback,
405 browser->filelist);
406 return true;
407 case CMD_LIST_JUMP:
408 screen_jump(browser->lw, browser_lw_callback,
409 NULL, browser->filelist);
410 return true;
412 #ifdef HAVE_GETMOUSE
413 case CMD_MOUSE_EVENT:
414 browser_handle_mouse_event(browser, c);
415 return true;
416 #endif
418 #ifdef ENABLE_SONG_SCREEN
419 case CMD_SCREEN_SONG:
420 song = browser_get_selected_song(browser);
421 if (song == NULL)
422 return false;
424 screen_song_switch(c, song);
425 return true;
426 #endif
428 #ifdef ENABLE_LYRICS_SCREEN
429 case CMD_SCREEN_LYRICS:
430 song = browser_get_selected_song(browser);
431 if (song == NULL)
432 return false;
434 screen_lyrics_switch(c, song, false);
435 return true;
436 #endif
437 case CMD_SCREEN_SWAP:
438 screen_swap(c, browser_get_selected_song(browser));
439 return true;
441 default:
442 break;
443 }
445 if (!mpdclient_is_connected(c))
446 return false;
448 switch (cmd) {
449 case CMD_PLAY:
450 browser_handle_enter(browser, c);
451 return true;
453 case CMD_SELECT:
454 if (browser_handle_select(browser, c))
455 list_window_cmd(browser->lw, CMD_LIST_NEXT);
456 return true;
458 case CMD_ADD:
459 if (browser_handle_add(browser, c))
460 list_window_cmd(browser->lw, CMD_LIST_NEXT);
461 return true;
463 case CMD_SELECT_ALL:
464 browser_handle_select_all(browser, c);
465 return true;
467 case CMD_LOCATE:
468 song = browser_get_selected_song(browser);
469 if (song == NULL)
470 return false;
472 screen_file_goto_song(c, song);
473 return true;
475 default:
476 break;
477 }
479 return false;
480 }
482 void
483 screen_browser_paint(const struct screen_browser *browser)
484 {
485 list_window_paint(browser->lw, browser_lw_callback, browser->filelist);
486 }