/* $Id: playlists.c,v 1.1.1.1 2000/01/30 16:39:32 benediktroth Exp $ */ #include #include #include #include #include #include #include #include #include #include "yamt.h" #include "playlists.h" GtkWidget *playlists_box_new( ) { GtkWidget *box; GtkWidget *playlists_directory_frame; GtkWidget *playlists_directory_table; GtkWidget *playlists_directory_label; GtkWidget *playlists_directory_buttons_select; GtkWidget *playlists_task_frame; GtkWidget *playlists_task_table; GtkWidget *playlists_options_frame; GtkWidget *playlists_options_table; GtkWidget *playlists_buttons_box; GtkWidget *playlists_buttons_start; GtkWidget *playlists_buttons_quit; /* The box it which everything is put */ box = gtk_vbox_new( FALSE, 0); /* Frame for directory */ playlists_directory_frame = gtk_frame_new( NULL ); gtk_box_pack_start( GTK_BOX (box), playlists_directory_frame, TRUE, FALSE, 0 ); gtk_widget_show( playlists_directory_frame ); /* Table for directory */ playlists_directory_table = gtk_table_new( 1, 1, FALSE); gtk_container_set_border_width( GTK_CONTAINER (playlists_directory_table), 5); gtk_container_add( GTK_CONTAINER (playlists_directory_frame), playlists_directory_table ); gtk_widget_show( playlists_directory_table ); /* Directory */ playlists_directory_label=gtk_label_new( _("Directory: ") ); gtk_table_attach_defaults( GTK_TABLE (playlists_directory_table), playlists_directory_label, 0, 1, 0, 1); gtk_widget_show( playlists_directory_label ); playlists_directory=gtk_label_new( _("[no dir selected]") ); gtk_table_attach_defaults( GTK_TABLE (playlists_directory_table), playlists_directory, 1, 2, 0, 1); gtk_widget_show( playlists_directory ); /* The select button */ playlists_directory_buttons_select = gnome_stock_or_ordinary_button( GNOME_STOCK_PIXMAP_OPEN ); gtk_signal_connect( GTK_OBJECT (playlists_directory_buttons_select), "clicked", GTK_SIGNAL_FUNC (playlists_directory_select), NULL ); gtk_table_attach_defaults( GTK_TABLE (playlists_directory_table), playlists_directory_buttons_select, 2, 3, 0, 1); gtk_widget_show( playlists_directory_buttons_select ); /* Task Frame */ /* ------------------------------------------------------------------------ */ playlists_task_frame = gtk_frame_new( _("Task") ); gtk_box_pack_start( GTK_BOX (box), playlists_task_frame, TRUE, FALSE, 0 ); gtk_widget_show( playlists_task_frame ); /* Table */ playlists_task_table = gtk_table_new( 3, 1, FALSE ); gtk_container_add( GTK_CONTAINER (playlists_task_frame), playlists_task_table ); gtk_widget_show( playlists_task_table ); /* The Group of Radio Buttons */ /* one */ playlists_buttons_one = gtk_radio_button_new_with_label( NULL, _("Just make a playlist with the files in this directory") ); gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON (playlists_buttons_one), TRUE ); gtk_table_attach_defaults( GTK_TABLE (playlists_task_table), playlists_buttons_one, 0, 1, 0, 1); gtk_widget_show( playlists_buttons_one ); /* all */ playlists_buttons_all = gtk_radio_button_new_with_label( gtk_radio_button_group( GTK_RADIO_BUTTON (playlists_buttons_one)), _("Make one playlist with all mp3s in all subdirs") ); gtk_table_attach_defaults( GTK_TABLE (playlists_task_table), playlists_buttons_all, 0, 1, 1, 2); gtk_widget_show( playlists_buttons_all ); /* sub */ playlists_buttons_sub = gtk_radio_button_new_with_label( gtk_radio_button_group( GTK_RADIO_BUTTON (playlists_buttons_all)), _("Create a Playlist in every subdirectory") ); gtk_table_attach_defaults( GTK_TABLE (playlists_task_table), playlists_buttons_sub, 0, 1, 2, 3); gtk_widget_show( playlists_buttons_sub ); /* remove */ playlists_buttons_remove = gtk_radio_button_new_with_label( gtk_radio_button_group( GTK_RADIO_BUTTON (playlists_buttons_all)), _("Remove Playlists (with the name of the directory they are in)") ); gtk_table_attach_defaults( GTK_TABLE (playlists_task_table), playlists_buttons_remove, 0, 1, 3, 4); gtk_widget_show( playlists_buttons_remove ); /* ultra */ /* playlists_buttons_ultra = gtk_radio_button_new_with_label( gtk_radio_button_group( GTK_RADIO_BUTTON (playlists_buttons_all)), _("The supa doopa ultra feature! */ /* Usefull for the Artist/Album dir structure. */ /* It creates a Playlist with all your mp3s, */ /* one for each artist and one for each album. Enjoy!") ); */ /* gtk_table_attach_defaults( GTK_TABLE (playlists_task_table), playlists_buttons_ultra, 1, 2, 0, 2); */ /* gtk_widget_show( playlists_buttons_ultra ); */ /* End Task Frame */ /* ------------------------------------------------------------------------ */ /* Options Frame */ /* ------------------------------------------------------------------------ */ playlists_options_frame = gtk_frame_new( _("Options") ); gtk_box_pack_start( GTK_BOX (box), playlists_options_frame, TRUE, FALSE, 0 ); gtk_widget_show( playlists_options_frame ); /* Table */ playlists_options_table = gtk_table_new( 4, 2, FALSE ); gtk_container_add( GTK_CONTAINER (playlists_options_frame), playlists_options_table ); gtk_widget_show( playlists_options_table ); /* relative */ playlists_buttons_relative = gtk_radio_button_new_with_label( NULL, _("Relative paths") ); gtk_table_attach_defaults( GTK_TABLE (playlists_options_table), playlists_buttons_relative, 0, 1, 0, 1); gtk_widget_show( playlists_buttons_relative ); /* Playlist Name */ playlists_buttons_playlist_name = gtk_check_button_new_with_label(_("Use this Filename:") ); gtk_signal_connect( GTK_OBJECT(playlists_buttons_playlist_name), "clicked", GTK_SIGNAL_FUNC(playlists_use_filename_toggled), NULL ); gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON (playlists_buttons_playlist_name), FALSE ); gtk_table_attach_defaults( GTK_TABLE (playlists_options_table), playlists_buttons_playlist_name, 1, 2, 0, 1); gtk_widget_show(playlists_buttons_playlist_name); playlists_playlist_name = gtk_entry_new_with_max_length(15); gtk_entry_set_editable( GTK_ENTRY (playlists_playlist_name), TRUE ); gtk_entry_set_text( GTK_ENTRY (playlists_playlist_name), "mp3s.m3u" ); gtk_widget_set_sensitive( GTK_WIDGET(playlists_playlist_name), FALSE ); gtk_table_attach_defaults( GTK_TABLE (playlists_options_table), playlists_playlist_name, 2, 3, 0, 1); gtk_widget_show(playlists_playlist_name); /* absolute */ playlists_buttons_absolute = gtk_radio_button_new_with_label( gtk_radio_button_group( GTK_RADIO_BUTTON (playlists_buttons_relative)), _("Absolute paths") ); gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON (playlists_buttons_absolute), TRUE ); gtk_table_attach_defaults( GTK_TABLE (playlists_options_table), playlists_buttons_absolute, 0, 1, 1, 2); gtk_widget_show( playlists_buttons_absolute ); /* End Options Frame */ /* ------------------------------------------------------------------------ */ /* The Box for the buttons */ playlists_buttons_box = gtk_hbox_new( TRUE, 10); gtk_container_set_border_width( GTK_CONTAINER (playlists_buttons_box), 5); gtk_box_pack_start( GTK_BOX (box), playlists_buttons_box, TRUE, FALSE, 0 ); gtk_widget_show( playlists_buttons_box ); /* The Buttons */ playlists_buttons_start = gnome_stock_or_ordinary_button( GNOME_STOCK_PIXMAP_EXEC ); gtk_signal_connect( GTK_OBJECT (playlists_buttons_start), "clicked", GTK_SIGNAL_FUNC (playlists_start), NULL ); gtk_box_pack_start( GTK_BOX (playlists_buttons_box), playlists_buttons_start, TRUE, TRUE, 0 ); gtk_widget_show( playlists_buttons_start ); playlists_buttons_quit = gnome_stock_or_ordinary_button( GNOME_STOCK_PIXMAP_QUIT ); gtk_signal_connect( GTK_OBJECT (playlists_buttons_quit), "clicked", GTK_SIGNAL_FUNC (gtk_exit), NULL ); gtk_box_pack_start( GTK_BOX (playlists_buttons_box), playlists_buttons_quit, TRUE, TRUE, 0 ); gtk_widget_show( playlists_buttons_quit ); return(box); } void playlists_load_properties( void ) { gtk_label_set_text( GTK_LABEL (playlists_directory), prop_mp3_dir); } void playlists_use_filename_toggled( GtkWidget *widget, gpointer *data ) { gboolean active; active = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(playlists_buttons_playlist_name) ); gtk_widget_set_sensitive( GTK_WIDGET(playlists_playlist_name), active*-1 ); } void playlists_directory_select( void ) { GtkWidget *filew; gchar *buffer; gtk_label_get( GTK_LABEL (playlists_directory), &buffer ); filew = gtk_file_selection_new( _("Select a Directory") ); gtk_file_selection_set_filename( GTK_FILE_SELECTION (filew), buffer ); gtk_signal_connect( GTK_OBJECT (filew), "destroy", GTK_SIGNAL_FUNC (gtk_widget_destroy), filew); /* Connect the ok buton to load_file */ gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button), "clicked", GTK_SIGNAL_FUNC (playlists_lf), filew); gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION (filew)->ok_button), "clicked", GTK_SIGNAL_FUNC (destroy), filew); /* Connect the cancel buton to destroy */ gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION (filew)->cancel_button), "clicked", GTK_SIGNAL_FUNC (destroy), filew); gtk_widget_show(filew); } /* I don't know how to pass the filename directly to load_file so I use this function */ void playlists_lf( GtkWidget *widget, GtkFileSelection *file_selection ) { gchar *filename=gtk_file_selection_get_filename( GTK_FILE_SELECTION (file_selection)); playlists_directory_load( filename ); } void playlists_directory_load( gchar *dirname ) { gtk_label_set_text( GTK_LABEL (playlists_directory),dirname); /* g_print("dir: %s\n", dirname ); */ } void playlists_start( ) { FILE *file=NULL; gchar *directory=NULL; gchar *filename=NULL; gboolean one; gboolean all; gboolean sub; gboolean remove; /* gboolean ultra; */ gboolean relative; gboolean name; gchar playlist[80]; playlist[0]='\0'; gtk_label_get( GTK_LABEL (playlists_directory), &directory ); if(directory[strlen(directory)-1]=='/') directory[strlen(directory)-1]=0; g_print("directory: %s\n", directory); one = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_one) ); g_print("one: %i\n", one ); all = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_all) ); g_print("all: %i\n", all ); sub = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_sub) ); g_print("sub: %i\n", sub ); remove = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_remove) ); g_print("remove: %i\n", remove ); /* ultra = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_ultra) ); */ /* g_print("ultra: %i\n", ultra ); */ relative = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_relative) ); g_print("relative: %i\n", relative ); name = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (playlists_buttons_playlist_name) ); g_print("name: %i\n", name ); if( name == 1) /* A given name should be used */ filename = gtk_entry_get_text( GTK_ENTRY (playlists_playlist_name) ); if( name == 1 ) /* Use the given name */ strncpy( playlist, filename, 74 ); /* Just make a playlist with the files in this directory */ if( one == 1 ) { g_print("Playlist: %s\n", playlist); g_print("Removing old playlist...\n"); playlists_clean_up( directory, 0, 0, playlist, "all" ); g_print("Building new playlist...\n"); playlists_go( directory, 0, 0, relative, playlist ); } /* Make one playlist with all mp3s in all subdirs */ else if( all == 1 ) { if( playlist[0] == '\0' ) { strncpy( playlist, (g_basename(directory)), 74 ); strcat( playlist, ".m3u" ); } g_print("Playlist: %s\n", playlist); chdir(directory); if( (file = fopen( playlist, "w" )) == 0 ) { g_print("file: ERROR while opening %s!\n", playlist); return; } chdir(".."); g_print("Building new playlist...\n"); playlists_go_all( directory, "./", 0, 1, relative, file ); fclose(file); } /* Create a Playlist in every subdirectory */ else if( sub == 1 ) { g_print("Playlist: %s\n", playlist); g_print("Removing old playlists...\n"); playlists_clean_up( directory, 0, 1, playlist, "all" ); g_print("Building new playlists...\n"); playlists_go( directory, 0, 1, relative, playlist ); g_print("Removing playlists with zero size...\n"); playlists_clean_up( directory, 0, 1, playlist, "empty" ); } else if( remove == 1) { g_print("Removing old playlists...\n"); playlists_clean_up( directory, 0, 1, playlist, "all" ); } /* else if( ultra == 1 ) */ /* { */ /* g_print("Ultra!\n"); */ /* } */ } /* This function removes previously created playlists */ void playlists_clean_up( gchar *dir, gint depth, gboolean recursive, gchar *filename, gchar *what ) { DIR *dp; struct dirent *entry; struct stat statbuf; gchar playlist[80]; gint size; gboolean tmp=0; /* Set to one if playlist[0] should be set back to '\0' */ /* g_print("filename: %s\n", filename); */ if(dir[strlen(dir)-1]=='/') dir[strlen(dir)-1]=0; chdir(dir); /* if( filename[0] == 0 || filename == NULL ) */ /* { */ strncpy( playlist, (g_basename(dir)), 74 ); strcat( playlist, ".m3u" ); /* } */ /* else */ /* { */ /* strncpy( playlist, filename, 74 ); */ /* tmp=1; */ /* } */ g_print("Playlist: %s\n", playlist); if( g_file_exists(playlist) == FALSE ) /* g_print("Playlist %s does not exist\n", playlist) */; else if( (strcmp( what, "all")) == 0 ) { if( remove(playlist) != 0 ) g_print("Couldn't delete playlist %s\n", playlist); else g_print("Deleted playlist '%s/%s'!\n", dir, playlist); } else if( (strcmp( what, "empty")) == 0 ) { /* If the file size is 0 remove it */ if (stat(playlist, &statbuf) == -1) g_print("ERROR File not found: %s \n", playlist); size = (long)statbuf.st_size; if( size == 0 ) { if( remove(playlist) != 0 ) g_print("Couldn't delete playlist %s\n", playlist); else g_print("Deleted playlist '%s/%s'!\n", dir, playlist); } } if( tmp == 1 ) playlist[0]='\0'; chdir(".."); if( (dp = opendir(dir))==NULL ) { g_print("opendir: ERROR while opening %s!\n", dir); /* gnome_error_dialog( _("Could not open directory!") ); */ return; } chdir(dir); while( (entry = readdir(dp)) != NULL ) { update_progress_bar(); stat( entry->d_name, &statbuf); if( S_ISDIR(statbuf.st_mode)) { /* Shall it be recursive ? */ if( recursive == 0 ) continue; /* Found a directory, ignore "." and ".." */ if( strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0 ) continue; /* printf("%*s%s/\n", depth, " ", entry->d_name ); */ /* Recurse but increase the indent level */ playlists_clean_up( entry->d_name, depth+4, recursive, playlist, what ); } else { /* g_print("%*s%s\n", depth, " ", entry->d_name ); */ continue; } } chdir(".."); closedir(dp); } void playlists_go( gchar *dir, gint depth, gboolean recursive, gboolean relative, gchar playlist[] ) { DIR *dp; FILE *file=NULL; struct dirent *entry; struct stat statbuf; /* gchar playlist[80]; */ gchar *dirandfile; if(dir[strlen(dir)-1]=='/') dir[strlen(dir)-1]=0; chdir(dir); /* If no name for the playlist(s) is given use the directory name */ if( playlist[0] == '\0' ) { strncpy( playlist, (g_basename(dir)), 74 ); strcat( playlist, ".m3u" ); g_print("Playlist: %s\n", playlist); if( (file = fopen( playlist, "a+" )) == 0 ) g_print("file: ERROR while opening %s!\n", playlist); playlist[0]='\0'; } /* Else use the given name */ else { g_print("Playlist: %s\n", playlist); if( (file = fopen( playlist, "a+" )) == 0 ) g_print("file: ERROR while opening %s!\n", playlist); } chdir(".."); if( (dp = opendir(dir))==NULL ) { g_print("opendir: ERROR while opening %s!\n", dir); /* gnome_error_dialog( _("Could not open directory!") ); */ return; } chdir(dir); while( (entry = readdir(dp)) != NULL ) { update_progress_bar(); lstat( entry->d_name, &statbuf); if( S_ISDIR(statbuf.st_mode)) { /* Shall it be recursive ? */ if( recursive == 0 ) continue; /* Found a directory, ignore "." and ".." */ if( strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0 ) continue; printf("%*s%s/\n", depth, " ", entry->d_name ); /* I need the whole path */ dirandfile = g_concat_dir_and_file( dir, entry->d_name ); /* Recurse but increase the indent level */ playlists_go( dirandfile, depth+4, recursive, relative, playlist ); } else { g_print("%*s%s", depth, " ", entry->d_name ); if( !mp3_is_valid(entry->d_name) ) { g_print(" NO!\n"); continue; } /* Use relative paths ? */ if( relative == 1 ) dirandfile = entry->d_name; else dirandfile = g_concat_dir_and_file( dir, entry->d_name ); g_print(" ok!\n"); /* g_print("dirandfile: %s\n", dirandfile); */ /* Write the file to the playlist */ fprintf( file, dirandfile ); fprintf( file, "\n" ); } } fclose(file); chdir(".."); closedir(dp); } void playlists_go_all( gchar *rootdir, gchar *dir, gint depth, gboolean recursive, gboolean relative, FILE *file ) { DIR *dp; struct dirent *entry; struct stat statbuf; gchar *dirandfile; gchar *buffer; gint i; if(rootdir[strlen(rootdir)-1]=='/') rootdir[strlen(rootdir)-1]=0; /* if(dir[strlen(dir)-1]=='/') */ /* dir[strlen(dir)-1]=0; */ /* g_print("rootdir: %s\n", rootdir); */ /* g_print("dir: %s\n", dir); */ chdir(rootdir); if( (dp = opendir(dir))==NULL ) { yamtlog("%s %s", _("ERROR while opening "), dir); /* gnome_error_dialog( _("Could not open directory!") ); */ return; } chdir(dir); while( (entry = readdir(dp)) != NULL ) { update_progress_bar(); lstat( entry->d_name, &statbuf); if( S_ISDIR(statbuf.st_mode)) { /* Shall it be recursive ? */ if( recursive == 0 ) continue; /* Found a directory, ignore "." and ".." */ if( strcmp(".", entry->d_name) == 0 || strcmp("..", entry->d_name) == 0 ) continue; printf("%*s%s/\n", depth, " ", entry->d_name ); /* I need the whole path */ dirandfile = g_concat_dir_and_file( dir, entry->d_name ); /* Recurse but increase the indent level */ playlists_go_all( rootdir, dirandfile, depth+4, recursive, relative, file ); } else { g_print("%*s%s", depth, " ", entry->d_name ); if( !mp3_is_valid(entry->d_name) ) { g_print(" NO!\n"); continue; } /* Use relative paths ? */ if( relative == 1 ) dirandfile = g_concat_dir_and_file( dir, entry->d_name ); else { buffer = g_strdup(dir); /* get rid of the '.' before the '/' */ if( buffer[0]=='.' && buffer[1]=='/' ) { for( i=2; id_name ); g_free(buffer); } g_print(" ok!\n"); /* g_print("dirandfile: %s\n", dirandfile); */ /* Write the file to the playlist */ fprintf( file, dirandfile ); fprintf( file, "\n" ); } } chdir(".."); closedir(dp); }