1 /*****************************************************************************
2 * gtk_playlist.c : Interface for the playlist dialog
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: gtk_playlist.c,v 1.9 2001/04/08 13:09:32 octplane Exp $
7 * Authors: Pierre Baillet <oct@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 #define MODULE_NAME gtk
25 #include "modules_inner.h"
27 /*****************************************************************************
29 *****************************************************************************/
38 #include <sys/types.h> /* for readdir and stat stuff */
48 #include "stream_control.h"
49 #include "input_ext-intf.h"
51 #include "interface.h"
52 #include "intf_playlist.h"
54 #include "intf_urldecode.h"
56 #include "gtk_callbacks.h"
57 #include "gtk_interface.h"
58 #include "gtk_support.h"
59 #include "gtk_playlist.h"
66 on_menubar_playlist_activate (GtkMenuItem *menuitem,
70 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(menuitem), "intf_window" );
71 playlist_t * p_playlist ;
74 if( !GTK_IS_WIDGET( p_intf->p_sys->p_playlist ) )
76 /* this shoud never happen */
77 intf_ErrMsgImm("intf_playlist is not a widget !");
78 p_intf->p_sys->p_playlist = create_intf_playlist();
79 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
83 vlc_mutex_lock( &p_main->p_playlist->change_lock );
84 if(p_main->p_playlist->i_size > 0 )
86 p_playlist = p_main->p_playlist;
87 list = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" )) ;
88 rebuildCList( list, p_playlist );
90 vlc_mutex_unlock( &p_main->p_playlist->change_lock );
92 gtk_widget_show( p_intf->p_sys->p_playlist );
93 gdk_window_raise( p_intf->p_sys->p_playlist->window );
98 on_toolbar_playlist_clicked (GtkButton *button,
101 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(button), "intf_window" );
103 if( !GTK_IS_WIDGET( p_intf->p_sys->p_playlist ) )
105 /* this should never happen */
106 intf_ErrMsgImm("intf_playlist is not a widget !");
108 p_intf->p_sys->p_playlist = create_intf_playlist();
109 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
112 if( GTK_WIDGET_VISIBLE(p_intf->p_sys->p_playlist) ) {
113 gtk_widget_hide( p_intf->p_sys->p_playlist);
118 gtk_widget_show( p_intf->p_sys->p_playlist );
119 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
120 gdk_window_raise( p_intf->p_sys->p_playlist->window );
121 rebuildCList( clist , p_main->p_playlist );
126 on_playlist_ok_clicked (GtkButton *button,
129 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(button), "intf_playlist" );
130 gtk_widget_hide( p_intf->p_sys->p_playlist );
133 void deleteGListItem(gpointer data, gpointer param)
135 int curRow = ( int )data;
136 intf_thread_t * p_intf = param;
138 intf_PlaylistDelete( p_main->p_playlist, curRow );
140 /* are we deleting the current played stream */
141 if( p_intf->p_sys->i_playing == curRow )
144 p_intf->p_input->b_eof = 1;
145 /* this has to set the slider to 0 */
148 p_intf->p_sys->i_playing-- ;
149 p_main->p_playlist->i_index-- ;
152 gint compareItems(gconstpointer a, gconstpointer b)
158 rebuildCList(GtkCList * clist, playlist_t * playlist_p)
168 gtk_clist_freeze( clist );
169 gtk_clist_clear( clist );
171 for( dummy=0; dummy < playlist_p->i_size; dummy++ )
173 text[0] = g_strdup( rindex( (char *)(playlist_p->p_item[playlist_p->i_size -1 - dummy].psz_name ), '/' ) + 1 );
174 text[1] = g_strdup( "no info");
176 gtk_clist_insert( clist, 0, text );
181 gtk_clist_set_background (
185 gtk_clist_thaw( clist );
189 on_invertselection_clicked (GtkMenuItem *item, gpointer user_data)
191 int * selected, sel_l;
193 playlist_t * playlist_p;
196 /* catch the thread back */
197 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
198 playlist_p = p_main->p_playlist;
200 /* lock the struct */
201 vlc_mutex_lock( &p_intf->change_lock );
202 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
204 /* have to copy the selection to an int *
205 I wasn't able to copy the g_list to another g_list
206 glib only does pointer copies, not real copies :( */
208 selected = malloc(sizeof(int)* g_list_length(clist->selection));
209 sel_l = g_list_length(clist->selection);
210 for(dummy=0; dummy < sel_l; dummy++)
212 selected[dummy] = (int)g_list_nth_data(clist->selection,dummy);
215 gtk_clist_freeze( clist );
216 gtk_clist_select_all( clist );
217 for(dummy=0; dummy < sel_l; dummy++)
219 gtk_clist_unselect_row( clist, selected[dummy],0);
220 gtk_clist_unselect_row( clist, selected[dummy],1);
223 gtk_clist_thaw( clist );
224 vlc_mutex_unlock( &p_intf->change_lock );
228 on_crop_activate (GtkMenuItem *menuitem,
231 /* Ok, this is a really small thing, but, hey, it works and
232 might be useful, who knows ? */
234 on_invertselection_clicked (menuitem, user_data);
235 on_delete_clicked(menuitem, user_data);
240 on_delete_clicked (GtkMenuItem *item,
243 /* user wants to delete a file in the queue */
246 playlist_t * playlist_p;
248 /* catch the thread back */
249 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
250 playlist_p = p_main->p_playlist;
252 /* lock the struct */
253 vlc_mutex_lock( &p_intf->change_lock );
254 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
256 /* I use UNDOCUMENTED features to retrieve the selection... */
257 selection = clist->selection;
259 if( g_list_length(selection)>0 )
261 /* reverse-sort so that we can delete from the furthest to the
262 closest item to delete...
264 selection = g_list_sort( selection, compareItems );
265 g_list_foreach( selection,
268 /* rebuild the CList */
269 rebuildCList( clist, playlist_p );
272 vlc_mutex_unlock( &p_intf->change_lock );
276 on_intf_playlist_destroy_event (GtkWidget *widget,
281 gtk_widget_hide(widget);
286 on_intf_playlist_drag_data_received (GtkWidget *widget,
287 GdkDragContext *drag_context,
290 GtkSelectionData *data,
295 /* catch the interface back */
296 intf_thread_t * p_intf = GetIntf( GTK_WIDGET(widget), "intf_playlist" );
300 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
302 /* are we dropping somewhere into the clist items ? */
303 if( gtk_clist_get_selection_info( clist,
309 on_generic_drop_data_received( p_intf, data, info, row );
311 /* else, put that at the end of the playlist */
314 on_generic_drop_data_received( p_intf, data, info, PLAYLIST_END);
318 void on_generic_drop_data_received( intf_thread_t * p_intf,
319 GtkSelectionData *data, guint info, int position)
321 /* first we'll have to split against all the '\n' we have */
324 gchar * string = data->data ;
325 GList * files = NULL;
329 /* catch the playlist back */
330 playlist_t * p_playlist = p_main->p_playlist ;
333 /* if this has been URLencoded, decode it
335 * Is it a good thing to do it in place ?
338 if(info == DROP_ACCEPT_TEXT_URI_LIST)
340 urldecode_path( string );
343 /* this cuts string into single file drops */
344 /* this code was borrowed from xmms, thx guys :) */
347 temp = strchr(string, '\n');
350 if (*(temp - 1) == '\r')
356 /* do we have a protocol or something ? */
357 protocol = strstr( string, ":/" );
358 if( protocol != NULL )
360 protocol = calloc( protocol - string + 2 ,
362 protocol = strncpy( protocol, string, strstr( string, ":/") + 1 - string );
364 intf_WarnMsg(1,"Protocol dropped is %s",protocol);
365 string += strlen(protocol) ;
367 /* Allowed things are proto: or proto:// */
368 if(string[0]=='/' && string[1]=='/')
373 intf_WarnMsg(1,"Dropped %s",string);
378 protocol = strdup("");
381 /* if it uses the file protocol we can do something, else, sorry :(
382 * I think this is a good choice for now, as we don't have any
383 * ability to read http:// or ftp:// files
384 * what about adding dvd:// to the list of authorized proto ? */
386 if( strcmp(protocol,"file:")==0 )
388 files = g_list_concat( files, intf_readFiles( string ) );
391 /* free the malloc and go on... */
398 /* At this point, we have a nice big list maybe NULL */
401 /* lock the interface */
402 vlc_mutex_lock( &p_intf->change_lock );
403 intf_WarnMsg( 1, "List has %d elements",g_list_length( files ) );
404 intf_AppendList( p_playlist, position, files );
406 /* get the CList and rebuild it. */
408 lookup_widget( p_intf->p_sys->p_playlist,
409 "playlist_clist" ) );
410 rebuildCList( clist , p_playlist );
412 /* unlock the interface */
413 vlc_mutex_unlock( &p_intf->change_lock );
417 /* check a file (string) against supposed valid extension */
419 hasValidExtension( gchar * filename )
421 char * ext[6] = {"mpg","mpeg","vob","mp2","ts","ps"};
424 gchar * p_filename = strrchr( filename, '.' ) + sizeof( char );
425 for( dummy=0; dummy<i_ext;dummy++ )
427 if( strcmp( p_filename,ext[dummy] )==0 )
433 /* recursive function: descend into folders and build a list of valid filenames */
435 intf_readFiles( gchar * fsname )
438 GList * current = NULL;
440 /* get the attributes of this file */
441 stat(fsname, &statbuf);
443 /* is it a regular file ? */
444 if( S_ISREG( statbuf.st_mode ) )
446 if( hasValidExtension(fsname) )
448 intf_WarnMsg( 3, "%s is a valid file. Stacking on the playlist", fsname );
449 return g_list_append( NULL, g_strdup(fsname) );
456 /* is it a directory (should we check for symlinks ?) */
457 else if( S_ISDIR( statbuf.st_mode ) )
459 /* have to cd into this dir */
460 DIR * currentDir = opendir( fsname );
461 struct dirent * dirContent;
463 intf_WarnMsg( 3, "%s is a folder.", fsname );
465 if( currentDir == NULL )
467 /* something went bad, get out of here ! */
470 dirContent = readdir( currentDir );
472 /* while we still have entries in the directory */
473 while( dirContent != NULL )
475 /* if it is "." or "..", forget it */
476 if(strcmp(dirContent->d_name,".") != 0
477 && strcmp(dirContent->d_name,"..") != 0)
479 /* else build the new directory by adding
480 fsname "/" and the current entry name
483 char * newfs = malloc ( 2 +
485 strlen( dirContent->d_name ) * sizeof( char ) );
486 strcpy( newfs, fsname );
487 strcpy( newfs + strlen( fsname )+1, dirContent->d_name);
488 newfs[strlen( fsname )] = '/';
490 current = g_list_concat( current, intf_readFiles( newfs ) );
494 dirContent = readdir( currentDir );
501 /* add items in a playlist
502 when i_pos==-1 add to the end of the list...
504 int intf_AppendList( playlist_t * p_playlist, int i_pos, GList * list )
507 length = g_list_length( list );
508 for( dummy=0; dummy<length; dummy++ )
510 intf_PlaylistAdd( p_playlist,
511 /* ok; this is a really nasty trick to insert
512 the item where they are suppose to go but, hey
513 this works :P (btw, you are really nasty too) */
514 i_pos==PLAYLIST_END?PLAYLIST_END:( i_pos + dummy ),
515 g_list_nth_data(list, dummy));
520 on_playlist_clist_event (GtkWidget *widget,
524 intf_thread_t * p_intf = GetIntf( GTK_WIDGET( widget ), "intf_playlist" );
526 if( ( event->button ).type == GDK_2BUTTON_PRESS )
533 p_intf->p_sys->p_playlist,
534 "playlist_clist" ) );
536 if( gtk_clist_get_selection_info( clist,
543 /* clicked is in range. */
544 if( p_intf->p_input != NULL )
546 /* FIXME: temporary hack */
547 p_intf->p_input->b_eof = 1;
549 intf_PlaylistJumpto( p_main->p_playlist, row-1 );
556 /* statis timeouted function */
557 void GtkPlayListManage( gpointer p_data )
559 /* this thing really sucks for now :( */
561 /* TODO speak more with interface/intf_playlist.c */
563 intf_thread_t *p_intf = (void *)p_data;
564 playlist_t * p_playlist = p_main->p_playlist ;
566 vlc_mutex_lock( &p_intf->change_lock );
568 if( p_intf->p_sys->i_playing != p_playlist->i_index )
576 gtk_clist_set_background ( GTK_CLIST(
577 lookup_widget( p_intf->p_sys->p_playlist,
578 "playlist_clist" ) ),
582 if( p_intf->p_sys->i_playing != -1 )
585 color.green = 0xffff;
587 gtk_clist_set_background (
588 GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" ) ),
589 p_intf->p_sys->i_playing,
592 p_intf->p_sys->i_playing = p_playlist->i_index;
594 vlc_mutex_unlock( &p_intf->change_lock );