summaryrefslogtreecommitdiffstats
path: root/lib/pilotLocalDatabase.cc
blob: ea78ac06f1555134145df7e3a3a772a8133262eb (plain)
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* KPilot
**
** Copyright (C) 1998-2001 by Dan Pilone
** Copyright (C) 2003-2004 Reinhold Kainhofer <[email protected]>
**
** This defines an interface to Pilot databases on the local disk.
*/

/*
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation; either version 2.1 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/

/*
** Bug reports and questions can be sent to [email protected]
*/


#include "options.h"

#include <stdio.h>
#include <unistd.h>
#include <assert.h>

#include <iostream>

#include <pi-file.h>

#include <tqstring.h>
#include <tqfile.h>
#include <tqregexp.h>
#include <tqdatetime.h>
#include <tqvaluevector.h>

#include <kdebug.h>
#include <tdeglobal.h>
#include <kstandarddirs.h>
#include <ksavefile.h>

#include "pilotRecord.h"
#include "pilotLocalDatabase.h"

typedef TQValueVector<PilotRecord *> Records;

class PilotLocalDatabase::Private : public Records
{
public:
	static const int DEFAULT_SIZE = 128;
	Private(int size=DEFAULT_SIZE) : Records(size) { resetIndex(); }
	~Private() { deleteRecords(); }

	void deleteRecords()
	{
		for (unsigned int i=0; i<size(); i++)
		{
			delete at(i);
		}
		clear();
		resetIndex();
	}

	void resetIndex()
	{
		current = 0;
		pending = -1;
	}

	unsigned int current;
	int pending;
} ;

PilotLocalDatabase::PilotLocalDatabase(const TQString & path,
	const TQString & dbName, bool useDefaultPath) :
	PilotDatabase(dbName),
	fPathName(path),
	fDBName(dbName),
	fAppInfo(0L),
	fAppLen(0),
	d(0L)
{
	FUNCTIONSETUP;
	fixupDBName();
	openDatabase();

	if (!isOpen() && useDefaultPath)
	{
		if (fPathBase && !fPathBase->isEmpty())
		{
			fPathName = *fPathBase;
		}
		else
		{
			fPathName = TDEGlobal::dirs()->saveLocation("data",
				CSL1("kpilot/DBBackup/"));
		}
		fixupDBName();
		openDatabase();
		if (!isOpen())
		{
			fPathName=path;
		}
	}

}

PilotLocalDatabase::PilotLocalDatabase(const TQString &dbName) :
	PilotDatabase( TQString() ),
	fPathName( TQString() ),
	fDBName( TQString() ),
	fAppInfo(0L),
	fAppLen(0),
	d(0L)
{
	FUNCTIONSETUP;

	int p = dbName.findRev( '/' );
	if (p<0)
	{
		// No slash
		fPathName = CSL1(".");
		fDBName = dbName;
	}
	else
	{
		fPathName = dbName.left(p);
		fDBName = dbName.mid(p+1);
	}
	openDatabase();
}

PilotLocalDatabase::~PilotLocalDatabase()
{
	FUNCTIONSETUP;

	closeDatabase();
	delete[]fAppInfo;
	delete d;
}

// Changes any forward slashes to underscores
void PilotLocalDatabase::fixupDBName()
{
	FUNCTIONSETUP;
	fDBName = fDBName.replace(CSL1("/"),CSL1("_"));
}

bool PilotLocalDatabase::createDatabase(long creator, long type, int, int flags, int version)
{
	FUNCTIONSETUP;

	// if the database is already open, we cannot create it again.
	// How about completely resetting it? (i.e. deleting it and then
	// creating it again)
	if (isOpen())
	{
		DEBUGKPILOT << fname << ": Database " << fDBName
			<< " already open. Cannot recreate it." << endl;
		return true;
	}

	DEBUGKPILOT << fname << ": Creating database " << fDBName << endl;

	// Database names seem to be latin1.
	Pilot::toPilot(fDBName, fDBInfo.name, sizeof(fDBInfo.name));
	fDBInfo.creator=creator;
	fDBInfo.type=type;
	fDBInfo.more=0;
	fDBInfo.flags=flags;
	fDBInfo.miscFlags=0;
	fDBInfo.version=version;
	fDBInfo.modnum=0;
	fDBInfo.index=0;
	fDBInfo.createDate=(TQDateTime::currentDateTime()).toTime_t();
	fDBInfo.modifyDate=(TQDateTime::currentDateTime()).toTime_t();
	fDBInfo.backupDate=(TQDateTime::currentDateTime()).toTime_t();

	delete[] fAppInfo;
	fAppInfo=0L;
	fAppLen=0;

	d = new Private;

	// TODO: Do I have to open it explicitly???
	setDBOpen(true);
	return true;
}

