File: std-categories

package info (click to toggle)
coldsync 3.0%2Bpre3-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,188 kB
  • ctags: 2,033
  • sloc: ansic: 20,386; perl: 2,302; cpp: 1,640; yacc: 1,102; lex: 802; makefile: 533; sh: 177
file content (510 lines) | stat: -rwxr-xr-x 12,975 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/perl
#
#	Copyright (C) 2000, Andrew Arensburger.
#	You may distribute this file under the terms of the Artistic
#	License, as specified in the README file.
#
# $Id: std-categories,v 3.5 2001/06/04 01:41:10 arensb Exp $
#
# This is a ColdSync Sync conduit for synchronizing the categories between
# a database on the Palm and one on the desktop.
#
# This script is rather hairy, and probably doesn't do the Right Thing in
# all cases, but that's because categories aren't quite as clean as they
# could be. Certainly nowhere near as pretty as record syncing.

# XXX - "std-categories -config" gives a dangerous configuration:
#	type: */*;
# this includes databases that don't have a standard AppInfo block.

# What happens:
# - When you add a new category, the Palm finds an empty slot, picks an
#   unused category ID, fills in the slot, sets its "renamed" field to
#   true.
#   The Palm will grab the first empty slot, so if you delete one category
#   and create another, the new category can take the slot formerly
#   occupied by the previous one. I.e., you won't even see an empty name to
#   tell you that a category has been deleted.
# - When you delete a category, the Palm clears the "name" field, but
#   doesn't change the ID. "renamed" is false.
# - When you rename a category, the Palm picks a new ID, changes the name,
#   and sets "renamed" to true. The index remains the same, though.

use strict;
use ColdSync;
use ColdSync::SPC;
use Palm::StdAppInfo;

use vars qw( %Pappinfo $DEBUG );
use constant NumCategories => Palm::StdAppInfo::numCategories;
					# Number of categories in a
					# standard AppInfo block

$DEBUG = 1;		# Turns debugging on

StartConduit("sync");
select STDERR;		# XXX - Just for debugging

my $err;
my $dbinfo;		# Information about currently-open database
my $dbh;		# Database handle

# Find out which database we're dealing with
$dbinfo = spc_get_dbinfo;
die "401 Error reading database info" if !defined($dbinfo);

# Open the database
($dbh = &dlp_OpenDB($dbinfo->{name}, 0x80))
	or die "402 Can't open database";

# Get the category list from the Palm
use vars qw( $Pappinfo_raw );	# Raw AppInfo block data
use vars qw( %Pappinfo );	# Parsed category information from AppInfo
				# block

$Pappinfo_raw = dlp_ReadAppBlock($dbh);
				# Download the AppInfo block from the Palm
# XXX - Error-checking
# XXX - Might be a good idea to make sure that $Pappinfo_raw is at least
# long enough for a standard set of categories, in case some dumbass runs
# this on a non-StdAppInfo database.
&Palm::StdAppInfo::parse_StdAppInfo(\%Pappinfo, $Pappinfo_raw);
				# Parse the AppInfo block into a set of
				# categories.
# XXX - Might be a good idea to make sure that category 0 has ID 0, in case
# some dumbass runs this on a non-StdAppInfo database.

if ($DEBUG)
{
	print "* Palm categories:\n";
	&dumphash(\%Pappinfo, "");
	print "* Desktop categories:\n";
	&dumphash($PDB->{appinfo}, "");
}

# Phase 1: Underpants!

