Code

use g_ascii_strdown() instead of the custom lowerstr()
[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 "support.h"
25 #include "strfsong.h"
26 #include "screen_utils.h"
27 #include "gcc.h"
29 #include <string.h>
31 #undef  USE_OLD_ADD
33 #define BUFSIZE 1024
35 #define HIGHLIGHT  (0x01)
37 static const char playlist_format[] = "*%s*";
39 /* clear the highlight flag for all items in the filelist */
40 static void
41 clear_highlights(mpdclient_filelist_t *fl)
42 {
43         guint i;
45         for (i = 0; i < filelist_length(fl); ++i) {
46                 struct filelist_entry *entry = filelist_get(fl, i);
48                 entry->flags &= ~HIGHLIGHT;
49         }
50 }
52 /* change the highlight flag for a song */
53 static void
54 set_highlight(mpdclient_filelist_t *fl, mpd_Song *song, int highlight)
55 {
56         struct filelist_entry *entry = filelist_find_song(fl, song);
57         mpd_InfoEntity *entity;
59         if (entry == NULL)
60                 return;
62         entity = entry->entity;
63         if (highlight)
64                 entry->flags |= HIGHLIGHT;
65         else
66                 entry->flags &= ~HIGHLIGHT;
67 }
69 /* sync highlight flags with playlist */
70 void
71 sync_highlights(mpdclient_t *c, mpdclient_filelist_t *fl)
72 {
73         guint i;
75         for (i = 0; i < filelist_length(fl); ++i) {
76                 struct filelist_entry *entry = filelist_get(fl, i);
77                 mpd_InfoEntity *entity = entry->entity;
79                 if ( entity && entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
80                         mpd_Song *song = entity->info.song;
82                         if( playlist_get_index_from_file(c, song->file) >= 0 )
83                                 entry->flags |= HIGHLIGHT;
84                         else
85                                 entry->flags &= ~HIGHLIGHT;
86                 }
87         }
88 }
90 /* the playlist have been updated -> fix highlights */
91 void
92 browser_playlist_changed(struct screen_browser *browser, mpdclient_t *c,
93                          int event, gpointer data)
94 {
95         if (browser->filelist == NULL)
96                 return;
98         switch(event) {
99         case PLAYLIST_EVENT_CLEAR:
100                 clear_highlights(browser->filelist);
101                 break;
102         case PLAYLIST_EVENT_ADD:
103                 set_highlight(browser->filelist, (mpd_Song *) data, 1);
104                 break;
105         case PLAYLIST_EVENT_DELETE:
106                 set_highlight(browser->filelist, (mpd_Song *) data, 0);
107                 break;
108         case PLAYLIST_EVENT_MOVE:
109                 break;
110         default:
111                 sync_highlights(c, browser->filelist);
112                 break;
113         }
116 /* list_window callback */
117 const char *
118 browser_lw_callback(unsigned idx, int *highlight, void *data)
120         static char buf[BUFSIZE];
121         mpdclient_filelist_t *fl = (mpdclient_filelist_t *) data;
122         filelist_entry_t *entry;
123         mpd_InfoEntity *entity;
125         if (idx >= filelist_length(fl))
126                 return NULL;
128         entry = filelist_get(fl, idx);
129         assert(entry != NULL);
131         entity = entry->entity;
132         *highlight = (entry->flags & HIGHLIGHT);
134         if( entity == NULL )
135                 return "[..]";
137         if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
138                 mpd_Directory *dir = entity->info.directory;
139                 char *directory = utf8_to_locale(basename(dir->path));
141                 g_snprintf(buf, BUFSIZE, "[%s]", directory);
142                 g_free(directory);
143                 return buf;
144         } else if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
145                 mpd_Song *song = entity->info.song;
147                 strfsong(buf, BUFSIZE, options.list_format, song);
148                 return buf;
149         } else if( entity->type==MPD_INFO_ENTITY_TYPE_PLAYLISTFILE ) {
150                 mpd_PlaylistFile *plf = entity->info.playlistFile;
151                 char *filename = utf8_to_locale(basename(plf->path));
153                 g_snprintf(buf, BUFSIZE, playlist_format, filename);
154                 g_free(filename);
155                 return buf;
156         }
158         return "Error: Unknown entry!";
161 /* chdir */
162 int
163 browser_change_directory(struct screen_browser *browser, mpdclient_t *c,
164                          filelist_entry_t *entry, const char *new_path)
166         mpd_InfoEntity *entity = NULL;
167         gchar *path = NULL;
169         if( entry!=NULL )
170                 entity = entry->entity;
171         else if( new_path==NULL )
172                 return -1;
174         if( entity==NULL ) {
175                 if( entry || 0==strcmp(new_path, "..") ) {
176                         /* return to parent */
177                         char *parent = g_path_get_dirname(browser->filelist->path);
178                         if( strcmp(parent, ".") == 0 )
179                                 parent[0] = '\0';
180                         path = g_strdup(parent);
181                         list_window_reset(browser->lw);
182                         /* restore previous list window state */
183                         list_window_pop_state(browser->lw_state, browser->lw);
184                 } else {
185                         /* entry==NULL, then new_path ("" is root) */
186                         path = g_strdup(new_path);
187                         list_window_reset(browser->lw);
188                         /* restore first list window state (pop while returning true) */
189                         while(list_window_pop_state(browser->lw_state, browser->lw));
190                 }
191         } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY) {
192                 /* enter sub */
193                 mpd_Directory *dir = entity->info.directory;
194                 path = utf8_to_locale(dir->path);
195                 /* save current list window state */
196                 list_window_push_state(browser->lw_state, browser->lw);
197         } else
198                 return -1;
200         filelist_free(browser->filelist);
201         browser->filelist = mpdclient_filelist_get(c, path);
202         sync_highlights(c, browser->filelist);
203         list_window_check_selected(browser->lw, filelist_length(browser->filelist));
204         g_free(path);
205         return 0;
208 static int
209 load_playlist(mpdclient_t *c, filelist_entry_t *entry)
211         mpd_InfoEntity *entity = entry->entity;
212         mpd_PlaylistFile *plf = entity->info.playlistFile;
213         char *filename = utf8_to_locale(plf->path);
215         if (mpdclient_cmd_load_playlist_utf8(c, plf->path) == 0)
216                 screen_status_printf(_("Loading playlist %s..."), basename(filename));
217         g_free(filename);
218         return 0;
221 static int
222 enqueue_and_play(mpdclient_t *c, filelist_entry_t *entry)
224         int idx;
225         mpd_InfoEntity *entity = entry->entity;
226         mpd_Song *song = entity->info.song;
228         if (!(entry->flags & HIGHLIGHT)) {
229                 if (mpdclient_cmd_add(c, song) == 0) {
230                         char buf[BUFSIZE];
232                         entry->flags |= HIGHLIGHT;
233                         strfsong(buf, BUFSIZE, options.list_format, song);
234                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
235                         mpdclient_update(c); /* get song id */
236                 } else
237                         return -1;
238         }
240         idx = playlist_get_index_from_file(c, song->file);
241         mpdclient_cmd_play(c, idx);
242         return 0;
245 static struct filelist_entry *
246 browser_get_selected(const struct screen_browser *browser)
248         if (browser->filelist == NULL ||
249             browser->lw->selected >= filelist_length(browser->filelist))
250                 return NULL;
252         return filelist_get(browser->filelist, browser->lw->selected);
255 static int
256 browser_handle_enter(struct screen_browser *browser, mpdclient_t *c)
258         struct filelist_entry *entry = browser_get_selected(browser);
259         mpd_InfoEntity *entity;
261         if( entry==NULL )
262                 return -1;
264         entity = entry->entity;
265         if (entity == NULL || entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY)
266                 return browser_change_directory(browser, c, entry, NULL);
267         else if (entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
268                 return load_playlist(c, entry);
269         else if (entity->type == MPD_INFO_ENTITY_TYPE_SONG)
270                 return enqueue_and_play(c, entry);
271         return -1;
275 #ifdef USE_OLD_ADD
276 /* NOTE - The add_directory functions should move to mpdclient.c */
277 extern gint mpdclient_finish_command(mpdclient_t *c);
279 static int
280 add_directory(mpdclient_t *c, char *dir)
282         mpd_InfoEntity *entity;
283         GList *subdir_list = NULL;
284         GList *list = NULL;
285         char *dirname;
287         dirname = utf8_to_locale(dir);
288         screen_status_printf(_("Adding directory %s...\n"), dirname);
289         doupdate();
290         g_free(dirname);
291         dirname = NULL;
293         mpd_sendLsInfoCommand(c->connection, dir);
294         mpd_sendCommandListBegin(c->connection);
295         while( (entity=mpd_getNextInfoEntity(c->connection)) ) {
296                 if( entity->type==MPD_INFO_ENTITY_TYPE_SONG ) {
297                         mpd_Song *song = entity->info.song;
298                         mpd_sendAddCommand(c->connection, song->file);
299                         mpd_freeInfoEntity(entity);
300                 } else if( entity->type==MPD_INFO_ENTITY_TYPE_DIRECTORY ) {
301                         subdir_list = g_list_append(subdir_list, (gpointer) entity);
302                 } else
303                         mpd_freeInfoEntity(entity);
304         }
305         mpd_sendCommandListEnd(c->connection);
306         mpdclient_finish_command(c);
307         c->need_update = TRUE;
309         list = g_list_first(subdir_list);
310         while( list!=NULL ) {
311                 mpd_Directory *dir;
313                 entity = list->data;
314                 dir = entity->info.directory;
315                 add_directory(c, dir->path);
316                 mpd_freeInfoEntity(entity);
317                 list->data=NULL;
318                 list=list->next;
319         }
320         g_list_free(subdir_list);
321         return 0;
323 #endif
325 static int
326 browser_select_entry(mpdclient_t *c, filelist_entry_t *entry,
327                      gboolean toggle)
329         assert(entry != NULL);
330         assert(entry->entity != NULL);
332         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_PLAYLISTFILE)
333                 return load_playlist(c, entry);
335         if (entry->entity->type == MPD_INFO_ENTITY_TYPE_DIRECTORY) {
336                 mpd_Directory *dir = entry->entity->info.directory;
337 #ifdef USE_OLD_ADD
338                 add_directory(c, tmp);
339 #else
340                 if (mpdclient_cmd_add_path_utf8(c, dir->path) == 0) {
341                         char *tmp = utf8_to_locale(dir->path);
343                         screen_status_printf(_("Adding \'%s\' to playlist\n"), tmp);
344                         g_free(tmp);
345                 }
346 #endif
347                 return 0;
348         }
350         if (entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
351                 return -1;
353         assert(entry->entity->info.song != NULL);
355         if (!toggle || (entry->flags & HIGHLIGHT) == 0) {
356                 mpd_Song *song = entry->entity->info.song;
358                 entry->flags |= HIGHLIGHT;
360                 if (mpdclient_cmd_add(c, song) == 0) {
361                         char buf[BUFSIZE];
363                         strfsong(buf, BUFSIZE, options.list_format, song);
364                         screen_status_printf(_("Adding \'%s\' to playlist\n"), buf);
365                 }
366         } else {
367                 /* remove song from playlist */
368                 mpd_Song *song = entry->entity->info.song;
369                 int idx;
371                 entry->flags &= ~HIGHLIGHT;
373                 while ((idx = playlist_get_index_from_file(c, song->file)) >=0)
374                         mpdclient_cmd_delete(c, idx);
375         }
377         return 0;
380 static int
381 browser_handle_select(struct screen_browser *browser, mpdclient_t *c)
383         struct filelist_entry *entry = browser_get_selected(browser);
385         if (entry == NULL || entry->entity == NULL)
386                 return -1;
388         return browser_select_entry(c, entry, TRUE);
391 static int
392 browser_handle_add(struct screen_browser *browser, mpdclient_t *c)
394         struct filelist_entry *entry = browser_get_selected(browser);
396         if (entry == NULL || entry->entity == NULL)
397                 return -1;
399         return browser_select_entry(c, entry, FALSE);
402 static void
403 browser_handle_select_all(struct screen_browser *browser, mpdclient_t *c)
405         guint i;
407         if (browser->filelist == NULL)
408                 return;
410         for (i = 0; i < filelist_length(browser->filelist); ++i) {
411                 struct filelist_entry *entry = filelist_get(browser->filelist, i);
413                 if (entry != NULL && entry->entity != NULL)
414                         browser_select_entry(c, entry, FALSE);
415         }
418 #ifdef HAVE_GETMOUSE
419 static int
420 browser_handle_mouse_event(struct screen_browser *browser, mpdclient_t *c)
422         int row;
423         unsigned prev_selected = browser->lw->selected;
424         unsigned long bstate;
425         int length;
427         if (browser->filelist)
428                 length = filelist_length(browser->filelist);
429         else
430                 length = 0;
432         if (screen_get_mouse_event(c, &bstate, &row) ||
433             list_window_mouse(browser->lw, length, bstate, row))
434                 return 1;
436         browser->lw->selected = browser->lw->start + row;
437         list_window_check_selected(browser->lw, length);
439         if( bstate & BUTTON1_CLICKED ) {
440                 if (prev_selected == browser->lw->selected)
441                         browser_handle_enter(browser, c);
442         } else if (bstate & BUTTON3_CLICKED) {
443                 if (prev_selected == browser->lw->selected)
444                         browser_handle_select(browser, c);
445         }
447         return 1;
449 #endif
451 bool
452 browser_cmd(struct screen_browser *browser, struct screen *screen,
453             struct mpdclient *c, command_t cmd)
455         struct filelist_entry *entry;
457         switch (cmd) {
458         case CMD_PLAY:
459                 browser_handle_enter(browser, c);
460                 return true;
462         case CMD_SELECT:
463                 if (browser_handle_select(browser, c) == 0)
464                         /* continue and select next item... */
465                         cmd = CMD_LIST_NEXT;
467                 /* call list_window_cmd to go to the next item */
468                 break;
470         case CMD_ADD:
471                 if (browser_handle_add(browser, c) == 0)
472                         /* continue and select next item... */
473                         cmd = CMD_LIST_NEXT;
475                 /* call list_window_cmd to go to the next item */
476                 break;
478         case CMD_SELECT_ALL:
479                 browser_handle_select_all(browser, c);
480                 return true;
482         case CMD_LIST_FIND:
483         case CMD_LIST_RFIND:
484         case CMD_LIST_FIND_NEXT:
485         case CMD_LIST_RFIND_NEXT:
486                 screen_find(screen,
487                             browser->lw, filelist_length(browser->filelist),
488                             cmd, browser_lw_callback,
489                             browser->filelist);
490                 return true;
492 #ifdef HAVE_GETMOUSE
493         case CMD_MOUSE_EVENT:
494                 browser_handle_mouse_event(browser, c);
495                 return true;
496 #endif
498 #ifdef ENABLE_LYRICS_SCREEN
499         case CMD_SCREEN_LYRICS:
500                 entry = browser_get_selected(browser);
501                 if (entry == NULL)
502                         return false;
504                 if (entry->entity == NULL ||
505                     entry->entity->type != MPD_INFO_ENTITY_TYPE_SONG)
506                         return true;
508                 screen_lyrics_switch(c, entry->entity->info.song);
509                 return true;
510 #endif
512         default:
513                 break;
514         }
516         if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
517                             cmd))
518                 return true;
520         return false;