int PilotLocalDatabase::deleteDatabase()
{
	FUNCTIONSETUP;
	if (isOpen())
	{
		closeDatabase();
	}

	TQString dbpath=dbPathName();
	TQFile fl(dbpath);
	if (TQFile::remove(dbPathName()))
	{
		return 0;
	}
	else
	{
		return -1;
	}
}



// Reads the application block info
int PilotLocalDatabase::readAppBlock(unsigned char *buffer, int size)
{
	FUNCTIONSETUP;

	size_t m = kMin((size_t)size,(size_t)fAppLen);

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		memset(buffer,0,m);
		return -1;
	}

	memcpy((void *) buffer, fAppInfo, m);
	return fAppLen;
}

int PilotLocalDatabase::writeAppBlock(unsigned char *buffer, int len)
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return -1;
	}
	delete[]fAppInfo;
	fAppLen = len;
	fAppInfo = new char[fAppLen];

	memcpy(fAppInfo, (void *) buffer, fAppLen);
	return 0;
}


// returns the number of records in the database
unsigned int PilotLocalDatabase::recordCount() const
{
	if (d && isOpen())
	{
		return d->size();
	}
	else
	{
		return 0;
	}
}


// Returns a TQValueList of all record ids in the database.
TQValueList<recordid_t> PilotLocalDatabase::idList()
{
	int idlen=recordCount();
	TQValueList<recordid_t> idlist;
	if (idlen<=0)
	{
		return idlist;
	}

	// now create the TQValue list from the idarr:
	for (int i=0; i<idlen; i++)
	{
		idlist.append((*d)[i]->id());
	}

	return idlist;
}

// Reads a record from database by id, returns record length
PilotRecord *PilotLocalDatabase::readRecordById(recordid_t id)
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "Database '" << fDBName << " not open!" << endl;
		return 0L;
	}

	d->pending = -1;

	for (unsigned int i = 0; i < d->size(); i++)
	{
		if ((*d)[i]->id() == id)
		{
			PilotRecord *newRecord = new PilotRecord((*d)[i]);
			d->current = i;
			return newRecord;
		}
	}
	return 0L;
}

// Reads a record from database, returns the record
PilotRecord *PilotLocalDatabase::readRecordByIndex(int index)
{
	FUNCTIONSETUP;

	if (index < 0)
	{
		DEBUGKPILOT << fname << ": Index " << index << " is bogus." << endl;
		return 0L;
	}

	d->pending = -1;
	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return 0L;
	}

	DEBUGKPILOT << fname << ": Index=" << index << " Count=" << recordCount() << endl;

	if ( (unsigned int)index >= recordCount() )
	{
		return 0L;
	}
	PilotRecord *newRecord = new PilotRecord((*d)[index]);
	d->current = index;

	return newRecord;
}

// Reads the next record from database in category 'category'
PilotRecord *PilotLocalDatabase::readNextRecInCategory(int category)
{
	FUNCTIONSETUP;
	d->pending  = -1;
	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return 0L;
	}

	while ((d->current < d->size())
		&& ((*d)[d->current]->category() != category))
	{
		d->current++;
	}

	if (d->current >= d->size())
		return 0L;
	PilotRecord *newRecord = new PilotRecord((*d)[d->current]);

	d->current++;	// so we skip it next time
	return newRecord;
}

const PilotRecord *PilotLocalDatabase::findNextNewRecord()
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return 0L;
	}
	DEBUGKPILOT << fname << ": looking for new record from " << d->current << endl;
	// Should this also check for deleted?
	while ((d->current < d->size())
		&& ((*d)[d->current]->id() != 0 ))
	{
		d->current++;
	}

	if (d->current >= d->size())
		return 0L;

	d->pending = d->current;	// Record which one needs the new id
	d->current++;	// so we skip it next time
	return (*d)[d->pending];
}

