package GuiMp3mover; use Tk; use File::Basename; use File::Copy; use File::Path; #use strict; not quite there yet my @mp3_widget; my @source_mp3_widg; my @dest_mp3_widg; my @check_mp3_widg; my $mp3_canvas; my $recalc_button; my $main; my $mp3_frame; #---------------------------------------------# my $option_p; my $gui_option_p; my $date_p; my $version_p; my $FILEMASK_p; my $DIRECTORYMASK_p; my $ID3TITLEMASK_p; my $cfg_file_p; #pointers to options #passed by calling perl script #---------------------------------------------# local @mp3; local $index; sub gui ($$$$$$$$) { $option_p = $_[0]; $gui_option_p = $_[1]; $date_p = $_[2]; $version_p = $_[3]; $FILEMASK_p = $_[4]; $DIRECTORYMASK_p = $_[5]; $ID3TITLEMASK_p = $_[6]; $cfg_file_p = $_[7]; #-- set up main window --# $main = MainWindow->new; $main->minsize(400,500); $main->title("mp3mover GUI"); $main->packPropagate(1); #stops window resizing all the time #-- menu bar --# my $menu_bar = $main->Frame( -relief => 'groove', -borderwidth => '3' ) -> pack(-side => 'top', -fill => 'x'); #-- help menu --# my $help_mb = $menu_bar->Menubutton( -text => 'Help', -tearoff => 0 )->pack(-side => 'right'); $help_mb->command(-label => 'About',-command => \&about_txt); my $options_mb = $menu_bar->Menubutton( -text => 'Options', -tearoff => 0 )->pack(anchor => 'w'); $options_mb->command(-label => 'Masks', -command => \&config_masks); $options_mb->command(-label => 'GUI', -command => \&config_gui); #-- options and mp3 frames --# my $options_frame = $main->Frame->pack( -side => 'bottom', -padx => 10, -pady => 10, -fill => 'y' ); #this done above mp3s frame so it is not shifted off the bottom by #mp3s_frame $mp3_canvas = $main->Scrolled('Canvas', -scrollbars => 'ose'); $mp3_canvas->pack(-fill => 'both', -expand => 1); $mp3_frame = $mp3_canvas->Frame; $mp3_canvas->createWindow(0,0,-window => $mp3_frame, -anchor => 'nw', -tag => "bums"); my $t_options_frame = $options_frame->Frame->pack( -side => 'top', -fill => 'x' ); my $m_options_frame = $options_frame->Frame->pack( -side => 'top', -fill => 'x' ); my $i_options_frame = $options_frame->Frame->pack( -side => 'top', -fill => 'x' ); my $b_options_frame = $options_frame->Frame->pack( -side => 'top', -fill => 'x', -pady => 5 ); #-- create option buttons --# my $d_option = $t_options_frame->Checkbutton( -variable => \$$option_p{"D"}, -text => 'Do not create directory structure' )->pack(-side => 'left', -anchor => 'w'); my $R_option = $t_options_frame->Checkbutton( -variable => \$$option_p{"R"}, -text => 'Recurse subdirectories' )->pack(-side => 'right', -anchor => 'e'); my $x_option = $m_options_frame->Checkbutton( -variable => \$$option_p{"x"}, -text => 'Try to get info from id3 tag' )->pack(-side => 'left', -anchor => 'w'); my $g_option = $m_options_frame->Checkbutton( -variable => \$$option_p{"g"}, -text => 'Use id3 tag preferrentially' )->pack(-side => 'right', -anchor => 'e'); my $id3_v; if ( $$option_p{"i"} ) { $id3_v = $$option_p{"i"} } else { $id3_v = 2 } my $radio_id3v1 = $i_options_frame->Radiobutton( -variable => \$id3_v, -value => '1', -text => '1' ); my $radio_id3v2 = $i_options_frame->Radiobutton( -variable => \$id3_v, -value => '2', -text => '2' ); my $i_option = $i_options_frame->Checkbutton( -variable => \$$option_p{"i"}, -text => 'Write id3 tag version:', -onvalue => $id3_v, -offvalue => 0 )->pack(-side => 'left'); $radio_id3v1->pack(-side => 'left'); $radio_id3v2->pack(-side => 'left'); #buttons $recalc_button = $b_options_frame->Button( -text => 'Calculate', -command => \&create_list )->pack(-side => 'right', -anchor => 'e'); my $move_button = $b_options_frame->Button( -text => 'Move checked mp3s', -command => \&move_mp3s )->pack(-side => 'right', -anchor => 'e'); my $save_opts = $b_options_frame->Button( -text => 'Save Options', -command => [\&main::write_options,$option_p,$gui_option_p,$$cfg_file_p,$FILEMASK_p,$DIRECTORYMASK_p,$ID3TITLEMASK_p] )->pack(-side => 'left', -anchor => 'w'); MainLoop; } sub create_list { $recalc_button->configure(-text => "Recalculate"); @mp3 = (); $index = 0; &main::get_mp3s; my $canvas_height; my $canvas_width; my $ind = 1; while ($mp3_widget[$ind]) { $mp3_widget[$ind]->packForget; $mp3_widget[$ind]->destroy; $ind++ } my @mp3_widget_s; my @mp3_widget_d; foreach $ind (1..$index) { $mp3_widget[$ind] = $mp3_frame->Frame->pack( -side => 'top', -fill => 'both' ); $mp3_widget_s[$ind] = $mp3_widget[$ind]->Frame->pack( -side => 'top', -fill => 'both' ); $mp3_widget_d[$ind] = $mp3_widget[$ind]->Frame->pack( -side => 'bottom', -fill => 'both' ); if ($$gui_option_p{"Ds"}) { $source_mp3_widg[$ind] = $mp3_widget_s[$ind]->Label( -text => $mp3[$ind]->{source} )->pack(-side => 'top', -anchor => 'w'); } else { $source_mp3_widg[$ind] = $mp3_widget_s[$ind]->Label( -text => $mp3[$ind]->{sourcename} )->pack(-side => 'top', -anchor => 'w'); } if ($$gui_option_p{"Dd"} && ! $$option_p{"D"}) { $dest_mp3_widg[$ind] = $mp3_widget_d[$ind]->Entry( -textvariable => \$mp3[$ind]->{dest} ); } else { $dest_mp3_widg[$ind] = $mp3_widget_d[$ind]->Entry( -textvariable => \$mp3[$ind]->{newname} ); } #sets the width of the Entry box appropriately my $ebox_width = ($dest_mp3_widg[$ind]->bbox('end'))[0]; $canvas_width = $ebox_width if ($ebox_width > $canvas_width); $ebox_width = 132 if (($ebox_width/2 - 66) < 1); $dest_mp3_widg[$ind]->pack( -side => 'left', -anchor => 'w', -ipadx => $ebox_width/2 - 66 ); $check_mp3_widg[$ind] = $mp3_widget_d[$ind]->Checkbutton( -variable => \$mp3[$ind]->{move} )->pack(-side => 'left', -anchor => 'w'); } $canvas_height = $index * 42; $canvas_width = $canvas_width + 38; $mp3_canvas->configure( -scrollregion => [0,0,$canvas_width,$canvas_height] ); } sub move_mp3s { my $mp3; foreach $mp3 (@mp3) { if ( $mp3->{move} ) { if ( $$option_p{"i"} ) { &main::id3tag($mp3->{source},$mp3->{artist},$mp3->{album},$mp3->{songnumber},$mp3->{title},$$ID3TITLEMASK_p); } mkpath(dirname($mp3->{dest})); if ( $$option_p{"D"} || ! $$gui_option_p{"Dd"} ) { $mp3->{dest} = $mp3->{newdir}.$mp3->{newname}; } rename ($mp3->{source},$mp3->{dest}); } } } sub about_txt { my $about_window = $main->Toplevel; $about_window->title("About"); $about_window->resizable(0,0); my $aw_frame = $about_window->Frame->pack(-fill => 'both'); my $aw_text = $aw_frame->Label( -text => "Hey baby\nThis is mp3mover version $$version_p ($$date_p)\nReleased under the GPL\n\nauthor: James Maddison ")->pack(-side => 'top', pady => 5, padx => 10 ); my $exit_button = $aw_frame->Button( -text => "OK Baby", -command => sub { $about_window->withdraw; $about_window->destroy } )->pack(-side => 'bottom', pady => 5, padx => 10); } sub config_masks { my $masks_window = $main->Toplevel; $masks_window->title("Adjust masks"); $masks_window->resizable(0,0); my $mw_frame = $masks_window->Frame->pack( -fill => 'both', -padx => 5, -pady => 5 ); my $buttons_frame = $mw_frame->Frame->pack( -fill => 'y', -side => 'bottom' ); my $f_frame = $mw_frame->Frame->pack( -side => 'top', -fill => 'x', -anchor => 'n', -pady => 3 ); my $d_frame = $mw_frame->Frame->pack( -side => 'top', -fill => 'x', -anchor => 'n', -pady => 3 ); my $i_frame = $mw_frame->Frame->pack( -side => 'top', -fill => 'x', -anchor => 'n', -pady => 3 ); my $a_frame = $mw_frame->Frame->pack( -side => 'bottom', -fill => 'x', -anchor => 's', -pady => 3 ); my $fmask_lab = $f_frame->Label(-text => "File mask")->pack( -side => 'left', -anchor => 'w' ); my $fmask_wid = $f_frame->Entry(-textvariable => $FILEMASK_p)->pack( -side => 'right', -anchor => 'e' ); my $dmask_lab = $d_frame->Label(-text => "Directory mask")->pack( -side => 'left', -anchor => 'w' ); my $dmask_wid = $d_frame->Entry( -textvariable => $DIRECTORYMASK_p )->pack( -side => 'right', -anchor => 'e' ); my $imask_lab = $i_frame->Label(-text => "Id3 mask")->pack( -side => 'left', -anchor => 'w' ); my $imask_wid = $i_frame->Entry( -textvariable => $ID3TITLEMASK_p )->pack( -side => 'right', -anchor => 'e' ); my $advice_label = $a_frame->Label( -text => "\nKey :\t\%r = Artist\n\t\%a = Album\n\t\%n = song number\n\t\%t = Song Title\n\nPrefixes:\n\tu for upper case\n\tl for lower case\n\teg: \%ur = upper case artist\n\n\t\%fa = first letter of artist\n\nNote:\n\tDividers will be intelligently\n\tremoved according to missing\n\tinformation", -justify => 'left')->pack( -anchor => 'w', -side => 'left' ); my $exit_button = $buttons_frame->Button( -text => "Close", -command => sub{ $masks_window->withdraw; $masks_window->destroy } )->pack(-side => "left"); my $save_exit = $buttons_frame->Button( -text => "Save and Exit", -command => sub{ &main::write_options($option_p,$gui_option_p,$$cfg_file_p,$FILEMASK_p,$DIRECTORYMASK_p,$ID3TITLEMASK_p); $masks_window->withdraw; $masks_window->destroy } )->pack(-side => "left"); } sub config_gui { my $gui_window = $main->Toplevel; $gui_window->title("Change GUI options"); $gui_window->resizable(0,0); my $gui_frame = $gui_window->Frame->pack( -fill => 'both', -padx => 5, -pady => 5 ); my $buttons_frame = $gui_frame->Frame->pack( -fill => 'y', -side => 'bottom', -pady => 10 ); my $left_frame = $gui_frame->Frame->pack(-side => 'left'); my $right_frame = $gui_frame->Frame->pack(-side => 'right'); my %gui_label; my %gui_box; $gui_label{"Ds"} = $left_frame->Label( -text => 'Show directory information in main window for source mp3' )->pack(-side => 'top', -anchor => 'w', -padx => 10); $gui_box{"Ds"} = $right_frame->Checkbutton( -variable => \$$gui_option_p{Ds} )->pack(-side => 'top', -anchor => 'w', -padx => 10); $gui_label{"Dd"} = $left_frame->Label( -text => 'Show directory information in main window for destination mp3' )->pack(-side => 'top', -anchor => 'w', -padx => 10); $gui_box{"Dd"} = $right_frame->Checkbutton( -variable => \$$gui_option_p{Dd} )->pack(-side => 'top', -anchor => 'w', -padx => 10); my $exit_button = $buttons_frame->Button( -text => "Close", -command => sub{ $gui_window->withdraw; $gui_window->destroy } )->pack(-side => "left"); my $save_exit = $buttons_frame->Button( -text => "Save and Exit", -command => sub{ &main::write_options($option_p,$gui_option_p,$$cfg_file_p,$FILEMASK_p,$DIRECTORYMASK_p,$ID3TITLEMASK_p); $gui_window->withdraw; $gui_window->destroy } )->pack(-side => "left"); } 1;