AddressBook

 view release on metacpan or  search on metacpan

lib/AddressBook/DB/PDB.pm  view on Meta::CPAN


Any of these options can be specified in the constructor, or in the configuration file.

=cut

use strict;
use PDA::Pilot;
use AddressBook;
use Carp;
use Date::Manip;
use vars qw($VERSION @ISA);

$VERSION = '0.13';

@ISA = qw(AddressBook);

=head2 new

  $a = AddressBook->new(source => "PDB");
  $a = AddressBook->new(source => "PDB",
                        port => "/dev/pilot");

If a "pdb" parameter is supplied, it will be used as a reference to an already
created PDA::Pilot::DLP::DBPtr object.  Otherwise, if a "port" is supplied,
the user will be prompted to press the hotsync button to establish the connection.

=cut

sub new {
  my $class = shift;
  my $self = {};
  bless ($self,$class);
  my %args = @_;
  foreach (keys %args) {
    $self->{$_} = $args{$_};
  }
  if (! $self->{pdb}) {
    if (! $self->{dlp}) {
      my $socket = PDA::Pilot::openPort($self->{port});
      print "Now press the HotSync button\n";
      $self->{dlp} = PDA::Pilot::accept($socket);
    } 
    $self->{pdb} = $self->{dlp}->open("AddressDB");
  }
  if (! $self->{pdb}) {
    croak "Error: No port, dlp, or pdb specified";
  }
  $self->reset;
  unless (defined $self->{intra_attr_sep_char}) {
    $self->{intra_attr_sep_char} = ' & ';
  }
  return $self;

#   croak("PDB file backends are not currently implemented");  
#   if (-e $self->{file}) {
#     $self->{pdb} = PDA::Pilot::File::open($self->{file};
#   } else {
#      croak("File: ".$self->file." does not exist");
#      my $info;
#      $info->{name}="AddressDB";
#      $info->{type}="DATA";
#      $self->{pdb} = PDA::Pilot::File::create($self->{file},$info);
#   }

}

sub reset {
  my $self = shift;
  my $class = ref $self || croak "Not a method call";
  $self->{index} = 0;
  $self->_read_appinfo;
  #$self->_remove_deleted_records;
}

sub read {
  my $self = shift;
  my $class = ref $self || croak "Not a method call";
  my %reverse_category_hash = reverse %{$self->{category_hash}};
  my ($i,%attr,$found);
  if ($self->{index} < $self->{pdb}->getRecords) {
    my $record = PDA::Pilot::Address::Unpack($self->{pdb}->getRecord($self->{index}++));
    if ($record->{deleted}) {
      $self->{pdb}->deleteRecord($record->{id});
      return ($self->read);
    }
    my %labels = %{$self->_insert_phone_labels($record->{phoneLabel})};
    for ($i=0;$i<=$#{$record->{entry}};$i++) {
      if (defined $record->{entry}->[$i]) {
	@{$attr{$labels{$i}}} = split /$self->{intra_attr_sep_char}/ ,$record->{entry}->[$i];
      }
    }
    my $entry = AddressBook::Entry->new(config=>$self->{config},
				        db=>$self->{db_name},
				        attr=>\%attr);
    $entry->add(db=>$self->{db_name},
		attr=>{category=>$reverse_category_hash{$record->{category}}});
    if ($record->{modified}) {$entry->{timestamp} = ParseDate("Today")}
    else {$entry->{timestamp} = 0}
    return $entry;
  } else {
    return undef;
  }
}

sub _insert_phone_labels {
  my $self = shift;
  my $class = ref $self || croak "Not a method call";
  my ($phoneLayout) = @_;
  my $i;
  my %labels = reverse %{$self->{field_labels}};
  my %phone_labels = reverse %{$self->{phone_labels}};
  for ($i=0;$i<=4;$i++) {
    $labels{$i+3} = $phone_labels{$phoneLayout->[$i]};
  }
  return \%labels;
}

sub write {
  my $self = shift;
  my $class = ref $self || croak "Not a method call";
  my $entry = shift;



( run in 0.851 second using v1.01-cache-2.11-cpan-140bd7fdf52 )