Skip to content

Commit

Permalink
manage disabling/enabling GNOME/Nautilus media automount on start/stop
Browse files Browse the repository at this point in the history
  • Loading branch information
grantm committed Nov 29, 2009
1 parent 4c72991 commit 0779386
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 15 deletions.
3 changes: 2 additions & 1 deletion 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
Expand Down
3 changes: 0 additions & 3 deletions TODO
Expand Up @@ -3,6 +3,3 @@
- select a sound file (+ test button)
- verbose console output

- disable desktop automount on startup
- re-enable desktop automount on shutdown

17 changes: 6 additions & 11 deletions bin/usb-key-copy-con
Expand Up @@ -85,18 +85,13 @@ blank keys. See L<App::USBKeyCopyCon> 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
Expand Down
45 changes: 45 additions & 0 deletions lib/App/USBKeyCopyCon.pm
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -582,6 +607,7 @@ sub fork_copier {
$key_info->{pid} = $pid;
$key_info->{fh} = $rd;
$key_info->{output} = '';
$key_info->{status} = 0;
}


Expand Down Expand Up @@ -1061,6 +1087,7 @@ sub run {

Gtk2->main;

$self->restore_automount;
$self->clean_temp_dir;
}

Expand All @@ -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.
Expand Down Expand Up @@ -1329,6 +1362,13 @@ passed to the C<start_master_read> 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<require_master_key> to disable the menu and text
Expand Down Expand Up @@ -1446,6 +1486,11 @@ Called from the constructor to put the app in the C<MASTER-WAIT> mode (waiting
for the master key to be inserted). Can also be called from the
C<on_menu_file_new> 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
Expand Down

0 comments on commit 0779386

Please sign in to comment.