00001 #ifndef _KPILOT_PILOTDATABASE_H
00002 #define _KPILOT_PILOTDATABASE_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include <qstring.h>
00035 #include <qvaluelist.h>
00036
00037
00038
00039
00040 #ifdef TIME_WITH_SYS_TIME
00041 # include <sys/time.h>
00042 # include <time.h>
00043 #else
00044 # ifdef HAVE_SYS_TIME_H
00045 # include <sys/time.h>
00046 # else
00047 # include <time.h>
00048 # endif
00049 #endif
00050
00051 #include "pilotLinkVersion.h"
00052
00053 #include <pi-dlp.h>
00054
00055
00056 class PilotRecord;
00057 struct CategoryAppInfo;
00058
00059 typedef QValueList<recordid_t> RecordIDList;
00060
00061
00062
00071 class KDE_EXPORT PilotDatabase
00072 {
00073 public:
00074 PilotDatabase(const QString &name = QString::null);
00075 virtual ~PilotDatabase();
00076
00077
00078 QString name() const { return fName; } ;
00079
00084 static int count();
00085
00086
00087
00093 virtual bool createDatabase(long creator=0, long type=0,
00094 int cardno=0, int flags=0, int version=0) = 0;
00095
00101 virtual int deleteDatabase()=0;
00102
00104 virtual int readAppBlock(unsigned char* buffer, int maxLen) = 0;
00105
00107 virtual int writeAppBlock(unsigned char* buffer, int len) = 0;
00108
00110 virtual int recordCount()=0;
00111
00114 virtual RecordIDList idList();
00117 virtual RecordIDList modifiedIDList();
00118
00119
00121 virtual PilotRecord* readRecordById(recordid_t id) = 0;
00122
00124 virtual PilotRecord* readRecordByIndex(int index) = 0;
00125
00127 virtual PilotRecord* readNextRecInCategory(int category) = 0;
00128
00135 virtual PilotRecord* readNextModifiedRec(int *ind=NULL) = 0;
00136
00141 virtual recordid_t writeRecord(PilotRecord* newRecord) = 0;
00142
00150 virtual int deleteRecord(recordid_t id, bool all=false) = 0;
00151
00153 virtual int resetSyncFlags() = 0;
00154
00156 virtual int resetDBIndex() = 0;
00157
00159 virtual int cleanup() = 0;
00160
00161 bool isDBOpen() const { return fDBOpen; }
00162
00167 virtual QString dbPathName() const = 0;
00168
00173 typedef enum { eNone=0,
00174 eLocalDB=1,
00175 eSerialDB=2 } DBType;
00176 virtual DBType dbType() const = 0;
00177
00178 static inline bool isResource(struct DBInfo *info)
00179 {
00180 return (info->flags & dlpDBFlagResource);
00181 }
00182
00183 protected:
00184 virtual void openDatabase() = 0;
00185 virtual void closeDatabase() = 0;
00186
00187 void setDBOpen(bool yesno) { fDBOpen = yesno; }
00188
00189 private:
00190 bool fDBOpen;
00191 QString fName;
00192 };
00193
00195 class KDE_EXPORT PilotAppInfoBase
00196 {
00197 protected:
00202 PilotAppInfoBase() : fC(0L), fLen(0), fOwn(false) { } ;
00203
00207 void init(struct CategoryAppInfo *c, int len)
00208 {
00209 fC = c;
00210 fLen = len ;
00211 } ;
00212
00213 public:
00215 static const int MAX_APPINFO_SIZE=8192;
00216
00221 PilotAppInfoBase(PilotDatabase *d);
00223 virtual ~PilotAppInfoBase();
00224
00229 struct CategoryAppInfo *categoryInfo() { return fC; } ;
00231 const struct CategoryAppInfo *categoryInfo() const { return fC; } ;
00233 PI_SIZE_T length() const { return fLen; } ;
00234
00249 static int findCategory(const QString &name, bool unknownIsUnfiled, struct CategoryAppInfo *info);
00251 int findCategory(const QString &name, bool unknownIsUnfiled = false)
00252 { return findCategory(name,unknownIsUnfiled,categoryInfo()); } ;
00253
00255 void dump() const;
00256
00260 static void dumpCategories(const struct CategoryAppInfo &info);
00261
00264 QString category(unsigned int i);
00265
00270 bool setCategoryName(unsigned int i, const QString &s);
00271
00272 private:
00273 struct CategoryAppInfo *fC;
00274 PI_SIZE_T fLen;
00275 bool fOwn;
00276 } ;
00277
00285 template <typename appinfo,
00286 int(*unpack)(appinfo *, unsigned char *, PI_SIZE_T),
00287 int(*pack)(appinfo *, unsigned char *, PI_SIZE_T)>
00288 class PilotAppInfo : public PilotAppInfoBase
00289 {
00290 public:
00294 PilotAppInfo(PilotDatabase *d) : PilotAppInfoBase()
00295 {
00296 int appLen = MAX_APPINFO_SIZE;
00297 unsigned char buffer[MAX_APPINFO_SIZE];
00298
00299 memset(&fInfo,0,sizeof(fInfo));
00300 if (d && d->isDBOpen())
00301 {
00302 appLen = d->readAppBlock(buffer,appLen);
00303 (*unpack)(&fInfo, buffer, appLen);
00304 }
00305
00306 init(&fInfo.category,appLen);
00307 } ;
00308
00313 int write(PilotDatabase *d)
00314 {
00315 unsigned char buffer[MAX_APPINFO_SIZE];
00316 if (!d || !d->isDBOpen())
00317 {
00318 return -1;
00319 }
00320 int appLen = (*pack)(&fInfo, buffer, length());
00321 if (appLen > 0)
00322 {
00323 d->writeAppBlock(buffer,appLen);
00324 }
00325 return appLen;
00326 } ;
00327
00331 appinfo *info() { return &fInfo; } ;
00332
00333 protected:
00334 appinfo fInfo;
00335 } ;
00336
00352 template <class kdetype, class pilottype, class mapper>
00353 class DatabaseInterpreter
00354 {
00355 private:
00357 kdetype *interpret(PilotRecord *r)
00358 {
00359
00360 if (!r) return 0;
00361
00362 pilottype *a = new pilottype(r);
00363
00364 delete r;
00365
00366 if (!a) { return 0; }
00367
00368 kdetype *t = mapper::convert(a);
00369
00370
00371 if ( (void *)t != (void *)a )
00372 {
00373 delete a;
00374 }
00375 return t;
00376 }
00377 public:
00379 DatabaseInterpreter(PilotDatabase *d) : fDB(d) { } ;
00380
00382 kdetype *readRecordById(recordid_t id)
00383 {
00384 return interpret(fDB->readRecordById(id));
00385 }
00386
00388 kdetype *readRecordByIndex(int index)
00389 {
00390 return interpret(fDB->readRecordByIndex(index));
00391 }
00392
00394 kdetype *readNextRecInCategory(int category)
00395 {
00396 return interpret(fDB->readNextRecInCategory(category));
00397 }
00398
00405 kdetype *readNextModifiedRec(int *ind=NULL)
00406 {
00407 return interpret(fDB->readNextModifiedRec(ind));
00408 }
00409
00410
00415 PilotDatabase *db() const { return fDB; }
00416
00417 protected:
00418 PilotDatabase *fDB;
00419 } ;
00420
00425 template <class T>
00426 class NullMapper
00427 {
00428 public:
00430 static T *convert(T *t) { return t; }
00431 } ;
00432
00433 #endif