File Coverage

blib/lib/BBDB/Export/vCard.pm
Criterion Covered Total %
statement 52 62 83.8
branch 13 24 54.1
condition 2 3 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 76 98 77.5


line stmt bran cond sub pod time code
1             package BBDB::Export::vCard;
2 1     1   733 use strict;
  1         2  
  1         34  
3 1     1   6 use warnings;
  1         2  
  1         99  
4              
5             our $VERSION = '0.015';
6              
7              
8             our @ISA = qw(BBDB::Export);
9              
10 1     1   7 use Data::Dumper;
  1         2  
  1         84  
11 1     1   1523 use Text::vFile::asData;
  1         8723  
  1         10  
12              
13             #
14             #_* To Do
15             #
16             # BDAY birthday
17             # MAILER
18             # TZ timezone
19             # TITLE
20             # ROLE
21             # PRODID
22             # REV
23             # SORT-STRING
24             # UID
25             # URL
26             # CLASS
27             # NICKNAME
28             # PHOTO
29              
30             #
31             #_* config
32             #
33              
34             # supported keys, in preferred order
35             # TODO: make this configurable
36             my @keys = qw( begin version fn n org note tel email end);
37              
38             #
39             #_* process_record
40             #
41              
42             sub process_record
43             {
44 7     7 1 13 my ( $self, $record ) = @_;
45              
46 7         59 my $vcard = Text::vFile::asData->new();
47              
48              
49 7         69 my $data;
50 7         33 $data->{'properties'}->{'begin'} = [ { value => "vcard" } ];
51 7         30 $data->{'properties'}->{'version'} = [ { value => "3.0" } ];
52 7         28 $data->{'properties'}->{'fn'} = [ { value => $record->{'full'} } ];
53 7         35 $data->{'properties'}->{'n'} = [ { value => join ";", (
54             $record->{'last'},
55             $record->{'first'},
56             "",
57             "",
58             ""
59             )
60             } ];
61 7         31 $data->{'properties'}->{'org'} = [ { value => $record->{'company'} } ];
62 7         24 $data->{'properties'}->{'note'} = [ { value => $record->{'notes'} } ];
63 7         128 $data->{'properties'}->{'tel'} = [
64             {
65             value => $record->{'phone'}->{'home' },
66             param => {
67             type => "home",
68             }
69             },
70              
71             {
72             value => $record->{'phone'}->{'work' },
73             param => {
74             type => "work",
75             }
76             },
77              
78             {
79             value => $record->{'phone'}->{'mobile' },
80             param => {
81             type => "mobile",
82             }
83             },
84              
85             {
86             value => $record->{'phone'}->{'fax' },
87             param => {
88             type => "fax",
89             }
90             },
91              
92             ];
93              
94              
95 7 100 66     31 if ( $record->{ 'net' } && ref $record->{ 'net' } eq "ARRAY" )
96             {
97 2         4 for my $index ( 0 .. $#{ $record->{ 'net' } } )
  2         7  
98             {
99 4         5 push @{ $data->{'properties'}->{'email'} }, {
  4         23  
100             value => $record->{'net'}->[$index],
101             param => {
102             type => "internet",
103             }
104             };
105             }
106             }
107              
108              
109 7         26 $data->{'properties'}->{'end'} = [ { value => "vcard" } ];
110              
111 7         23 my @lines = $self->_generate_lines( $data );
112              
113 7         27 my $return = join( "\n", @lines );
114              
115 7 50       22 if ( my $dir = $self->{'data'}->{'output_dir'} )
116             {
117 0         0 my $file = join ( "_", ( $record->{'first'}, $record->{'last'} ) );
118 0         0 $file =~ tr,A-Za-z\-_,,cd;
119 0         0 $file = $dir . "/" . $file;
120 0         0 $file .= ".vcf";
121 0         0 print "Writing file: $file\n";
122 0 0       0 open ( my $vcard_fh, ">", $file ) or die "Unable to write vcard: $file: $!";
123 0         0 print $vcard_fh $return, "\n";
124 0 0       0 close ( $vcard_fh ) or die "Error writing vcard: $file: $!";
125             }
126              
127 7         123 return $return;
128             }
129              
130             #
131             #_* Text::vFile::asData::generate_lines needs a little work
132             #
133             sub _generate_lines
134             {
135 7     7   11 my ( $self, $data ) = @_;
136              
137 7         8 my @lines;
138              
139 7 50       19 push @lines, "BEGIN:$data->{type}" if exists $data->{type};
140 7 50       18 if (exists $data->{properties})
141             {
142             # TODO: handle properties that are not listed in global @keys
143 7         9 for my $name ( @keys )
144             {
145 63 100       149 next unless $data->{properties}->{ $name };
146 58         83 my $v = $data->{properties}->{ $name };
147              
148 58         75 for my $value (@$v) {
149 81 100       160 next unless $value->{'value'};
150             # XXX so we're taking params in preference to param,
151             # let's be sure to document that when we document this
152             # method
153 49         55 my $param = join ';', '', map {
154 49 50       218 my $hash = $_;
155 9 50       48 map {
156 49         137 "$_" . (defined $hash->{$_} ? "=" . $hash->{$_} : "")
157             } keys %$hash
158 49         60 } @{ $value->{params} || [ $value->{param} ] };
159 49         190 push @lines, "$name$param:$value->{value}";
160             }
161             }
162             }
163              
164 7 50       27 if (exists $data->{objects}) {
165 0         0 push @lines, $self->generate_lines( $_ ) for @{ $data->{objects} }
  0         0  
166             }
167 7 50       15 push @lines, "END:$data->{type}" if exists $data->{type};
168 7         38 return @lines;
169             }
170              
171             #
172             #_* post_processing
173             #
174              
175             # no post processing necessary for vcard since there is one entry per
176             # file.
177             sub post_processing
178             {
179 7     7 1 13 my ( $self, $output ) = @_;
180 7         17 return $output;
181             }
182              
183             1;
184              
185