1 /*****************************************************************************
2 * gtk_playlist.c : Interface for the playlist dialog
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: gtk_playlist.c,v 1.11 2001/05/07 03:14:09 stef 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 #ifdef WIN32 /* WIN32 HACK */
174 text[0] = g_strdup( "" );
176 text[0] = g_strdup( rindex( (char *)(playlist_p->p_item[playlist_p->i_size -1 - dummy].psz_name ), '/' ) + 1 );
178 text[1] = g_strdup( "no info");
180 gtk_clist_insert( clist, 0, text );
185 gtk_clist_set_background (
189 gtk_clist_thaw( clist );
193 on_invertselection_clicked (GtkMenuItem *item, gpointer user_data)
195 int * selected, sel_l;
197 playlist_t * playlist_p;
200 /* catch the thread back */
201 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
202 playlist_p = p_main->p_playlist;
204 /* lock the struct */
205 vlc_mutex_lock( &p_intf->change_lock );
206 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
208 /* have to copy the selection to an int *
209 I wasn't able to copy the g_list to another g_list
210 glib only does pointer copies, not real copies :( */
212 selected = malloc(sizeof(int)* g_list_length(clist->selection));
213 sel_l = g_list_length(clist->selection);
214 for(dummy=0; dummy < sel_l; dummy++)
216 selected[dummy] = (int)g_list_nth_data(clist->selection,dummy);
219 gtk_clist_freeze( clist );
220 gtk_clist_select_all( clist );
221 for(dummy=0; dummy < sel_l; dummy++)
223 gtk_clist_unselect_row( clist, selected[dummy],0);
224 gtk_clist_unselect_row( clist, selected[dummy],1);
227 gtk_clist_thaw( clist );
228 vlc_mutex_unlock( &p_intf->change_lock );
232 on_crop_activate (GtkMenuItem *menuitem,
235 /* Ok, this is a really small thing, but, hey, it works and
236 might be useful, who knows ? */
238 on_invertselection_clicked (menuitem, user_data);
239 on_delete_clicked(menuitem, user_data);
244 on_delete_clicked (GtkMenuItem *item,
247 /* user wants to delete a file in the queue */
250 playlist_t * playlist_p;
252 /* catch the thread back */
253 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
254 playlist_p = p_main->p_playlist;
256 /* lock the struct */
257 vlc_mutex_lock( &p_intf->change_lock );
258 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
260 /* I use UNDOCUMENTED features to retrieve the selection... */
261 selection = clist->selection;
263 if( g_list_length(selection)>0 )
265 /* reverse-sort so that we can delete from the furthest to the
266 closest item to delete...
268 selection = g_list_sort( selection, compareItems );
269 g_list_foreach( selection,
272 /* rebuild the CList */
273 rebuildCList( clist, playlist_p );
276 vlc_mutex_unlock( &p_intf->change_lock );
280 on_intf_playlist_destroy_event (GtkWidget *widget,
285 gtk_widget_hide(widget);
290 on_intf_playlist_drag_data_received (GtkWidget *widget,
291 GdkDragContext *drag_context,
294 GtkSelectionData *data,
299 /* catch the interface back */
300 intf_thread_t * p_intf = GetIntf( GTK_WIDGET(widget), "intf_playlist" );
304 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
306 /* are we dropping somewhere into the clist items ? */
307 if( gtk_clist_get_selection_info( clist,
313 on_generic_drop_data_received( p_intf, data, info, row );
315 /* else, put that at the end of the playlist */
318 on_generic_drop_data_received( p_intf, data, info, PLAYLIST_END);
322 void on_generic_drop_data_received( intf_thread_t * p_intf,
323 GtkSelectionData *data, guint info, int position)
325 /* first we'll have to split against all the '\n' we have */
328 gchar * string = data->data ;
329 GList * files = NULL;
333 /* catch the playlist back */
334 playlist_t * p_playlist = p_main->p_playlist ;
337 /* if this has been URLencoded, decode it
339 * Is it a good thing to do it in place ?
342 if(info == DROP_ACCEPT_TEXT_URI_LIST)
344 urldecode_path( string );
347 /* this cuts string into single file drops */
348 /* this code was borrowed from xmms, thx guys :) */
351 temp = strchr(string, '\n');
354 if (*(temp - 1) == '\r')
360 /* do we have a protocol or something ? */
361 protocol = strstr( string, ":/" );
362 if( protocol != NULL )
364 protocol = calloc( protocol - string + 2 ,
366 protocol = strncpy( protocol, string, strstr( string, ":/") + 1 - string );
368 intf_WarnMsg(4,"Protocol dropped is %s",protocol);
369 string += strlen(protocol) ;
371 /* Allowed things are proto: or proto:// */
372 if(string[0]=='/' && string[1]=='/')
377 intf_WarnMsg(4,"Dropped %s",string);
382 protocol = strdup("");
385 /* if it uses the file protocol we can do something, else, sorry :(
386 * I think this is a good choice for now, as we don't have any
387 * ability to read http:// or ftp:// files
388 * what about adding dvd:// to the list of authorized proto ? */
390 if( strcmp(protocol,"file:")==0 )
392 files = g_list_concat( files, intf_readFiles( string ) );
395 /* free the malloc and go on... */
402 /* At this point, we have a nice big list maybe NULL */
405 /* lock the interface */
406 vlc_mutex_lock( &p_intf->change_lock );
407 intf_WarnMsg( 4, "List has %d elements",g_list_length( files ) );
408 intf_AppendList( p_playlist, position, files );
410 /* get the CList and rebuild it. */
412 lookup_widget( p_intf->p_sys->p_playlist,
413 "playlist_clist" ) );
414 rebuildCList( clist , p_playlist );
416 /* unlock the interface */
417 vlc_mutex_unlock( &p_intf->change_lock );
421 /* check a file (string) against supposed valid extension */
423 hasValidExtension( gchar * filename )
425 char * ext[6] = {"mpg","mpeg","vob","mp2","ts","ps"};
428 gchar * p_filename = strrchr( filename, '.' ) + sizeof( char );
429 for( dummy=0; dummy<i_ext;dummy++ )
431 if( strcmp( p_filename,ext[dummy] )==0 )
437 /* recursive function: descend into folders and build a list of valid filenames */
439 intf_readFiles( gchar * fsname )
442 GList * current = NULL;
444 /* get the attributes of this file */
445 stat(fsname, &statbuf);
447 /* is it a regular file ? */
448 if( S_ISREG( statbuf.st_mode ) )
450 if( hasValidExtension(fsname) )
452 intf_WarnMsg( 2, "%s is a valid file. Stacking on the playlist", fsname );
453 return g_list_append( NULL, g_strdup(fsname) );
460 /* is it a directory (should we check for symlinks ?) */
461 else if( S_ISDIR( statbuf.st_mode ) )
463 /* have to cd into this dir */
464 DIR * currentDir = opendir( fsname );
465 struct dirent * dirContent;
467 intf_WarnMsg( 2, "%s is a folder.", fsname );
469 if( currentDir == NULL )
471 /* something went bad, get out of here ! */
474 dirContent = readdir( currentDir );
476 /* while we still have entries in the directory */
477 while( dirContent != NULL )
479 /* if it is "." or "..", forget it */
480 if(strcmp(dirContent->d_name,".") != 0
481 && strcmp(dirContent->d_name,"..") != 0)
483 /* else build the new directory by adding
484 fsname "/" and the current entry name
487 char * newfs = malloc ( 2 +
489 strlen( dirContent->d_name ) * sizeof( char ) );
490 strcpy( newfs, fsname );
491 strcpy( newfs + strlen( fsname )+1, dirContent->d_name);
492 newfs[strlen( fsname )] = '/';
494 current = g_list_concat( current, intf_readFiles( newfs ) );
498 dirContent = readdir( currentDir );
505 /* add items in a playlist
506 when i_pos==-1 add to the end of the list...
508 int intf_AppendList( playlist_t * p_playlist, int i_pos, GList * list )
511 length = g_list_length( list );
512 for( dummy=0; dummy<length; dummy++ )
514 intf_PlaylistAdd( p_playlist,
515 /* ok; this is a really nasty trick to insert
516 the item where they are suppose to go but, hey
517 this works :P (btw, you are really nasty too) */
518 i_pos==PLAYLIST_END?PLAYLIST_END:( i_pos + dummy ),
519 g_list_nth_data(list, dummy));
524 on_playlist_clist_event (GtkWidget *widget,
528 intf_thread_t * p_intf = GetIntf( GTK_WIDGET( widget ), "intf_playlist" );
530 if( ( event->button ).type == GDK_2BUTTON_PRESS )
537 p_intf->p_sys->p_playlist,
538 "playlist_clist" ) );
540 if( gtk_clist_get_selection_info( clist,
547 /* clicked is in range. */
548 if( p_intf->p_input != NULL )
550 /* FIXME: temporary hack */
551 p_intf->p_input->b_eof = 1;
553 intf_PlaylistJumpto( p_main->p_playlist, row-1 );
560 /* statis timeouted function */
561 void GtkPlayListManage( gpointer p_data )
563 /* this thing really sucks for now :( */
565 /* TODO speak more with interface/intf_playlist.c */
567 intf_thread_t *p_intf = (void *)p_data;
568 playlist_t * p_playlist = p_main->p_playlist ;
570 vlc_mutex_lock( &p_intf->change_lock );
572 if( p_intf->p_sys->i_playing != p_playlist->i_index )
580 gtk_clist_set_background ( GTK_CLIST(
581 lookup_widget( p_intf->p_sys->p_playlist,
582 "playlist_clist" ) ),
586 if( p_intf->p_sys->i_playing != -1 )
589 color.green = 0xffff;
591 gtk_clist_set_background (
592 GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" ) ),
593 p_intf->p_sys->i_playing,
596 p_intf->p_sys->i_playing = p_playlist->i_index;
598 vlc_mutex_unlock( &p_intf->change_lock );