1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <glib.h>
25 #include <ncurses.h>
26 #include <panel.h>
28 #include "config.h"
29 #include "ncmpc.h"
30 #include "options.h"
31 #include "support.h"
32 #include "mpdclient.h"
33 #include "utils.h"
34 #include "strfsong.h"
35 #include "wreadln.h"
36 #include "command.h"
37 #include "colors.h"
38 #include "screen.h"
39 #include "screen_utils.h"
41 #define MAX_SONG_LENGTH 512
43 static list_window_t *lw = NULL;
45 static void
46 playlist_changed_callback(mpdclient_t *c, int event, gpointer data)
47 {
48 D("screen_play.c> playlist_callback() [%d]\n", event);
49 switch(event)
50 {
51 case PLAYLIST_EVENT_DELETE:
52 break;
53 case PLAYLIST_EVENT_MOVE:
54 lw->selected = *((int *) data);
55 break;
56 default:
57 break;
58 }
59 /* make shure the playlist is repainted */
60 lw->clear = 1;
61 lw->repaint = 1;
62 list_window_check_selected(lw, c->playlist.length);
63 }
65 static char *
66 list_callback(int index, int *highlight, void *data)
67 {
68 static char songname[MAX_SONG_LENGTH];
69 mpdclient_t *c = (mpdclient_t *) data;
70 mpd_Song *song;
72 *highlight = 0;
73 if( (song=playlist_get_song(c, index)) == NULL )
74 {
75 return NULL;
76 }
78 if( c->song && song->id==c->song->id && !IS_STOPPED(c->status->state) )
79 {
80 *highlight = 1;
81 }
82 strfsong(songname, MAX_SONG_LENGTH, LIST_FORMAT, song);
83 return songname;
84 }
86 static int
87 center_playing_item(screen_t *screen, mpdclient_t *c)
88 {
89 int length = c->playlist.length;
90 int offset = lw->selected-lw->start;
91 int index;
93 if( !lw || !c->song || length<lw->rows || IS_STOPPED(c->status->state) )
94 return 0;
96 /* try to center the song that are playing */
97 index = playlist_get_index(c, c->song);
98 D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,index);
99 lw->start = index-(lw->rows/2);
100 if( lw->start+lw->rows > length )
101 lw->start = length-lw->rows;
102 if( lw->start<0 )
103 lw->start=0;
105 /* make sure the cursor is in the window */
106 lw->selected = lw->start+offset;
107 list_window_check_selected(lw, length);
109 lw->clear = 1;
110 lw->repaint = 1;
112 return 0;
113 }
115 static int
116 handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
117 {
118 gchar *filename;
119 gint error;
121 if( name==NULL )
122 {
123 /* query the user for a filename */
124 filename=screen_getstr(screen->status_window.w, _("Save playlist as: "));
125 filename=trim(filename);
126 }
127 else
128 {
129 filename=g_strdup(name);
130 }
131 if( filename==NULL || filename[0]=='\0' )
132 return -1;
133 /* send save command to mpd */
134 D("Saving playlist as \'%s \'...\n", filename);
135 if( (error=mpdclient_cmd_save_playlist(c, filename)) )
136 {
137 gint code = GET_ACK_ERROR_CODE(error);
139 if( code == MPD_ACK_ERROR_EXIST )
140 {
141 char buf[256];
142 int key;
144 snprintf(buf, 256, _("Replace %s [%s/%s] ? "), filename, YES, NO);
145 key = tolower(screen_getch(screen->status_window.w, buf));
146 if( key == YES[0] )
147 {
148 char *filename_utf8 = locale_to_utf8(filename);
150 if( mpdclient_cmd_delete_playlist(c, filename_utf8) )
151 {
152 g_free(filename);
153 g_free(filename_utf8);
154 return -1;
155 }
156 g_free(filename_utf8);
157 error = handle_save_playlist(screen, c, filename);
158 g_free(filename);
159 return error;
160 }
161 screen_status_printf(_("Aborted!"));
162 }
163 g_free(filename);
164 return -1;
165 }
166 /* success */
167 screen_status_printf(_("Saved %s"), filename);
168 g_free(filename);
169 return 0;
170 }
172 static int
173 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
174 {
175 gchar *path;
176 GCompletion *gcmp;
177 GList *list = NULL;
178 GList *dir_list = NULL;
180 void add_dir(gchar *dir)
181 {
182 g_completion_remove_items(gcmp, list);
183 list = string_list_remove(list, dir);
184 list = gcmp_list_from_path(c, dir, list);
185 g_completion_add_items(gcmp, list);
186 dir_list = g_list_append(dir_list, g_strdup(dir));
187 }
189 void pre_completion_cb(GCompletion *gcmp, gchar *line)
190 {
191 if( list == NULL )
192 {
193 /* create initial list */
194 list = gcmp_list_from_path(c, "", NULL);
195 g_completion_add_items(gcmp, list);
196 }
197 else if( line && line[0] && line[strlen(line)-1]=='/' &&
198 string_list_find(dir_list, line) == NULL )
199 {
200 /* add directory content to list */
201 add_dir(line);
202 }
203 }
205 void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
206 {
207 if( g_list_length(items)>1 )
208 screen_display_completion_list(screen, items);
210 if( line && line[0] && line[strlen(line)-1]=='/' &&
211 string_list_find(dir_list, line) == NULL )
212 {
213 /* add directory content to list */
214 add_dir(line);
215 }
216 }
219 gcmp = g_completion_new(NULL);
220 g_completion_set_compare(gcmp, strncmp);
222 wrln_pre_completion_callback = pre_completion_cb;
223 wrln_post_completion_callback = post_completion_cb;
224 path = screen_readln(screen->status_window.w,
225 _("Add: "),
226 NULL,
227 NULL,
228 gcmp);
229 wrln_pre_completion_callback = NULL;
230 wrln_post_completion_callback = NULL;
232 g_completion_free(gcmp);
234 string_list_free(list);
235 string_list_free(dir_list);
237 if( path && path[0] )
238 mpdclient_cmd_add_path(c, path);
240 return 0;
241 }
243 static void
244 play_init(WINDOW *w, int cols, int rows)
245 {
246 lw = list_window_init(w, cols, rows);
247 }
249 static void
250 play_open(screen_t *screen, mpdclient_t *c)
251 {
252 static gboolean install_cb = TRUE;
254 if( install_cb )
255 {
256 mpdclient_install_playlist_callback(c, playlist_changed_callback);
257 install_cb = FALSE;
258 }
259 }
261 static void
262 play_resize(int cols, int rows)
263 {
264 lw->cols = cols;
265 lw->rows = rows;
266 }
269 static void
270 play_exit(void)
271 {
272 list_window_free(lw);
273 }
275 static char *
276 play_title(char *str, size_t size)
277 {
278 if( strcmp(options.host, "localhost") == 0 )
279 return _("Playlist");
281 snprintf(str, size, _("Playlist on %s"), options.host);
283 return str;
284 }
286 static void
287 play_paint(screen_t *screen, mpdclient_t *c)
288 {
289 lw->clear = 1;
291 list_window_paint(lw, list_callback, (void *) c);
292 wnoutrefresh(lw->w);
293 }
295 static void
296 play_update(screen_t *screen, mpdclient_t *c)
297 {
298 if( options.auto_center )
299 {
300 static int prev_song_id = 0;
302 if( c->song && prev_song_id != c->song->id )
303 {
304 center_playing_item(screen, c);
305 prev_song_id = c->song->id;
306 }
307 }
309 if( c->playlist.updated )
310 {
311 if( lw->selected >= c->playlist.length )
312 lw->selected = c->playlist.length-1;
313 if( lw->start >= c->playlist.length )
314 list_window_reset(lw);
316 play_paint(screen, c);
317 c->playlist.updated = FALSE;
318 }
319 else if( lw->repaint || 1)
320 {
321 list_window_paint(lw, list_callback, (void *) c);
322 wnoutrefresh(lw->w);
323 lw->repaint = 0;
324 }
325 }
327 static int
328 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
329 {
330 switch(cmd)
331 {
332 case CMD_PLAY:
333 mpdclient_cmd_play(c, lw->selected);
334 return 1;
335 case CMD_DELETE:
336 mpdclient_cmd_delete(c, lw->selected);
337 return 1;
338 case CMD_SAVE_PLAYLIST:
339 handle_save_playlist(screen, c, NULL);
340 return 1;
341 case CMD_ADD:
342 handle_add_to_playlist(screen, c);
343 return 1;
344 case CMD_SCREEN_UPDATE:
345 center_playing_item(screen, c);
346 return 1;
347 case CMD_LIST_MOVE_UP:
348 mpdclient_cmd_move(c, lw->selected, lw->selected-1);
349 return 1;
350 case CMD_LIST_MOVE_DOWN:
351 mpdclient_cmd_move(c, lw->selected, lw->selected+1);
352 return 1;
353 case CMD_LIST_FIND:
354 case CMD_LIST_RFIND:
355 case CMD_LIST_FIND_NEXT:
356 case CMD_LIST_RFIND_NEXT:
357 return screen_find(screen, c,
358 lw, c->playlist.length,
359 cmd, list_callback);
360 default:
361 break;
362 }
363 return list_window_cmd(lw, c->playlist.length, cmd) ;
364 }
368 static list_window_t *
369 play_lw(void)
370 {
371 return lw;
372 }
375 screen_functions_t *
376 get_screen_playlist(void)
377 {
378 static screen_functions_t functions;
380 memset(&functions, 0, sizeof(screen_functions_t));
381 functions.init = play_init;
382 functions.exit = play_exit;
383 functions.open = play_open;
384 functions.close = NULL;
385 functions.resize = play_resize;
386 functions.paint = play_paint;
387 functions.update = play_update;
388 functions.cmd = play_cmd;
389 functions.get_lw = play_lw;
390 functions.get_title = play_title;
392 return &functions;
393 }