# Phase 2: Get the names and IDs in sync between the Palm and Desktop
# Phase 2.1: Look at the categories on the Palm
print STDERR "===== Examining Palm categories:\n" if $DEBUG;
for (my $i = 0; $i < NumCategories; $i++)
{
	my $category = $Pappinfo{categories}[$i];
	my $Dcategory;

	print STDERR "category $i: [$category->{name}]\n" if $DEBUG;

	# XXX - This logic for finding the corresponding category on the
	# desktop is too primitive: if the Palm category is unchanged, then
	# we need to find either a clean category on the desktop with the
	# same name, or a modified category with the same ID.

	if ($category->{name} eq "")
	{
		$Dcategory = &find_cat_by_id($PDB->{appinfo},
					     $category->{id});

		if (!defined($Dcategory))
		{
			# This category exists neither on the Palm, nor
			# on the desktop. Nothing to do.
			print STDERR "This category doesn't exist. Nothing to do.\n"
				if $DEBUG;

			next;
		}

		# This category doesn't exist on the Palm, but does
		# exist on the desktop
		if ($Dcategory->{renamed})
		{

			# Doesn't exist on the Palm.
			# Exists on the desktop.
			# Renamed on the desktop.
			# -> Add it on the Palm.
			print STDERR "Renamed on the desktop. Renaming on Palm.\n"
				if $DEBUG;

			$category->{name} = $Dcategory->{name};
			$category->{renamed} = 0;
			$Dcategory->{renamed} = 0;
			$Dcategory->{_moveto} = $i;
		} else {

			# Doesn't exist on Palm.
			# Exists on desktop.
			# Not renamed.
			# -> Deleted on Palm. Delete it on desktop
			print STDERR "Deleted on Palm. Deleting on desktop.\n"
				if $DEBUG;

			$Dcategory->{name} = "";
			$category->{renamed} = 0;
			$Dcategory->{renamed} = 0;
			$Dcategory->{_moveto} = $i;
		}
	} else {
		# This category exists on the Palm

		if ($category->{renamed})
		{
			$Dcategory = &find_cat_by_id($PDB->{appinfo},
						     $category->{id});

			if (defined($Dcategory))
			{
				# Exists on Palm.
				# Exists on desktop.
				# Renamed on Palm.
				# -> Renamed on Palm. Change name on desktop.
				print STDERR "Renamed on Palm. Need to rename on desktop.\n"
					if $DEBUG;

				$Dcategory->{name} = $category->{name};
				$category->{renamed} = 0;
				$Dcategory->{renamed} = 0;
				$Dcategory->{_moveto} = $i;
			} else {
				# Exists on Palm.
				# Doesn't exist on desktop.
				# Renamed on Palm.
				# -> Add it on the desktop.
				print STDERR "New category. Need to add on desktop.\n"
					if $DEBUG;

				$category->{renamed} = 0;
				$Dcategory->{renamed} = 0;
				$Dcategory->{_moveto} = $i;
			}
		} else {

			# XXX - The category might exist, renamed, on the
			# desktop, in which case &find_cat_by_name won't
			# find it.
			$Dcategory = &find_cat_by_name($PDB->{appinfo},
						       $category->{name});

			if (defined($Dcategory))
			{
				# This category exists on the Palm and
				# on the desktop. It has not been renamed
				if ($Dcategory->{renamed})
				{
					# Exists on Palm.
					# Exists on desktop.
					# Not renamed on Palm.
					# Renamed on desktop.
					# -> Rename on Palm.
					print STDERR "Renamed on desktop. Need to rename on Palm.\n"
						if $DEBUG;
					$category->{name} = $Dcategory->{name};
					$category->{renamed} = 0;
					$Dcategory->{renamed} = 0;
					$Dcategory->{_moveto} = $i;
				} else {
					# Exists on Palm.
					# Exists on desktop.
					# Not renamed on Palm.
					# Not renamed on desktop.
					# -> Okay, but make sure the IDs are
					#    in sync.

					print STDERR "OK. Make sure IDs are in sync.\n"
						if $DEBUG;

					$Dcategory->{id} = $category->{id};
					$category->{renamed} = 0;
					$Dcategory->{renamed} = 0;
					$Dcategory->{_moveto} = $i;
				}
			} else {
				# Exists on Palm.
				# Doesn't exist on desktop.
				# Not renamed on Palm.
				# -> Not clear whether a) deleted on desktop,
				#    or b) added (and synced) on Palm.
				# -> Be conservative and add it on the desktop.

				print STDERR "Judgment call: add on the desktop.\n"
					if $DEBUG;
			}
		}
	}
}

