1 /*****************************************************************************
2 * playlist_interface.c : Interface for the playlist dialog
3 *****************************************************************************
4 * Copyright (C) 2000, 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_plst.h"
53 #include "intf_urldecode.h"
56 #include "gtk_callbacks.h"
57 #include "gtk_interface.h"
58 #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 static __inline__ intf_thread_t * GetIntf( GtkWidget *item, char * psz_parent )
75 return( gtk_object_get_data( GTK_OBJECT( lookup_widget(item, psz_parent) ),
82 on_menubar_playlist_activate (GtkMenuItem *menuitem,
85 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(menuitem), "intf_window" );
86 playlist_t * p_playlist ;
89 if( !GTK_IS_WIDGET( p_intf->p_sys->p_playlist ) )
91 p_intf->p_sys->p_playlist = create_intf_playlist();
92 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
97 vlc_mutex_lock( &p_main->p_playlist->change_lock );
98 if(p_main->p_playlist->i_size > 0 )
100 p_playlist = p_main->p_playlist;
102 list = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" )) ;
103 rebuildCList( list, p_playlist );
107 vlc_mutex_unlock( &p_main->p_playlist->change_lock );
109 gtk_widget_show( p_intf->p_sys->p_playlist );
110 gdk_window_raise( p_intf->p_sys->p_playlist->window );
115 on_toolbar_playlist_clicked (GtkButton *button,
118 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(button), "intf_window" );
120 if( !GTK_IS_WIDGET( p_intf->p_sys->p_playlist ) )
122 p_intf->p_sys->p_playlist = create_intf_playlist();
123 gtk_object_set_data( GTK_OBJECT( p_intf->p_sys->p_playlist ),
126 if( GTK_WIDGET_VISIBLE(p_intf->p_sys->p_playlist) ) {
127 gtk_widget_hide( p_intf->p_sys->p_playlist);
130 gtk_widget_show( p_intf->p_sys->p_playlist );
131 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
132 gdk_window_raise( p_intf->p_sys->p_playlist->window );
133 rebuildCList( clist , p_main->p_playlist );
138 on_playlist_ok_clicked (GtkButton *button,
141 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(button), "intf_playlist" );
142 gtk_widget_hide( p_intf->p_sys->p_playlist );
145 void deleteGListItem(gpointer data, gpointer param)
147 int curRow = ( int )data;
148 intf_thread_t * p_intf = param;
150 intf_PlstDelete( p_main->p_playlist, curRow );
152 /* are we deleting the current played stream */
153 if( p_intf->p_sys->i_playing == curRow )
156 p_intf->p_input->b_eof = 1;
157 /* this has to set the slider to 0 */
160 p_intf->p_sys->i_playing-- ;
161 p_main->p_playlist->i_index-- ;
164 gint compareItems(gconstpointer a, gconstpointer b)
170 rebuildCList(GtkCList * clist, playlist_t * playlist_p)
180 gtk_clist_freeze( clist );
181 gtk_clist_clear( clist );
183 for( dummy=0; dummy < playlist_p->i_size; dummy++ )
185 text[0] = g_strdup( rindex( (char *)(playlist_p->p_item[playlist_p->i_size -1 - dummy].psz_name ), '/' ) + 1 );
186 text[1] = g_strdup( "no info");
188 gtk_clist_insert( clist, 0, text );
193 gtk_clist_set_background (
197 gtk_clist_thaw( clist );
201 on_invertselection_clicked (GtkMenuItem *item, gpointer user_data)
203 int * selected, sel_l;
205 playlist_t * playlist_p;
208 /* catch the thread back */
209 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
210 playlist_p = p_main->p_playlist;
212 /* lock the struct */
213 vlc_mutex_lock( &p_intf->p_sys->change_lock );
214 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
216 /* have to copy the selection to an int *
217 I wasn't able to copy the g_list to another g_list
218 glib only does pointer copies, not real copies :( */
220 selected = malloc(sizeof(int)* g_list_length(clist->selection));
221 sel_l = g_list_length(clist->selection);
222 for(dummy=0; dummy < sel_l; dummy++)
224 selected[dummy] = (int)g_list_nth_data(clist->selection,dummy);
227 gtk_clist_freeze( clist );
228 gtk_clist_select_all( clist );
229 for(dummy=0; dummy < sel_l; dummy++)
231 gtk_clist_unselect_row( clist, selected[dummy],0);
232 gtk_clist_unselect_row( clist, selected[dummy],1);
235 gtk_clist_thaw( clist );
236 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
240 on_crop_activate (GtkMenuItem *menuitem,
243 /* Ok, this is a really small thing, but, hey, it works and
244 might be useful, who knows ? */
246 on_invertselection_clicked (menuitem, user_data);
247 on_delete_clicked(menuitem, user_data);
252 on_delete_clicked (GtkMenuItem *item,
255 /* user wants to delete a file in the queue */
258 playlist_t * playlist_p;
260 /* catch the thread back */
261 intf_thread_t *p_intf = GetIntf( GTK_WIDGET(item), "intf_playlist" );
262 playlist_p = p_main->p_playlist;
264 /* lock the struct */
265 vlc_mutex_lock( &p_intf->p_sys->change_lock );
266 clist = GTK_CLIST( lookup_widget(p_intf->p_sys->p_playlist,"playlist_clist") );
268 /* I use UNDOCUMENTED features to retrieve the selection... */
269 selection = clist->selection;
271 if( g_list_length(selection)>0 )
273 selection = g_list_sort( selection, compareItems );
274 g_list_foreach( selection,
278 rebuildCList( clist, playlist_p );
281 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
285 on_intf_playlist_destroy_event (GtkWidget *widget,
289 gtk_widget_hide(widget);
295 on_intf_playlist_drag_data_received (GtkWidget *widget,
296 GdkDragContext *drag_context,
299 GtkSelectionData *data,
304 /* catch the interface back */
305 intf_thread_t * p_intf = GetIntf( GTK_WIDGET(widget), "intf_playlist" );
309 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
311 if( gtk_clist_get_selection_info( clist,
317 on_generic_drop_data_received( p_intf, data, info, row);
319 on_generic_drop_data_received( p_intf, data, info, PLAYLIST_END);
323 void on_generic_drop_data_received( intf_thread_t * p_intf,
324 GtkSelectionData *data, guint info, int position)
326 /* first we'll have to split against all the '\n' we have */
329 gchar * string = data->data ;
330 GList * files = NULL;
334 /* catch the playlist back */
335 playlist_t * p_playlist = p_main->p_playlist ;
338 /* if this has been URLencoded, decode it
340 * Is it a good thing to do it in place ?
343 if(info == DROP_ACCEPT_TEXT_URI_LIST)
345 urldecode_path( string );
348 /* this cuts string into single file drops */
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(1,"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(1,"Dropped %s",string);
380 protocol = strdup("");
383 /* if it uses the file protocol we can do something, else, sorry :(
384 * I think this is a good choice for now, as we don't have any
385 * ability to read http:// or ftp:// files
386 * what about adding dvd:// to the list of authorized proto ? */
388 if( strcmp(protocol,"file:")==0 )
390 files = g_list_concat( files, intf_readFiles( string ) );
393 /* free the malloc and go on... */
400 /* At this point, we have a nice big list maybe NULL */
403 /* lock the interface */
404 vlc_mutex_lock( &p_intf->p_sys->change_lock );
405 intf_WarnMsg(1, "List has %d elements",g_list_length(files));
406 intf_AppendList( p_playlist, position, files );
407 /* get the CList and rebuild it. */
408 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
409 rebuildCList( clist , p_playlist );
411 /* unlock the interface */
412 vlc_mutex_unlock( &p_intf->p_sys->change_lock );
416 /* check a file (string) against supposed valid extension */
418 hasValidExtension(gchar * filename)
420 char * ext[6] = {"mpg","mpeg","vob","mp2","ts","ps"};
423 gchar * p_filename = strrchr( filename, '.') + sizeof( char );
424 for(dummy=0; dummy<i_ext;dummy++)
426 if(strcmp(p_filename,ext[dummy])==0)
432 /* recursive function: descend into folders and build a list of valid filenames */
434 intf_readFiles(gchar * fsname )
437 GList * current = NULL;
439 stat(fsname, &statbuf);
441 /* is it a regular file ? */
442 if( S_ISREG( statbuf.st_mode ) )
444 if( hasValidExtension(fsname) )
446 intf_WarnMsg( 3, "%s is a valid file. Stacking on the playlist", fsname );
447 return g_list_append( NULL, g_strdup(fsname) );
451 /* is it a directory (should we check for symlinks ?) */
452 else if( S_ISDIR( statbuf.st_mode ) )
454 /* have to cd into this dir */
455 DIR * currentDir = opendir( fsname );
456 struct dirent * dirContent;
458 intf_WarnMsg( 3, "%s is a folder.", fsname );
460 if( currentDir == NULL )
462 /* something went bad, get out of here ! */
465 dirContent = readdir( currentDir );
467 /* while we still have entries in the directory */
468 while( dirContent != NULL )
470 /* if it is "." or "..", forget it */
471 if(strcmp(dirContent->d_name,".") != 0
472 && strcmp(dirContent->d_name,"..") != 0)
474 /* else build the new directory by adding
475 fsname "/" and the current entry name
478 char * newfs = malloc ( 2 +
480 strlen( dirContent->d_name ) * sizeof( char ) );
481 strcpy( newfs, fsname );
482 strcpy( newfs + strlen( fsname )+1, dirContent->d_name);
483 newfs[strlen(fsname)] = '/';
485 current = g_list_concat( current, intf_readFiles( newfs ) );
489 dirContent = readdir( currentDir );
496 /* add items in a playlist
497 when i_pos==-1 add to the end of the list...
499 int intf_AppendList( playlist_t * p_playlist, int i_pos, GList * list )
502 length = g_list_length( list );
503 for(dummy=0; dummy<length; dummy++)
505 intf_PlstAdd( p_playlist,
506 i_pos==PLAYLIST_END?PLAYLIST_END:(i_pos + dummy),
507 g_list_nth_data(list, dummy));
512 on_playlist_clist_event (GtkWidget *widget,
516 intf_thread_t * p_intf = GetIntf( GTK_WIDGET(widget), "intf_playlist" );
518 if( (event->button).type == GDK_2BUTTON_PRESS )
523 clist = GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist,"playlist_clist" ));
524 if( gtk_clist_get_selection_info( clist,
531 /* clicked is in range. */
532 if( p_intf->p_input != NULL )
534 /* FIXME: temporary hack */
535 p_intf->p_input->b_eof = 1;
537 intf_PlstJumpto( p_main->p_playlist, row-1 );
544 /* statis timeouted function */
545 void GtkPlayListManage( gpointer p_data )
548 /* this thing really sucks for now :( */
550 /* TODO speak more with interface/intf_plst.c */
552 intf_thread_t *p_intf = (void *)p_data;
553 playlist_t * p_playlist = p_main->p_playlist ;
555 vlc_mutex_lock( &p_intf->p_sys->change_lock );
557 if(p_intf->p_sys->i_playing != p_playlist->i_index)
565 gtk_clist_set_background (
566 GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" ) ),
569 if( p_intf->p_sys->i_playing != -1 )
574 gtk_clist_set_background (
575 GTK_CLIST(lookup_widget( p_intf->p_sys->p_playlist, "playlist_clist" ) ),
576 p_intf->p_sys->i_playing,
579 p_intf->p_sys->i_playing = p_playlist->i_index;
581 vlc_mutex_unlock( &p_intf->p_sys->change_lock );