| 1 | # Palm::Address.pm | ||
| 2 | # | ||
| 3 | # Perl class for dealing with Palm AddressBook databases. | ||
| 4 | # | ||
| 5 | # Copyright (C) 1999, 2000, Andrew Arensburger. | ||
| 6 | # You may distribute this file under the terms of the Artistic | ||
| 7 | # License, as specified in the README file. | ||
| 8 | # | ||
| 9 | # $Id: Address.pm,v 1.19 2002/11/07 14:11:42 arensb Exp $ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | package Palm::Address; | ||
| 13 | use Palm::Raw(); | ||
| 14 | use Palm::StdAppInfo(); | ||
| 15 | |||
| 16 | use vars qw( $VERSION @ISA | ||
| 17 | $numFieldLabels $addrLabelLength @phoneLabels @countries | ||
| 18 | %fieldMapBits ); | ||
| 19 | |||
| 20 | # One liner, to allow MakeMaker to work. | ||
| 21 | $VERSION = do { my @r = (q$Revision: 1.19 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | ||
| 22 | |||
| 23 | @ISA = qw( Palm::StdAppInfo Palm::Raw ); | ||
| 24 | |||
| 25 | # AddressDB records are quite flexible and customizable, and therefore | ||
| 26 | # a pain in the ass to deal with correctly. | ||
| 27 | |||
| 28 | =head1 NAME | ||
| 29 | |||
| 30 | Palm::Address - Handler for Palm AddressBook databases | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | use Palm::Address; | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | The Address PDB handler is a helper class for the Palm::PDB package. | ||
| 39 | It parses AddressBook databases. | ||
| 40 | |||
| 41 | =head2 AppInfo block | ||
| 42 | |||
| 43 | The AppInfo block begins with standard category support. See | ||
| 44 | L<Palm::StdAppInfo> for details. | ||
| 45 | |||
| 46 | Other fields include: | ||
| 47 | |||
| 48 | $pdb->{appinfo}{lastUniqueID} | ||
| 49 | $pdb->{appinfo}{dirtyFields} | ||
| 50 | |||
| 51 | I don't know what these are. | ||
| 52 | |||
| 53 | $pdb->{appinfo}{fieldLabels}{name} | ||
| 54 | $pdb->{appinfo}{fieldLabels}{firstName} | ||
| 55 | $pdb->{appinfo}{fieldLabels}{company} | ||
| 56 | $pdb->{appinfo}{fieldLabels}{phone1} | ||
| 57 | $pdb->{appinfo}{fieldLabels}{phone2} | ||
| 58 | $pdb->{appinfo}{fieldLabels}{phone3} | ||
| 59 | $pdb->{appinfo}{fieldLabels}{phone4} | ||
| 60 | $pdb->{appinfo}{fieldLabels}{phone5} | ||
| 61 | $pdb->{appinfo}{fieldLabels}{phone6} | ||
| 62 | $pdb->{appinfo}{fieldLabels}{phone7} | ||
| 63 | $pdb->{appinfo}{fieldLabels}{phone8} | ||
| 64 | $pdb->{appinfo}{fieldLabels}{address} | ||
| 65 | $pdb->{appinfo}{fieldLabels}{city} | ||
| 66 | $pdb->{appinfo}{fieldLabels}{state} | ||
| 67 | $pdb->{appinfo}{fieldLabels}{zipCode} | ||
| 68 | $pdb->{appinfo}{fieldLabels}{country} | ||
| 69 | $pdb->{appinfo}{fieldLabels}{title} | ||
| 70 | $pdb->{appinfo}{fieldLabels}{custom1} | ||
| 71 | $pdb->{appinfo}{fieldLabels}{custom2} | ||
| 72 | $pdb->{appinfo}{fieldLabels}{custom3} | ||
| 73 | $pdb->{appinfo}{fieldLabels}{custom4} | ||
| 74 | $pdb->{appinfo}{fieldLabels}{note} | ||
| 75 | |||
| 76 | These are the names of the various fields in the address record. | ||
| 77 | |||
| 78 | $pdb->{appinfo}{country} | ||
| 79 | |||
| 80 | An integer: the code for the country for which these labels were | ||
| 81 | designed. The country name is available as | ||
| 82 | |||
| 83 | $Palm::Address::countries[$pdb->{appinfo}{country}]; | ||
| 84 | |||
| 85 | $pdb->{appinfo}{misc} | ||
| 86 | |||
| 87 | An integer. The least-significant bit is a flag that indicates whether | ||
| 88 | the database should be sorted by company. The other bits are reserved. | ||
| 89 | |||
| 90 | =head2 Sort block | ||
| 91 | |||
| 92 | $pdb->{sort} | ||
| 93 | |||
| 94 | This is a scalar, the raw data of the sort block. | ||
| 95 | |||
| 96 | =head2 Records | ||
| 97 | |||
| 98 | $record = $pdb->{records}[N]; | ||
| 99 | |||
| 100 | $record->{fields}{name} | ||
| 101 | $record->{fields}{firstName} | ||
| 102 | $record->{fields}{company} | ||
| 103 | $record->{fields}{phone1} | ||
| 104 | $record->{fields}{phone2} | ||
| 105 | $record->{fields}{phone3} | ||
| 106 | $record->{fields}{phone4} | ||
| 107 | $record->{fields}{phone5} | ||
| 108 | $record->{fields}{address} | ||
| 109 | $record->{fields}{city} | ||
| 110 | $record->{fields}{state} | ||
| 111 | $record->{fields}{zipCode} | ||
| 112 | $record->{fields}{country} | ||
| 113 | $record->{fields}{title} | ||
| 114 | $record->{fields}{custom1} | ||
| 115 | $record->{fields}{custom2} | ||
| 116 | $record->{fields}{custom3} | ||
| 117 | $record->{fields}{custom4} | ||
| 118 | $record->{fields}{note} | ||
| 119 | |||
| 120 | These are scalars, the values of the various address book fields. | ||
| 121 | |||
| 122 | $record->{phoneLabel}{phone1} | ||
| 123 | $record->{phoneLabel}{phone2} | ||
| 124 | $record->{phoneLabel}{phone3} | ||
| 125 | $record->{phoneLabel}{phone4} | ||
| 126 | $record->{phoneLabel}{phone5} | ||
| 127 | |||
| 128 | Most fields in an AddressBook record are straightforward: the "name" | ||
| 129 | field always gives the person's last name. | ||
| 130 | |||
| 131 | The "phoneI<N>" fields, on the other hand, can mean different things | ||
| 132 | in different records. There are five such fields in each record, each | ||
| 133 | of which can take on one of eight different values: "Work", "Home", | ||
| 134 | "Fax", "Other", "E-mail", "Main", "Pager" and "Mobile". | ||
| 135 | |||
| 136 | The $record->{phoneLabel}{phone*} fields are integers. Each one is | ||
| 137 | an index into @Palm::Address::phoneLabels, and indicates which | ||
| 138 | particular type of phone number each of the $record->{phone*} fields | ||
| 139 | represents. | ||
| 140 | |||
| 141 | $record->{phoneLabel}{display} | ||
| 142 | |||
| 143 | Like the phone* fields above, this is an index into | ||
| 144 | @Palm::Address::phoneLabels. It indicates which of the phone* | ||
| 145 | fields to display in the list view. | ||
| 146 | |||
| 147 | $record->{phoneLabel}{reserved} | ||
| 148 | |||
| 149 | I don't know what this is. | ||
| 150 | |||
| 151 | =head1 METHODS | ||
| 152 | |||
| 153 | =cut | ||
| 154 | #' | ||
| 155 | |||
| 156 | $addrLabelLength = 16; | ||
| 157 | $numFieldLabels = 22; | ||
| 158 | |||
| 159 | @phoneLabels = ( | ||
| 160 | "Work", | ||
| 161 | "Home", | ||
| 162 | "Fax", | ||
| 163 | "Other", | ||
| 164 | "E-mail", | ||
| 165 | "Main", | ||
| 166 | "Pager", | ||
| 167 | "Mobile", | ||
| 168 | ); | ||
| 169 | |||
| 170 | @countries = ( | ||
| 171 | "Australia", | ||
| 172 | "Austria", | ||
| 173 | "Belgium", | ||
| 174 | "Brazil", | ||
| 175 | "Canada", | ||
| 176 | "Denmark", | ||
| 177 | "Finland", | ||
| 178 | "France", | ||
| 179 | "Germany", | ||
| 180 | "Hong Kong", | ||
| 181 | "Iceland", | ||
| 182 | "Ireland", | ||
| 183 | "Italy", | ||
| 184 | "Japan", | ||
| 185 | "Luxembourg", | ||
| 186 | "Mexico", | ||
| 187 | "Netherlands", | ||
| 188 | "New Zealand", | ||
| 189 | "Norway", | ||
| 190 | "Spain", | ||
| 191 | "Sweden", | ||
| 192 | "Switzerland", | ||
| 193 | "United Kingdom", | ||
| 194 | "United States", | ||
| 195 | ); | ||
| 196 | |||
| 197 | # fieldMapBits | ||
| 198 | # Each Address record contains a flag record ($fieldMap, in | ||
| 199 | # &PackRecord) that indicates which fields exist in the record. This | ||
| 200 | # hash defines these flags' values. | ||
| 201 | %fieldMapBits = ( | ||
| 202 | name => 0x0001, | ||
| 203 | firstName => 0x0002, | ||
| 204 | company => 0x0004, | ||
| 205 | phone1 => 0x0008, | ||
| 206 | phone2 => 0x0010, | ||
| 207 | phone3 => 0x0020, | ||
| 208 | phone4 => 0x0040, | ||
| 209 | phone5 => 0x0080, | ||
| 210 | address => 0x0100, | ||
| 211 | city => 0x0200, | ||
| 212 | state => 0x0400, | ||
| 213 | zipCode => 0x0800, | ||
| 214 | country => 0x1000, | ||
| 215 | title => 0x2000, | ||
| 216 | custom1 => 0x4000, | ||
| 217 | custom2 => 0x8000, | ||
| 218 | custom3 => 0x10000, | ||
| 219 | custom4 => 0x20000, | ||
| 220 | note => 0x40000, | ||
| 221 | ); | ||
| 222 | |||
| 223 | sub import | ||
| 224 | { | ||
| 225 | &Palm::PDB::RegisterPDBHandlers(__PACKAGE__, | ||
| 226 | [ "addr", "DATA" ], | ||
| 227 | ); | ||
| 228 | } | ||
| 229 | |||
| 230 | =head2 new | ||
| 231 | |||
| 232 | $pdb = new Palm::Address; | ||
| 233 | |||
| 234 | Create a new PDB, initialized with the various Palm::Address fields | ||
| 235 | and an empty record list. | ||
| 236 | |||
| 237 | Use this method if you're creating an Address PDB from scratch. | ||
| 238 | |||
| 239 | =cut | ||
| 240 | #' | ||
| 241 | |||
| 242 | # new | ||
| 243 | # Create a new Palm::Address database, and return it | ||
| 244 | sub new | ||
| 245 | { | ||
| 246 | my $classname = shift; | ||
| 247 | my $self = $classname->SUPER::new(@_); | ||
| 248 | # Create a generic PDB. No need to rebless it, | ||
| 249 | # though. | ||
| 250 | |||
| 251 | $self->{name} = "AddressDB"; # Default | ||
| 252 | $self->{creator} = "addr"; | ||
| 253 | $self->{type} = "DATA"; | ||
| 254 | $self->{attributes}{resource} = 0; | ||
| 255 | # The PDB is not a resource database by | ||
| 256 | # default, but it's worth emphasizing, | ||
| 257 | # since AddressDB is explicitly not a PRC. | ||
| 258 | |||
| 259 | # Initialize the AppInfo block | ||
| 260 | $self->{appinfo} = { | ||
| 261 | fieldLabels => { | ||
| 262 | # Displayed labels for the various fields in | ||
| 263 | # each address record. | ||
| 264 | # XXX - These are American English defaults. It'd | ||
| 265 | # be way keen to allow i18n. | ||
| 266 | name => "Name", | ||
| 267 | firstName => "First name", | ||
| 268 | company => "Company", | ||
| 269 | phone1 => "Work", | ||
| 270 | phone2 => "Home", | ||
| 271 | phone3 => "Fax", | ||
| 272 | phone4 => "Other", | ||
| 273 | phone5 => "E-mail", | ||
| 274 | phone6 => "Main", | ||
| 275 | phone7 => "Pager", | ||
| 276 | phone8 => "Mobile", | ||
| 277 | address => "Address", | ||
| 278 | city => "City", | ||
| 279 | state => "State", | ||
| 280 | zipCode => "Zip Code", | ||
| 281 | country => "Country", | ||
| 282 | title => "Title", | ||
| 283 | custom1 => "Custom 1", | ||
| 284 | custom2 => "Custom 2", | ||
| 285 | custom3 => "Custom 3", | ||
| 286 | custom4 => "Custom 4", | ||
| 287 | note => "Note", | ||
| 288 | }, | ||
| 289 | |||
| 290 | # XXX - The country code corresponds to "United | ||
| 291 | # States". Again, it'd be keen to allow the user's # | ||
| 292 | # country-specific defaults. | ||
| 293 | country => 22, | ||
| 294 | |||
| 295 | misc => 0, | ||
| 296 | }; | ||
| 297 | |||
| 298 | # Add the standard AppInfo block stuff | ||
| 299 | &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo}); | ||
| 300 | |||
| 301 | # Give the PDB a blank sort block | ||
| 302 | $self->{sort} = undef; | ||
| 303 | |||
| 304 | # Give the PDB an empty list of records | ||
| 305 | $self->{records} = []; | ||
| 306 | |||
| 307 | return $self; | ||
| 308 | } | ||
| 309 | |||
| 310 | =head2 new_Record | ||
| 311 | |||
| 312 | $record = $pdb->new_Record; | ||
| 313 | |||
| 314 | Creates a new Address record, with blank values for all of the fields. | ||
| 315 | The AppInfo block will contain only an "Unfiled" category, with ID 0. | ||
| 316 | |||
| 317 | C<new_Record> does B<not> add the new record to C<$pdb>. For that, | ||
| 318 | you want C<$pdb-E<gt>append_Record>. | ||
| 319 | |||
| 320 | =cut | ||
| 321 | |||
| 322 | # new_Record | ||
| 323 | # Create a new, initialized record. | ||
| 324 | sub new_Record | ||
| 325 | { | ||
| 326 | my $classname = shift; | ||
| 327 | my $retval = $classname->SUPER::new_Record(@_); | ||
| 328 | |||
| 329 | # Initialize the fields. This isn't particularly enlightening, | ||
| 330 | # but every AddressDB record has these. | ||
| 331 | $retval->{fields} = { | ||
| 332 | name => undef, | ||
| 333 | firstName => undef, | ||
| 334 | company => undef, | ||
| 335 | phone1 => undef, | ||
| 336 | phone2 => undef, | ||
| 337 | phone3 => undef, | ||
| 338 | phone4 => undef, | ||
| 339 | phone5 => undef, | ||
| 340 | address => undef, | ||
| 341 | city => undef, | ||
| 342 | state => undef, | ||
| 343 | zipCode => undef, | ||
| 344 | country => undef, | ||
| 345 | title => undef, | ||
| 346 | custom1 => undef, | ||
| 347 | custom2 => undef, | ||
| 348 | custom3 => undef, | ||
| 349 | custom4 => undef, | ||
| 350 | note => undef, | ||
| 351 | }; | ||
| 352 | |||
| 353 | # Initialize the phone labels | ||
| 354 | $retval->{phoneLabel} = { | ||
| 355 | phone1 => 0, # Work | ||
| 356 | phone2 => 1, # Home | ||
| 357 | phone3 => 2, # Fax | ||
| 358 | phone4 => 3, # Other | ||
| 359 | phone5 => 4, # E-mail | ||
| 360 | display => 0, # Display work phone by default | ||
| 361 | reserved => undef # ??? | ||
| 362 | }; | ||
| 363 | |||
| 364 | return $retval; | ||
| 365 | } | ||
| 366 | |||
| 367 | # ParseAppInfoBlock | ||
| 368 | # Parse the AppInfo block for Address databases. | ||
| 369 | # | ||
| 370 | # The AppInfo block has the following overall structure: | ||
| 371 | # 1: Categories (see StdAppInfo.pm) | ||
| 372 | # 2: reserved word | ||
| 373 | # 3: dirty field labels | ||
| 374 | # 4: field labels | ||
| 375 | # 5: country | ||
| 376 | # 6: misc | ||
| 377 | # 3: I think this is similar to the first part of the standard AppInfo | ||
| 378 | # blocka, a bit field of which field labels have changed (i.e., | ||
| 379 | # which fields have been renamed). | ||
| 380 | # 4: An array of field labels (16-character strings, NUL-terminated). | ||
| 381 | # 5: The code for the country for which the labels were designed. | ||
| 382 | # 6: 7 reserved bits followed by one flag that's set if the database | ||
| 383 | # should be sorted by company. | ||
| 384 | sub ParseAppInfoBlock | ||
| 385 | { | ||
| 386 | my $self = shift; | ||
| 387 | my $data = shift; | ||
| 388 | my $dirtyFields; | ||
| 389 | my @fieldLabels; | ||
| 390 | my $country; | ||
| 391 | my $misc; | ||
| 392 | |||
| 393 | my $i; | ||
| 394 | my $appinfo = {}; | ||
| 395 | my $std_len; | ||
| 396 | |||
| 397 | # Get the standard parts of the AppInfo block | ||
| 398 | $std_len = &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data); | ||
| 399 | |||
| 400 | $data = $appinfo->{other}; # Look at the non-standard part | ||
| 401 | |||
| 402 | # Get the rest of the AppInfo block | ||
| 403 | my $unpackstr = # Argument to unpack() | ||
| 404 | "x2" . # Reserved | ||
| 405 | "N" . # Dirty flags | ||
| 406 | "a$addrLabelLength" x $numFieldLabels . | ||
| 407 | # Address labels | ||
| 408 | "C" . # Country | ||
| 409 | "C"; # Misc | ||
| 410 | |||
| 411 | ($dirtyFields, | ||
| 412 | @fieldLabels[0..($numFieldLabels-1)], | ||
| 413 | $country, | ||
| 414 | $misc) = | ||
| 415 | unpack $unpackstr, $data; | ||
| 416 | for (@fieldLabels) | ||
| 417 | { | ||
| 418 | s/\0.*$//; # Trim everything after the first NUL | ||
| 419 | # (when renaming custom fields, might | ||
| 420 | # have something like "Foo\0om 1" | ||
| 421 | } | ||
| 422 | |||
| 423 | $appinfo->{dirtyFields} = $dirtyFields; | ||
| 424 | $appinfo->{fieldLabels} = { | ||
| 425 | name => $fieldLabels[0], | ||
| 426 | firstName => $fieldLabels[1], | ||
| 427 | company => $fieldLabels[2], | ||
| 428 | phone1 => $fieldLabels[3], | ||
| 429 | phone2 => $fieldLabels[4], | ||
| 430 | phone3 => $fieldLabels[5], | ||
| 431 | phone4 => $fieldLabels[6], | ||
| 432 | phone5 => $fieldLabels[7], | ||
| 433 | address => $fieldLabels[8], | ||
| 434 | city => $fieldLabels[9], | ||
| 435 | state => $fieldLabels[10], | ||
| 436 | zipCode => $fieldLabels[11], | ||
| 437 | country => $fieldLabels[12], | ||
| 438 | title => $fieldLabels[13], | ||
| 439 | custom1 => $fieldLabels[14], | ||
| 440 | custom2 => $fieldLabels[15], | ||
| 441 | custom3 => $fieldLabels[16], | ||
| 442 | custom4 => $fieldLabels[17], | ||
| 443 | note => $fieldLabels[18], | ||
| 444 | phone6 => $fieldLabels[19], | ||
| 445 | phone7 => $fieldLabels[20], | ||
| 446 | phone8 => $fieldLabels[21], | ||
| 447 | }; | ||
| 448 | $appinfo->{country} = $country; | ||
| 449 | $appinfo->{misc} = $misc; # XXX - Parse the "misc" field further | ||
| 450 | |||
| 451 | return $appinfo; | ||
| 452 | } | ||
| 453 | |||
| 454 | sub PackAppInfoBlock | ||
| 455 | { | ||
| 456 | my $self = shift; | ||
| 457 | my $retval; | ||
| 458 | my $i; | ||
| 459 | my $other; # Non-standard AppInfo stuff | ||
| 460 | |||
| 461 | # Pack the application-specific part of the AppInfo block | ||
| 462 | $other = pack("x2 N", $self->{appinfo}{dirtyFields}); | ||
| 463 | $other .= pack("a$addrLabelLength" x $numFieldLabels, | ||
| 464 | $self->{appinfo}{fieldLabels}{name}, | ||
| 465 | $self->{appinfo}{fieldLabels}{firstName}, | ||
| 466 | $self->{appinfo}{fieldLabels}{company}, | ||
| 467 | $self->{appinfo}{fieldLabels}{phone1}, | ||
| 468 | $self->{appinfo}{fieldLabels}{phone2}, | ||
| 469 | $self->{appinfo}{fieldLabels}{phone3}, | ||
| 470 | $self->{appinfo}{fieldLabels}{phone4}, | ||
| 471 | $self->{appinfo}{fieldLabels}{phone5}, | ||
| 472 | $self->{appinfo}{fieldLabels}{address}, | ||
| 473 | $self->{appinfo}{fieldLabels}{city}, | ||
| 474 | $self->{appinfo}{fieldLabels}{state}, | ||
| 475 | $self->{appinfo}{fieldLabels}{zipCode}, | ||
| 476 | $self->{appinfo}{fieldLabels}{country}, | ||
| 477 | $self->{appinfo}{fieldLabels}{title}, | ||
| 478 | $self->{appinfo}{fieldLabels}{custom1}, | ||
| 479 | $self->{appinfo}{fieldLabels}{custom2}, | ||
| 480 | $self->{appinfo}{fieldLabels}{custom3}, | ||
| 481 | $self->{appinfo}{fieldLabels}{custom4}, | ||
| 482 | $self->{appinfo}{fieldLabels}{note}, | ||
| 483 | $self->{appinfo}{fieldLabels}{phone6}, | ||
| 484 | $self->{appinfo}{fieldLabels}{phone7}, | ||
| 485 | $self->{appinfo}{fieldLabels}{phone8}); | ||
| 486 | $other .= pack("C C x2", | ||
| 487 | $self->{appinfo}{country}, | ||
| 488 | $self->{appinfo}{misc}); | ||
| 489 | $self->{appinfo}{other} = $other; | ||
| 490 | |||
| 491 | # Pack the standard part of the AppInfo block | ||
| 492 | $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo}); | ||
| 493 | |||
| 494 | return $retval; | ||
| 495 | } | ||
| 496 | |||
| 497 | # ParseRecord | ||
| 498 | # Parse an Address Book record. | ||
| 499 | |||
| 500 | # Address book records have the following overall structure: | ||
| 501 | # 1: phone labels | ||
| 502 | # 2: field map | ||
| 503 | # 3: fields | ||
| 504 | |||
| 505 | # Each record can contain a number of fields, such as "name", | ||
| 506 | # "address", "city", "company", and so forth. Each field has an | ||
| 507 | # internal name ("zipCode"), a printable name ("Zip Code"), and a | ||
| 508 | # value ("90210"). | ||
| 509 | # | ||
| 510 | # For most fields, there is a hard mapping between internal and | ||
| 511 | # printed names: "name" always corresponds to "Last Name". The fields | ||
| 512 | # "phone1" through "phone5" are different: each of these can be mapped | ||
| 513 | # to one of several printed names: "Work", "Home", "Fax", "Other", | ||
| 514 | # "E-Mail", "Main", "Pager" or "Mobile". Multiple internal names can | ||
| 515 | # map to the same printed name (a person might have several e-mail | ||
| 516 | # addresses), and the mapping is part of the record (i.e., each record | ||
| 517 | # has its own mapping). | ||
| 518 | # | ||
| 519 | # Part (3) is simply a series of NUL-terminated strings, giving the | ||
| 520 | # values of the various fields in the record, in a certain order. If a | ||
| 521 | # record does not have a given field, there is no string corresponding | ||
| 522 | # to it in this part. | ||
| 523 | # | ||
| 524 | # Part (2) is a bit field that specifies which fields the record | ||
| 525 | # contains. | ||
| 526 | # | ||
| 527 | # Part (1) determines the phone mapping described above. This is | ||
| 528 | # implemented as an unsigned long, but what we're interested in are | ||
| 529 | # the six least-significant nybbles. They are: | ||
| 530 | # disp phone5 phone4 phone3 phone2 phone1 | ||
| 531 | # ("phone1" is the least-significant nybble). Each nybble holds a | ||
| 532 | # value in the range 0-15 which in turn specifies the printed name for | ||
| 533 | # that particular internal name. | ||
| 534 | |||
| 535 | sub ParseRecord | ||
| 536 | { | ||
| 537 | my $self = shift; | ||
| 538 | my %record = @_; | ||
| 539 | |||
| 540 | delete $record{offset}; # This is useless | ||
| 541 | |||
| 542 | my $phoneFlags; | ||
| 543 | my @phoneTypes; | ||
| 544 | my $dispPhone; # Which phone to display in the phone list | ||
| 545 | my $reserved; # Not sure what this is. It's the 8 high bits | ||
| 546 | # of the "phone types" field. | ||
| 547 | my $fieldMap; | ||
| 548 | my $companyFieldOff; # Company field offset: offset into the | ||
| 549 | # raw "fields" string of the beginning of | ||
| 550 | # the company name, plus 1. Presumably this | ||
| 551 | # is to allow the address book app to quickly | ||
| 552 | # display by company name. It is 0 in entries | ||
| 553 | # that don't have a "Company" field. | ||
| 554 | # This can be ignored when reading, and | ||
| 555 | # must be computed when writing. | ||
| 556 | my $fields; | ||
| 557 | my @fields; | ||
| 558 | |||
| 559 | ($phoneFlags, $fieldMap, $companyFieldOff, $fields) = | ||
| 560 | unpack("N N C a*", $record{data}); | ||
| 561 | @fields = split /\0/, $fields; | ||
| 562 | |||
| 563 | # Parse the phone flags | ||
| 564 | $phoneTypes[0] = $phoneFlags & 0x0f; | ||
| 565 | $phoneTypes[1] = ($phoneFlags >> 4) & 0x0f; | ||
| 566 | $phoneTypes[2] = ($phoneFlags >> 8) & 0x0f; | ||
| 567 | $phoneTypes[3] = ($phoneFlags >> 12) & 0x0f; | ||
| 568 | $phoneTypes[4] = ($phoneFlags >> 16) & 0x0f; | ||
| 569 | $dispPhone = ($phoneFlags >> 20) & 0x0f; | ||
| 570 | $reserved = ($phoneFlags >> 24) & 0xff; | ||
| 571 | |||
| 572 | $record{phoneLabel}{phone1} = $phoneTypes[0]; | ||
| 573 | $record{phoneLabel}{phone2} = $phoneTypes[1]; | ||
| 574 | $record{phoneLabel}{phone3} = $phoneTypes[2]; | ||
| 575 | $record{phoneLabel}{phone4} = $phoneTypes[3]; | ||
| 576 | $record{phoneLabel}{phone5} = $phoneTypes[4]; | ||
| 577 | $record{phoneLabel}{display} = $dispPhone; | ||
| 578 | $record{phoneLabel}{reserved} = $reserved; | ||
| 579 | |||
| 580 | # Get the relevant fields | ||
| 581 | $fieldMap & 0x0001 and $record{fields}{name} = shift @fields; | ||
| 582 | $fieldMap & 0x0002 and $record{fields}{firstName} = | ||
| 583 | shift @fields; | ||
| 584 | $fieldMap & 0x0004 and $record{fields}{company} = shift @fields; | ||
| 585 | $fieldMap & 0x0008 and $record{fields}{phone1} = shift @fields; | ||
| 586 | $fieldMap & 0x0010 and $record{fields}{phone2} = shift @fields; | ||
| 587 | $fieldMap & 0x0020 and $record{fields}{phone3} = shift @fields; | ||
| 588 | $fieldMap & 0x0040 and $record{fields}{phone4} = shift @fields; | ||
| 589 | $fieldMap & 0x0080 and $record{fields}{phone5} = shift @fields; | ||
| 590 | $fieldMap & 0x0100 and $record{fields}{address} = shift @fields; | ||
| 591 | $fieldMap & 0x0200 and $record{fields}{city} = shift @fields; | ||
| 592 | $fieldMap & 0x0400 and $record{fields}{state} = shift @fields; | ||
| 593 | $fieldMap & 0x0800 and $record{fields}{zipCode} = shift @fields; | ||
| 594 | $fieldMap & 0x1000 and $record{fields}{country} = shift @fields; | ||
| 595 | $fieldMap & 0x2000 and $record{fields}{title} = shift @fields; | ||
| 596 | $fieldMap & 0x4000 and $record{fields}{custom1} = shift @fields; | ||
| 597 | $fieldMap & 0x8000 and $record{fields}{custom2} = shift @fields; | ||
| 598 | $fieldMap & 0x10000 and $record{fields}{custom3} = shift @fields; | ||
| 599 | $fieldMap & 0x20000 and $record{fields}{custom4} = shift @fields; | ||
| 600 | $fieldMap & 0x40000 and $record{fields}{note} = shift @fields; | ||
| 601 | |||
| 602 | delete $record{data}; | ||
| 603 | |||
| 604 | return \%record; | ||
| 605 | } | ||
| 606 | |||
| 607 | sub PackRecord | ||
| 608 | { | ||
| 609 | my $self = shift; | ||
| 610 | my $record = shift; | ||
| 611 | my $retval; | ||
| 612 | |||
| 613 | $retval = pack("N", | ||
| 614 | ($record->{phoneLabel}{phone1} & 0x0f) | | ||
| 615 | (($record->{phoneLabel}{phone2} & 0x0f) << 4) | | ||
| 616 | (($record->{phoneLabel}{phone3} & 0x0f) << 8) | | ||
| 617 | (($record->{phoneLabel}{phone4} & 0x0f) << 12) | | ||
| 618 | (($record->{phoneLabel}{phone5} & 0x0f) << 16) | | ||
| 619 | (($record->{phoneLabel}{display} & 0x0f) << 20) | | ||
| 620 | (($record->{phoneLabel}{reserved} & 0xff) << 24)); | ||
| 621 | |||
| 622 | # Set the flag bits that indicate which fields exist in this | ||
| 623 | # record. | ||
| 624 | my $fieldMap = 0; | ||
| 625 | |||
| 626 | foreach my $fieldname (qw(name firstName company | ||
| 627 | phone1 phone2 phone3 phone4 phone5 | ||
| 628 | address city state zipCode country title | ||
| 629 | custom1 custom2 custom3 custom4 | ||
| 630 | note)) | ||
| 631 | { | ||
| 632 | if (defined($record->{fields}{$fieldname}) && | ||
| 633 | ($record->{fields}{$fieldname} ne "")) | ||
| 634 | { | ||
| 635 | $fieldMap |= $fieldMapBits{$fieldname}; | ||
| 636 | } | ||
| 637 | else | ||
| 638 | { | ||
| 639 | $record->{fields}{$fieldname} = ""; | ||
| 640 | } | ||
| 641 | } | ||
| 642 | |||
| 643 | $retval .= pack("N", $fieldMap); | ||
| 644 | |||
| 645 | my $fields = ''; | ||
| 646 | my $companyFieldOff = 0; | ||
| 647 | |||
| 648 | $fields .= $record->{fields}{name} . "\0" | ||
| 649 | unless $record->{fields}{name} eq ""; | ||
| 650 | $fields .= $record->{fields}{firstName} . "\0" | ||
| 651 | unless $record->{fields}{firstName} eq ""; | ||
| 652 | if ($record->{fields}{company} ne "") | ||
| 653 | { | ||
| 654 | $companyFieldOff = length($fields) + 1; | ||
| 655 | $fields .= $record->{fields}{company} . "\0" | ||
| 656 | } | ||
| 657 | |||
| 658 | # Append each nonempty field in turn to $fields. | ||
| 659 | foreach my $fieldname (qw(phone1 phone2 phone3 phone4 phone5 | ||
| 660 | address city state zipCode country title | ||
| 661 | custom1 custom2 custom3 custom4 note)) | ||
| 662 | { | ||
| 663 | # Skip empty fields (either blank or undefined). | ||
| 664 | next if !defined($record->{fields}{$fieldname}); | ||
| 665 | next if $record->{fields}{$fieldname} eq ""; | ||
| 666 | |||
| 667 | # Append the field (with a terminating NUL) | ||
| 668 | $fields .= $record->{fields}{$fieldname} . "\0"; | ||
| 669 | } | ||
| 670 | |||
| 671 | $retval .= pack("C", $companyFieldOff); | ||
| 672 | $retval .= $fields; | ||
| 673 | |||
| 674 | return $retval; | ||
| 675 | } | ||
| 676 | |||
| 677 | 1; | ||
| 678 | __END__ |