File Coverage

blib/lib/PsionToGnomecard.pm
Criterion Covered Total %
statement 160 187 85.5
branch 53 98 54.0
condition 6 18 33.3
subroutine 4 4 100.0
pod 0 1 0.0
total 223 308 72.4


line stmt bran cond sub pod time code
1             package PsionToGnomecard;
2              
3             # psiontognomecard: Converts Psion vcard format from Epoc
4             # Contacts application to GnomeCard vcard format
5             # Copyright (C) 2001 Ramin Nakisa
6              
7             # This program is free software; you can redistribute it and/or modify
8             # it under the terms of the GNU General Public License as published by
9             # the Free Software Foundation; either version 2 of the License, or
10             # (at your option) any later version.
11              
12             # This program is distributed in the hope that it will be useful,
13             # but WITHOUT ANY WARRANTY; without even the implied warranty of
14             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15             # GNU General Public License for more details.
16              
17             # You should have received a copy of the GNU General Public License
18             # along with this program; if not, write to the Free Software
19             # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20              
21             require 5.005_62;
22 1     1   684 use strict;
  1         3  
  1         34  
23 1     1   6 use warnings;
  1         1  
  1         38  
24              
25             require Exporter;
26 1     1   281172 use AutoLoader qw(AUTOLOAD);
  1         1740  
  1         7  
