1 #include <stdlib.h>
2 #include <string.h>
3 #include <glib.h>
4 #include <ncurses.h>
6 #include "config.h"
7 #include "support.h"
8 #include "libmpdclient.h"
9 #include "mpc.h"
10 #include "command.h"
11 #include "screen.h"
12 #include "screen_utils.h"
13 #include "screen_file.h"
14 #include "screen_play.h"
17 #define BUFSIZE 256
19 static char *
20 list_callback(int index, int *highlight, void *data)
21 {
22 mpd_client_t *c = (mpd_client_t *) data;
23 mpd_Song *song;
25 *highlight = 0;
26 if( (song=mpc_playlist_get_song(c, index)) == NULL )
27 {
28 return NULL;
29 }
31 if( IS_PLAYING(c->status->state) && index==c->song_id )
32 {
33 *highlight = 1;
34 }
36 return mpc_get_song_name(song);
37 }
39 static int
40 handle_save_playlist(screen_t *screen, mpd_client_t *c)
41 {
42 char *filename, *filename_utf8;
44 filename=screen_getstr(screen->status_window.w, "Save playlist as: ");
45 filename=trim(filename);
46 if( filename==NULL || filename[0]=='\0' )
47 return -1;
48 /* convert filename to utf-8 */
49 filename_utf8 = locale_to_utf8(filename);
50 /* send save command to mpd */
51 mpd_sendSaveCommand(c->connection, filename_utf8);
52 mpd_finishCommand(c->connection);
53 g_free(filename_utf8);
54 /* handle errors */
55 if( mpc_error(c))
56 {
57 if( mpc_error_str(c) )
58 {
59 char *str = utf8_to_locale(mpc_error_str(c));
60 screen_status_message(str);
61 g_free(str);
62 }
63 else
64 screen_status_printf("Error: Unable to save playlist as %s", filename);
65 mpd_clearError(c->connection);
66 beep();
67 return -1;
68 }
69 /* success */
70 screen_status_printf("Saved %s", filename);
71 g_free(filename);
72 /* update the file list if it has been initalized */
73 if( c->filelist )
74 {
75 mpc_update_filelist(c);
76 list_window_check_selected(screen->filelist, c->filelist_length);
77 }
78 return 0;
79 }
81 void
82 play_open(screen_t *screen, mpd_client_t *c)
83 {
85 }
87 void
88 play_close(screen_t *screen, mpd_client_t *c)
89 {
90 }
92 void
93 play_paint(screen_t *screen, mpd_client_t *c)
94 {
95 list_window_t *w = screen->playlist;
97 w->clear = 1;
99 list_window_paint(screen->playlist, list_callback, (void *) c);
100 wnoutrefresh(screen->playlist->w);
101 }
103 void
104 play_update(screen_t *screen, mpd_client_t *c)
105 {
106 if( c->playlist_updated )
107 {
108 if( screen->playlist->selected >= c->playlist_length )
109 screen->playlist->selected = c->playlist_length-1;
110 if( screen->playlist->start >= c->playlist_length )
111 list_window_reset(screen->playlist);
113 play_paint(screen, c);
114 c->playlist_updated = 0;
115 }
116 else if( screen->playlist->repaint || 1)
117 {
118 list_window_paint(screen->playlist, list_callback, (void *) c);
119 wnoutrefresh(screen->playlist->w);
120 screen->playlist->repaint = 0;
121 }
122 }
124 int
125 play_cmd(screen_t *screen, mpd_client_t *c, command_t cmd)
126 {
127 mpd_Song *song;
129 switch(cmd)
130 {
131 case CMD_DELETE:
132 song = mpc_playlist_get_song(c, screen->playlist->selected);
133 if( song )
134 {
135 file_clear_highlight(c, song);
136 mpd_sendDeleteCommand(c->connection, screen->playlist->selected);
137 mpd_finishCommand(c->connection);
138 screen_status_printf("Removed \'%s\' from playlist!",
139 mpc_get_song_name(song));
140 }
141 return 1;
142 case CMD_SAVE_PLAYLIST:
143 handle_save_playlist(screen, c);
144 return 1;
145 case CMD_LIST_FIND:
146 case CMD_LIST_RFIND:
147 case CMD_LIST_FIND_NEXT:
148 case CMD_LIST_RFIND_NEXT:
149 return screen_find(screen, c,
150 screen->playlist, c->playlist_length,
151 cmd, list_callback);
152 default:
153 break;
154 }
155 return list_window_cmd(screen->playlist, c->playlist_length, cmd) ;
156 }