| 1 | package Gtk2::Ex::Email::AddressVBox; | ||
| 2 | |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Gtk2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Gtk2::Ex::Email::AddressVBox - Creates a VBox for handling email addresses similar to Claws-Mail. | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | Version 0.0.1 | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | our $VERSION = '0.0.1'; | ||
| 18 | |||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use Gtk2::Ex::Email::AddressVBox; | ||
| 23 | use Gtk2; | ||
| 24 | use Data::Dumper; | ||
| 25 | |||
| 26 | Gtk2->init; | ||
| 27 | |||
| 28 | #init it | ||
| 29 | my $avb=Gtk2::Ex::Email::AddressVBox->new(); | ||
| 30 | |||
| 31 | #get the VBox and add it | ||
| 32 | my $vbox=Gtk2::VBox->new; | ||
| 33 | $vbox->show; | ||
| 34 | my $vbox2=$avb->vbox; | ||
| 35 | $vbox->pack_start($vbox2, 1, 1, 1); | ||
| 36 | |||
| 37 | #adds a button that calls getAddresses | ||
| 38 | my $button=Gtk2::Button->new; | ||
| 39 | $button->show; | ||
| 40 | my $buttonLabel=Gtk2::Label->new('get addresses'); | ||
| 41 | $buttonLabel->show; | ||
| 42 | $button->add($buttonLabel); | ||
| 43 | $vbox->pack_start($button, 1, 1, 1); | ||
| 44 | $button->signal_connect(activated=>{ | ||
| 45 | my %addresses=$avb->getAddresses; | ||
| 46 | print Dumper(\%addresses); | ||
| 47 | } | ||
| 48 | ); | ||
| 49 | |||
| 50 | #add the VBox to the window | ||
| 51 | my $window=Gtk2::Window->new; | ||
| 52 | $window->add($vbox); | ||
| 53 | $window->show; | ||
| 54 | |||
| 55 | #run it | ||
| 56 | Gtk2->main; | ||
| 57 | |||
| 58 | =head1 METHODS | ||
| 59 | |||
| 60 | =head2 new | ||
| 61 | |||
| 62 | This initiates the object. | ||
| 63 | |||
| 64 | my $avb=Gtk2::Ex::Email::AddressVBox->new(); | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | sub new{ | ||
| 69 | my $self={error=>undef, | ||
| 70 | perror=>undef, | ||
| 71 | errorString=>undef, | ||
| 72 | gui=>{ | ||
| 73 | hboxes=>{}, | ||
| 74 | }, | ||
| 75 | }; | ||
| 76 | bless $self; | ||
| 77 | |||
| 78 | return $self; | ||
| 79 | } | ||
| 80 | |||
| 81 | =head2 addHB | ||
| 82 | |||
| 83 | This adds a new HBox to the VBox. The HBox contains | ||
| 84 | the stuff for a single address. | ||
| 85 | |||
| 86 | Two option arguements are taken the first is the type to add | ||
| 87 | and the second is the address. | ||
| 88 | |||
| 89 | #adds a new blank one | ||
| 90 | $avd->addHB(); | ||
| 91 | |||
| 92 | #adds a new blank To one | ||
| 93 | $avd->addHB('to'); | ||
| 94 | |||
| 95 | #adds a new blank CC one | ||
| 96 | $avd->addHB('cc'); | ||
| 97 | |||
| 98 | #adds a new blank BCC one | ||
| 99 | $avd->addHB('bcc'); | ||
| 100 | |||
| 101 | #adds a new blank To one set to foo@bar | ||
| 102 | $avd->addHB('to', 'foo@bar'); | ||
| 103 | |||
| 104 | =cut | ||
| 105 | |||
| 106 | sub addHB{ | ||
| 107 | my $self=$_[0]; | ||
| 108 | my $type=$_[1]; | ||
| 109 | my $address=$_[2]; | ||
| 110 | |||
| 111 | my %hbox; | ||
| 112 | |||
| 113 | #this is the ID of it this specific one... zero is the top and they increase going down | ||
| 114 | $hbox{id}=0; | ||
| 115 | |||
| 116 | #once this is set to true, it means that a new one has been added and should not be done again | ||
| 117 | $hbox{new}=0; | ||
| 118 | |||
| 119 | #sets the id | ||
| 120 | $hbox{id}=rand().rand().rand(); | ||
| 121 | |||
| 122 | #creates the first HB | ||
| 123 | $hbox{hbox}=Gtk2::HBox->new; | ||
| 124 | $hbox{hbox}->show; | ||
| 125 | |||
| 126 | #creates the combo box to display to, cc, and bcc | ||
| 127 | $hbox{combobox}=Gtk2::ComboBox->new_text; | ||
| 128 | $hbox{combobox}->show; | ||
| 129 | $hbox{combobox}->append_text('To: '); | ||
| 130 | $hbox{combobox}->append_text('CC: '); | ||
| 131 | $hbox{combobox}->append_text('BCC: '); | ||
| 132 | $hbox{hbox}->pack_start($hbox{combobox}, 0, 1, 0); | ||
| 133 | |||
| 134 | #sets the type | ||
| 135 | my $typeSet=undef; | ||
| 136 | if (defined($type)) { | ||
| 137 | if ($type eq 'to') { | ||
| 138 | $hbox{combobox}->set_active(0); | ||
| 139 | } | ||
| 140 | if ($type eq 'cc') { | ||
| 141 | $hbox{combobox}->set_active(1); | ||
| 142 | } | ||
| 143 | if ($type eq 'bcc') { | ||
| 144 | $hbox{combobox}->set_active(2); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | if (!$typeSet) { | ||
| 148 | $hbox{combobox}->set_active(0); | ||
| 149 | } | ||
| 150 | |||
| 151 | #adds the entry box | ||
| 152 | $hbox{entry}=Gtk2::Entry->new; | ||
| 153 | $hbox{entry}->show; | ||
| 154 | $hbox{hbox}->pack_start($hbox{entry}, 1, 1, 1); | ||
| 155 | |||
| 156 | #sets the address if needed | ||
| 157 | if (defined($address)) { | ||
| 158 | $hbox{entry}->set_text($address); | ||
| 159 | } | ||
| 160 | |||
| 161 | # | ||
| 162 | $hbox{entry}->signal_connect (changed => sub { | ||
| 163 | if ($_[1]{self}{gui}{hboxes}{ $_[1]{id} }{new} eq '0'){ | ||
| 164 | $_[1]{self}->addHB; | ||
| 165 | $_[1]{self}{gui}{hboxes}{ $_[1]{id} }{new}=1; | ||
| 166 | } | ||
| 167 | }, | ||
| 168 | { | ||
| 169 | self=>$self, | ||
| 170 | id=>$hbox{id}, | ||
| 171 | } | ||
| 172 | ); | ||
| 173 | |||
| 174 | #adds the delete button to the hbox | ||
| 175 | $hbox{del}=Gtk2::Button->new; | ||
| 176 | $hbox{del}->show; | ||
| 177 | $hbox{delLabel}=Gtk2::Label->new('del'); | ||
| 178 | $hbox{delLabel}->show; | ||
| 179 | $hbox{del}->add($hbox{delLabel}); | ||
| 180 | $hbox{hbox}->pack_start($hbox{del}, 0, 1, 1); | ||
| 181 | $hbox{del}->signal_connect(clicked=>sub{ | ||
| 182 | my @HBIDs=keys(%{ $self->{gui}{hboxes} }); | ||
| 183 | if (defined($HBIDs[1])) { | ||
| 184 | $self->{gui}{hboxes}{ $_[1]{id} }{hbox}->destroy; | ||
| 185 | delete($self->{gui}{hboxes}{ $_[1]{id} }); | ||
| 186 | }else { | ||
| 187 | $self->{gui}{hboxes}{ $_[1]{id} }{entry}->set_text(''); | ||
| 188 | } | ||
| 189 | }, | ||
| 190 | { | ||
| 191 | self=>$self, | ||
| 192 | id=>$hbox{id}, | ||
| 193 | } | ||
| 194 | ); | ||
| 195 | |||
| 196 | #saves the hbox info | ||
| 197 | $self->{gui}{hboxes}{ $hbox{id} }=\%hbox; | ||
| 198 | |||
| 199 | $self->{gui}{vbox}->pack_start($hbox{hbox}, 0, 1, 1); | ||
| 200 | |||
| 201 | return $hbox{id}; | ||
| 202 | } | ||
| 203 | |||
| 204 | =head2 getAddresses | ||
| 205 | |||
| 206 | This gets the the addresses users have entered. Any that match | ||
| 207 | '' or /^ *$/ will be ignored. | ||
| 208 | |||
| 209 | my %addresses=$avb->getAddresses; | ||
| 210 | if(defined($addresses{to})){ | ||
| 211 | print 'To: '.join(' ', @{$addresses{to}}); | ||
| 212 | } | ||
| 213 | if(defined($addresses{cc})){ | ||
| 214 | print 'CC: '.join(' ', @{$addresses{cc}}); | ||
| 215 | } | ||
| 216 | if(defined($addresses{bcc})){ | ||
| 217 | print 'BCC: '.join(' ', @{$addresses{bcc}}); | ||
| 218 | } | ||
| 219 | |||
| 220 | =cut | ||
| 221 | |||
| 222 | sub getAddresses{ | ||
| 223 | my $self=$_[0]; | ||
| 224 | |||
| 225 | #this will be returned | ||
| 226 | my %addresses; | ||
| 227 | |||
| 228 | #these will be added later | ||
| 229 | my @to; | ||
| 230 | my @cc; | ||
| 231 | my @bcc; | ||
| 232 | |||
| 233 | my @hboxes=keys(%{$self->{gui}{hboxes}}); | ||
| 234 | |||
| 235 | #processes each one | ||
| 236 | my $int=0; | ||
| 237 | while (defined($hboxes[$int])) { | ||
| 238 | my $address=$self->{gui}{hboxes}{ $hboxes[$int] }{entry}->get_text; | ||
| 239 | |||
| 240 | my $typeInt=$self->{gui}{hboxes}{ $hboxes[$int] }{combobox}->get_active; | ||
| 241 | |||
| 242 | #figures out the type | ||
| 243 | my $type; | ||
| 244 | if ($typeInt eq '0') { | ||
| 245 | $type='to'; | ||
| 246 | } | ||
| 247 | if ($typeInt eq '1') { | ||
| 248 | $type='cc'; | ||
| 249 | } | ||
| 250 | if ($typeInt eq '2') { | ||
| 251 | $type='bcc'; | ||
| 252 | } | ||
| 253 | |||
| 254 | #controls if it will be added or not | ||
| 255 | my $add=1; | ||
| 256 | |||
| 257 | #if it is blank do not add it | ||
| 258 | if ($address eq '') { | ||
| 259 | $add=undef; | ||
| 260 | } | ||
| 261 | |||
| 262 | #if it is all spaces do not add it | ||
| 263 | if ($address=~/^ *$/) { | ||
| 264 | $add=undef; | ||
| 265 | } | ||
| 266 | |||
| 267 | #add it | ||
| 268 | if ($add) { | ||
| 269 | if ($type eq 'to') { | ||
| 270 | push(@to, $address); | ||
| 271 | } | ||
| 272 | if ($type eq 'cc') { | ||
| 273 | push(@cc, $address); | ||
| 274 | } | ||
| 275 | if ($type eq 'bcc') { | ||
| 276 | push(@bcc, $address); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | |||
| 280 | $int++; | ||
| 281 | } | ||
| 282 | |||
| 283 | #adds them to the hash | ||
| 284 | if (defined($to[0])) { | ||
| 285 | $addresses{to}=\@to; | ||
| 286 | } | ||
| 287 | if (defined($cc[0])) { | ||
| 288 | $addresses{cc}=\@cc; | ||
| 289 | } | ||
| 290 | if (defined($bcc[0])) { | ||
| 291 | $addresses{bcc}=\@bcc; | ||
| 292 | } | ||
| 293 | |||
| 294 | return %addresses; | ||
| 295 | } | ||
| 296 | |||
| 297 | =head2 vbox | ||
| 298 | |||
| 299 | This creates the VBox that contains the widgets. | ||
| 300 | |||
| 301 | =head3 args hash | ||
| 302 | |||
| 303 | =head4 bcc | ||
| 304 | |||
| 305 | This is a array of BCC addresses. | ||
| 306 | |||
| 307 | =head4 cc | ||
| 308 | |||
| 309 | This is a array of CC addresses. | ||
| 310 | |||
| 311 | =head4 to | ||
| 312 | |||
| 313 | This is a array of To addresses. | ||
| 314 | |||
| 315 | my $vbox=$avb->vbox({ | ||
| 316 | to=>['foo@bar'], | ||
| 317 | }); | ||
| 318 | |||
| 319 | =cut | ||
| 320 | |||
| 321 | sub vbox{ | ||
| 322 | my $self=$_[0]; | ||
| 323 | my %args; | ||
| 324 | if(defined($_[1])){ | ||
| 325 | %args= %{$_[1]}; | ||
| 326 | } | ||
| 327 | |||
| 328 | #this is what will be returned | ||
| 329 | $self->{gui}{vbox}=Gtk2::VBox->new; | ||
| 330 | $self->{gui}{vbox}->show; | ||
| 331 | |||
| 332 | #adds any To if needed | ||
| 333 | my $int=0; | ||
| 334 | if (defined($args{to})) { | ||
| 335 | if (defined($args{to}[0])) { | ||
| 336 | while (defined($args{to}[$int])) { | ||
| 337 | $self->addHB('to', $args{to}[$int]); | ||
| 338 | |||
| 339 | $int++; | ||
| 340 | } | ||
| 341 | } | ||
| 342 | } | ||
| 343 | |||
| 344 | #adds any CC if needed | ||
| 345 | if (defined($args{cc})) { | ||
| 346 | $int=0; | ||
| 347 | if (defined($args{cc}[0])) { | ||
| 348 | while (defined($args{cc}[$int])) { | ||
| 349 | $self->addHB('cc', $args{cc}[$int]); | ||
| 350 | |||
| 351 | $int++; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | #adds any BCC if needed | ||
| 357 | if (defined($args{bcc})) { | ||
| 358 | $int=0; | ||
| 359 | if (defined($args{bcc}[0])) { | ||
| 360 | while (defined($args{bcc}[$int])) { | ||
| 361 | $self->addHB('bcc', $args{bcc}[$int]); | ||
| 362 | |||
| 363 | $int++; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | #adds the final HBox | ||
| 369 | $self->addHB; | ||
| 370 | |||
| 371 | return $self->{gui}{vbox}; | ||
| 372 | } | ||
| 373 | |||
| 374 | =head1 AUTHOR | ||
| 375 | |||
| 376 | Zane C. Bowers, C<< <vvelox at vvelox.net> >> | ||
| 377 | |||
| 378 | =head1 BUGS | ||
| 379 | |||
| 380 | Please report any bugs or feature requests to C<bug-gtk2-ex-email-addressvbox at rt.cpan.org>, or through | ||
| 381 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Gtk2-Ex-Email-AddressVBox>. I will be notified, and then you'll | ||
| 382 | automatically be notified of progress on your bug as I make changes. | ||
| 383 | |||
| 384 | |||
| 385 | |||
| 386 | |||
| 387 | =head1 SUPPORT | ||
| 388 | |||
| 389 | You can find documentation for this module with the perldoc command. | ||
| 390 | |||
| 391 | perldoc Gtk2::Ex::Email::AddressVBox | ||
| 392 | |||
| 393 | |||
| 394 | You can also look for information at: | ||
| 395 | |||
| 396 | =over 4 | ||
| 397 | |||
| 398 | =item * RT: CPAN's request tracker | ||
| 399 | |||
| 400 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Gtk2-Ex-Email-AddressVBox> | ||
| 401 | |||
| 402 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 403 | |||
| 404 | L<http://annocpan.org/dist/Gtk2-Ex-Email-AddressVBox> | ||
| 405 | |||
| 406 | =item * CPAN Ratings | ||
| 407 | |||
| 408 | L<http://cpanratings.perl.org/d/Gtk2-Ex-Email-AddressVBox> | ||
| 409 | |||
| 410 | =item * Search CPAN | ||
| 411 | |||
| 412 | L<http://search.cpan.org/dist/Gtk2-Ex-Email-AddressVBox/> | ||
| 413 | |||
| 414 | =back | ||
| 415 | |||
| 416 | |||
| 417 | =head1 ACKNOWLEDGEMENTS | ||
| 418 | |||
| 419 | |||
| 420 | =head1 COPYRIGHT & LICENSE | ||
| 421 | |||
| 422 | Copyright 2009 Zane C. Bowers, all rights reserved. | ||
| 423 | |||
| 424 | This program is free software; you can redistribute it and/or modify it | ||
| 425 | under the same terms as Perl itself. | ||
| 426 | |||
| 427 | |||
| 428 | =cut | ||
| 429 | |||
| 430 | 1; # End of Gtk2::Ex::Email::AddressVBox | ||