27              
28             our @ISA = qw(Exporter);
29              
30             # Items to export into callers namespace by default. Note: do not export
31             # names by default without a very good reason. Use EXPORT_OK instead.
32             # Do not simply export all your public functions/methods/constants.
33              
34             # This allows declaration use PsionToGnomecard ':all';
35             # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
36             # will save memory.
37             our %EXPORT_TAGS = ( 'all' => [ qw(
38             convert
39             ) ] );
40              
41             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
42              
43             our @EXPORT = qw(
44            
45             );
46             our $VERSION = '0.01';
47              
48              
49             # Preloaded methods go here.
50              
51             sub convert {
52 1     1 0 44 my $inputFile = shift;
53 1         2 my $outputFile = shift;
54              
55 1 50       33 open( INPUT, $inputFile )
56             or die( "Couldn't read exported Psion contacts file $inputFile." );
57 1 50       90 open( OUTPUT, ">$outputFile" )
58             or die( "Couldn't write gnomecard file $outputFile." );
59              
60 1         2 my $id = 0;
61 1         2 my $NotesStarted = 0;
62 1         2 my $fieldname;
63             my $fieldvalue;
64 0         0 my @fieldnames;
65 0         0 my @fieldvalues;
66 0         0 my %data;
67              
68 1         24 while ( ) {
69 11         30 s/\cM//g;
70 11 100       21 if (/^N;/) # Name
71             {
72 1         3 chomp;
73 1         4 s/^N;//;
74 1         6 @_ = split(/\:/);
75 1         10 $_[0] =~ s/X-EPOCCNTMODELLABEL\d+\=//g;
76 1         5 @fieldnames = split( /\;/, $_[0] );
77 1         3 @fieldvalues = split( /\;/, $_[1] );
78 1         4 for ( my $i = 0; $i <= $#fieldnames; $i++ ) {
79 5         19 $data{$id,$fieldnames[$i]} = $fieldvalues[$i];
80             }
81             }
82              
83 11 100       27 if (/^ADR;/) {
84 2         3 chomp;
85 2         9 s/^ADR;(WORK|HOME);//;
86 2         8 @_ = split(/\:/);
87 2         17 $_[0] =~ s/X-EPOCCNTMODELLABEL\d+\=//g;
88 2         5 $_[1] =~ s/=0D=0A/, /g; # translate CR symbol
89 2         21 @fieldnames = split( /\;/, $_[0] );
90 2         10 @fieldvalues = split( /\;/, $_[1] );
91 2         8 for ( my $i = 0; $i <= $#fieldnames; $i++ ) {
92 14         48 $data{$id,$fieldnames[$i]} = $fieldvalues[$i];
93             }
94             }
95              
96 11 100       21 if (/^EMAIL;INTERNET;/) {
97 2         4 chomp;
98 2         7 s/^EMAIL;INTERNET;(WORK|HOME);//;
99 2         6 @_ = split(/\:/);
100 2         5 $_[0] =~ s/X-EPOCCNTMODELFIELDLABEL\=//g;
101 2         2 $fieldname = $_[0];
102 2         3 $fieldvalue = $_[1];
103 2         5 $data{$id,$fieldname} = $fieldvalue;
104             }
105              
106 11 100       24 if (/^TEL;/) {
107 4         5 chomp;
108 4         17 s/^TEL;(WORK|HOME);(CELL|FAX){0,1};{0,1}//;
109 4         13 @_ = split(/\:/);
110 4         10 $_[0] =~ s/X-EPOCCNTMODELFIELDLABEL\=//g;
111 4         5 $fieldname = $_[0];
112 4         3 $fieldvalue = $_[1];
113 4         9 $data{$id,$fieldname} = $fieldvalue;
114             }
115              
116 11 50       21 if (/^ORG;/) {
117 0         0 chomp;
118 0         0 s/^ORG;//;
119 0         0 @_ = split(/\:/);
120 0         0 $_[0] =~ s/X-EPOCCNTMODELFIELDLABEL\=//g;
121 0         0 $fieldname = $_[0];
122 0         0 ( $fieldvalue = join("",@_[1..$#_]) ) =~ s/;//;
123 0         0 $data{$id,$fieldname} = $fieldvalue;
124             }
125              
126 11 100       18 if (/^URL;/) {
127 1         3 chomp;
128 1         4 s/^URL;//;
129 1         19 @_ = split(/\:/);
130 1         4 $_[0] =~ s/X-EPOCCNTMODELFIELDLABEL\=//g;
131 1         1 $fieldname = $_[0];
132 1         4 ( $fieldvalue = join("",@_[1..$#_]) ) =~ s/;//;
133 1         3 $data{$id,$fieldname} = $fieldvalue;
134             }
135              
136 11 50       19 if ( /^NOTE;/ ) {
137 0         0 $NotesStarted = 1;
138 0         0 s/^NOTE;//;
139 0         0 @_ = split(/\:/);
140 0         0 $_[0] =~ s/X-EPOCCNTMODELFIELDLABEL\=//g;
141 0         0 ( $fieldname = $_[0] ) =~ s/;ENCODING=QUOTED-PRINTABLE//;
142 0         0 $fieldvalue = $_[1];
143             }
144 11 50       19 if ( $NotesStarted ) {
145 0         0 my $l = $_;
146 0         0 $l =~ s/X-EPOCCNTMODELFIELDLABEL=Notes(;ENCODING=QUOTED-PRINTABLE){0,1}://;
147 0         0 $data{$id,$fieldname} .= $l;
148 0         0 $data{$id,$fieldname} =~ s/=0D=0A/=0A/g; # translate CR symbol
149 0 0       0 if ( $_ !~ /=$/ ) {
150 0         0 $NotesStarted = 0;
151             }
152             }
153              
154              
155 11 100       35 if ( /END:VCARD/ ) {
156 1         11 print OUTPUT "BEGIN:VCARD\n";
157              
158             # First field is FN, which is the record name
159              
160             # Not a person (no first or last name), so must be a company
161 1 50 33     7 if ( ! defined( $data{$id,"First name"} ) &&
162             ! defined( $data{$id,"Last name"} ) ) {
163 0         0 print OUTPUT "FN:";
164 0         0 print OUTPUT $data{$id,"Company"};
165 0         0 print OUTPUT "\n";
166             } else {
167 1         2 print OUTPUT "FN:";
168 1 50       5 if ( defined( $data{$id,"First name"} ) ) {
169 1         3 print OUTPUT $data{$id,"First name"};
170 1 50 33     4 if ( defined( $data{$id,"Middle name"} ) ||
171             defined( $data{$id,"Last name"} ) ) {
172 1         3 print OUTPUT " ";
173             }
174             }
175 1 50       3 if ( defined( $data{$id,"Middle name"} ) ) {
176 1         7 print OUTPUT $data{$id,"Middle name"};
177 1 50       4 if ( defined( $data{$id,"Last name"} ) ) {
178 1         2 print OUTPUT " ";
179             }
180             }
181 1 50       3 if ( defined( $data{$id,"Last name"} ) ) {
182 1         2 print OUTPUT $data{$id,"Last name"};
183             }
184 1         2 print OUTPUT "\n";
185             }
186              
187             # N
188              
189 1 0 33     4 if ( defined( $data{$id,"Last name"} ) ||
      33        
190             defined( $data{$id,"Middle name"} ) ||
191             defined( $data{$id,"First name"} ) ) {
192 1         2 print OUTPUT "N:";
193              
194 1 50       6 if ( defined( $data{$id,"Last name"} ) ) {
195 1         3 print OUTPUT $data{$id,"Last name"};
196             }
197 1         7 print OUTPUT ";";
198 1 50       4 if ( defined( $data{$id,"First name"} ) ) {
199 1         3 print OUTPUT $data{$id,"First name"};
200             }
201 1         1 print OUTPUT ";";
202 1 50       4 if ( defined( $data{$id,"Middle name"} ) ) {
203 1         2 print OUTPUT $data{$id,"Middle name"};
204             }
205 1         1 print OUTPUT ";";
206 1 50       8 if ( defined( $data{$id,"Suffix"} ) ) {
207 1         3 print OUTPUT $data{$id,"Title"};
208             }
209 1         2 print OUTPUT ";";
210 1 50       8 if ( defined( $data{$id,"Suffix"} ) ) {
211 1         2 print OUTPUT $data{$id,"Suffix"};
212             }
213 1         2 print OUTPUT "\n";
214             }
215              
216             # ADR (Work)
217              
218 1 50       4 if ( defined( $data{$id,"Work address"} ) ) {
219 1         2 print OUTPUT "ADR;WORK:";
220 1 50       3 if ( defined( $data{$id,"Work PO box"} ) ) {
221 1         3 print OUTPUT $data{$id,"Work PO box"};
222             }
223 1         2 print OUTPUT ";";
224 1 50       3 if ( defined( $data{$id,"Work ext address"} ) ) {
225 1         2 print OUTPUT $data{$id,"Work ext address"};
226             }
227 1         2 print OUTPUT ";";
228 1 50       4 if ( defined( $data{$id,"Work address"} ) ) {
229 1         3 print OUTPUT $data{$id,"Work address"};
230             }
231 1         1 print OUTPUT ";";
232 1 50       4 if ( defined( $data{$id,"Work city"} ) ) {
233 1         2 print OUTPUT $data{$id,"Work city"};
234             }
235 1         1 print OUTPUT ";";
236 1 50       3 if ( defined( $data{$id,"Work region"} ) ) {
237 1         3 print OUTPUT $data{$id,"Work region"};
238             }
239 1         1 print OUTPUT ";";
240 1 50       4 if ( defined( $data{$id,"Work p'code"} ) ) {
241 1         2 print OUTPUT $data{$id,"Work p'code"};
242             }
243 1         2 print OUTPUT ";";
244 1 50       5 if ( defined( $data{$id,"Work country"} ) ) {
245 0         0 print OUTPUT $data{$id,"Work country"};
246             }
247 1         2 print OUTPUT "\n";
248             }
249              
250             # ADR (Home)
251              
252 1 50       10 if ( defined( $data{$id,"Home address"} ) ) {
253 1         3 print OUTPUT "ADR;HOME:";
254 1 50       4 if ( defined( $data{$id,"Home PO box"} ) ) {
255 1         3 print OUTPUT $data{$id,"Home PO box"};
256             }
257 1         2 print OUTPUT ";";
258 1 50       5 if ( defined( $data{$id,"Home ext address"} ) ) {
259 1         3 print OUTPUT $data{$id,"Home ext address"};
260             }
261 1         2 print OUTPUT ";";
262 1 50       6 if ( defined( $data{$id,"Home address"} ) ) {
263 1         4 print OUTPUT $data{$id,"Home address"};
264             }
265 1         2 print OUTPUT ";";
266 1 50       4 if ( defined( $data{$id,"Home city"} ) ) {
267 1         6 print OUTPUT $data{$id,"Home city"};
268             }
269 1         3 print OUTPUT ";";
270 1 50       5 if ( defined( $data{$id,"Home region"} ) ) {
271 1         4 print OUTPUT $data{$id,"Home region"};
272             }
273 1         2 print OUTPUT ";";
274 1 50       11 if ( defined( $data{$id,"Home p'code"} ) ) {
275 1         3 print OUTPUT $data{$id,"Home p'code"};
276             }
277 1         2 print OUTPUT ";";
278 1 50       4 if ( defined( $data{$id,"Home country"} ) ) {
279 1         3 print OUTPUT $data{$id,"Home country"};
280             }
281 1         2 print OUTPUT "\n";
282             }
283              
284             # TEL
285              
286 1 50 33     12 if ( defined( $data{$id,"Work tel"} ) ||
287             defined( $data{$id,"Home tel"} ) ) {
288 1 50       6 if ( defined( $data{$id,"Home tel"} ) ) {
289 1         3 print OUTPUT "TEL;HOME:", $data{$id,"Home tel"}, "\n";
290             }
291 1 50       5 if ( defined( $data{$id,"Work tel"} ) ) {
292 1         3 print OUTPUT "TEL;WORK:", $data{$id,"Work tel"}, "\n";
293             }
294 1 50       3 if ( defined( $data{$id,"Mobile"} ) ) {
295 1         3 print OUTPUT "TEL;CELL:", $data{$id,"Mobile"}, "\n";
296             }
297             }
298              
299             # EMAIL
300              
301 1 50 33     4 if ( defined( $data{$id,"Work email"} ) ||
302             defined( $data{$id,"Home email"} ) ) {
303 1 50       3 if ( defined( $data{$id,"Work email"} ) ) {
304 1         3 print OUTPUT "EMAIL;INTERNET:", $data{$id,"Work email"}, "\n";
305             }
306 1 50       3 if ( defined( $data{$id,"Home email"} ) ) {
307 1         4 print OUTPUT "EMAIL;INTERNET:", $data{$id,"Home email"}, "\n";
308             }
309             }
310              
311             # URL
312              
313 1 50       11 if ( defined( $data{$id,"Web page"} ) ) {
314 1         3 print OUTPUT "URL:", $data{$id,"Web page"}, "\n";
315             }
316              
317             # NOTE
318              
319 1 50       5 if ( defined( $data{$id,"Notes"} ) ) {
320 0         0 print OUTPUT "NOTE;QUOTED-PRINTABLE:", $data{$id,"Notes"};
321             }
322              
323 1         1 print OUTPUT "END:VCARD\n\n";
324 1         8 $id++;
325             }
326             }
327 1         9 close( INPUT );
328 1         190 close( OUTPUT );
329             }
330              
331              
332             # Autoload methods go after =cut, and are processed by the autosplit program.
333              
334             1;
335             __END__