Qt

 view release on metacpan or  search on metacpan

qtgui/examples/tutorial/addressbook/part7/AddressBook.pm  view on Meta::CPAN

        $out << [map{ values %{$_} } values %{this->{contacts}}];
        use warnings;
        $file->close();
    }       
    this->updateInterface(NavigationMode);
}

sub loadFromFile
{
    my $fileName = Qt::FileDialog::getOpenFileName(this,
        this->tr('Open Address Book'), '',
        this->tr('Address Book (*.abk);;All Files (*)'));
    if (!$fileName) {
        return;
    }
    else {
        
        my $file = Qt::File($fileName);
        
        if (!$file->open(Qt::IODevice::ReadOnly())) {
            Qt::MessageBox::information(this, this->tr('Unable to open file'),
                $file->errorString());
            return;
        }
        
        my $in = Qt::DataStream($file);
        $in->setVersion(Qt::DataStream::Qt_4_5());
        my @keys;
        my @valuekeys;
        my @valuevalues;
        no warnings qw(void);
        $in >> \@keys;
        $in >> \@valuekeys;
        $in >> \@valuevalues;
        use warnings;
        @{this->{contacts}}{@keys} = map{ {$valuekeys[$_*2] => $valuevalues[$_*2], $valuekeys[$_*2+1] => $valuevalues[$_*2+1]} } 0..((@valuevalues-1)/2);

        if (scalar keys %{this->{contacts}} == 0) {
            Qt::MessageBox::information(this, this->tr('No contacts in file'),
                this->tr('The file you are attempting to open contains no contacts.'));
        } else {
            this->{nameLine}->setText((keys %{this->{contacts}})[0]);
            this->{addressText}->setText((values %{this->{contacts}})[0]->{address});
        }
        $file->close();
    }

    this->updateInterface(NavigationMode);
}

# [export function part1]
sub exportAsVCard
{
    my $name = this->{nameLine}->text();
    my $address = this->{addressText}->toPlainText();
    my $firstName;
    my $lastName;
    my @nameList;

    if ($name =~ m/ /) {
        @nameList = split m/\s+/, $name;
        $firstName = $nameList[0];
        $lastName = $nameList[-1];
    } else {
        $firstName = $name;
        $lastName = '';
    }

    my $fileName = Qt::FileDialog::getSaveFileName(this,
        this->tr('Export Contact'), '',
        this->tr('vCard Files (*.vcf);;All Files (*)'));
        
    if (!$fileName) {
        return;
    }

    my $file = Qt::File($fileName);
# [export function part1]
    
# [export function part2]    
    if (!$file->open(Qt::IODevice::WriteOnly())) {
        Qt::MessageBox::information(this, this->tr('Unable to open file'),
            $file->errorString());
        return;
    }

    my $out = Qt::TextStream($file);
# [export function part2]

# [export function part3]
    no warnings qw(void);
    $out << "BEGIN:VCARD\n";
    $out << "VERSION:2.1\n";
    $out << "N:$lastName;$firstName\n";
        
    if (@nameList) {  
       $out << 'FN:' . join( ' ', @nameList ) . "\n";
    }
    else {
       $out << 'FN:' . $firstName . "\n";
    }
# [export function part3] 

# [export function part4]
    $address =~ s/;/\\;/g;
    $address =~ s/\n/;/g;
    $address =~ s/,/ /g;

    $out << 'ADR;HOME:;' . $address . "\n";
    $out << 'END:VCARD' . "\n";

    Qt::MessageBox::information(this, this->tr('Export Successful'),
        sprintf this->tr('\'%s\' has been exported as a vCard.'), $name);
    $file->close();
}
# [export function part4]

1;



( run in 0.693 second using v1.01-cache-2.11-cpan-71847e10f99 )