From 077938670d71c606be2dfce6e5e60a15bc899d59 Mon Sep 17 00:00:00 2001 From: Grant McLean Date: Sun, 29 Nov 2009 16:55:22 +1300 Subject: [PATCH] manage disabling/enabling GNOME/Nautilus media automount on start/stop --- Changes | 3 ++- TODO | 3 --- bin/usb-key-copy-con | 17 ++++++--------- lib/App/USBKeyCopyCon.pm | 45 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 15 deletions(-) diff --git a/Changes b/Changes index e70068f..135157f 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,8 @@ Revision history for App-USBKeyCopyCon -1.01 2009-??-?? +1.01 2009-11-29 - use (gk)sudo for running sub-commands if not running as root + - disable/restore GNOME/Nautilus media automount while running - add command-line option parsing: --no-root-check to skip the check for root permissions --profile-dir to specify location of custom profile scripts diff --git a/TODO b/TODO index cc16f7e..02472da 100644 --- a/TODO +++ b/TODO @@ -3,6 +3,3 @@ - select a sound file (+ test button) - verbose console output -- disable desktop automount on startup -- re-enable desktop automount on shutdown - diff --git a/bin/usb-key-copy-con b/bin/usb-key-copy-con index d644397..d73086a 100755 --- a/bin/usb-key-copy-con +++ b/bin/usb-key-copy-con @@ -85,18 +85,13 @@ blank keys. See L for more details. =head2 Desktop Media Automount -The GNOME desktop environment (and probably others) will try to automatically -mount any USB storage device when it is plugged in. This program is also -mounting and unmounting filesystems so things can get a bit confused. You -probably want to disable this feature in your desktop environment before -running this program. +While this program is running, it attempts to disable the GNOME desktop +environment's media automount behaviour - which would intefere with the copying +process. If you're not running GNOME you may need to manually disable the +equivalent function in your desktop of choice. -To disable automount in GNOME, run this command: - - gconftool-2 --type bool --set \ - /apps/nautilus/preferences/media_automount false - -and to enable it again, use this command: +On exit, the original state of the media automount function should be restored. +In the event of a crash, you can turn it back on with this command: gconftool-2 --type bool --set \ /apps/nautilus/preferences/media_automount true diff --git a/lib/App/USBKeyCopyCon.pm b/lib/App/USBKeyCopyCon.pm index 3916585..e1d8bda 100644 --- a/lib/App/USBKeyCopyCon.pm +++ b/lib/App/USBKeyCopyCon.pm @@ -98,6 +98,7 @@ has 'master_info' => ( is => 'rw' ); has 'options' => ( is => 'rw', default => sub { {} } ); has 'profiles' => ( is => 'rw', default => sub { {} } ); has 'selected_profile' => ( is => 'rw', isa => 'Str', default => '' ); +has 'automount_state' => ( is => 'rw', isa => 'Str', default => undef ); has 'temp_root' => ( is => 'rw', isa => 'Str', default => undef ); has 'master_root' => ( is => 'rw', isa => 'Str', default => undef ); has 'mount_dir' => ( is => 'rw', isa => 'Str', default => undef ); @@ -152,6 +153,8 @@ my %hal_key_map = ( 'linux.sysfs_path' => 'sysfs_path', ); +my $gconf_automount_path = '/apps/nautilus/preferences/media_automount'; + use constant VENDOR_EXACT => 0; use constant VENDOR_PATTERN => 1; use constant VENDOR_ANY => 2; @@ -167,6 +170,7 @@ sub BUILD { $self->set_temp_root('/tmp'); $self->scan_for_profiles; $self->select_profile; + $self->disable_automount; my($path) = __FILE__ =~ m{^(.*)[.]pm$}; $path = File::Spec->rel2abs($path) . "/copy-complete.wav"; @@ -286,6 +290,27 @@ sub check_for_root_user { } +sub disable_automount { + my $self = shift; + + my $state = `gconftool-2 --get $gconf_automount_path 2>/dev/null`; + return if !defined($state) or $? != 0; + + chomp($state); + $self->automount_state($state); + system("gconftool-2 --type bool --set $gconf_automount_path false 2>/dev/null"); +} + + +sub restore_automount { + my $self = shift; + + my $state = $self->automount_state or return; + system("gconftool-2 --type bool --set $gconf_automount_path $state 2>/dev/null"); + +} + + sub build_ui { my $self = shift; @@ -582,6 +607,7 @@ sub fork_copier { $key_info->{pid} = $pid; $key_info->{fh} = $rd; $key_info->{output} = ''; + $key_info->{status} = 0; } @@ -1061,6 +1087,7 @@ sub run { Gtk2->main; + $self->restore_automount; $self->clean_temp_dir; } @@ -1080,6 +1107,12 @@ accessor methods): The main Gtk2::Window object. +=item automount_state + +Stores the enabled state ('true' or 'false') of the GNOME/Nautilus media +automount option. The function will be disabled on startup and this value will +be restored on exit. + =item capacity_combo The Gtk2::ComboBox object for the device filter 'Capacity' drop-down menu. @@ -1329,6 +1362,13 @@ passed to the C method. Called when a 'writer' process exits. Checks the exit status and updates the icon in the key rack (0 = success, non-zero = failure). +=head2 disable_automount ( ) + +This method is called at startup to query GConf for the current GNOME/Nautilus +media automount status ('true'/'false' for enabled/disabled). The current +state is saved and then the value is set to false. The operation should fail +silently in non-GNOME environments. + =head2 disable_filter_inputs ( ) This method is called from C to disable the menu and text @@ -1446,6 +1486,11 @@ Called from the constructor to put the app in the C mode (waiting for the master key to be inserted). Can also be called from the C menu event handler. +=head2 restore_automount ( ) + +This method is called at exit time restore the original GConf setting for the +GNOME/Nautilus media automount function. + =head2 run ( ) This method is called from the wrapper script. It's job is to run the Gtk