#!/usr/bin/perl -w use strict; use Tk; use POSIX qw(WNOHANG setsid); use Errno qw(EAGAIN); use constant DEBUG => 0; # Change to 1 to enable debugging use Quantum::Superpositions; require Tk::ProgressBar; ## Establish global variables ####################################### my ( $location, $storage, $source, $home, $home_value ); my ( $exclude, $block, $exclusions ); ## Done with global variables ###################################### # Spin the message loop my $main = &create_ui; MainLoop; #################################################################### ## Make a pretty menu #################################################################### sub create_ui { # Create main window my $main = MainWindow->new; $main->geometry('+40+40'); $main->title("Personal Synch Tool"); ### Source Directory label and textbox $source = $main->Label( -text => 'Source Directory (no trailing /)', -background => 'lightgreen', -relief => 'ridge', )->pack( -expand => 1, -side => 'left', -fill => 'both', ); $source->grid( -row => 1, -column => 0, -columnspan => 1, #-sticky => 'w', -padx => 2, -pady => 4 ); $home = $main->Entry( -textvariable => "/home/jrobiso2" ); $home->grid( -row => 1, -column => 1, -columnspan => 1, #-sticky => 'w', -padx => 2, -pady => 4, ); ### End Source label and textbox ### Exclusion listing label and textbox $exclude = $main->Label( -text => 'Exclusions (seperate by space)', -background => 'PaleVioletRed', -relief => 'ridge', )->pack( -expand => 1, -side => 'left', -fill => 'both', ); $exclude->grid( -row => 2, -column => 0, -columnspan => 1, #-sticky => 'w', -padx => 2, -pady => 4 ); $block = $main->Entry( -textvariable => "X Y W" ); $block->grid( -row => 2, -column => 1, -columnspan => 1, #-sticky => 'w', -padx => 2, -pady => 4, ); ### End Exclusion listing label and textbox ### Target location label and textbox my $label = $main->Label( -text => 'Target Location: ', -background => 'lightblue', -relief => 'ridge', )->pack( -expand => 1, -side => 'left', -fill => 'both', ); $label->grid( -row => 3, -column => 0, -columnspan => 1, #-sticky => 'w', -padx => 2, -pady => 4 ); $location = $main->Entry( -textvariable => "/media/usbdisk/" ); $location->grid( -row => 3, -column => 1, -columnspan => 1, #-sticky => 'w', -padx => 2, -pady => 4, ); ### End target label and textbox ### Start "Synch" Button my $go = $main->Button( -text => 'Synch', -command => \&synch, -background => 'green', )->pack( -expand => 1, -side => 'left', -fill => 'both', ); $go->grid( -row => 4, -column => 0, -columnspan => 1, -sticky => 'e w', ); ### END Synch button ## Start Quit button my $quit = $main->Button( -text => 'Quit', -justify => 'right', -background => 'red', -command => [ $main => 'destroy' ] )->pack( -expand => 1, -side => 'right', -fill => 'both', ); $quit->grid( -row => 4, -column => 1, -columnspan => 1, -sticky => 'e w', ); ### END Quit button } ############# END pretty menu ############################ sub synch { ## establish all values needed $storage = $location->get(); $exclusions = $block->get(); $home_value = $home->get(); chomp $storage; chomp $home_value; chomp $exclusions; ### Get exclusions, and add "." and ".." to it. my @exclusion_list = split( /\s/, $exclusions ); push @exclusion_list, "."; push @exclusion_list, ".."; opendir( HOME, $home_value ) || die "Cannot open $home_value: $!"; my @SOURCE = readdir(HOME); closedir(HOME); my $tot = $#SOURCE - 1; #print "total items: $tot\n"; unless ( -e $storage ) { my $response = $main->messageBox( -message => "$storage does not exist or is not mounted\n", -title => "Error!", -icon => 'error', -type => 'RetryCancel', ); if ( $response eq 'Retry' ) { synch(); } else { return } } ## Progress Bar my $count = 0; my $mw = new MainWindow( -title => 'Copying Progress Bar' ); $mw->geometry('+340+340'); my $top = $mw->Frame()->pack( -expand => 1, -fill => 'both' ); my $percent_done = 0; my $pb = $top->ProgressBar( -width => 20, -height => 200, -from => 0, -to => 100, -blocks => 20, -gap => 0, -length => 400, -colors => [ 0, 'green', 50, 'green', 80, 'green' ], -troughcolor => 'red', -variable => \$percent_done )->pack(); ## End Progress Bar foreach my $file (@SOURCE) { next if ( $file eq any(@exclusion_list) ); my $cmd = "cp -au $home_value\/\"$file\" $storage"; #print "Executing: $cmd\n"; ## Open a message box to inform regarding the command. while (`$cmd`) { # $$ is the pid returned by the system com +mand my $status = $main->messageBox( -message => 'Executing $cmd', -background => 'lightgray', -title => 'Update', ); } ## Progress bar update $count++; #print $count; $percent_done = $count / $tot * 100; $pb->update; } ## END Progress Bar update ## Now close the progress bar $mw->destroy; my $response = $main->messageBox( -message => "Copying is complete!", -title => "Complete!", -type => 'OK', ); if ( $response eq 'OK' ) { exit(0); } else { return } }
In reply to Re: Tk::ProgressBar and system cp
by tame1
in thread Tk::ProgressBar and system cp
by tame1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |