Datebook.pm untested

filter: Statementstmt 0.0% 0 / 250 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 357 CC 33 SCAR 54.9
CC
33
Cov
0%
ParseRecord
306.0
PackRecord
306.0
import
2.0
CRAP 242.4
1 # Palm::Datebook.pm
2 #
3 # Perl class for dealing with Palm DateBook databases.
4 #
5 # Copyright (C) 1999-2001, 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: Datebook.pm,v 1.19 2002/11/07 14:11:51 arensb Exp $
10  
11 use strict;
12 package Palm::Datebook;
13 use Palm::Raw();
14 use Palm::StdAppInfo();
15  
16 use vars qw( $VERSION @ISA );
17  
18 # One liner, to allow MakeMaker to work.
19 $VERSION = do { my @r = (q$Revision: 1.19 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
20  
21 @ISA = qw( Palm::StdAppInfo Palm::Raw );
22  
23  
24 =head1 NAME
25  
26 Palm::Datebook - Handler for Palm DateBook databases.
27  
28 =head1 SYNOPSIS
29  
30 use Palm::Datebook;
31  
32 =head1 DESCRIPTION
33  
34 The Datebook PDB handler is a helper class for the Palm::PDB package.
35 It parses DateBook databases.
36  
37 =head2 AppInfo block
38  
39 The AppInfo block begins with standard category support. See
40 L<Palm::StdAppInfo> for details.
41  
42 =head2 Sort block
43  
44 $pdb->{sort}
45  
46 This is a scalar, the raw data of the sort block.
47  
48 =head2 Records
49  
50 $record = $pdb->{records}[N]
51  
52 $record->{day}
53 $record->{month}
54 $record->{year}
55  
56 The day, month and year of the event. The day and month start at 1
57 (I<i.e.>, for January, C<$record-E<gt>{month}> is set to 1). The year
58 is a four-digit number (for dates in 2001, C<$record-E<gt>{year}> is
59 "2001").
60  
61 For repeating events, these fields specify the first date at which the
62 event occurs.
63  
64 $record->{start_hour}
65 $record->{start_minute}
66 $record->{end_hour}
67 $record->{end_minute}
68  
69 The start and end times of the event. For untimed events, all of these
70 are 0xff.
71  
72 $record->{when_changed}
73  
74 This is defined and true iff the "when info" for the record has
75 changed. I don't know what this means.
76  
77 $record->{alarm}{advance}
78 $record->{alarm}{unit}
79  
80 If the record has an alarm associated with it, the
81 %{$record->{alarm}} hash exists. The "unit" subfield is an integer:
82 0 for minutes, 1 for hours, 2 for days. The "advance" subfield
83 specifies how many units before the event the alarm should ring.
84 I<e.g.>, if "unit" is 1 and "advance" is 5, then the alarm will sound
85 5 hours before the event.
86  
87 If "advance" is -1, then there is no alarm associated with this event.
88  
89 %{$record->{repeat}}
90  
91 This has exists iff this is a repeating event.
92  
93 $record->{repeat}{type}
94  
95 An integer which specifies the type of repeat:
96  
97 =over 4
98  
99 =item 0
100  
101 no repeat.
102  
103 =item 1
104  
105 a daily event, one that occurs every day.
106  
107 =item 2
108  
109 a weekly event, one that occurs every week on the same dayZ<>(s). An
110 event may occur on several days every week, I<e.g.>, every Monday,
111 Wednesday and Friday.
112  
113 For weekly events, the following fields are defined:
114  
115 @{$record->{repeat}{repeat_days}}
116  
117 This is an array of 7 elements; each element is true iff the event
118 occurs on the corresponding day. Element 0 is Sunday, element 1 is
119 Monday, and so forth.
120  
121 $record->{repeat}{start_of_week}
122  
123 I'm not sure what this is, but the Datebook app appears to perform
124 some hairy calculations involving this.
125  
126 =item 3
127  
128 a "monthly by day" event, I<e.g.>, one that occurs on the second
129 Friday of every month.
130  
131 For "monthly by day" events, the following fields are defined:
132  
133 $record->{repeat}{weeknum}
134  
135 The number of the week on which the event occurs. 0 means the first
136 week of the month, 1 means the second week of the month, and so forth.
137 A value of 5 means that the event occurs on the last week of the
138 month.
139  
140 $record->{repeat}{daynum}
141  
142 An integer, the day of the week on which the event occurs. 0 means
143 Sunday, 1 means Monday, and so forth.
144  
145 =item 4
146  
147 a "monthly by date" event, I<e.g.>, one that occurs on the 12th of
148 every month.
149  
150 =item 5
151  
152 a yearly event, I<e.g.>, one that occurs every year on December 25th.
153  
154 $record->{repeat}{frequency}
155  
156 Specifies the frequency of the repeat. For instance, if the event is a
157 daily one, and $record->{repeat}{frequency} is 3, then the event
158 occurs every 3 days.
159  
160 =back
161  
162 $record->{repeat}{unknown}
163  
164 I don't know what this is.
165  
166 $record->{repeat}{end_day}
167 $record->{repeat}{end_month}
168 $record->{repeat}{end_year}
169  
170 The last day, month and year on which the event occurs.
171  
172 @{$record->{exceptions}}
173 $day = $record->{exceptions}[N][0]
174 $month = $record->{exceptions}[N][1]
175 $year = $record->{exceptions}[N][2]
176  
177 If there are any exceptions to a repeating event, I<e.g.> a weekly
178 meeting that was cancelled one time, then the
179 @{$record->{exceptions}} array is defined.
180  
181 Each element in this array is a reference to an anonymous array with
182 three elements: the day, month, and year of the exception.
183  
184 $record->{description}
185  
186 A text string, the description of the event.
187  
188 $record->{note}
189  
190 A text string, the note (if any) attached to the event.
191  
192 =head1 METHODS
193  
194 =cut
195 #'
196  
197 sub import
198 {
199 &Palm::PDB::RegisterPDBHandlers(__PACKAGE__,
200 [ "date", "DATA" ],
201 );
202 }
203  
204 =head2 new
205  
206 $pdb = new Palm::Datebook;
207  
208 Create a new PDB, initialized with the various Palm::Datebook fields
209 and an empty record list.
210  
211 =cut
212 #'
213  
214 # new
215 # Create a new Palm::Datebook database, and return it
216 sub new
217 {
218 my $classname = shift;
219 my $self = $classname->SUPER::new(@_);
220 # Create a generic PDB. No need to rebless it,
221 # though.
222  
223 $self->{name} = "DatebookDB"; # Default
224 $self->{creator} = "date";
225 $self->{type} = "DATA";
226 $self->{attributes}{resource} = 0;
227 # The PDB is not a resource database by
228 # default, but it's worth emphasizing,
229 # since DatebookDB is explicitly not a PRC.
230 $self->{appinfo} = {
231 start_of_week => 0, # XXX - This is bogus
232 };
233 &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo});
234  
235 $self->{sort} = undef; # Empty sort block
236  
237 $self->{records} = []; # Empty list of records
238  
239 return $self;
240 }
241  
242 =head2 new_Record
243  
244 $record = $pdb->new_Record;
245  
246 Creates a new Datebook record, with blank values for all of the fields.
247  
248 C<new_Record> does B<not> add the new record to C<$pdb>. For that,
249 you want C<$pdb-E<gt>append_Record>.
250  
251 =cut
252  
253 sub new_Record
254 {
255 my $classname = shift;
256 my $retval = $classname->SUPER::new_Record(@_);
257  
258 # By default, the new record is an untimed event that occurs
259 # today.
260 my @now = localtime(time);
261  
262 $retval->{day} = $now[3];
263 $retval->{month} = $now[4] + 1;
264 $retval->{year} = $now[5] + 1900;
265  
266 $retval->{start_hour} =
267 $retval->{start_minute} =
268 $retval->{end_hour} =
269 $retval->{end_minute} = 0xff;
270  
271 # Set the alarm. Defaults to 10 minutes before the event.
272 $retval->{alarm}{advance} = 10;
273 $retval->{alarm}{unit} = 0; # Minutes
274  
275 $retval->{repeat} = {}; # No repeat
276 $retval->{exceptions} = []; # No exceptions
277  
278 $retval->{description} = "";
279 $retval->{note} = undef;
280  
281 return $retval;
282 }
283  
284 # ParseAppInfoBlock
285 # Parse the AppInfo block for Datebook databases.
286 # There appears to be one byte of padding at the end.
287 sub ParseAppInfoBlock
288 {
289 my $self = shift;
290 my $data = shift;
291 my $startOfWeek;
292 my $i;
293 my $appinfo = {};
294 my $std_len;
295  
296 # Get the standard parts of the AppInfo block
297 $std_len = &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data);
298  
299 $data = $appinfo->{other}; # Look at non-category part
300  
301 # Get the rest of the AppInfo block
302 my $unpackstr = # Argument to unpack(), since it's hairy
303 "x2" . # Padding
304 "C"; # Start of week
305  
306 # XXX - This is actually "sortOrder". Dunno what that is,
307 # though.
308 ($startOfWeek) = unpack $unpackstr, $data;
309  
310 $appinfo->{start_of_week} = $startOfWeek;
311  
312 return $appinfo;
313 }
314  
315 sub PackAppInfoBlock
316 {
317 my $self = shift;
318 my $retval;
319  
320 # Pack the non-category part of the AppInfo block
321 $self->{appinfo}{other} =
322 pack("x2 C x", $self->{appinfo}{start_of_week});
323  
324 # Pack the standard part of the AppInfo block
325 $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo});
326  
327 return $retval;
328 }
329  
330 sub ParseRecord
331 {
332 my $self = shift;
333 my %record = @_;
334 my $data;
335  
336 delete $record{offset}; # This is useless
337  
338 # Untimed events have 0xff for $startHour, $startMinute,
339 # $endHour and $endMinute.
340 my $startHour; # In 24-hour format
341 my $startMinute;
342 my $endHour; # In 24-hour format
343 my $endMinute;
344 my $rawDate;
345 my $flags;
346 my $unpackstr = # Argument to unpack().
347 "C C" . # Start hour, minute
348 "C C" . # End hour, minute
349 "n" . # Raw date
350 "n"; # Flags
351  
352 $data = $record{data};
353 ($startHour, $startMinute, $endHour, $endMinute, $rawDate,
354 $flags) =
355 unpack $unpackstr, $data;
356 $data = substr $data, 8; # Chop off the part we've just parsed
357  
358 my $year;
359 my $month;
360 my $day;
361  
362 $day = $rawDate & 0x001f; # 5 bits
363 $month = ($rawDate >> 5) & 0x000f; # 4 bits
364 $year = ($rawDate >> 9) & 0x007f; # 7 bits (years since 1904)
365 $year += 1904;
366  
367 $record{start_hour} = $startHour;
368 $record{start_minute} = $startMinute;
369 $record{end_hour} = $endHour;
370 $record{end_minute} = $endMinute;
371 $record{day} = $day;
372 $record{month} = $month;
373 $record{year} = $year;
374  
375 # Flags
376 my $when_changed = ($flags & 0x8000 ? 1 : 0);
377 my $have_alarm = ($flags & 0x4000 ? 1 : 0);
378 my $have_repeat = ($flags & 0x2000 ? 1 : 0);
379 my $have_note = ($flags & 0x1000 ? 1 : 0);
380 my $have_exceptions = ($flags & 0x0800 ? 1 : 0);
381 my $have_description = ($flags & 0x0400 ? 1 : 0);
382  
383 $record{other_flags} = $flags & 0x03ff;
384  
385 if ($when_changed)
386 {
387 $record{when_changed} = 1;
388 }
389  
390 if ($have_alarm)
391 {
392 my $advance;
393 my $adv_unit;
394  
395 ($advance, $adv_unit) = unpack "cC", $data;
396 $data = substr $data, 2; # Chop off alarm data
397  
398 $record{alarm}{advance} = $advance;
399 $record{alarm}{unit} = $adv_unit;
400 }
401  
402 if ($have_repeat)
403 {
404 my $type;
405 my $endDate;
406 my $frequency;
407 my $repeatOn;
408 my $repeatStartOfWeek;
409 my $unknown;
410  
411 ($type, $endDate, $frequency, $repeatOn, $repeatStartOfWeek,
412 $unknown) =
413 unpack "Cx n C C C C", $data;
414 $data = substr $data, 8; # Chop off repeat part
415  
416 $record{repeat}{type} = $type;
417 $record{repeat}{unknown} = $unknown;
418  
419 if ($endDate != 0xffff)
420 {
421 my $endYear;
422 my $endMonth;
423 my $endDay;
424  
425 $endDay = $endDate & 0x001f; # 5 bits
426 $endMonth = ($endDate >> 5) & 0x000f; # 4 bits
427 $endYear = ($endDate >> 9) & 0x007f; # 7 bits (years
428 $endYear += 1904; # since 1904)
429  
430 $record{repeat}{end_day} = $endDay;
431 $record{repeat}{end_month} = $endMonth;
432 $record{repeat}{end_year} = $endYear;
433 }
434  
435 $record{repeat}{frequency} = $frequency;
436 if ($type == 2)
437 {
438 # "Weekly" repeat
439 my $i;
440 my @days;
441  
442 # Build an array of 7 elements (one for each
443 # day of the week). Each element is set iff
444 # the appointment repeats on that day.
445 for ($i = 0; $i < 7; $i++)
446 {
447 if ($repeatOn & (1 << $i))
448 {
449 $days[$i] = 1;
450 } else {
451 $days[$i] = 0;
452 }
453 }
454  
455 $record{repeat}{repeat_days} = [ @days ];
456 $record{repeat}{start_of_week} =
457 $repeatStartOfWeek;
458 # I don't know what this is,
459 # but the Datebook app appears
460 # to perform some hairy
461 # calculations involving this.
462 } elsif ($type == 3) {
463 # "Monthly by day" repeat
464 # If "weeknum" is 5, it means the last week of
465 # the month
466 $record{repeat}{weeknum} = int($repeatOn / 7);
467 $record{repeat}{daynum} = $repeatOn % 7;
468 }
469 }
470  
471 if ($have_exceptions)
472 {
473 my $numExceptions;
474 my @exceptions;
475  
476 $numExceptions = unpack "n", $data;
477 $data = substr $data, 2;
478 @exceptions = unpack "n" x $numExceptions, $data;
479 $data = substr $data, 2 * $numExceptions;
480  
481 my $exception;
482 foreach $exception (@exceptions)
483 {
484 my $year;
485 my $month;
486 my $day;
487  
488 $day = $exception & 0x001f;
489 $month = ($exception >> 5) & 0x000f;
490 $year = ($exception >> 9) & 0x007f;
491 $year += 1904;
492  
493 push @{$record{exceptions}},
494 [ $day, $month, $year ];
495 }
496 }
497  
498 my @fields = split /\0/, $data;
499  
500 if ($have_description)
501 {
502 my $description;
503  
504 $description = shift @fields;
505 $record{description} = $description;
506 }
507  
508 if ($have_note)
509 {
510 my $note;
511  
512 $note = shift @fields;
513 $record{note} = $note;
514 }
515  
516 delete $record{data};
517  
518 return \%record;
519 }
520  
521 sub PackRecord
522 {
523 my $self = shift;
524 my $record = shift;
525 my $retval;
526  
527 my $rawDate;
528 my $flags;
529  
530 $rawDate = ($record->{day} & 0x001f) |
531 (($record->{month} & 0x000f) << 5) |
532 ((($record->{year} - 1904) & 0x007f) << 9);
533  
534 # XXX - Better to collect data first, then build flags.
535 $flags = $record->{other_flags};
536 # $flags |= 0x8000 if $record->{when_changed};
537 # $flags |= 0x4000 if keys %{$record->{alarm}} ne ();
538 # $flags |= 0x2000 if keys %{$record->{repeat}} ne ();
539 # $flags |= 0x1000 if $record->{note} ne "";
540 # $flags |= 0x0800 if $#{$record->{exceptions}} >= 0;
541 # $flags |= 0x0400 if $record->{description} ne "";
542  
543 # $retval = pack "C C C C n n",
544 # $record->{start_hour},
545 # $record->{start_minute},
546 # $record->{end_hour},
547 # $record->{end_minute},
548 # $rawDate,
549 # $flags;
550  
551 if ($record->{when_changed})
552 {
553 $flags |= 0x8000;
554 }
555  
556 my $alarm = undef;
557  
558 if (defined($record->{alarm}) && %{$record->{alarm}})
559 {
560 $flags |= 0x4000;
561 $alarm = pack "c C",
562 $record->{alarm}{advance},
563 $record->{alarm}{unit};
564 }
565  
566 my $repeat = undef;
567  
568 if (defined($record->{repeat}) && %{$record->{repeat}})
569 {
570 my $type; # Repeat type
571 my $endDate = 0xffff; # No end date defined by default
572 my $repeatOn = 0;
573 my $repeatStartOfWeek = 0;
574  
575 $flags |= 0x2000;
576  
577 if (defined($record->{repeat}{end_day}))
578 {
579 # End date defined
580 $endDate =
581 ($record->{repeat}{end_day} & 0x001f) |
582 (($record->{repeat}{end_month}
583 & 0x000f) << 5) |
584 ((($record->{repeat}{end_year} - 1904)
585 & 0x007f) << 9);
586 }
587  
588 if ($record->{repeat}{type} == 2)
589 {
590 # Weekly repeat
591 my $i;
592  
593 $repeatOn = 0;
594 for ($i = 0; $i < 7; $i++)
595 {
596 if ($record->{repeat}{repeat_days}[$i])
597 {
598 $repeatOn |= (1 << $i);
599 }
600 }
601 $repeatStartOfWeek = $record->{repeat}{start_of_week};
602 } elsif ($record->{repeat}{type} == 3)
603 {
604 # "Monthly by day" repeat
605 my $weeknum = $record->{repeat}{weeknum};
606  
607 if ($weeknum > 5)
608 {
609 $weeknum = 5;
610 }
611 $repeatOn = ($record->{repeat}{weeknum} * 7) +
612 ($record->{repeat}{daynum} % 7);
613 }
614  
615 $repeat = pack "Cx n C C C C",
616 $record->{repeat}{type},
617 $endDate,
618 $record->{repeat}{frequency},
619 $repeatOn,
620 $repeatStartOfWeek,
621 $record->{repeat}{unknown};
622 }
623  
624 my $exceptions = undef;
625  
626 if (defined($record->{exceptions}) && @{$record->{exceptions}})
627 {
628 my $numExceptions = $#{$record->{exceptions}} + 1;
629 my $exception;
630  
631 $flags |= 0x0800;
632  
633 $exceptions = pack("n", $numExceptions);
634  
635 foreach $exception (@{$record->{exceptions}})
636 {
637 my $day = $exception->[0];
638 my $month = $exception->[1];
639 my $year = $exception->[2];
640  
641 $exceptions .= pack("n",
642 ($day & 0x001f) |
643 (($month & 0x000f) << 5) |
644 ((($year - 1904) & 0x007f) << 9));
645 }
646 }
647  
648 my $description = undef;
649  
650 if (defined($record->{description}) && ($record->{description} ne ""))
651 {
652 $flags |= 0x0400;
653 $description = $record->{description} . "\0";
654 }
655  
656 my $note = undef;
657  
658 if (defined($record->{note}) && ($record->{note} ne ""))
659 {
660 $flags |= 0x1000;
661 $note = $record->{note} . "\0";
662 }
663  
664 $retval = pack "C C C C n n",
665 $record->{start_hour},
666 $record->{start_minute},
667 $record->{end_hour},
668 $record->{end_minute},
669 $rawDate,
670 $flags;
671  
672 $retval .= $alarm if defined($alarm);
673 $retval .= $repeat if defined($repeat);
674 $retval .= $exceptions if defined($exceptions);
675 $retval .= $description if defined($description);
676 $retval .= $note if defined($note);
677  
678 return $retval;
679 }
680  
681 1;
682 __END__