| 1 | # Palm::StdAppInfo.pm | ||
| 2 | # | ||
| 3 | # Class for dealing with standard AppInfo blocks in PDBs. | ||
| 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: StdAppInfo.pm,v 1.19 2002/11/07 14:27:28 arensb Exp $ | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | package Palm::StdAppInfo; | ||
| 13 | use Palm::Raw(); | ||
| 14 | |||
| 15 | # Don't harass me about these variables | ||
| 16 | use vars qw( $VERSION @ISA $error ); | ||
| 17 | # $error acts like $! in that it reports the error that occurred | ||
| 18 | |||
| 19 | # One liner, to allow MakeMaker to work. | ||
| 20 | $VERSION = do { my @r = (q$Revision: 1.19 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; | ||
| 21 | |||
| 22 | @ISA = qw( Palm::Raw ); | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Palm::StdAppInfo - Handles standard AppInfo block (categories) | ||
| 27 | |||
| 28 | =head1 SYNOPSIS | ||
| 29 | |||
| 30 | Usually: | ||
| 31 | |||
| 32 | package MyPDBHandler; | ||
| 33 | use Palm::StdAppInfo(); # Note the parentheses | ||
| 34 | |||
| 35 | @ISA = qw( Palm::StdAppInfo ); | ||
| 36 | |||
| 37 | use constant APPINFO_PADDING = 1; | ||
| 38 | |||
| 39 | sub ParseAppInfoBlock { | ||
| 40 | my $self = shift; | ||
| 41 | my $data = shift; | ||
| 42 | my $appinfo = {}; | ||
| 43 | |||
| 44 | &Palm::StdAppInfo::parse_StdAppInfo($appinfo, $data); | ||
| 45 | |||
| 46 | $app_specific_data = $appinfo->{other}; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub PackAppInfoBlock { | ||
| 50 | my $self = shift; | ||
| 51 | my $retval; | ||
| 52 | |||
| 53 | $self->{appinfo}{other} = <pack application-specific data>; | ||
| 54 | $retval = &Palm::StdAppInfo::pack_StdAppInfo($self->{appinfo}); | ||
| 55 | return $retval; | ||
| 56 | } | ||
| 57 | |||
| 58 | Or as a standalone C<PDB> helper class: | ||
| 59 | |||
| 60 | use Palm::StdAppInfo; | ||
| 61 | |||
| 62 | =head1 DESCRIPTION | ||
| 63 | |||
| 64 | Many Palm applications use a common format for keeping track of categories. | ||
| 65 | The C<Palm::StdAppInfo> class deals with this common format: | ||
| 66 | |||
| 67 | $pdb = new Palm::PDB; | ||
| 68 | $pdb->Load("myfile.pdb"); | ||
| 69 | |||
| 70 | @categories = @{$pdb->{appinfo}{categories}}; | ||
| 71 | $lastUniqueID = $pdb->{appinfo}{lastUniqueID}; | ||
| 72 | $other = $pdb->{appinfo}{other}; | ||
| 73 | |||
| 74 | where: | ||
| 75 | |||
| 76 | C<@categories> is an array of references-to-hash: | ||
| 77 | |||
| 78 | =item C<$cat = $categories[0];> | ||
| 79 | |||
| 80 | =over 4 | ||
| 81 | |||
| 82 | =item C<$cat-E<gt>{name}> | ||
| 83 | |||
| 84 | The name of the category, a string of at most 16 characters. | ||
| 85 | |||
| 86 | =item C<$cat-E<gt>{id}> | ||
| 87 | |||
| 88 | The category ID, an integer in the range 0-255. Each category has a | ||
| 89 | unique ID. By convention, 0 is reserved for the "Unfiled" category; | ||
| 90 | IDs assigned by the Palm are in the range 1-127, and IDs assigned by | ||
| 91 | the desktop are in the range 128-255. | ||
| 92 | |||
| 93 | =item C<$cat-E<gt>{renamed}> | ||
| 94 | |||
| 95 | A boolean. This field is true iff the category has been renamed since | ||
| 96 | the last sync. | ||
| 97 | |||
| 98 | =back | ||
| 99 | |||
| 100 | C<$lastUniqueID> is (I think) the last category ID that was assigned. | ||
| 101 | |||
| 102 | C<$other> is any data that follows the category list in the AppInfo | ||
| 103 | block. If you're writing a helper class for a PDB that includes a | ||
| 104 | category list, you should parse this field to get any data that | ||
| 105 | follows the category list; you should also make sure that this field | ||
| 106 | is initialized before you call C<&Palm::StdAppInfo::pack_AppInfo>. | ||
| 107 | |||
| 108 | =head2 APPINFO_PADDING | ||
| 109 | |||
| 110 | Normally, the AppInfo block includes a byte of padding at the end, to | ||
| 111 | bring its length to an even number. However, some databases use this | ||
| 112 | byte for data. | ||
| 113 | |||
| 114 | If your database uses the padding byte for data, then your | ||
| 115 | C<&ParseAppInfoBlock> method (see L<"SYNOPSIS">) should call | ||
| 116 | C<&parse_StdAppInfo> with a true $nopadding argument. | ||
| 117 | |||
| 118 | If, for whatever reason, you wish to inherit | ||
| 119 | C<&StdAppInfo::ParseAppInfoBlock>, then add | ||
| 120 | |||
| 121 | use constant APPINFO_PADDING => 0; | ||
| 122 | |||
| 123 | to your handler package, to tell it that the padding byte is really | ||
| 124 | data. | ||
| 125 | |||
| 126 | =head1 FUNCTIONS | ||
| 127 | |||
| 128 | =cut | ||
| 129 | #' | ||
| 130 | |||
| 131 | use constant APPINFO_PADDING => 1; # Whether to add the padding byte at | ||
| 132 | # the end of the AppInfo block. | ||
| 133 | # Note that this might be considered a hack: | ||
| 134 | # this relies on the fact that 'use constant' | ||
| 135 | # defines a function with no arguments; that | ||
| 136 | # therefore this can be called as an instance | ||
| 137 | # method, with full inheritance. That is, if | ||
| 138 | # the handler class doesn't define it, Perl | ||
| 139 | # will find the constant in the parent. If | ||
| 140 | # this ever changes, the code below that uses | ||
| 141 | # $self->APPINFO_PADDING will need to be | ||
| 142 | # changed. | ||
| 143 | use constant numCategories => 16; # Number of categories in AppInfo block | ||
| 144 | use constant categoryLength => 16; # Length of category names | ||
| 145 | use constant stdAppInfoSize => # Length of a standard AppInfo block | ||
| 146 | 2 + | ||
| 147 | (categoryLength * numCategories) + | ||
| 148 | numCategories + | ||
| 149 | 1 + 1; # The padding byte at the end may | ||
| 150 | # be omitted | ||
| 151 | |||
| 152 | sub import | ||
| 153 | { | ||
| 154 | &Palm::PDB::RegisterPDBHandlers(__PACKAGE__, | ||
| 155 | [ "", "" ], | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | =head2 seed_StdAppInfo | ||
| 160 | |||
| 161 | &Palm::StdAppInfo::seed_StdAppInfo(\%appinfo); | ||
| 162 | |||
| 163 | Creates the standard fields in an existing AppInfo hash. Usually used | ||
| 164 | to ensure that a newly-created AppInfo block contains an initialized | ||
| 165 | category array: | ||
| 166 | |||
| 167 | my $appinfo = {}; | ||
| 168 | |||
| 169 | &Palm::StdAppInfo::seed_StdAppInfo($appinfo); | ||
| 170 | |||
| 171 | Note: this is not a method. | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | # seed_StdAppInfo | ||
| 176 | # *** THIS IS NOT A METHOD *** | ||
| 177 | # Given a reference to an appinfo hash, creates all of the fields for | ||
| 178 | # a new AppInfo block. | ||
| 179 | sub seed_StdAppInfo | ||
| 180 | { | ||
| 181 | my $appinfo = shift; | ||
| 182 | my $i; | ||
| 183 | |||
| 184 | $appinfo->{categories} = []; # Create array of categories | ||
| 185 | |||
| 186 | # Initialize the categories | ||
| 187 | # Note that all of the IDs are initialized to $i. There's no | ||
| 188 | # real good reason for doing it this way, except that that's | ||
| 189 | # what the Palm appears to do with new category lists. | ||
| 190 | for ($i = 0; $i < numCategories; $i++) | ||
| 191 | { | ||
| 192 | $appinfo->{categories}[$i] = {}; | ||
| 193 | |||
| 194 | $appinfo->{categories}[$i]{renamed} = 0; | ||
| 195 | $appinfo->{categories}[$i]{name} = undef; | ||
| 196 | $appinfo->{categories}[$i]{id} = $i; | ||
| 197 | } | ||
| 198 | |||
| 199 | # The only fixed category is "Unfiled". Initialize it now | ||
| 200 | $appinfo->{categories}[0]{name} = "Unfiled"; | ||
| 201 | $appinfo->{categories}[0]{id} = 0; | ||
| 202 | |||
| 203 | # I'm not sure what this is, but let's initialize it. | ||
| 204 | # The Palm appears to initialize this to numCategories - 1. | ||
| 205 | $appinfo->{lastUniqueID} = numCategories - 1; | ||
| 206 | } | ||
| 207 | |||
| 208 | =head2 newStdAppInfo | ||
| 209 | |||
| 210 | $appinfo = Palm::StdAppInfo->newStdAppInfo; | ||
| 211 | |||
| 212 | Like C<seed_StdAppInfo>, but creates an AppInfo hash and returns a | ||
| 213 | reference to it. | ||
| 214 | |||
| 215 | =cut | ||
| 216 | |||
| 217 | sub newStdAppInfo | ||
| 218 | { | ||
| 219 | my $class = shift; | ||
| 220 | my $retval = {}; | ||
| 221 | |||
| 222 | &seed_StdAppInfo($retval); | ||
| 223 | return $retval; | ||
| 224 | } | ||
| 225 | |||
| 226 | =head2 new | ||
| 227 | |||
| 228 | $pdb = new Palm::StdAppInfo; | ||
| 229 | |||
| 230 | Create a new PDB, initialized with nothing but a standard AppInfo block. | ||
| 231 | |||
| 232 | There are very few reasons to use this, and even fewer good ones. If | ||
| 233 | you're writing a helper class to parse some PDB format that contains a | ||
| 234 | category list, then you should make that helper class a subclass of | ||
| 235 | C<Palm::StdAppInfo>. | ||
| 236 | |||
| 237 | =cut | ||
| 238 | #' | ||
| 239 | |||
| 240 | sub new | ||
| 241 | { | ||
| 242 | my $classname = shift; | ||
| 243 | my $self = $classname->SUPER::new(@_); | ||
| 244 | # Create a generic PDB. No need to rebless it, | ||
| 245 | # though. | ||
| 246 | |||
| 247 | # Initialize the AppInfo block | ||
| 248 | $self->{appinfo} = &newStdAppInfo(); | ||
| 249 | |||
| 250 | return $self; | ||
| 251 | } | ||
| 252 | |||
| 253 | =head2 parse_StdAppInfo | ||
| 254 | |||
| 255 | $len = &Palm::StdAppInfo::parse_StdAppInfo(\%appinfo, $data, $nopadding); | ||
| 256 | |||
| 257 | This function (this is not a method) is intended to be called from | ||
| 258 | within a PDB helper class's C<ParseAppInfoBlock> method. | ||
| 259 | |||
| 260 | C<parse_StdAppInfo()> parses a standard AppInfo block from the raw | ||
| 261 | data C<$data> and fills in the fields in C<%appinfo>. It returns the | ||
| 262 | number of bytes parsed. | ||
| 263 | |||
| 264 | C<$nopadding> is optional, and defaults to false. Normally, the | ||
| 265 | AppInfo block includes a padding byte at the end. If C<$nopadding> is | ||
| 266 | true, then C<&parse_StdAppInfo> assumes that the padding byte is | ||
| 267 | application data, and includes it in C<$appinfo{'other'}>, so that the | ||
| 268 | caller can parse it. | ||
| 269 | |||
| 270 | =cut | ||
| 271 | #' | ||
| 272 | |||
| 273 | # parse_StdAppInfo | ||
| 274 | # *** THIS IS NOT A METHOD *** | ||
| 275 | # | ||
| 276 | # Reads the raw data from $data, parses it as a standard AppInfo | ||
| 277 | # block, and fills in the corresponding fields in %$appinfo. Returns | ||
| 278 | # the number of bytes parsed. | ||
| 279 | sub parse_StdAppInfo | ||
| 280 | { | ||
| 281 | my $appinfo = shift; # A reference to hash, to fill in | ||
| 282 | my $data = shift; # Raw data to read | ||
| 283 | my $nopadding = shift; # Optional: no padding byte at end | ||
| 284 | my $unpackstr; # First argument to unpack() | ||
| 285 | my $renamed; # Bitmap of renamed categories | ||
| 286 | my @labels; # Array of category labels | ||
| 287 | my @uniqueIDs; # Array of category IDs | ||
| 288 | my $lastUniqueID; # Not sure what this is | ||
| 289 | |||
| 290 | if (!defined($nopadding)) | ||
| 291 | { | ||
| 292 | $nopadding = 0; | ||
| 293 | } | ||
| 294 | |||
| 295 | # Make sure $appinfo contains all of the requisite fields | ||
| 296 | &seed_StdAppInfo($appinfo); | ||
| 297 | |||
| 298 | # The argument to unpack() isn't hard to understand, it's just | ||
| 299 | # hard to write in a readable fashion. | ||
| 300 | $unpackstr = # Argument to unpack(), since it's hairy | ||
| 301 | "n" . # Renamed categories | ||
| 302 | ("a" . categoryLength) x numCategories . | ||
| 303 | # Category labels | ||
| 304 | "C" x numCategories . | ||
| 305 | # Category IDs | ||
| 306 | "C" . # Last unique ID | ||
| 307 | "x"; | ||
| 308 | |||
| 309 | # Unpack the data | ||
| 310 | ($renamed, | ||
| 311 | @labels[0..(numCategories-1)], | ||
| 312 | @uniqueIDs[0..(numCategories-1)], | ||
| 313 | $lastUniqueID) = | ||
| 314 | unpack $unpackstr, $data; | ||
| 315 | |||
| 316 | # Clean this stuff up a bit | ||
| 317 | for (@labels) | ||
| 318 | { | ||
| 319 | s/\0.*$//; # Trim at NUL | ||
| 320 | } | ||
| 321 | |||
| 322 | # Now put the data into $appinfo | ||
| 323 | my $i; | ||
| 324 | |||
| 325 | for ($i = 0; $i < numCategories; $i++) | ||
| 326 | { | ||
| 327 | $appinfo->{categories}[$i]{renamed} = | ||
| 328 | ($renamed & (1 << $i) ? 1 : 0); | ||
| 329 | $appinfo->{categories}[$i]{name} = $labels[$i]; | ||
| 330 | $appinfo->{categories}[$i]{id} = $uniqueIDs[$i]; | ||
| 331 | } | ||
| 332 | $appinfo->{lastUniqueID} = $lastUniqueID; | ||
| 333 | |||
| 334 | # There might be other stuff in the AppInfo block other than | ||
| 335 | # the standard categories. Put everything else in | ||
| 336 | # $appinfo->{other}. | ||
| 337 | $appinfo->{other} = substr($data, | ||
| 338 | stdAppInfoSize - ($nopadding ? 1 : 0)); | ||
| 339 | |||
| 340 | return ($nopadding ? stdAppInfoSize - 1 : stdAppInfoSize); | ||
| 341 | } | ||
| 342 | |||
| 343 | =head2 ParseAppInfoBlock | ||
| 344 | |||
| 345 | $pdb = new Palm::StdAppInfo; | ||
| 346 | $pdb->ParseAppInfoBlock($data); | ||
| 347 | |||
| 348 | If your application's AppInfo block contains standard category support | ||
| 349 | and nothing else, you may choose to just inherit this method instead | ||
| 350 | of writing your own C<ParseAppInfoBlock> method. Otherwise, see the | ||
| 351 | example in L<"SYNOPSIS">. | ||
| 352 | |||
| 353 | =cut | ||
| 354 | #' | ||
| 355 | |||
| 356 | sub ParseAppInfoBlock | ||
| 357 | { | ||
| 358 | my $self = shift; | ||
| 359 | my $data = shift; | ||
| 360 | |||
| 361 | my $appinfo = {}; | ||
| 362 | |||
| 363 | &parse_StdAppInfo($appinfo, $data, $self->APPINFO_PADDING); | ||
| 364 | return $appinfo; | ||
| 365 | } | ||
| 366 | |||
| 367 | =head2 pack_StdAppInfo | ||
| 368 | |||
| 369 | $data = &Palm::StdAppInfo::pack_StdAppInfo(\%appinfo); | ||
| 370 | |||
| 371 | This function (this is not a method) is intended to be called from | ||
| 372 | within a PDB helper class's C<PackAppInfoBlock> method. | ||
| 373 | |||
| 374 | C<pack_StdAppInfo> takes an AppInfo hash and packs it as a string of | ||
| 375 | raw data that can be written to a PDB. | ||
| 376 | |||
| 377 | Note that if you're using this inside a helper class's | ||
| 378 | C<PackAppInfoBlock> method, you should make sure that | ||
| 379 | C<$appinfo{other}> is properly initialized before you call | ||
| 380 | C<&Palm::StdAppInfo::pack_StdAppInfo>. | ||
| 381 | |||
| 382 | C<$nopadding> is optional, and defaults to false. Normally, the | ||
| 383 | AppInfo block includes a byte of padding at the end. If C<$nopadding> | ||
| 384 | is true, then C<&pack_StdAppInfo> doesn't include this byte of | ||
| 385 | padding, so that the application can use it. | ||
| 386 | |||
| 387 | =cut | ||
| 388 | #' | ||
| 389 | |||
| 390 | # pack_StdAppInfo | ||
| 391 | # *** THIS IS NOT A METHOD *** | ||
| 392 | # | ||
| 393 | # Given a reference to a hash containing an AppInfo block (such as | ||
| 394 | # that initialized by parse_StdAppInfo()), returns a packed string | ||
| 395 | # that can be written to the PDB file. | ||
| 396 | sub pack_StdAppInfo | ||
| 397 | { | ||
| 398 | my $appinfo = shift; | ||
| 399 | my $nopadding = shift; | ||
| 400 | my $retval; | ||
| 401 | my $i; | ||
| 402 | |||
| 403 | $nopadding = 0 if !defined($nopadding); | ||
| 404 | |||
| 405 | # Create the bitfield of renamed categories | ||
| 406 | my $renamed; | ||
| 407 | |||
| 408 | $renamed = 0; | ||
| 409 | for ($i = 0; $i < numCategories; $i++) | ||
| 410 | { | ||
| 411 | if ($appinfo->{categories}[$i]{renamed}) | ||
| 412 | { | ||
| 413 | $renamed |= (1 << $i); | ||
| 414 | } | ||
| 415 | } | ||
| 416 | $retval = pack("n", $renamed); | ||
| 417 | |||
| 418 | # There have to be exactly 16 categories in the AppInfo block, | ||
| 419 | # even though $appinfo->{categories} may have been mangled | ||
| 420 | # by a naive (or clever) user or broken program. | ||
| 421 | for ($i = 0; $i < numCategories; $i++) | ||
| 422 | { | ||
| 423 | my $name; # Category name | ||
| 424 | |||
| 425 | # This is mainly to stop Perl 5.6 from complaining if | ||
| 426 | # the category name is undefined. | ||
| 427 | if ((!defined($appinfo->{categories}[$i]{name})) || | ||
| 428 | $appinfo->{categories}[$i]{name} eq "") | ||
| 429 | { | ||
| 430 | $name = ""; | ||
| 431 | } else { | ||
| 432 | $name = $appinfo->{categories}[$i]{name}; | ||
| 433 | } | ||
| 434 | |||
| 435 | $retval .= pack("a" . categoryLength, $name); | ||
| 436 | } | ||
| 437 | |||
| 438 | # Ditto for category IDs | ||
| 439 | for ($i = 0; $i < numCategories; $i++) | ||
| 440 | { | ||
| 441 | $retval .= pack("C", $appinfo->{categories}[$i]{id}); | ||
| 442 | } | ||
| 443 | |||
| 444 | # Last unique ID, and alignment padding | ||
| 445 | $retval .= pack("Cx", $appinfo->{lastUniqueID}); | ||
| 446 | |||
| 447 | $retval .= $appinfo->{other} if defined($appinfo->{other}); | ||
| 448 | |||
| 449 | return $retval; | ||
| 450 | } | ||
| 451 | |||
| 452 | =head2 PackAppInfoBlock | ||
| 453 | |||
| 454 | $pdb = new Palm::StdAppInfo; | ||
| 455 | $data = $pdb->PackAppInfoBlock(); | ||
| 456 | |||
| 457 | If your application's AppInfo block contains standard category support | ||
| 458 | and nothing else, you may choose to just inherit this method instead | ||
| 459 | of writing your own C<PackAppInfoBlock> method. Otherwise, see the | ||
| 460 | example in L<"SYNOPSIS">. | ||
| 461 | |||
| 462 | =cut | ||
| 463 | #' | ||
| 464 | |||
| 465 | sub PackAppInfoBlock | ||
| 466 | { | ||
| 467 | my $self = shift; | ||
| 468 | |||
| 469 | return &pack_StdAppInfo($self->{appinfo}, $self->{APPINFO_PADDING}); | ||
| 470 | } | ||
| 471 | |||
| 472 | =head2 addCategory | ||
| 473 | |||
| 474 | $pdb->addCategory($name [, $id [, $renamed]]); | ||
| 475 | |||
| 476 | Adds a category to $pdb. | ||
| 477 | |||
| 478 | The $name argument specifies the new category's name. | ||
| 479 | |||
| 480 | The optional $id argument specifies the new category's numeric ID; if | ||
| 481 | omitted or undefined, &addCategory will pick one. | ||
| 482 | |||
| 483 | The optional $renamed argument is a boolean value indicating whether | ||
| 484 | the new category should be marked as having been modified. This | ||
| 485 | defaults to true since, conceptually, &addCategory doesn't really add | ||
| 486 | a category: it finds one whose name happens to be empty, and renames | ||
| 487 | it. | ||
| 488 | |||
| 489 | Returns a true value if successful, false otherwise. In case of | ||
| 490 | failure, &addCategory sets $Palm::StdAppInfo::error to an error | ||
| 491 | message. | ||
| 492 | |||
| 493 | =cut | ||
| 494 | #' | ||
| 495 | # XXX - When choosing a new category ID, should pick them from the | ||
| 496 | # range 128-255. | ||
| 497 | sub addCategory | ||
| 498 | { | ||
| 499 | my $self = shift; # PDB | ||
| 500 | my $name = shift; # Category name | ||
| 501 | my $id = shift; # Category ID (optional) | ||
| 502 | my $renamed = $#_ >= 0 ? $_[0] : 1; | ||
| 503 | # Flag: was the category renamed (optional) | ||
| 504 | # This initialization may look weird, | ||
| 505 | # but it's this way so that it'll | ||
| 506 | # default to true if omitted. | ||
| 507 | my $categories = $self->{appinfo}{categories}; | ||
| 508 | my $i; | ||
| 509 | my %used; # Category IDs in use | ||
| 510 | |||
| 511 | # Collect all the IDs in the current list | ||
| 512 | for (@{$categories}) | ||
| 513 | { | ||
| 514 | next if !defined($_->{name}) || $_->{name} eq ""; | ||
| 515 | $used{$_->{id}} = 1; | ||
| 516 | } | ||
| 517 | |||
| 518 | if (defined($id)) | ||
| 519 | { | ||
| 520 | # Sanity check: make sure this ID isn't already in use | ||
| 521 | if (defined($used{$id})) | ||
| 522 | { | ||
| 523 | $error = "Category ID already in use"; | ||
| 524 | return undef; | ||
| 525 | } | ||
| 526 | } else { | ||
| 527 | # Find an unused category number, if none was specified | ||
| 528 | for ($id = 128; $id < 256; $id++) | ||
| 529 | { | ||
| 530 | last if !defined($used{$id}); | ||
| 531 | } | ||
| 532 | } | ||
| 533 | |||
| 534 | # Go through the list of categories, looking for an unused slot | ||
| 535 | for ($i = 0; $i < numCategories; $i++) | ||
| 536 | { | ||
| 537 | # Ignore named categories | ||
| 538 | next unless !defined($categories->[$i]{name}) or | ||
| 539 | $categories->[$i]{name} eq ""; | ||
| 540 | |||
| 541 | # Found an empty slot | ||
| 542 | $categories->[$i]{name} = $name; | ||
| 543 | $categories->[$i]{id} = $id; | ||
| 544 | $categories->[$i]{renamed} = $renamed; | ||
| 545 | return 1; | ||
| 546 | } | ||
| 547 | |||
| 548 | # If we get this far, there are no empty category slots | ||
| 549 | $error = "No unused categories"; | ||
| 550 | return undef; | ||
| 551 | } | ||
| 552 | |||
| 553 | =head2 deleteCategory | ||
| 554 | |||
| 555 | $pdb->deleteCategory($name); | ||
| 556 | |||
| 557 | Deletes the category with name $name. Actually, though, it doesn't | ||
| 558 | delete the category: it just changes its name to the empty string, and | ||
| 559 | marks the category as renamed. | ||
| 560 | |||
| 561 | =cut | ||
| 562 | #' | ||
| 563 | sub deleteCategory | ||
| 564 | { | ||
| 565 | my $self = shift; | ||
| 566 | my $name = shift; # Category name | ||
| 567 | |||
| 568 | for (@{$self->{appinfo}{categories}}) | ||
| 569 | { | ||
| 570 | # Find the category named $name | ||
| 571 | next if $_->{name} ne $name; | ||
| 572 | |||
| 573 | # Erase this category | ||
| 574 | $_->{name} = ""; | ||
| 575 | |||
| 576 | # You'd think it would make sense to set the "renamed" | ||
| 577 | # field here, but the Palm doesn't do that. | ||
| 578 | } | ||
| 579 | } | ||
| 580 | |||
| 581 | =head2 renameCategory | ||
| 582 | |||
| 583 | $pdb->renameCategory($oldname, $newname); | ||
| 584 | |||
| 585 | Renames the category named $oldname to $newname. | ||
| 586 | |||
| 587 | If successful, returns a true value. If there is no category named | ||
| 588 | $oldname, returns a false value and sets $Palm::StdAppInfo::error to | ||
| 589 | an error message. | ||
| 590 | |||
| 591 | =cut | ||
| 592 | #' | ||
| 593 | # XXX - This doesn't behave the same way as the Palm: the Palm also | ||
| 594 | # picks a new category ID. | ||
| 595 | sub renameCategory | ||
| 596 | { | ||
| 597 | my $self = shift; | ||
| 598 | my $oldname = shift; | ||
| 599 | my $newname = shift; | ||
| 600 | |||
| 601 | for (@{$self->{appinfo}{categories}}) | ||
| 602 | { | ||
| 603 | # Look for a category named $oldname | ||
| 604 | next if !defined($_->{name}) || $_->{name} ne $oldname; | ||
| 605 | |||
| 606 | # Found it. Rename it and mark it as renamed. | ||
| 607 | $_->{name} = $newname; | ||
| 608 | $_->{renamed} = 1; | ||
| 609 | return 1; | ||
| 610 | } | ||
| 611 | |||
| 612 | $error = "No such category"; | ||
| 613 | return undef; | ||
| 614 | } | ||
| 615 | |||
| 616 | 1; | ||
| 617 | __END__ |