blib/lib/Gtk2/Ex/Email/AttachmentVBox.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Gtk2::Ex::Email::AttachmentVBox;
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 2 ▶use File::MimeInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use Gtk2;
Subroutine BEGIN: called CC=1 SCAR=2.6
7 use Gtk2::SimpleList;
8  
9 =head1 NAME
10  
11 Gtk2::Ex::Email::AttachmentVBox - Creates a VBox for handling attachments.
12  
13 =head1 VERSION
14  
15 Version 0.0.1
16  
17 =cut
18  
19 our $VERSION = '0.0.1';
20  
21  
22 =head1 SYNOPSIS
23  
24 use Gtk2::Ex::Email::AttachmentVBox;
25 use Gtk2;
26 use Data::Dumper;
27
28 Gtk2->init;
29
30 #init it
31 my $avb=Gtk2::Ex::Email::AddressVBox->new();
32
33 #get the VBox and add it
34 my $vbox=Gtk2::VBox->new;
35 $vbox->show;
36 my $vbox2=$avb->vbox;
37 $vbox->pack_start($vbox2, 1, 1, 1);
38
39 #adds a button that calls getFiles
40 my $button=Gtk2::Button->new;
41 $button->show;
42 my $buttonLabel=Gtk2::Label->new('get files');
43 $buttonLabel->show;
44 $button->add($buttonLabel);
45 $vbox->pack_start($button, 1, 1, 1);
46 $button->signal_connect(activated=>{
47 my @files=$avb->getFiles;
48 print Dumper(\@files);
49 }
50 );
51
52 #add the VBox to the window
53 my $window=Gtk2::Window->new;
54 $window->add($vbox);
55 $window->show;
56
57 #run it
58 Gtk2->main;
59  
60 =head1 METHODS
61  
62 =head2 new
63  
64 This initiates the object.
65  
66 my $avb=Gtk2::Ex::Email::AttachmentVBox->new();
67  
68 =cut
69  
70 sub new{
71 my $self={error=>undef,
72 perror=>undef,
73 errorString=>undef,
74 gui=>{},
75 module=>'Gtk2-Ex-Email-AttachmentVBox',
76 };
77 bless $self;
78
79 return $self;
80 }
81  
82 =head2 addFile
83  
84 This adds a file to the list.
85  
86 One arguement is required and it is the file to add.
87  
88 If it fails, it returns undef, otherwise '1'.
89  
90 my $returned=$avb->addFile($someFile);
91 if(!$returned){
92 print "Error!\n";
93 }
94  
95 =cut
96  
97 sub addFile{
98 my $self=$_[0];
99 my $file=$_[1];
100 my $function='addFile';
101  
102 #make sure we have a file
103 if (!defined($file)) {
104 warn($self->{module}.' '.$function.': No file specified');
105 return undef;
106 }
107  
108 #make sure it exists
109 if (! -e $file) {
110 warn($self->{module}.' '.$function.': The file does not exist');
111 return undef;
112 }
113  
114 #gets the mime type
115 my $mimetype=mimetype($file);
116 if (!defined($mimetype)) {
117 warn($self->{module}.' '.$function.': Unable to get mime type');
118 return undef;
119 }
120  
121 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($file);
122  
123 my @topush;
124 push(@topush, $mimetype);
125 push(@topush, $size);
126 push(@topush, $file);
127  
128 #adds the new entry
129 push(@{$self->{gui}{list}->{data}}, \@topush);
130  
131 return 1;
132 }
133  
134 =head2 addFileDialog
135  
136 This calls Gtk2::FileChooserDialog and the file
137 to the list.
138  
139 A return of undef means the user canceled. A return of
140 '1' means a file was added.
141  
142 my $returned=$avb->addFileDialog;
143 if($returned){
144 print "added a file\n";
145 }else{
146 print "user canceled\n";
147 }
148  
149 =cut
150  
151 sub addFileDialog{
152 my $self=$_[0];
153  
154 my $file_chooser = Gtk2::FileChooserDialog->new (
155 'Select a file to attach...',
156 undef,
157 'open',
158 'gtk-cancel' => 'cancel',
159 'gtk-ok' => 'ok'
160 );
161  
162 if ('ok' ne $file_chooser->run){
163 $file_chooser->destroy;
164 print "test\n";
165 return undef;
166 }
167  
168 my $filename = $file_chooser->get_filename;
169  
170 $file_chooser->destroy;
171  
172 $self->addFile($filename);
173  
174 return 1;
175 }
176  
177 =head2 getFiles
178  
179 Gets a list of the files that should be attached.
180  
181 my @files=$avb->getFiles;
182  
183 =cut
184  
185 sub getFiles{
186 my $self=$_[0];
187  
188 my @files;
189  
190 my $int=0;
191 while (defined( $self->{gui}{list}->{data}[$int][2] )) {
192 push( @files, $self->{gui}{list}->{data}[$int][2] );
193
194 $int++;
195 }
196 #unfortunately the defined check will add a new row
197 #so there for we remove it
198 delete(@{$self->{gui}{list}->{data}}[$int]);
199  
200 return @files;
201 }
202  
203 =head2 removeSelected
204  
205 This removes the currently selected file from the list.
206  
207 my $returned=$avb->removeSelected;
208 if($returned){
209 print "removed\n";
210 }else{
211 print "nothing selected\n";
212 }
213  
214 =cut
215  
216 sub removeSelected{
217 my $self=$_[0];
218  
219 my @selected=$self->{gui}{list}->get_selected_indices;
220  
221 #nothing selected
222 if (!defined($selected[0])) {
223 return undef;
224 }
225  
226 #removes it
227 delete(@{$self->{gui}{list}->{data}}[$selected[0]]);
228  
229 return 1;
230 }
231  
232 =head2 vbox
233  
234 This creates the VBox that contains the widgets.
235  
236 One arguement is taken and it is a array of files
237 to initially attach.
238  
239 $avb->vbox(\@files);
240  
241 =cut
242  
243 sub vbox{
244 my $self=$_[0];
245 my @files;
246 if(defined($_[1])){
247 @files= @{$_[1]};
248 }
249  
250 #the vbox
251 $self->{gui}{vbox}=Gtk2::VBox->new;
252 $self->{gui}{vbox}->show;
253  
254 #holds the buttons
255 $self->{gui}{buttonHB}=Gtk2::HBox->new;
256 $self->{gui}{buttonHB}->show;
257 $self->{gui}{vbox}->pack_start($self->{gui}{buttonHB}, 0, 1, 1);
258  
259 #the add button
260 $self->{gui}{add}=Gtk2::Button->new;
261 $self->{gui}{add}->show;
262 $self->{gui}{addLabel}=Gtk2::Label->new('Add Attachment');
263 $self->{gui}{addLabel}->show;
264 $self->{gui}{add}->add($self->{gui}{addLabel});
265 $self->{gui}{buttonHB}->pack_start($self->{gui}{add}, 0, 1, 1);
266 $self->{gui}{add}->signal_connect(clicked=>sub{
267 $_[1]{self}->addFileDialog;
268 },
269 {
270 self=>$self,
271 }
272 );
273  
274 #the remove button
275 $self->{gui}{remove}=Gtk2::Button->new;
276 $self->{gui}{remove}->show;
277 $self->{gui}{removeLabel}=Gtk2::Label->new('Remove Attachment');
278 $self->{gui}{removeLabel}->show;
279 $self->{gui}{remove}->add($self->{gui}{removeLabel});
280 $self->{gui}{buttonHB}->pack_start($self->{gui}{remove}, 0, 1, 1);
281 $self->{gui}{remove}->signal_connect(clicked=>sub{
282 $_[1]{self}->removeSelected;
283 },
284 {
285 self=>$self,
286 }
287 );
288  
289 #the SW window that will hold attachment list
290 $self->{gui}{attachmentSW}=Gtk2::ScrolledWindow->new;
291 $self->{gui}{attachmentSW}->show;
292 $self->{gui}{vbox}->pack_start($self->{gui}{attachmentSW}, 1, 1, 1);
293  
294 #the list that will hold the attachmentlist
295 $self->{gui}{list}=Gtk2::SimpleList->new(
296 'Mime Type'=>'text',
297 'Size'=>'text',
298 'File'=>'text',
299 );
300 $self->{gui}{list}->show;
301 $self->{gui}{attachmentSW}->add($self->{gui}{list});
302  
303 #add any files if asked to
304 if (defined( $files[0] )) {
305 my $int=0;
306 while (defined( $files[$int] )) {
307 $self->addFile($files[$int]);
308  
309 $int++;
310 }
311  
312 }
313  
314 return $self->{gui}{vbox};
315 }
316  
317 =head1 AUTHOR
318  
319 Zane C. Bowers-Hadley, C<< <vvelox at vvelox.net> >>
320  
321 =head1 BUGS
322  
323 Please report any bugs or feature requests to C<bug-gtk2-ex-email-attachmentvbox at rt.cpan.org>, or through
324 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Gtk2-Ex-Email-AttachmentVBox>. I will be notified, and then you'll
325 automatically be notified of progress on your bug as I make changes.
326  
327  
328  
329  
330 =head1 SUPPORT
331  
332 You can find documentation for this module with the perldoc command.
333  
334 perldoc Gtk2::Ex::Email::AttachmentVBox
335  
336  
337 You can also look for information at:
338  
339 =over 4
340  
341 =item * RT: CPAN's request tracker
342  
343 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Gtk2-Ex-Email-AttachmentVBox>
344  
345 =item * AnnoCPAN: Annotated CPAN documentation
346  
347 L<http://annocpan.org/dist/Gtk2-Ex-Email-AttachmentVBox>
348  
349 =item * CPAN Ratings
350  
351 L<http://cpanratings.perl.org/d/Gtk2-Ex-Email-AttachmentVBox>
352  
353 =item * Search CPAN
354  
355 L<http://search.cpan.org/dist/Gtk2-Ex-Email-AttachmentVBox/>
356  
357 =back
358  
359  
360 =head1 ACKNOWLEDGEMENTS
361  
362 ANDK, #51565, notified be about a missing dependency
363  
364 =head1 COPYRIGHT & LICENSE
365  
366 Copyright 2011 Zane C. Bowers-Hadley, all rights reserved.
367  
368 This program is free software; you can redistribute it and/or modify it
369 under the same terms as Perl itself.
370  
371  
372 =cut
373  
374 1; # End of Gtk2::Ex::Email::AttachmentVBox
↑ Summary