# Phase 2.2: Look at the categories on the desktop
print STDERR "===== Examining desktop categories:\n" if $DEBUG;
for (my $i = 0; $i < NumCategories; $i++)
{
	my $category = $PDB->{appinfo}{categories}[$i];

	print STDERR "category $i: [$category->{name}]\n" if $DEBUG;
	if (defined($category->{_moveto}))
	{
		print STDERR "\tSeen it already.\n" if $DEBUG;
		next;
	}

	if ($category->{renamed})
	{
		# This category exists on the desktop, but not on the Palm.
		if ($category->{name} eq "")
		{
			# Doesn't exist on Palm.
			# Exists on desktop.
			# Deleted on desktop
			# -> Delete on Palm.

			# XXX - Is there anything to do, really?
			print STDERR "Deleted on desktop. Deleting on Palm.\n"
				if $DEBUG;

			$category->{_moveto} = 0;
		} else {
			# Doesn't exist on Palm.
			# Exists on desktop.
			# Renamed on desktop.
			# -> Add to Palm
			print STDERR "Added on desktop. Adding on Palm.\n"
				if $DEBUG;

			my $newindex;

			if ($Pappinfo{categories}[$i]{name} eq "")
			{
				# This index is free on the Palm, so we
				# don't have to move the category.
				$newindex = $i;
			} else {
				$newindex = &find_empty_cat(\%Pappinfo);
				if ($newindex < 0)
				{
					# No free slots available.
					print STDERR "No free categories. Dropping desktop category \"$category->{name}\".\n"
						if $DEBUG;

					$category->{_moveto} = 0;
					next;
				}
			}

			print STDERR "Adding on Palm, index $newindex\n"
				if $DEBUG;

			my $Pcategory = $Pappinfo{categories}[$newindex];

			$Pcategory->{name} = $category->{name};
			$Pcategory->{id} = $category->{id};
			$Pcategory->{renamed} = 0;
			$category->{_moveto} = $i;
		}
	} else {
		# This category exists on the desktop, but not on the Palm.
		# It has not been renamed.
		# It may have been either added (and synced) on the desktop,
		# or it might have been deleted on the Palm.
		# -> Be conservative, and add it on the Palm, if possible.

		my $newindex;

		if ($Pappinfo{categories}[$i]{name} eq "")
		{
			# This index is free on the Palm, so we don't have
			# to move the category.
			$newindex = $i;
		} else {
			$newindex = &find_empty_cat(\%Pappinfo);
			if ($newindex < 0)
			{
				# No free slots available.
				print STDERR "No free categories. Dropping desktop category \"$category->{name}\".\n"
					if $DEBUG;

				$category->{_moveto} = 0;
				next;
			}
		}

		print STDERR "Adding on Palm, index $newindex\n"
			if $DEBUG;

		my $Pcategory = $Pappinfo{categories}[$newindex];

		$Pcategory->{name} = $category->{name};
		$Pcategory->{id} = $category->{id};
		$Pcategory->{renamed} = 0;
		$category->{_moveto} = $i;
	}
}

# XXX - Should probably double-check that category 0 has ID 0 (Unfiled).

if ($DEBUG)
{
	print STDERR "===== Final set of categories:\n";
	&dumphash(\%Pappinfo, "");
}

# Phase 2.3: Move new categories to the Palm.
for (@{$PDB->{records}})
{
	my $oldcat = $_->{category};
	my $newcat = $PDB->{appinfo}{categories}[$oldcat]{_moveto};

	if (!defined($newcat))
	{
		die("555 Category $oldcat doesn't have a _moveto. Aborting.");
	}

	next if $oldcat == $newcat;

	print STDERR "Moving record $_->{id} from $oldcat to $newcat.\n"
		if $DEBUG;

	$_->{category} = $newcat;
	$_->{attributes}{dirty} = 1;
}

