iPodDB::Menu::File - the file menu


iPodDB documentation Contained in the iPodDB distribution.

Index


Code Index:

NAME

Top

iPodDB::Menu::File - the file menu

SYNOPSIS

Top

    my $file = iPodDB::Menu::File->new( $frame );

DESCRIPTION

Top

This is the File menu portion of the menu bar. It is also a popup menu when a user right-clicks a song.

METHODS

Top

new( $frame )

Creates the menu and sets up the callbacks when menu items are clicked.

EVENTS

Top

on_copyto( )

When the "Copy To..." option is selected this event is triggered. It will popup a dialog asking the user to select a destination directory, then a progress dialog to show them the progress of the copy operation.

on_copy( )

When the "Copy" option is selected this event is triggered. It simply copies the list of selected files to the clipboard. Thus, a user can do a paste operation in to any folder they desire.

AUTHOR

Top

* Brian Cassidy <bricas@cpan.org>

COPYRIGHT AND LICENSE

Top


iPodDB documentation Contained in the iPodDB distribution.
package iPodDB::Menu::File;

use base qw( Wx::Menu );
use Wx qw( wxOK wxID_OK wxTheClipboard wxPD_CAN_ABORT wxPD_APP_MODAL wxYES_NO wxNO_DEFAULT wxICON_EXCLAMATION wxID_YES );
use Wx::DND;

use strict;
use warnings;

use iPodDB::Songlist qw( song_to_path );

use File::Copy;
use Path::Class;

our $VERSION = '0.03';

sub new {
    my $class  = shift;
    my $parent = shift;
    my $self   = $class->SUPER::new;

    bless $self, $class;

    $self->Append( my $copyto_id = Wx::NewId, "&Copy To...\tCtrl-T", 'Copy files to a new location' );
    $self->Append( my $copy_id   = Wx::NewId, "&Copy\tCtrl-C", 'Copy files to the clipboard' );

    unless( $parent->songlist and $parent->songlist->GetSelectedItemCount ) {
        $self->Enable( $copyto_id, 0 );
        $self->Enable( $copy_id, 0 );
    }

    $parent->EVT_MENU( $copyto_id, \&on_copyto );
    $parent->EVT_MENU( $copy_id, \&on_copy );

    return $self;
}

sub on_copyto {
    my $self     = shift;
    my $songlist = $self->songlist;
    my $path     = dir( $self->preferences->mountpoint );

    return unless $songlist->GetSelectedItemCount;

    my $dialog = Wx::DirDialog->new( $self, 'Choose a destination directory' );

    return unless $dialog->ShowModal == wxID_OK;

    my $dpath    = dir( $dialog->GetPath );
    my $text     = "Copying files to $dpath:\n%s";
    my $progress = Wx::ProgressDialog->new( 'Copying Files...', sprintf( $text, '' ), $songlist->GetSelectedItemCount, $self, wxPD_CAN_ABORT | wxPD_APP_MODAL );

    my $i = 0;
    for my $song ( $songlist->as_songobject ) {
        my $source      = song_to_path( $path, $song );
        my $file        = $source->basename;
        my $destination = $dpath->file( $file );

        last unless $progress->Update( $i++, sprintf( $text, $file ) );

        if( -e $destination ) {
            next unless Wx::MessageDialog->new(
                $self,
                "This folder already contains a file named '$file'.\nWould you like to replace the existing file?",
                'Confirm File Replace',
                wxYES_NO | wxNO_DEFAULT | wxICON_EXCLAMATION
            )->ShowModal == wxID_YES;

            unless( unlink $destination ) {
                Wx::MessageDialog->new( $self, "Cannot delete $destination!", 'Error', wxOK )->ShowModal;
                next;
            }
        }


        eval{ copy( $source, $destination ); };

        if( $@ ) {
            Wx::MessageDialog->new( $self, "Cannot copy file: $@", 'Error', wxOK )->ShowModal;
            last;
        }
    }
    $progress->Destroy;
}

sub on_copy {
    my $self       = shift;
    my $songlist   = $self->songlist;
    my $path       = dir( $self->preferences->mountpoint );

    return unless $songlist->GetSelectedItemCount;

    my $files      = $songlist->as_filedataobject;

    wxTheClipboard->Open;
    wxTheClipboard->SetData( $files );
    wxTheClipboard->Close;
}

1;