Code

Removed the trim() function, using glib's g_strstrip() instead
[ncmpc.git] / src / screen_play.c
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;
92   
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;
115 static int
116 handle_save_playlist(screen_t *screen, mpdclient_t *c, char *name)
118   gchar *filename;
119   gint error;
120   GCompletion *gcmp;
121   GList *list = NULL;
123   void pre_completion_cb(GCompletion *gcmp, gchar *line)
124     {
125       if( list == NULL )
126         {
127           /* create completion list */
128           list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_PLAYLIST);
129           g_completion_add_items(gcmp, list);
130         }
131     }
133   void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
134     {
135       if( g_list_length(items)>=1 )
136         {
137           screen_display_completion_list(screen, items);
138           lw->clear = 1;
139           lw->repaint = 1;
140         }
141     }
143   if( name==NULL )
144     {
145       /* initialize completion support */
146       gcmp = g_completion_new(NULL);
147       g_completion_set_compare(gcmp, strncmp);
148       wrln_pre_completion_callback = pre_completion_cb;
149       wrln_post_completion_callback = post_completion_cb;
151       /* query the user for a filename */
152       filename = screen_readln(screen->status_window.w,
153                                _("Save playlist as: "),
154                                NULL,
155                                NULL,
156                                gcmp);                                  
157       filename=g_strstrip(filename);
159       /* destroy completion support */
160       wrln_pre_completion_callback = NULL;
161       wrln_post_completion_callback = NULL;
162       g_completion_free(gcmp);
163       list = string_list_free(list);
164     }
165   else
166     {
167       filename=g_strdup(name);
168     }
169   if( filename==NULL || filename[0]=='\0' )
170     return -1;
171   /* send save command to mpd */
172   D("Saving playlist as \'%s \'...\n", filename);
173   if( (error=mpdclient_cmd_save_playlist(c, filename)) )
174     {
175       gint code = GET_ACK_ERROR_CODE(error);
177       if( code == MPD_ACK_ERROR_EXIST )
178         {
179           char buf[256];
180           int key;
182           snprintf(buf, 256, _("Replace %s [%s/%s] ? "), filename, YES, NO);
183           key = tolower(screen_getch(screen->status_window.w, buf));
184           if( key == YES[0] )
185             {
186               if( mpdclient_cmd_delete_playlist(c, filename) )
187                 {
188                   g_free(filename);
189                   return -1;
190                 }
191               error = handle_save_playlist(screen, c, filename);
192               g_free(filename);
193               return error;
194             }     
195           screen_status_printf(_("Aborted!"));
196         }
197       g_free(filename);
198       return -1;
199     }
200   /* success */
201   screen_status_printf(_("Saved %s"), filename);
202   g_free(filename);
203   return 0;
206 static int
207 handle_add_to_playlist(screen_t *screen, mpdclient_t *c)
209   gchar *path;
210   GCompletion *gcmp;
211   GList *list = NULL;
212   GList *dir_list = NULL;
214   void add_dir(gchar *dir)
215     {
216       g_completion_remove_items(gcmp, list);
217       list = string_list_remove(list, dir);
218       list = gcmp_list_from_path(c, dir, list, GCMP_TYPE_RFILE);
219       g_completion_add_items(gcmp, list);
220       dir_list = g_list_append(dir_list, g_strdup(dir));
221     }
223   void pre_completion_cb(GCompletion *gcmp, gchar *line)        
224     {
225       D("pre_completion()...\n");
226       if( list == NULL )
227         {
228           /* create initial list */
229           list = gcmp_list_from_path(c, "", NULL, GCMP_TYPE_RFILE);
230           g_completion_add_items(gcmp, list);
231         }
232       else if( line && line[0] && line[strlen(line)-1]=='/' &&
233                string_list_find(dir_list, line) == NULL )
234         {         
235           /* add directory content to list */
236           add_dir(line);
237         }
238     }
240   void post_completion_cb(GCompletion *gcmp, gchar *line, GList *items)
241     {
242       D("post_completion()...\n");
243       if( g_list_length(items)>=1 )
244         {
245           screen_display_completion_list(screen, items);
246           lw->clear = 1;
247           lw->repaint = 1;
248         }
250       if( line && line[0] && line[strlen(line)-1]=='/' &&
251           string_list_find(dir_list, line) == NULL )
252         {         
253           /* add directory content to list */
254           add_dir(line);
255         }
256     }
257     
258   /* initialize completion support */
259   gcmp = g_completion_new(NULL);
260   g_completion_set_compare(gcmp, strncmp);
261   wrln_pre_completion_callback = pre_completion_cb;
262   wrln_post_completion_callback = post_completion_cb;
263   /* get path */
264   path = screen_readln(screen->status_window.w, 
265                        _("Add: "),
266                        NULL, 
267                        NULL, 
268                        gcmp);
270   /* destroy completion data */
271   wrln_pre_completion_callback = NULL;
272   wrln_post_completion_callback = NULL;
273   g_completion_free(gcmp);
274   string_list_free(list);
275   string_list_free(dir_list);
277   /* add the path to the playlist */
278   if( path && path[0] )
279     mpdclient_cmd_add_path(c, path);
281   return 0;
284 static void
285 play_init(WINDOW *w, int cols, int rows)
287   lw = list_window_init(w, cols, rows);
290 static void
291 play_open(screen_t *screen, mpdclient_t *c)
293   static gboolean install_cb = TRUE;
295   if( install_cb )
296     {
297       mpdclient_install_playlist_callback(c, playlist_changed_callback);
298       install_cb = FALSE;
299     }
302 static void
303 play_resize(int cols, int rows)
305   lw->cols = cols;
306   lw->rows = rows;
310 static void
311 play_exit(void)
313   list_window_free(lw);
316 static char *
317 play_title(char *str, size_t size)
319   if( strcmp(options.host, "localhost") == 0 )
320     return _("Playlist");
321   
322   snprintf(str, size, _("Playlist on %s"), options.host);
324   return str;
327 static void
328 play_paint(screen_t *screen, mpdclient_t *c)
329
330   lw->clear = 1;
332   list_window_paint(lw, list_callback, (void *) c);
333   wnoutrefresh(lw->w);
336 static void
337 play_update(screen_t *screen, mpdclient_t *c)
339   if( options.auto_center )
340     {
341       static int prev_song_id = 0;
342       
343       if( c->song && prev_song_id != c->song->id )      
344         {
345           center_playing_item(screen, c);
346           prev_song_id = c->song->id;
347         }
348     }
350   if( c->playlist.updated )
351     {
352       if( lw->selected >= c->playlist.length )
353         lw->selected = c->playlist.length-1;
354       if( lw->start    >= c->playlist.length )
355         list_window_reset(lw);
357       play_paint(screen, c);
358       c->playlist.updated = FALSE;
359     }
360   else if( lw->repaint || 1)
361     {
362       list_window_paint(lw, list_callback, (void *) c);
363       wnoutrefresh(lw->w);
364       lw->repaint = 0;
365     }
368 static int
369 play_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
371   switch(cmd)
372     {
373     case CMD_PLAY:
374       mpdclient_cmd_play(c, lw->selected);
375       return 1;
376     case CMD_DELETE:
377       mpdclient_cmd_delete(c, lw->selected);
378       return 1;
379     case CMD_SAVE_PLAYLIST:
380       handle_save_playlist(screen, c, NULL);
381       return 1;
382     case CMD_ADD:
383       handle_add_to_playlist(screen, c);
384       return 1;
385     case CMD_SCREEN_UPDATE:
386       screen->painted = 0;
387       lw->clear = 1;
388       lw->repaint = 1;
389       center_playing_item(screen, c);
390       return 1;
391     case CMD_LIST_MOVE_UP:
392       mpdclient_cmd_move(c, lw->selected, lw->selected-1);
393       return 1;
394     case CMD_LIST_MOVE_DOWN:
395       mpdclient_cmd_move(c, lw->selected, lw->selected+1);
396       return 1;
397     case CMD_LIST_FIND:
398     case CMD_LIST_RFIND:
399     case CMD_LIST_FIND_NEXT:
400     case CMD_LIST_RFIND_NEXT:
401       return screen_find(screen, c, 
402                          lw, c->playlist.length,
403                          cmd, list_callback);
404     default:
405       break;
406     }
407   return list_window_cmd(lw, c->playlist.length, cmd) ;
412 static list_window_t *
413 play_lw(void)
415   return lw;
419 screen_functions_t *
420 get_screen_playlist(void)
422   static screen_functions_t functions;
424   memset(&functions, 0, sizeof(screen_functions_t));
425   functions.init   = play_init;
426   functions.exit   = play_exit;
427   functions.open   = play_open;
428   functions.close  = NULL;
429   functions.resize = play_resize;
430   functions.paint  = play_paint;
431   functions.update = play_update;
432   functions.cmd    = play_cmd;
433   functions.get_lw = play_lw;
434   functions.get_title = play_title;
436   return &functions;