PilotRecord *PilotLocalDatabase::readNextModifiedRec(int *ind)
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return 0L;
	}

	d->pending = -1;
	// Should this also check for deleted?
	while ((d->current < d->size())
		&& !((*d)[d->current]->isModified())  && ((*d)[d->current]->id()>0 ))
	{
		d->current++;
	}

	if (d->current >= d->size())
	{
		return 0L;
	}
	PilotRecord *newRecord = new PilotRecord((*d)[d->current]);
	if (ind)
	{
		*ind=d->current;
	}

	d->pending = d->current;	// Record which one needs the new id
	d->current++;	// so we skip it next time
	return newRecord;
}

// Writes a new ID to the record specified the index.  Not supported on Serial connections
recordid_t PilotLocalDatabase::updateID(recordid_t id)
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return 0;
	}
	if (d->pending  < 0)
	{
		WARNINGKPILOT << "Last call was NOT readNextModifiedRec()" << endl;
		return 0;
	}
	(*d)[d->pending]->setID(id);
	d->pending = -1;
	return id;
}

// Writes a new record to database (if 'id' == 0, it is assumed that this is a new record to be installed on pilot)
recordid_t PilotLocalDatabase::writeRecord(PilotRecord * newRecord)
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return 0;
	}

	d->pending = -1;
	if (!newRecord)
	{
		WARNINGKPILOT << "Record to be written is invalid!" << endl;
		return 0;
	}

	// Instead of making the app do it, assume that whenever a record is
	// written to the database it is dirty.  (You can clean up the database with
	// resetSyncFlags().)  This will make things get copied twice during a hot-sync
	// but shouldn't cause any other major headaches.
	newRecord->setModified( true );

	// First check to see if we have this record:
  	if (newRecord->id() != 0)
  	{
 		for (unsigned int i = 0; i < d->size(); i++)
 			if ((*d)[i]->id() == newRecord->id())
  			{
 				delete (*d)[i];

 				(*d)[i] = new PilotRecord(newRecord);
  				return 0;
  			}
  	}
  	// Ok, we don't have it, so just tack it on.
 	d->append( new PilotRecord(newRecord) );
  	return newRecord->id();
}

// Deletes a record with the given recordid_t from the database, or all records, if all is set to true. The recordid_t will be ignored in this case
int PilotLocalDatabase::deleteRecord(recordid_t id, bool all)
{
	FUNCTIONSETUP;
	if (!isOpen())
	{
		WARNINGKPILOT <<"DB not open"<<endl;
		return -1;
	}
	d->resetIndex();
	if (all)
	{
		d->deleteRecords();
		d->clear();
		return 0;
	}
	else
	{
		Private::Iterator i;
		for ( i=d->begin() ; i!=d->end(); ++i)
		{
			if ((*i) && (*i)->id() == id) break;
		}
		if ( (i!=d->end()) && (*i) && (*i)->id() == id)
		{
			d->erase(i);
		}
		else
		{
			// Record with this id does not exist!
			return -1;
		}
	}
	return 0;
}


// Resets all records in the database to not dirty.
int PilotLocalDatabase::resetSyncFlags()
{
	FUNCTIONSETUP;

	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return -1;
	}
	d->pending = -1;
	for (unsigned int i = 0; i < d->size(); i++)
	{
		(*d)[i]->setModified( false );
	}
	return 0;
}

// Resets next record index to beginning
int PilotLocalDatabase::resetDBIndex()
{
	FUNCTIONSETUP;
	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return -1;
	}
	d->resetIndex();
	return 0;
}

// Purges all Archived/Deleted records from Palm Pilot database
int PilotLocalDatabase::cleanup()
{
	FUNCTIONSETUP;
	if (!isOpen())
	{
		WARNINGKPILOT << "DB not open!" << endl;
		return -1;
	}
	d->resetIndex();

	/* Not the for loop one might expect since when we erase()
	* a record the iterator changes too.
	*/
	Private::Iterator i = d->begin();
	while ( i!=d->end() )
	{
		if ( (*i)->isDeleted() || (*i)->isArchived() )
		{
			delete (*i);
			i = d->erase(i);
		}
		else
		{
			++i;
		}
	}

	// Don't have to do anything.  Will be taken care of by closeDatabase()...
	// Changed!
	return 0;
}

