1 /*****************************************************************************
2 * gtk_playlist.c : Interface for the playlist dialog
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
6 * Authors: Pierre Baillet <oct@zoy.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 #define MODULE_NAME gtk
24 #include "modules_inner.h"
26 /*****************************************************************************
28 *****************************************************************************/
37 #include <sys/types.h> /* for readdir and stat stuff */
47 #include "stream_control.h"
48 #include "input_ext-intf.h"
50 #include "interface.h"
51 #include "intf_playlist.h"
53 #include "intf_urldecode.h"
55 #include "gtk_callbacks.h"
56 #include "gtk_interface.h"
57 #include "gtk_support.h"
62 /* Playlist specific functions */
63 void rebuildCList(GtkCList * clist, playlist_t * playlist_p);
64 gint compareItems(gconstpointer a, gconstpointer b);
65 int hasValidExtension(gchar * filename);
66 GList * intf_readFiles(gchar * fsname );
67 int intf_AppendList( playlist_t * p_playlist, int i_pos, GList * list );
68 void GtkPlayListManage( gpointer p_data );
69 void on_generic_drop_data_received( intf_thread_t * p_intf,
70 GtkSelectionData *data, guint info, int position);
73 on_menubar_playlist_activate (GtkMenuItem *menuitem,
77 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(menuitem), "intf_window" );
78 playlist_t * p_playlist ;
81 if( !GTK_IS_WIDGET( p_intf->p_sys->p_playlist ) )
83 /* this shoud never happen */
84 intf_ErrMsgImm("intf_playlist is not a widget !");
85 p_intf->p_sys->p_playlist = create_intf_playlist();
86 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
90 vlc_mutex_lock( &p_main->p_playlist->change_lock );
91 if(p_main->p_playlist->i_size > 0 )
93 p_playlist = p_main->p_playlist;
94 list = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" )) ;
95 rebuildCList( list, p_playlist );
97 vlc_mutex_unlock( &p_main->p_playlist->change_lock );
99 gtk_widget_show( p_intf->p_sys->p_playlist );
100 gdk_window_raise( p_intf->p_sys->p_playlist->window );
105 on_toolbar_playlist_clicked (GtkButton *button,
108 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(button), "intf_window" );
110 if( !GTK_IS_WIDGET( p_intf->p_sys->p_playlist ) )
112 /* this shoud never happen */
113 intf_ErrMsgImm("intf_playlist is not a widget !");
115 p_intf->p_sys->p_playlist = create_intf_playlist();
116 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
119 if( GTK_WIDGET_VISIBLE(p_intf->p_sys->p_playlist) ) {
120 gtk_widget_hide( p_intf->p_sys->p_playlist);
125 gtk_widget_show( p_intf->p_sys->p_playlist );
126 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
127 gdk_window_raise( p_intf->p_sys->p_playlist->window );
128 rebuildCList( clist , p_main->p_playlist );
133 on_playlist_ok_clicked (GtkButton *button,
136 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(button), "intf_playlist" );
137 gtk_widget_hide( p_intf->p_sys->p_playlist );
140 void deleteGListItem(gpointer data, gpointer param)
142 int curRow = ( int )data;
143 intf_thread_t * p_intf = param;
145 intf_PlaylistDelete( p_main->p_playlist, curRow );
147 /* are we deleting the current played stream */
148 if( p_intf->p_sys->i_playing == curRow )
151 p_intf->p_input->b_eof = 1;
152 /* this has to set the slider to 0 */
155 p_intf->p_sys->i_playing-- ;
156 p_main->p_playlist->i_index-- ;
159 gint compareItems(gconstpointer a, gconstpointer b)
165 rebuildCList(GtkCList * clist, playlist_t * playlist_p)
175 gtk_clist_freeze( clist );
176 gtk_clist_clear( clist );
178 for( dummy=0; dummy < playlist_p->i_size; dummy++ )
180 text[0] = g_strdup( rindex( (char *)(playlist_p->p_item[playlist_p->i_size -1 - dummy].psz_name ), '/' ) + 1 );
181 text[1] = g_strdup( "no info");
183 gtk_clist_insert( clist, 0, text );
188 gtk_clist_set_background (
192 gtk_clist_thaw( clist );
196 on_invertselection_clicked (GtkMenuItem *item, gpointer user_data)
198 int * selected, sel_l;
200 playlist_t * playlist_p;
203 /* catch the thread back */
204 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
205 playlist_p = p_main->p_playlist;
207 /* lock the struct */
208 vlc_mutex_lock( &p_intf->change_lock );
209 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
211 /* have to copy the selection to an int *
212 I wasn't able to copy the g_list to another g_list
213 glib only does pointer copies, not real copies :( */
215 selected = malloc(sizeof(int)* g_list_length(clist->selection));
216 sel_l = g_list_length(clist->selection);
217 for(dummy=0; dummy < sel_l; dummy++)
219 selected[dummy] = (int)g_list_nth_data(clist->selection,dummy);
222 gtk_clist_freeze( clist );
223 gtk_clist_select_all( clist );
224 for(dummy=0; dummy < sel_l; dummy++)
226 gtk_clist_unselect_row( clist, selected[dummy],0);
227 gtk_clist_unselect_row( clist, selected[dummy],1);
230 gtk_clist_thaw( clist );
231 vlc_mutex_unlock( &p_intf->change_lock );
235 on_crop_activate (GtkMenuItem *menuitem,
238 /* Ok, this is a really small thing, but, hey, it works and
239 might be useful, who knows ? */
241 on_invertselection_clicked (menuitem, user_data);
242 on_delete_clicked(menuitem, user_data);
247 on_delete_clicked (GtkMenuItem *item,
250 /* user wants to delete a file in the queue */
253 playlist_t * playlist_p;
255 /* catch the thread back */
256 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
257 playlist_p = p_main->p_playlist;
259 /* lock the struct */
260 vlc_mutex_lock( &p_intf->change_lock );
261 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
263 /* I use UNDOCUMENTED features to retrieve the selection... */
264 selection = clist->selection;
266 if( g_list_length(selection)>0 )
268 /* reverse-sort so that we can delete from the furthest to the
269 closest item to delete...
271 selection = g_list_sort( selection, compareItems );
272 g_list_foreach( selection,
276 rebuildCList( clist, playlist_p );
279 vlc_mutex_unlock( &p_intf->change_lock );
283 on_intf_playlist_destroy_event (GtkWidget *widget,
288 gtk_widget_hide(widget);
293 on_intf_playlist_drag_data_received (GtkWidget *widget,
294 GdkDragContext *drag_context,
297 GtkSelectionData *data,
302 /* catch the interface back */
303 intf_thread_t * p_intf = GetIntf( GTK_WIDGET(widget), "intf_playlist" );
307 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
309 /* are we dropping somewhere into the clist items ? */
310 if( gtk_clist_get_selection_info( clist,
316 on_generic_drop_data_received( p_intf, data, info, row );
318 /* else, put that at the end of the playlist */
321 on_generic_drop_data_received( p_intf, data, info, PLAYLIST_END);
325 void on_generic_drop_data_received( intf_thread_t * p_intf,
326 GtkSelectionData *data, guint info, int position)
328 /* first we'll have to split against all the '\n' we have */
331 gchar * string = data->data ;
332 GList * files = NULL;
336 /* catch the playlist back */
337 playlist_t * p_playlist = p_main->p_playlist ;
340 /* if this has been URLencoded, decode it
342 * Is it a good thing to do it in place ?
345 if(info == DROP_ACCEPT_TEXT_URI_LIST)
347 urldecode_path( string );
350 /* this cuts string into single file drops */
351 /* this code was borrowed from xmms, thx guys :) */
354 temp = strchr(string, '\n');
357 if (*(temp - 1) == '\r')
363 /* do we have a protocol or something ? */
364 protocol = strstr( string, ":/" );
365 if( protocol != NULL )
367 protocol = calloc( protocol - string + 2 ,
369 protocol = strncpy( protocol, string, strstr( string, ":/") + 1 - string );
371 intf_WarnMsg(1,"Protocol dropped is %s",protocol);
372 string += strlen(protocol) ;
374 /* Allowed things are proto: or proto:// */
375 if(string[0]=='/' && string[1]=='/')
380 intf_WarnMsg(1,"Dropped %s",string);
385 protocol = strdup("");
388 /* if it uses the file protocol we can do something, else, sorry :(
389 * I think this is a good choice for now, as we don't have any
390 * ability to read http:// or ftp:// files
391 * what about adding dvd:// to the list of authorized proto ? */
393 if( strcmp(protocol,"file:")==0 )
395 files = g_list_concat( files, intf_readFiles( string ) );
398 /* free the malloc and go on... */
405 /* At this point, we have a nice big list maybe NULL */
408 /* lock the interface */
409 vlc_mutex_lock( &p_intf->change_lock );
410 intf_WarnMsg( 1, "List has %d elements",g_list_length( files ) );
411 intf_AppendList( p_playlist, position, files );
413 /* get the CList and rebuild it. */
415 lookup_widget( p_intf->p_sys->p_playlist,
416 "playlist_clist" ) );
417 rebuildCList( clist , p_playlist );
419 /* unlock the interface */
420 vlc_mutex_unlock( &p_intf->change_lock );
424 /* check a file (string) against supposed valid extension */
426 hasValidExtension( gchar * filename )
428 char * ext[6] = {"mpg","mpeg","vob","mp2","ts","ps"};
431 gchar * p_filename = strrchr( filename, '.' ) + sizeof( char );
432 for( dummy=0; dummy<i_ext;dummy++ )
434 if( strcmp( p_filename,ext[dummy] )==0 )
440 /* recursive function: descend into folders and build a list of valid filenames */
442 intf_readFiles( gchar * fsname )
445 GList * current = NULL;
447 /* get the attributes of this file */
448 stat(fsname, &statbuf);
450 /* is it a regular file ? */
451 if( S_ISREG( statbuf.st_mode ) )
453 if( hasValidExtension(fsname) )
455 intf_WarnMsg( 3, "%s is a valid file. Stacking on the playlist", fsname );
456 return g_list_append( NULL, g_strdup(fsname) );
463 /* is it a directory (should we check for symlinks ?) */
464 else if( S_ISDIR( statbuf.st_mode ) )
466 /* have to cd into this dir */
467 DIR * currentDir = opendir( fsname );
468 struct dirent * dirContent;
470 intf_WarnMsg( 3, "%s is a folder.", fsname );
472 if( currentDir == NULL )
474 /* something went bad, get out of here ! */
477 dirContent = readdir( currentDir );
479 /* while we still have entries in the directory */
480 while( dirContent != NULL )
482 /* if it is "." or "..", forget it */
483 if(strcmp(dirContent->d_name,".") != 0
484 && strcmp(dirContent->d_name,"..") != 0)
486 /* else build the new directory by adding
487 fsname "/" and the current entry name
490 char * newfs = malloc ( 2 +
492 strlen( dirContent->d_name ) * sizeof( char ) );
493 strcpy( newfs, fsname );
494 strcpy( newfs + strlen( fsname )+1, dirContent->d_name);
495 newfs[strlen( fsname )] = '/';
497 current = g_list_concat( current, intf_readFiles( newfs ) );
501 dirContent = readdir( currentDir );
508 /* add items in a playlist
509 when i_pos==-1 add to the end of the list...
511 int intf_AppendList( playlist_t * p_playlist, int i_pos, GList * list )
514 length = g_list_length( list );
515 for( dummy=0; dummy<length; dummy++ )
517 intf_PlaylistAdd( p_playlist,
518 /* ok; this is a really nasty trick to insert
519 the item where they are suppose to go but, hey
520 this works :P (btw, you are really nasty too) */
521 i_pos==PLAYLIST_END?PLAYLIST_END:( i_pos + dummy ),
522 g_list_nth_data(list, dummy));
527 on_playlist_clist_event (GtkWidget *widget,
531 intf_thread_t * p_intf = GetIntf( GTK_WIDGET( widget ), "intf_playlist" );
533 if( ( event->button ).type == GDK_2BUTTON_PRESS )
540 p_intf->p_sys->p_playlist,
541 "playlist_clist" ) );
543 if( gtk_clist_get_selection_info( clist,
550 /* clicked is in range. */
551 if( p_intf->p_input != NULL )
553 /* FIXME: temporary hack */
554 p_intf->p_input->b_eof = 1;
556 intf_PlaylistJumpto( p_main->p_playlist, row-1 );
563 /* statis timeouted function */
564 void GtkPlayListManage( gpointer p_data )
566 /* this thing really sucks for now :( */
568 /* TODO speak more with interface/intf_playlist.c */
570 intf_thread_t *p_intf = (void *)p_data;
571 playlist_t * p_playlist = p_main->p_playlist ;
573 vlc_mutex_lock( &p_intf->change_lock );
575 if( p_intf->p_sys->i_playing != p_playlist->i_index )
583 gtk_clist_set_background ( GTK_CLIST(
584 lookup_widget( p_intf->p_sys->p_playlist,
585 "playlist_clist" ) ),
589 if( p_intf->p_sys->i_playing != -1 )
592 color.green = 0xffff;
594 gtk_clist_set_background (
595 GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" ) ),
596 p_intf->p_sys->i_playing,
599 p_intf->p_sys->i_playing = p_playlist->i_index;
601 vlc_mutex_unlock( &p_intf->change_lock );