| 1 | # Palm::ToDo.pm | ||
| 2 | # | ||
| 3 | # Perl class for dealing with Palm ToDo 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: ToDo.pm,v 1.12 2002/11/07 14:12:10 arensb Exp $ | ||
| 10 | |||
| 11 | # XXX - Bug: apparently, the first ToDo item shows up with a category | ||
| 12 | # of "unfiled" | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | package Palm::ToDo; | ||
| 16 | use Palm::Raw(); | ||
| 17 | use Palm::StdAppInfo(); | ||
| 18 | |||
| 19 | use vars qw( $VERSION @ISA ); | ||
| 20 | |||
| 21 | # One liner, to allow MakeMaker to work. | ||
| 22 | $VERSION = do { my @r = (q$Revision: 1.12 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | ||
| 23 | |||
| 24 | @ISA = qw( Palm::StdAppInfo Palm::Raw ); | ||
| 25 | |||
| 26 | =head1 NAME | ||
| 27 | |||
| 28 | Palm::ToDo - Handler for Palm ToDo databases. | ||
| 29 | |||
| 30 | =head1 SYNOPSIS | ||
| 31 | |||
| 32 | use Palm::ToDo; | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | The ToDo PDB handler is a helper class for the Palm::PDB package. It | ||
| 37 | parses ToDo databases. | ||
| 38 | |||
| 39 | =head2 AppInfo block | ||
| 40 | |||
| 41 | The AppInfo block begins with standard category support. See | ||
| 42 | L<Palm::StdAppInfo> for details. | ||
| 43 | |||
| 44 | Other fields include: | ||
| 45 | |||
| 46 | $pdb->{appinfo}{dirty_appinfo} | ||
| 47 | $pdb->{appinfo}{sortOrder} | ||
| 48 | |||
| 49 | I don't know what these are. | ||
| 50 | |||
| 51 | =head2 Sort block | ||
| 52 | |||
| 53 | $pdb->{sort} | ||
| 54 | |||
| 55 | This is a scalar, the raw data of the sort block. | ||
| 56 | |||
| 57 | =head2 Records | ||
| 58 | |||
| 59 | $record = $pdb->{records}[N] | ||
| 60 | |||
| 61 | $record->{due_day} | ||
| 62 | $record->{due_month} | ||
| 63 | $record->{due_year} | ||
| 64 | |||
| 65 | The due date of the ToDo item. If the item has no due date, these are | ||
| 66 | undefined. | ||
| 67 | |||
| 68 | $record->{completed} | ||
| 69 | |||
| 70 | This is defined and true iff the item has been completed. | ||
| 71 | |||
| 72 | $record->{priority} | ||
| 73 | |||
| 74 | An integer. The priority of the item. | ||
| 75 | |||
| 76 | $record->{description} | ||
| 77 | |||
| 78 | A text string. The description of the item. | ||
| 79 | |||
| 80 | $record->{note} | ||
| 81 | |||
| 82 | A text string. The note attached to the item. Undefined if the item | ||
| 83 | has no note. | ||
| 84 | |||
| 85 | =cut | ||
| 86 | #' | ||
| 87 | |||
| 88 | sub import | ||
| 89 | { | ||
| 90 | &Palm::PDB::RegisterPDBHandlers(__PACKAGE__, | ||
| 91 | [ "todo", "DATA" ], | ||
| 92 | ); | ||
| 93 | } | ||
| 94 | |||
| 95 | =head2 new | ||
| 96 | |||
| 97 | $pdb = new Palm::ToDo; | ||
| 98 | |||
| 99 | Create a new PDB, initialized with the various Palm::ToDo fields | ||
| 100 | and an empty record list. | ||
| 101 | |||
| 102 | Use this method if you're creating a ToDo PDB from scratch. | ||
| 103 | |||
| 104 | =cut | ||
| 105 | #' | ||
| 106 | |||
| 107 | # new | ||
| 108 | # Create a new Palm::ToDo database, and return it | ||
| 109 | sub new | ||
| 110 | { | ||
| 111 | my $classname = shift; | ||
| 112 | my $self = $classname->SUPER::new(@_); | ||
| 113 | # Create a generic PDB. No need to rebless it, | ||
| 114 | # though. | ||
| 115 | |||
| 116 | $self->{name} = "ToDoDB"; # Default | ||
| 117 | $self->{creator} = "todo"; | ||
| 118 | $self->{type} = "DATA"; | ||
| 119 | $self->{attributes}{resource} = 0; | ||
| 120 | # The PDB is not a resource database by | ||
| 121 | # default, but it's worth emphasizing, | ||
| 122 | # since ToDoDB is explicitly not a PRC. | ||
| 123 | |||
| 124 | # Initialize the AppInfo block | ||
| 125 | $self->{appinfo} = { | ||
| 126 | dirty_appinfo => undef, # ? | ||
| 127 | sortOrder => undef, # ? | ||
| 128 | }; | ||
| 129 | |||
| 130 | # Add the standard AppInfo block stuff | ||
| 131 | &Palm::StdAppInfo::seed_StdAppInfo($self->{appinfo}); | ||
| 132 | |||
| 133 | # Give the PDB a blank sort block | ||
| 134 | $self->{sort} = undef; | ||
| 135 | |||
| 136 | # Give the PDB an empty list of records | ||
| 137 | $self->{records} = []; | ||
| 138 | |||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | =head2 new_Record | ||
| 143 | |||
| 144 | $record = $pdb->new_Record; | ||
| 145 | |||
| 146 | Creates a new ToDo record, with blank values for all of the fields. | ||
| 147 | |||
| 148 | C<new_Record> does B<not> add the new record to C<$pdb>. For that, | ||
| 149 | you want C<$pdb-E<gt>append_Record>. | ||
| 150 | |||
| 151 | =cut | ||
| 152 | |||
| 153 | # new_Record | ||
| 154 | # Create a new, initialized record. | ||
| 155 | sub new_Record | ||
| 156 | { | ||
| 157 | my $classname = shift; | ||
| 158 | my $retval = $classname->SUPER::new_Record(@_); | ||
| 159 | |||
| 160 | # Item has no due date by default. | ||
| 161 | $retval->{due_day} = undef; | ||
| 162 | $retval->{due_month} = undef; | ||
| 163 | $retval->{due_year} = undef; | ||
| 164 | |||
| 165 | $retval->{completed} = 0; # Not completed | ||
| 166 | $retval->{priority} = 1; | ||
| 167 | |||
| 168 | # Empty description, no note. | ||
| 169 | $retval->{description} = ""; | ||
| 170 | $retval->{note} = undef; | ||
| 171 | |||
| 172 | return $retval; | ||
| 173 | } | ||
| 174 | |||
| 175 | # ParseAppInfoBlock | ||
| 176 | # Parse the AppInfo block for ToDo databases. | ||
| 177 | sub ParseAppInfoBlock | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | my $data = shift; | ||
| 181 | my $dirtyAppInfo; | ||
| 182 | my $sortOrder; | ||
| 183 | my $appinfo = {}; | ||
| 184 | my $std_len; | ||
| 185 | |||
| 186 | # Get the standard parts of the AppInfo block | ||
| 187 | $std_len = &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data); | ||
| 188 | |||
| 189 | $data = $appinfo->{other}; # Look at the non-category part | ||
| 190 | |||
| 191 | # Get the rest of the AppInfo block | ||
| 192 | my $unpackstr = # Argument to unpack() | ||
| 193 | "x2" . # Reserved | ||
| 194 | "n" . # XXX - Dirty AppInfo (what is this?) | ||
| 195 | "Cx"; # Sort order | ||
| 196 | |||
| 197 | ($dirtyAppInfo, $sortOrder) = unpack $unpackstr, $data; | ||
| 198 | |||
| 199 | $appinfo->{dirty_appinfo} = $dirtyAppInfo; | ||
| 200 | $appinfo->{sort_order} = $sortOrder; | ||
| 201 | |||
| 202 | return $appinfo; | ||
| 203 | } | ||
| 204 | |||
| 205 | sub PackAppInfoBlock | ||
| 206 | { | ||
| 207 | my $self = shift; | ||
| 208 | my $retval; | ||
| 209 | |||
| 210 | # Pack the application-specific part of the AppInfo block | ||
| 211 | $self->{appinfo}{other} = pack("x2 n Cx", | ||
| 212 | $self->{appinfo}{dirty_appinfo}, | ||
| 213 | $self->{appinfo}{sort_order}); | ||
| 214 | |||
| 215 | # Pack the AppInfo block | ||
| 216 | $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo}); | ||
| 217 | |||
| 218 | return $retval; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub ParseRecord | ||
| 222 | { | ||
| 223 | my $self = shift; | ||
| 224 | my %record = @_; | ||
| 225 | my $data = $record{data}; | ||
| 226 | |||
| 227 | delete $record{offset}; # This is useless | ||
| 228 | delete $record{data}; # No longer necessary | ||
| 229 | |||
| 230 | my $date; | ||
| 231 | my $priority; | ||
| 232 | |||
| 233 | ($date, $priority) = unpack "n C", $data; | ||
| 234 | $data = substr $data, 3; # Remove the stuff we've already seen | ||
| 235 | |||
| 236 | if ($date != 0xffff) | ||
| 237 | { | ||
| 238 | my $day; | ||
| 239 | my $month; | ||
| 240 | my $year; | ||
| 241 | |||
| 242 | $day = $date & 0x001f; # 5 bits | ||
| 243 | $month = ($date >> 5) & 0x000f; # 4 bits | ||
| 244 | $year = ($date >> 9) & 0x007f; # 7 bits (years since 1904) | ||
| 245 | $year += 1904; | ||
| 246 | |||
| 247 | $record{due_day} = $day; | ||
| 248 | $record{due_month} = $month; | ||
| 249 | $record{due_year} = $year; | ||
| 250 | } | ||
| 251 | |||
| 252 | my $completed; # Boolean | ||
| 253 | |||
| 254 | $completed = $priority & 0x80; | ||
| 255 | $priority &= 0x7f; # Strip high bit | ||
| 256 | |||
| 257 | $record{completed} = 1 if $completed; | ||
| 258 | $record{priority} = $priority; | ||
| 259 | |||
| 260 | my $description; | ||
| 261 | my $note; | ||
| 262 | |||
| 263 | ($description, $note) = split /\0/, $data; | ||
| 264 | |||
| 265 | $record{description} = $description; | ||
| 266 | $record{note} = $note unless $note eq ""; | ||
| 267 | |||
| 268 | return \%record; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub PackRecord | ||
| 272 | { | ||
| 273 | my $self = shift; | ||
| 274 | my $record = shift; | ||
| 275 | my $retval; | ||
| 276 | my $rawDate; | ||
| 277 | my $priority; | ||
| 278 | |||
| 279 | if (defined($record->{due_day})) | ||
| 280 | { | ||
| 281 | $rawDate = ($record->{due_day} & 0x001f) | | ||
| 282 | (($record->{due_month} & 0x000f) << 5) | | ||
| 283 | ((($record->{due_year} - 1904) & 0x007f) << 9); | ||
| 284 | } else { | ||
| 285 | $rawDate = 0xffff; | ||
| 286 | } | ||
| 287 | $priority = $record->{priority} & 0x7f; | ||
| 288 | $priority |= 0x80 if $record->{completed}; | ||
| 289 | |||
| 290 | $retval = pack "n C", | ||
| 291 | $rawDate, | ||
| 292 | $priority; | ||
| 293 | $retval .= $record->{description} . "\0"; | ||
| 294 | $retval .= $record->{note} . "\0"; | ||
| 295 | |||
| 296 | return $retval; | ||
| 297 | } | ||
| 298 | |||
| 299 | 1; | ||
| 300 | __END__ |