/* $Id: set.c,v 1.1.1.1 2000/01/30 16:39:22 benediktroth Exp $ */ #include #include #include #include #include #include #include #include #include "yamt.h" #include "auto.h" GtkWidget *auto_box_new( ) { GtkWidget *box; GtkWidget *auto_frame_directory; GtkWidget *auto_table_directory; GtkWidget *auto_label_directory; GtkWidget *auto_frame_format; GtkWidget *auto_table_format; GtkWidget *auto_label_part1; GtkWidget *auto_label_part2; GtkWidget *auto_label_separator; GtkWidget *auto_buttons_select; GtkWidget *auto_buttons_box; GtkWidget *auto_buttons_start; GtkWidget *auto_buttons_quit; auto_part1_list=NULL; auto_part2_list=NULL; auto_separator_list=NULL; /* The box it which everything is put */ box = gtk_vbox_new( FALSE, 0); /* Frame for directory */ auto_frame_directory = gtk_frame_new( NULL ); gtk_box_pack_start( GTK_BOX (box), auto_frame_directory, TRUE, FALSE, 0 ); gtk_widget_show( auto_frame_directory ); /* Table for directory */ auto_table_directory = gtk_table_new( 1, 1, TRUE); gtk_container_set_border_width( GTK_CONTAINER (auto_table_directory), 5); gtk_container_add( GTK_CONTAINER (auto_frame_directory), auto_table_directory ); gtk_widget_show( auto_table_directory ); /* Directory */ auto_label_directory=gtk_label_new( _("Directory: ") ); gtk_table_attach_defaults( GTK_TABLE (auto_table_directory), auto_label_directory, 0, 1, 0, 1); gtk_widget_show( auto_label_directory ); auto_directory=gtk_label_new( _("[no dir selected]") ); gtk_table_attach_defaults( GTK_TABLE (auto_table_directory), auto_directory, 1, 2, 0, 1); gtk_widget_show( auto_directory ); /* The select button */ auto_buttons_select = gnome_stock_or_ordinary_button( GNOME_STOCK_PIXMAP_OPEN ); gtk_signal_connect( GTK_OBJECT (auto_buttons_select), "clicked", GTK_SIGNAL_FUNC (auto_directory_select), NULL ); gtk_table_attach_defaults( GTK_TABLE (auto_table_directory), auto_buttons_select, 2, 3, 0, 1); gtk_widget_show( auto_buttons_select ); /* The recursive button */ auto_buttons_recursive = gtk_check_button_new_with_label( _("Recurse Subdirs") ); gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON (auto_buttons_recursive), FALSE ); gtk_table_attach_defaults( GTK_TABLE (auto_table_directory), auto_buttons_recursive, 1, 2, 1, 2); gtk_widget_show( auto_buttons_recursive ); /******************************** START ********************************/ /* The format frame */ auto_frame_format = gtk_frame_new( NULL ); gtk_frame_set_label( GTK_FRAME (auto_frame_format), "Format"); gtk_box_pack_start( GTK_BOX (box), auto_frame_format, TRUE, FALSE, 0 ); gtk_widget_show( auto_frame_format ); /* Table */ auto_table_format = gtk_table_new( 5, 3, TRUE); gtk_container_add( GTK_CONTAINER (auto_frame_format), auto_table_format ); gtk_widget_show( auto_table_format ); /* Label for part 1 */ auto_label_part1 = gtk_label_new( _("Part 1:") ); gtk_table_attach_defaults( GTK_TABLE (auto_table_format), auto_label_part1, 0, 1, 0, 1); gtk_widget_show( auto_label_part1); /* Create a new combo box */ auto_part1 = gtk_combo_new(); gtk_entry_set_editable( GTK_ENTRY (GTK_COMBO(auto_part1)->entry), FALSE); /* Create the CList */ if ( !auto_part1_list ) { auto_part1_list = g_list_append( auto_part1_list, _("Artist") ); auto_part1_list = g_list_append( auto_part1_list, _("Album") ); auto_part1_list = g_list_append( auto_part1_list, _("Year") ); auto_part1_list = g_list_append( auto_part1_list, _("Genre") ); auto_part1_list = g_list_append( auto_part1_list, _("Comment") ); auto_part1_list = g_list_append( auto_part1_list, _("Title") ); } /* Connect the combo and the list */ gtk_combo_set_popdown_strings( GTK_COMBO(auto_part1), auto_part1_list); gtk_table_attach_defaults( GTK_TABLE (auto_table_format), auto_part1, 1, 2, 0, 1); gtk_widget_show( auto_part1 ); /* Label for part 2 */ auto_label_part2 = gtk_label_new( _("Part 2:") ); gtk_table_attach_defaults( GTK_TABLE (auto_table_format), auto_label_part2, 0, 1, 2, 3); gtk_widget_show( auto_label_part2 ); /* Create a new combo box */ auto_part2 = gtk_combo_new(); gtk_entry_set_editable( GTK_ENTRY (GTK_COMBO(auto_part2)->entry), FALSE); /* Create the CList */ if ( !auto_part2_list ) { auto_part2_list = g_list_append( auto_part2_list, _("Title") ); auto_part2_list = g_list_append( auto_part2_list, _("Artist") ); auto_part2_list = g_list_append( auto_part2_list, _("Album") ); auto_part2_list = g_list_append( auto_part2_list, _("Comment") ); auto_part2_list = g_list_append( auto_part2_list, _("Year") ); auto_part2_list = g_list_append( auto_part2_list, _("Genre") ); } /* Connect the combo and the list */ gtk_combo_set_popdown_strings( GTK_COMBO(auto_part2), auto_part2_list); gtk_table_attach_defaults( GTK_TABLE (auto_table_format), auto_part2, 1, 2, 2, 3); gtk_widget_show( auto_part2 ); /* Label for Separator */ auto_label_separator = gtk_label_new( _("Separator") ); gtk_table_attach_defaults( GTK_TABLE (auto_table_format), auto_label_separator, 0, 1, 4, 5); gtk_widget_show( auto_label_separator ); /* Create a new combo box */ auto_separator = gtk_combo_new(); gtk_entry_set_editable( GTK_ENTRY (GTK_COMBO(auto_separator)->entry), FALSE); /* Create the CList */ if ( !auto_separator_list ) { auto_separator_list = g_list_append( auto_separator_list, "-" ); auto_separator_list = g_list_append( auto_separator_list, "_" ); } /* Connect the combo and the list */ gtk_combo_set_popdown_strings( GTK_COMBO(auto_separator), auto_separator_list); gtk_table_attach_defaults( GTK_TABLE (auto_table_format), auto_separator, 1, 2, 4, 5); gtk_widget_show( auto_separator ); /* MoveParenthesesToComment label */ auto_buttons_mptc = gtk_check_button_new_with_label( _("Move everything between\nparantheses to the comment") ); gtk_table_attach( GTK_TABLE (auto_table_format), auto_buttons_mptc, 1, 2, 5, 7, 0, 0, 5, 5); gtk_widget_show( auto_buttons_mptc ); /******************************** END ********************************/ /* The Box for the buttons */ auto_buttons_box = gtk_hbox_new( TRUE, 10); gtk_container_set_border_width( GTK_CONTAINER (auto_buttons_box), 5); gtk_box_pack_start( GTK_BOX (box), auto_buttons_box, TRUE, FALSE, 0 ); gtk_widget_show( auto_buttons_box ); /* The Buttons */ auto_buttons_start = gnome_stock_or_ordinary_button( GNOME_STOCK_PIXMAP_EXEC ); gtk_signal_connect( GTK_OBJECT (auto_buttons_start), "clicked", GTK_SIGNAL_FUNC (auto_start), NULL ); gtk_box_pack_start( GTK_BOX (auto_buttons_box), auto_buttons_start, TRUE, TRUE, 0 ); gtk_widget_show( auto_buttons_start ); auto_buttons_quit = gnome_stock_or_ordinary_button( GNOME_STOCK_PIXMAP_QUIT ); gtk_signal_connect( GTK_OBJECT (auto_buttons_quit), "clicked", GTK_SIGNAL_FUNC (gtk_exit), NULL ); gtk_box_pack_start( GTK_BOX (auto_buttons_box), auto_buttons_quit, TRUE, TRUE, 0 ); gtk_widget_show( auto_buttons_quit ); return(box); } void auto_load_properties( void ) { gtk_label_set_text( GTK_LABEL (auto_directory), prop_mp3_dir ); } void auto_directory_select( void ) { GtkWidget *filew; gchar *buffer; gtk_label_get( GTK_LABEL (auto_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 (auto_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 auto_lf( GtkWidget *widget, GtkFileSelection *file_selection ) { gchar *filename=gtk_file_selection_get_filename( GTK_FILE_SELECTION (file_selection)); auto_directory_load( filename ); } void auto_directory_load( gchar *dirname ) { gtk_label_set_text( GTK_LABEL (auto_directory),dirname); /* g_print("dir: %s\n", dirname ); */ } gchar *auto_get_part1( void ) { gchar *part1; part1 = gtk_entry_get_text( GTK_ENTRY (GTK_COMBO(auto_part1)->entry) ); /* Replace the internationalized string with english */ /* so that id3tag_sort() can use it */ if( !strcmp( part1, _("Title") ) ) part1 = "Title"; else if( !strcmp( part1, _("Artist") ) ) part1 = "Artist"; else if( !strcmp( part1, _("Album") ) ) part1 = "Album"; else if( !strcmp( part1, _("Comment") ) ) part1 = "Comment"; else if( !strcmp( part1, _("Year") ) ) part1 = "Year"; else if( !strcmp( part1, _("Genre") ) ) part1 = "Genre"; return(part1); } gchar *auto_get_part2( void ) { gchar *part2; part2 = gtk_entry_get_text( GTK_ENTRY (GTK_COMBO(auto_part2)->entry) ); /* Replace the internationalized string with english */ /* so that id3tag_sort() can use them */ if( !strcmp( part2, _("Title") ) ) part2 = "Title"; else if( !strcmp( part2, _("Artist") ) ) part2 = "Artist"; else if( !strcmp( part2, _("Album") ) ) part2 = "Album"; else if( !strcmp( part2, _("Comment") ) ) part2 = "Comment"; else if( !strcmp( part2, _("Year") ) ) part2 = "Year"; else if( !strcmp( part2, _("Genre") ) ) part2 = "Genre"; return(part2); } gchar *auto_get_separator( void ) { gchar *tmp; tmp = gtk_entry_get_text( GTK_ENTRY (GTK_COMBO(auto_separator)->entry) ); return(tmp); } gint auto_get_mptc( void ) { gint mptc; mptc = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(auto_buttons_mptc) ); return(mptc); } void auto_start( ) { gchar *directory; gboolean recursive; gchar *part1; gchar *part2; gchar *tmp; gchar separator; gint mptc; gtk_label_get( GTK_LABEL (auto_directory), &directory ); yamtlog("%s %s", _("Auto-Directory: "), directory ); recursive = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON (auto_buttons_recursive) ); yamtlog("%s %s", _("Auto-Recursive: "), recursive ? "TRUE" : "FALSE" ); part1 = auto_get_part1(); yamtlog("%s %s", "Auto-Part Level1: ", part1); part2 = auto_get_part2(); yamtlog("%s %s", "Auto-Part Level2: ", part2); tmp = auto_get_separator(); separator = tmp[0]; yamtlog("%s %s", "Separator: ", tmp); mptc = auto_get_mptc(); yamtlog("%s %s", _("Auto-mptc: "), mptc ? "TRUE" : "FALSE" ); auto_go( directory, 0, recursive, part1, part2, separator, mptc ); } void auto_go( gchar *dir, gint depth, gboolean recursive, gchar *part1, gchar *part2, gchar separator, int mptc ) { DIR *dp; struct dirent *entry; struct stat statbuf; struct id3tag tag; if( (dp = opendir(dir))==NULL ) { yamtlog("%s %s", _("ERROR while opening "), dir); gnome_error_dialog( _("Could not open directory!") ); return; } chdir(dir); if(dir[strlen(dir)-1]=='/') dir[strlen(dir)-1]=0; 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 */ auto_go( entry->d_name, depth+4, recursive, part1, part2, separator, mptc ); } else { /* printf("%*s%s", depth, " ", entry->d_name ); */ if( !mp3_is_valid(entry->d_name) ) { yamtlog("%s %s", entry->d_name, _(" skipped") ); continue; } tag = id3tag_get_from_filename( g_strdup(entry->d_name), part1, part2, separator, mptc ); /* No valid tag was returned */ if( id3tag_is_valid( tag ) != 1 ) { yamtlog("%s %s", entry->d_name, _(" not tagged") ); continue; } /* id3tag_show( tag ); */ id3tag_set( g_concat_dir_and_file( dir, entry->d_name), tag ); yamtlog("%s %s", entry->d_name, _(" tagged") ); } } chdir(".."); closedir(dp); }