# Phase 2.4: Copy the Palm AppInfo block to the desktop PDB
for (my $i = 0; $i < NumCategories; $i++)
{
	my $Pcategory = $Pappinfo{categories}[$i];
	my $Dcategory = $PDB->{appinfo}{categories}[$i];

	$Dcategory->{name}	= $Pcategory->{name};
	$Dcategory->{id}	= $Pcategory->{id};
	$Dcategory->{renamed}	= $Pcategory->{renamed};
}

# Phase 3: Profits!

# Upload the AppInfo block to the Palm.
$Pappinfo_raw = &Palm::StdAppInfo::pack_StdAppInfo(\%Pappinfo);
#$err = &dlp_WriteAppBlock($dbh, $Pappinfo_raw);
# XXX - Error-checking
$err = &dlp_CloseDB($dbh);
# XXX - Error-checking

#die "599 Dying for no good reason";
		# XXX - Just to avoid writing the database prematurely
EndConduit;

sub find_cat_by_name
{
	my $appinfo = shift;
	my $name = shift;

	for (@{$appinfo->{categories}})
	{
		next unless $_->{name} eq $name;
		return $_;		# Found it
	}

	return undef;			# Can't find it
}

sub find_cat_by_id
{
	my $appinfo = shift;
	my $id = shift;

	for (@{$appinfo->{categories}})
	{
		next unless $_->{id} eq $id;
		return $_;		# Found it
	}

	return undef;			# Can't find it
}

sub find_empty_cat
{
	my $appinfo = shift;
	my $i;

	for ($i = 0; $i < NumCategories; $i++)
	{
		next unless $appinfo->{categories}[$i]{name} eq "";
		return $i;
	}

	return -1;
}

##### Debugging functions

sub hexdump
{
	my $prefix = shift;	# What to print in front of each line
	my $data = shift;	# The data to dump
	my $maxlines = shift;	# Max # of lines to dump
	my $offset;		# Offset of current chunk

	for ($offset = 0; $offset < length($data); $offset += 16)
	{
		my $hex;		# Hex values of the data
		my $ascii;		# ASCII values of the data
		my $chunk;		# Current chunk of data

		last if defined($maxlines) && ($offset >= ($maxlines * 16));

		$chunk = substr($data, $offset, 16);

		($hex = $chunk) =~ s/./sprintf "%02x ", ord($&)/ges;

		($ascii = $chunk) =~ y/\040-\176/./c;

		printf "%s %-48s|%-16s|\n", $prefix, $hex, $ascii;
	}
}

sub dumphash
{
	my $hash = shift;
	my $indent = shift;
	my $key;
	my $value;

	while (($key, $value) = each %{$hash})
	{
		if (ref($value) eq "HASH")
		{
			print $indent, $key, ":\n";
			&dumphash($value, $indent . "\t");
		} elsif (ref($value) eq "ARRAY")
		{
			print $indent, $key, ":\n";
			&dumparray($value, $indent . "\t");
		} else {
			print $indent, $key, " -> [", $value, "]\n";
		}
	}
}

sub dumparray
{
	my $array = shift;
	my $indent = shift;
	my $i;

	for ($i = 0; $i <= $#{$array}; $i++)
	{
		if (ref($array->[$i]) eq "HASH")
		{
			print $indent, $i, ":\n";
			&dumphash($array->[$i], $indent . "\t");
		} elsif (ref($array->[$i]) eq "ARRAY")
		{
			print $indent, $i, ":\n";
			&dumparray($array->[$i], $indent . "\t");
		} else {
			print $indent, $i, ": ", $array->[$i], "\n";
		}
	}
}

# This is for Emacs's benefit:
# Local Variables:	***
# fill-column:	75	***
# End:			***