App-USBKeyCopyCon

 view release on metacpan or  search on metacpan

lib/App/USBKeyCopyCon.pm  view on Meta::CPAN

        sleep(2);
        $ENV{USB_BLOCK_DEVICE} = $key_info->{block_device};
        $ENV{USB_MOUNT_DIR}    = $self->mount_dir . "/$key_info->{dev}";
        $ENV{USB_MASTER_ROOT}  = $self->master_root;
        $ENV{USB_VOLUME_NAME}  = $self->volume_label;
        mkpath($ENV{USB_MOUNT_DIR}) if not -d $ENV{USB_MOUNT_DIR};
        close($rd);
        close STDOUT;
        open STDOUT, '>&', $wr or die "error reopening STDOUT: $!";
        close STDERR;
        open STDERR, '>&', $wr or die "error reopening STDERR: $!";
        my $command = $self->sudo_wrap(
            $self->writer_script,
            qw(USB_BLOCK_DEVICE USB_MOUNT_DIR USB_MASTER_ROOT USB_VOLUME_NAME),
        );
        exec($command) or die "Error starting copy script: $!";
        exit; # never reached;
    }
    close($wr);
    $rd->blocking(0);
    Glib::IO->add_watch(
        fileno($rd), ['in', 'err', 'hup'],
        sub { $self->on_copier_pipe_read(@_); },
        $key_info->{udi}
    );
    $key_info->{pid}    = $pid;
    $key_info->{fh}     = $rd;
    $key_info->{output} = '';
    $key_info->{status} = 0;
}


sub on_copier_pipe_read {
    my($self, $fd, $cond, $udi) = @_;

    my $key_info = $self->current_keys->{$udi} or return FALSE;
    my $fh = $key_info->{fh};
    my $buffer;
    if(sysread($fh, $buffer, 100000)) {
        $key_info->{output} .= $buffer;
        if($key_info->{output} =~ m/\A.*^\{(\d+)\/(\d+)\}/sm) {
            $self->update_key_progress($udi, int(9 * $1 / $2));
        }
        return TRUE;
    }
    close($fh);
    delete $key_info->{fh};
    return FALSE;
}


sub remove_key_from_rack {
    my($self, $udi) = @_;

    my $key_info = delete $self->current_keys->{$udi} or return;
    $self->key_rack->remove($key_info->{widget});
    return;
}


sub update_key_progress {
    my($self, $udi, $status) = @_;

    $status = -1 if !defined $status or $status < -1 or $status > 10;

    my $key_info = $self->current_keys->{$udi} or return;
    $key_info->{status} = $status;
    $key_info->{icon_widget}->set_from_pixbuf(
        App::USBKeyCopyCon::Chrome::usb_icon($status)
    );
}


sub on_menu_file_new {
    my $self = shift;
    $self->require_master_key;
}


sub on_menu_file_quit {
    my $self = shift;
    # TODO: check for work in progress
    # TODO: check if desktop automount should be re-enabled
    Gtk2->main_quit;
}


sub on_menu_edit_preferences {
    my $self = shift;
    $self->say("Edit>Preferences - not implemented\n");
}


sub on_menu_help_about {
    my $self = shift;

    my $dialog = Gtk2::Dialog->new(
        'About: usb-key-copy-con',
        $self->app_win,
        [qw/modal destroy-with-parent/],
        'gtk-close' => 'ok',
    );
    $dialog->set_default_size (90, 80);

    my $panel = Gtk2::VBox->new(FALSE, 12);

    my $title = Gtk2::Label->new;
    $title->set_markup("<span font_desc='sans 20'> USB Key Copy Console </span>");
    $title->set_selectable(TRUE);
    $panel->pack_start($title, FALSE, FALSE, 10);

    my $version = Gtk2::Label->new;
    $version->set_markup("<span font_desc='sans 16'>Version: $VERSION</span>");
    $version->set_selectable(TRUE);
    $panel->pack_start($version, FALSE, FALSE, 0);

    my $author = Gtk2::Label->new;
    my $detail = '(c) 2009 Grant McLean &lt;grantm@cpan.org&gt;';
    $author->set_markup("  <span font_desc='sans 10'>$detail</span>  ");
    $author->set_selectable(TRUE);
    $panel->pack_start($author, FALSE, FALSE, 10);



( run in 1.319 second using v1.01-cache-2.11-cpan-e1769b4cff6 )