kpilot/lib

pilotDatabase.h

00001 #ifndef _KPILOT_PILOTDATABASE_H
00002 #define _KPILOT_PILOTDATABASE_H
00003 /* pilotDatabase.h          KPilot
00004 **
00005 ** Copyright (C) 1998-2001 by Dan Pilone
00006 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00007 **
00008 ** This is the abstract base class for databases, which is used both
00009 ** by local databases and by the serial databases held in the Pilot.
00010 */
00011 
00012 /*
00013 ** This program is free software; you can redistribute it and/or modify
00014 ** it under the terms of the GNU Lesser General Public License as published by
00015 ** the Free Software Foundation; either version 2.1 of the License, or
00016 ** (at your option) any later version.
00017 **
00018 ** This program is distributed in the hope that it will be useful,
00019 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00020 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00021 ** GNU Lesser General Public License for more details.
00022 **
00023 ** You should have received a copy of the GNU Lesser General Public License
00024 ** along with this program in a file called COPYING; if not, write to
00025 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00026 ** MA 02110-1301, USA.
00027 */
00028 
00029 /*
00030 ** Bug reports and questions can be sent to kde-pim@kde.org
00031 */
00032 
00033 
00034 #include <qstring.h>
00035 #include <qvaluelist.h>
00036 
00037 // Handle all time.h variations properly.
00038 // Required because pi-macros.h sometimes forgets it.
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     /* -------------------- Abstract interface for subclasses ----------------- */
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         // fInfo is just a struct, so we can point to it anyway.
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         // NULL records return NULL kde objects.
00360         if (!r) return 0;
00361         // Interpret the binary blob as a pilot-link object.
00362         pilottype *a = new pilottype(r);
00363         // The record is now obsolete.
00364         delete r;
00365         // Interpretation failed.
00366         if (!a) { return 0; }
00367         // Now convert to KDE type.
00368         kdetype *t = mapper::convert(a);
00369         // The NULL mapper just returns the pointer a, so we
00370         // need to check if anything has changed before deleting.
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
KDE Home | KDE Accessibility Home | Description of Access Keys