Code

don't import mpdclient_finish_command() twice
[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"
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         }
115 /* list_window callback */
116 const char *
117 browser_lw_callback(unsigned idx, int *highlight, void *data)
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!";
160 /* chdir */
161 int
162 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
163                          filelist_entry_t *entry, const char *new_path)
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;
213 static int
214 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
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;
227 static int
228 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
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;
251 static struct filelist_entry *
252 browser_get_selected(const struct screen_browser *browser)
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);
261 static int
262 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
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;
281 #ifdef USE_OLD_ADD
283 static int
284 add_directory(mpdclient_t *c, char *dir)
286         mpd_InfoEntity *entity;
287         GList *subdir_list = NULL;
288         GList *list = NULL;
289         char *dirname;
291         dirname = utf8_to_locale(dir);
292         screen_status_printf(_("Adding directory %s...\n"), dirname);
293         doupdate();
294         g_free(dirname);
295         dirname = NULL;
297         mpd_sendLsInfoCommand(c->connection, dir);
298         mpd_sendCommandListBegin(c->connection);
299         while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
300                 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
301                         mpd_Song *song = entity->info.song;
302                         mpd_sendAddCommand(c->connection, song->file);
303                         mpd_freeInfoEntity(entity);
304                 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
305                         subdir_list = g_list_append(subdir_list, (gpointer) entity);
306                 } else
307                         mpd_freeInfoEntity(entity);
308         }
309         mpd_sendCommandListEnd(c->connection);
310         mpdclient_finish_command(c);
311         c->need_update = TRUE;
313         list = g_list_first(subdir_list);
314         while( list!=NULL ) {
315                 mpd_Directory *dir;
317                 entity = list->data;
318                 dir = entity->info.directory;
319                 add_directory(c, dir->path);
320                 mpd_freeInfoEntity(entity);
321                 list->data=NULL;
322                 list=list->next;
323         }
324         g_list_free(subdir_list);
325         return 0;
327 #endif
329 static int
330 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
331                      gboolean toggle)
333         assert(entry != NULL);
334         assert(entry->entity != NULL);
336         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
337                 return load_playlist(c, entry);
339         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
340                 mpd_Directory *dir = entry->entity->info.directory;
341 #ifdef USE_OLD_ADD
342                 add_directory(c, tmp);
343 #else
344                 if (mpdclient_cmd_add_path_utf8(c, dir->path) == 0) {
345                         char *tmp = utf8_to_locale(dir->path);
347                         screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
348                         g_free(tmp);
349                 }
350 #endif
351                 return 0;
352         }
354         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
355                 return -1;
357         assert(entry->entity->info.song != NULL);
359         if (!toggle || (entry->flags & HIGHLIGHT) == 0) {
360                 mpd_Song *song = entry->entity->info.song;
362                 entry->flags |= HIGHLIGHT;
364                 if (mpdclient_cmd_add(c, song) == 0) {
365                         char buf[BUFSIZE];
367                         strfsong(buf, BUFSIZE, options.list_format, song);
368                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
369                 }
370         } else {
371                 /* remove song from playlist */
372                 mpd_Song *song = entry->entity->info.song;
373                 int idx;
375                 entry->flags &= ~HIGHLIGHT;
377                 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
378                         mpdclient_cmd_delete(c, idx);
379         }
381         return 0;
384 static int
385 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
387         struct filelist_entry *entry = browser_get_selected(browser);
389         if (entry == NULL || entry->entity == NULL)
390                 return -1;
392         return browser_select_entry(c, entry, TRUE);
395 static int
396 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
398         struct filelist_entry *entry = browser_get_selected(browser);
400         if (entry == NULL || entry->entity == NULL)
401                 return -1;
403         return browser_select_entry(c, entry, FALSE);
406 static void
407 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
409         guint i;
411         if (browser->filelist == NULL)
412                 return;
414         for (i = 0; i < filelist_length(browser->filelist); ++i) {
415                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
417                 if (entry != NULL && entry->entity != NULL)
418                         browser_select_entry(c, entry, FALSE);
419         }
422 #ifdef HAVE_GETMOUSE
423 static int
424 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
426         int row;
427         unsigned prev_selected = browser->lw->selected;
428         unsigned long bstate;
429         int length;
431         if (browser->filelist)
432                 length = filelist_length(browser->filelist);
433         else
434                 length = 0;
436         if (screen_get_mouse_event(c, &bstate, &row) ||
437             list_window_mouse(browser->lw, length, bstate, row))
438                 return 1;
440         browser->lw->selected = browser->lw->start + row;
441         list_window_check_selected(browser->lw, length);
443         if( bstate & BUTTON1_CLICKED ) {
444                 if (prev_selected == browser->lw->selected)
445                         browser_handle_enter(browser, c);
446         } else if (bstate & BUTTON3_CLICKED) {
447                 if (prev_selected == browser->lw->selected)
448                         browser_handle_select(browser, c);
449         }
451         return 1;
453 #endif
455 bool
456 browser_cmd(struct screen_browser *browser, struct screen *screen,
457             struct mpdclient *c, command_t cmd)
459         struct filelist_entry *entry;
461         switch (cmd) {
462         case CMD_PLAY:
463                 browser_handle_enter(browser, c);
464                 return true;
466         case CMD_SELECT:
467                 if (browser_handle_select(browser, c) == 0)
468                         /* continue and select next item... */
469                         cmd = CMD_LIST_NEXT;
471                 /* call list_window_cmd to go to the next item */
472                 break;
474         case CMD_ADD:
475                 if (browser_handle_add(browser, c) == 0)
476                         /* continue and select next item... */
477                         cmd = CMD_LIST_NEXT;
479                 /* call list_window_cmd to go to the next item */
480                 break;
482         case CMD_SELECT_ALL:
483                 browser_handle_select_all(browser, c);
484                 return true;
486         case CMD_LIST_FIND:
487         case CMD_LIST_RFIND:
488         case CMD_LIST_FIND_NEXT:
489         case CMD_LIST_RFIND_NEXT:
490                 screen_find(screen,
491                             browser->lw, filelist_length(browser->filelist),
492                             cmd, browser_lw_callback,
493                             browser->filelist);
494                 return true;
496 #ifdef HAVE_GETMOUSE
497         case CMD_MOUSE_EVENT:
498                 browser_handle_mouse_event(browser, c);
499                 return true;
500 #endif
502 #ifdef ENABLE_LYRICS_SCREEN
503         case CMD_SCREEN_LYRICS:
504                 entry = browser_get_selected(browser);
505                 if (entry == NULL)
506                         return false;
508                 if (entry->entity == NULL ||
509                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
510                         return true;
512                 screen_lyrics_switch(c, entry->entity->info.song);
513                 return true;
514 #endif
516         default:
517                 break;
518         }
520         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
521                             cmd))
522                 return true;
524         return false;