Gtk2-Ex-Email-AddressVBox

 view release on metacpan or  search on metacpan

lib/Gtk2/Ex/Email/AddressVBox.pm  view on Meta::CPAN


sub new{
	my $self={error=>undef,
			  perror=>undef,
			  errorString=>undef,
			  gui=>{
					hboxes=>{},
					},
			  };
	bless $self;
	
	return $self;
}

=head2 addHB

This adds a new HBox to the VBox. The HBox contains
the stuff for a single address.

Two option arguements are taken the first is the type to add
and the second is the address.

    #adds a new blank one
    $avd->addHB();

    #adds a new blank To one
    $avd->addHB('to');

    #adds a new blank CC one
    $avd->addHB('cc');

    #adds a new blank BCC one
    $avd->addHB('bcc');

    #adds a new blank To one set to foo@bar
    $avd->addHB('to', 'foo@bar');

=cut

sub addHB{
	my $self=$_[0];
	my $type=$_[1];
	my $address=$_[2];

	my %hbox;

	#this is the ID of it this specific one... zero is the top and they increase going down
	$hbox{id}=0;

	#once this is set to true, it means that a new one has been added and should not be done again
	$hbox{new}=0;

	#sets the id
	$hbox{id}=rand().rand().rand();

	#creates the first HB
	$hbox{hbox}=Gtk2::HBox->new;
	$hbox{hbox}->show;
	
	#creates the combo box to display to, cc, and bcc
	$hbox{combobox}=Gtk2::ComboBox->new_text;
	$hbox{combobox}->show;
	$hbox{combobox}->append_text('To: ');
	$hbox{combobox}->append_text('CC: ');
	$hbox{combobox}->append_text('BCC: ');
	$hbox{hbox}->pack_start($hbox{combobox}, 0, 1, 0);

	#sets the type
	my $typeSet=undef;
	if (defined($type)) {
		if ($type eq 'to') {
			$hbox{combobox}->set_active(0);
		}
		if ($type eq 'cc') {
			$hbox{combobox}->set_active(1);
		}
		if ($type eq 'bcc') {
			$hbox{combobox}->set_active(2);
		}
	}
	if (!$typeSet) {
		$hbox{combobox}->set_active(0);
	}

	#adds the entry box
	$hbox{entry}=Gtk2::Entry->new;
	$hbox{entry}->show;
	$hbox{hbox}->pack_start($hbox{entry}, 1, 1, 1);

	#sets the address if needed
	if (defined($address)) {
		$hbox{entry}->set_text($address);
	}

	#
	$hbox{entry}->signal_connect (changed => sub {
									  if ($_[1]{self}{gui}{hboxes}{ $_[1]{id} }{new} eq '0'){
										  $_[1]{self}->addHB;
										  $_[1]{self}{gui}{hboxes}{ $_[1]{id} }{new}=1;
									  }
								  },
								  {
								   self=>$self,
								   id=>$hbox{id},
								   }
								  );

	#adds the delete button to the hbox
	$hbox{del}=Gtk2::Button->new;
	$hbox{del}->show;
	$hbox{delLabel}=Gtk2::Label->new('del');
	$hbox{delLabel}->show;
	$hbox{del}->add($hbox{delLabel});
	$hbox{hbox}->pack_start($hbox{del}, 0, 1, 1);
	$hbox{del}->signal_connect(clicked=>sub{
								   my @HBIDs=keys(%{ $self->{gui}{hboxes} });
								   if (defined($HBIDs[1])) {
									   $self->{gui}{hboxes}{ $_[1]{id} }{hbox}->destroy;
									   delete($self->{gui}{hboxes}{ $_[1]{id} });
								   }else {
									   $self->{gui}{hboxes}{ $_[1]{id} }{entry}->set_text('');
								   }
							   },
							   {
								self=>$self,
								id=>$hbox{id},
								}
							   );

	#saves the hbox info
	$self->{gui}{hboxes}{ $hbox{id} }=\%hbox;

	$self->{gui}{vbox}->pack_start($hbox{hbox}, 0, 1, 1);

	return $hbox{id};
}

=head2 getAddresses

This gets the the addresses users have entered. Any that match
'' or /^ *$/ will be ignored.

    my %addresses=$avb->getAddresses;
    if(defined($addresses{to})){
        print 'To: '.join(' ', @{$addresses{to}});
    }
    if(defined($addresses{cc})){
        print 'CC: '.join(' ', @{$addresses{cc}});
    }
    if(defined($addresses{bcc})){
        print 'BCC: '.join(' ', @{$addresses{bcc}});
    }

=cut

sub getAddresses{
	my $self=$_[0];

	#this will be returned
	my %addresses;

	#these will be added later
	my @to;
	my @cc;
	my @bcc;

	my @hboxes=keys(%{$self->{gui}{hboxes}});

	#processes each one
	my $int=0;
	while (defined($hboxes[$int])) {
		my $address=$self->{gui}{hboxes}{ $hboxes[$int] }{entry}->get_text;

		my $typeInt=$self->{gui}{hboxes}{ $hboxes[$int] }{combobox}->get_active;
		
		#figures out the type
		my $type;
		if ($typeInt eq '0') {
			$type='to';
		}
		if ($typeInt eq '1') {
			$type='cc';
		}
		if ($typeInt eq '2') {
			$type='bcc';
		}

		#controls if it will be added or not
		my $add=1;

		#if it is blank do not add it
		if ($address eq '') {
			$add=undef;
		}

		#if it is all spaces do not add it
		if ($address=~/^ *$/) {
			$add=undef;
		}

		#add it
		if ($add) {
			if ($type eq 'to') {
				push(@to, $address);
			}
			if ($type eq 'cc') {
				push(@cc, $address);
			}
			if ($type eq 'bcc') {
				push(@bcc, $address);
			}
		}

		$int++;
	}

	#adds them to the hash
	if (defined($to[0])) {
		$addresses{to}=\@to;
	}
	if (defined($cc[0])) {
		$addresses{cc}=\@cc;
	}
	if (defined($bcc[0])) {
		$addresses{bcc}=\@bcc;
	}

	return %addresses;
}

=head2 vbox

This creates the VBox that contains the widgets.



( run in 1.259 second using v1.01-cache-2.11-cpan-8dfa8b56332 )