#!/usr/bin/perl # BETA! # Copy-file-to engine for Nautilus or similar : raimo 20061212-BETA # license CC NC-SA: http://creativecommons.org/licenses/nc-sa/1.0/ # # WARNING! This is BETA!, use at Your own HIGH risk only! # libgtk2-perl required, sudo apt-get install libgtk2-perl # # INSTALL (copy commands one by one to Your terminal): # wget -O ~/copy-file-to.txt http://dash.atspace.org/ubuntu/nautilus/copy-file-to.txt # sudo cp -i ~/copy-file-to.txt /usr/bin/copy-file-to # sudo chmod 755 /usr/bin/copy-file-to # mkdir -p ~/.gnome2/nautilus-scripts # wget -O ~/.gnome2/nautilus-scripts/Copy-To http://dash.atspace.org/ubuntu/nautilus/Copy-To.txt # chmod 755 ~/.gnome2/nautilus-scripts/Copy-To # # RUN: # Select file with mouse right button (in Dektop or Nautilus) # Select Scripts -> Copy-To from menu # look http://dash.atspace.org/ubuntu/nautilus for more help # look STDIN for messages and errors (Ctrl+Alt+F1) # WARNING! This is BETA!, use at Your own HIGH risk only! # SETTINGS: (there is none) # # DO NOT EDIT BELOW THIS, THERE IS NO SETTINGS! # ÄLÄ kauheasti säädä alla olevia asioita, asetuksia tms. ei ole. # DEV: # http://gtk2-perl.sourceforge.net/ # http://gtk2-perl.sourceforge.net/doc/pod/Gtk2/FileChooser.html use strict; use File::Copy; use Gtk2 "-init"; sub open_dialog; sub save_lastdir; sub message; my $lic = "http://creativecommons.org/licenses/nc-sa/1.0/"; my $name = $0; $name =~ s/^.*\///; my $vers = 20061212 . "-BETA"; my $home = $ENV{"HOME"}; my $dir = $ENV{"PWD"}; my $user = $ENV{"USER"}; my $cp = 2; # copy-check, do not change our $dirmem; # our is for memory-file ~/.copy-file-torc die "$0 Some odd error $home $user" if !-d $home || !$user; message "Warning!", "Warning! You are ROOT!" if $user eq "root" || $> == 0; # $> is UID my $file = $ARGV[0] if -f $ARGV[0]; # path/to/file from 1. param # help message exit print "$name [$0] $vers\n$lic\n", "Use with Copy-To Nautilus script or similar!\n" if !-f $file || (grep/^-h$|^--help$/, @ARGV); # last-dir from memory file do "$home/.copy-file-torc" if -s "$home/.copy-file-torc" > 37; $dir = $dirmem if -d $dirmem; # directory from mem-file $dir = $ARGV[1] if -d $ARGV[1]; # directory form parameters open_dialog; # run now # dir-select dialog sub open_dialog{ my $dialog = Gtk2::FileChooserDialog -> new("Copy $file to..", undef, "select-folder", "gtk-cancel" => "cancel", "gtk-ok" => "ok"); $dialog -> set_select_multiple(0); #$dialog -> set_current_folder($dir); #bux-fix 1 $dialog -> set_filename($dir); #bug-fix 1 my $response = $dialog -> run; $dir = $dialog -> get_filename; $dialog -> destroy; if ($response eq "cancel"){ message "Canceled", "Copy canceled."; print "Canceled\n"; return 0; } else { die "Not OK\n" if $response ne "ok"; #bug-fix 2 my $filename = $file; $filename =~ s/^.*\///g; my $lock = "ok"; $lock = message "File exists!", "Overwrite $dir/$filename ?" if -f "$dir/$filename"; if ($lock eq "ok"){ print "copy $file -> $dir\n"; die "Not a directory $dir\n" if !-d $dir; # DO NOT REMOVE! $cp = copy $file, $dir if -f $file; my $diff = -s $file - -s "$dir/$filename"; # check sizes exit message "Failed", "Copy failed $file -> $dir" if $cp != 1 || $diff != 0; save_lastdir $dir; } else { save_lastdir $home if !-d $dir; message "Canceled", "Copy canceled."; print "Copy canceled\n"; exit 0; } } } # message dialog sub message{ my $capt = shift; my $msg = shift; my $dialog = Gtk2::Dialog -> new($capt, undef, [qw/modal destroy-with-parent/], "gtk-cancel" => "cancel", "gtk-ok" => "ok"); $dialog -> set_position("center-always"); $dialog -> set_resizable(0); $dialog -> set_skip_taskbar_hint(1); my $vbox = $dialog -> vbox; my $label = Gtk2::Label -> new($msg); $vbox -> pack_start($label, undef, undef, 0); $dialog -> show_all; my $response = $dialog -> run; $dialog -> destroy; return $response; } sub save_lastdir{ my $dir = shift; open FILE, "> $home/.copy-file-torc"; flock FILE, 2; print FILE "#~/.copy-file-torc\n\$dirmem = \"$dir\";"; close FILE; } exit 0; # license: http://creativecommons.org/licenses/nc-sa/1.0/