TQString PilotLocalDatabase::dbPathName() const
{
	FUNCTIONSETUP;
	TQString tempName(fPathName);
	TQString slash = CSL1("/");

	if (!tempName.endsWith(slash)) tempName += slash;
	tempName += getDBName();
	tempName += CSL1(".pdb");
	return tempName;
}

void PilotLocalDatabase::openDatabase()
{
	FUNCTIONSETUP;

	pi_file *dbFile;

	setDBOpen(false);

	dbFile = pi_file_open( TQFile::encodeName(dbPathName()) );
	if (dbFile == 0L)
	{
		TQString path = dbPathName();
		DEBUGKPILOT << fname << ": Failed to open " << path << endl;
		return;
	}


	PI_SIZE_T size = 0;
	void *tmpBuffer;
	pi_file_get_info(dbFile, &fDBInfo);
	pi_file_get_app_info(dbFile, &tmpBuffer, &size);
	fAppLen = size;
	fAppInfo = new char[fAppLen];
	memcpy(fAppInfo, tmpBuffer, fAppLen);

	int count;
	pi_file_get_entries(dbFile, &count);
	if (count >= 0)
	{
		KPILOT_DELETE(d);
		d = new Private(count);
	}

	int attr, cat;
	recordid_t id;
	unsigned int i = 0;
	while (pi_file_read_record(dbFile, i,
			&tmpBuffer, &size, &attr, &cat, &id) == 0)
	{
		pi_buffer_t *b = pi_buffer_new(size);
		memcpy(b->data,tmpBuffer,size);
		b->used = size;
		(*d)[i] = new PilotRecord(b, attr, cat, id);
		i++;
	}
	pi_file_close(dbFile);	// We done with it once we've read it in.

	KSaveFile::backupFile( dbPathName() );

	setDBOpen(true);
}

void PilotLocalDatabase::closeDatabase()
{
	FUNCTIONSETUP;
	pi_file *dbFile;

	if (!isOpen())
	{
		DEBUGKPILOT << fname << ": Database " << fDBName
			<< " is not open. Cannot close and write it"
			<< endl;
		return;
	}

	TQString newName = dbPathName() + CSL1(".new");
	TQString path = dbPathName();
	DEBUGKPILOT << fname
		<< ": Creating temp file " << newName
		<< " for the database file " << path << endl;

	dbFile = pi_file_create(TQFile::encodeName(newName),&fDBInfo);
	pi_file_set_app_info(dbFile, fAppInfo, fAppLen);

	for (unsigned int i = 0; i < d->size(); i++)
	{
		// How did a NULL pointer sneak in here?
		if (!(*d)[i])
		{
			continue;
		}

		if (((*d)[i]->id() == 0) && ((*d)[i]->isDeleted()))
		{
			// Just ignore it
		}
		else
		{
			pi_file_append_record(dbFile,
				(*d)[i]->data(),
				(*d)[i]->size(),
				(*d)[i]->attributes(), (*d)[i]->category(),
				(*d)[i]->id());
		}
	}

	pi_file_close(dbFile);
	TQFile::remove(dbPathName());
	rename((const char *) TQFile::encodeName(newName),
		(const char *) TQFile::encodeName(dbPathName()));
	setDBOpen(false);
}


TQString *PilotLocalDatabase::fPathBase = 0L;

void PilotLocalDatabase::setDBPath(const TQString &s)
{
	FUNCTIONSETUP;

	DEBUGKPILOT << fname
		<< ": Setting default DB path to "
		<< s
		<< endl;

	if (!fPathBase)
	{
		fPathBase = new TQString(s);
	}
	else
	{
		*fPathBase = s;
	}
}

/* virtual */ PilotDatabase::DBType PilotLocalDatabase::dbType() const
{
	return eLocalDB;
}


/* static */ bool PilotLocalDatabase::infoFromFile( const TQString &path, DBInfo *d )
{
	FUNCTIONSETUP;

	pi_file *f = 0L;

	if (!d)
	{
		return false;
	}
	if (!TQFile::exists(path))
	{
		return false;
	}

	TQCString fileName = TQFile::encodeName( path );
	f = pi_file_open( fileName );
	if (!f)
	{
		WARNINGKPILOT << "Can't open " << path << endl;
		return false;
	}

	pi_file_get_info(f,d);
	pi_file_close(f);

	return true;
}