kpilot

pilotDatabase.h

Go to the documentation of this file.
00001 #ifndef _KPILOT_PILOTDATABASE_H
00002 #define _KPILOT_PILOTDATABASE_H
00003 /* KPilot
00004 **
00005 ** Copyright (C) 1998-2001 by Dan Pilone
00006 ** Copyright (C) 2003-2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00007 ** Copyright (C) 2005-2006 Adriaan de Groot <groot@kde.org>
00008 **
00009 */
00010 
00011 /*
00012 ** This program is free software; you can redistribute it and/or modify
00013 ** it under the terms of the GNU Lesser General Public License as published by
00014 ** the Free Software Foundation; either version 2.1 of the License, or
00015 ** (at your option) any later version.
00016 **
00017 ** This program is distributed in the hope that it will be useful,
00018 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 ** GNU Lesser General Public License for more details.
00021 **
00022 ** You should have received a copy of the GNU Lesser General Public License
00023 ** along with this program in a file called COPYING; if not, write to
00024 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 ** MA 02110-1301, USA.
00026 */
00027 
00028 /*
00029 ** Bug reports and questions can be sent to kde-pim@kde.org
00030 */
00031 
00032 
00033 #include "pilot.h"
00034 
00035 
00050 class KDE_EXPORT PilotDatabase
00051 {
00052 public:
00053     PilotDatabase(const QString &name = QString::null);
00054     virtual ~PilotDatabase();
00055 
00056 
00057     QString name() const { return fName; } ;
00058 
00063     static int instanceCount();
00064 
00065     /* -------------------- Abstract interface for subclasses ----------------- */
00066 
00072     virtual bool createDatabase(long creator=0, long type=0,
00073         int cardno=0, int flags=0, int version=0) = 0;
00074 
00080     virtual int deleteDatabase()=0;
00081 
00083     virtual int readAppBlock(unsigned char* buffer, int maxLen) = 0;
00084 
00086     virtual int writeAppBlock(unsigned char* buffer, int len) = 0;
00087 
00091     virtual unsigned int recordCount() const=0;
00092 
00095     virtual Pilot::RecordIDList idList();
00096 
00099     virtual Pilot::RecordIDList modifiedIDList();
00100 
00101 
00103     virtual PilotRecord* readRecordById(recordid_t id) = 0;
00104 
00106     virtual PilotRecord* readRecordByIndex(int index) = 0;
00107 
00109     virtual PilotRecord* readNextRecInCategory(int category) = 0;
00110 
00117     virtual PilotRecord* readNextModifiedRec(int *ind=NULL) = 0;
00118 
00123     virtual recordid_t writeRecord(PilotRecord* newRecord) = 0;
00124 
00132     virtual int deleteRecord(recordid_t id, bool all=false) = 0;
00133 
00135     virtual int resetSyncFlags() = 0;
00136 
00138     virtual int resetDBIndex() = 0;
00139 
00141     virtual int cleanup() = 0;
00142 
00143     bool isOpen() const { return fDBOpen; }
00144 
00149     virtual QString dbPathName() const = 0;
00150 
00155     typedef enum { eNone=0,
00156         eLocalDB=1,
00157         eSerialDB=2 } DBType;
00158     virtual DBType dbType() const = 0;
00159 
00160     static inline bool isResource(struct DBInfo *info)
00161     {
00162         return (info->flags & dlpDBFlagResource);
00163     }
00164 
00165 protected:
00166     virtual void openDatabase() = 0;
00167     virtual void closeDatabase() = 0;
00168 
00169     void setDBOpen(bool yesno) { fDBOpen = yesno; }
00170 
00171 private:
00172     bool fDBOpen;
00173     QString fName;
00174 };
00175 
00191 template <class kdetype, class pilottype, class mapper>
00192 class DatabaseInterpreter
00193 {
00194 private:
00196     kdetype *interpret(PilotRecord *r)
00197     {
00198         // NULL records return NULL kde objects.
00199         if (!r) return 0;
00200         // Interpret the binary blob as a pilot-link object.
00201         pilottype *a = new pilottype(r);
00202         // The record is now obsolete.
00203         delete r;
00204         // Interpretation failed.
00205         if (!a) { return 0; }
00206         // Now convert to KDE type.
00207         kdetype *t = mapper::convert(a);
00208         // The NULL mapper just returns the pointer a, so we
00209         // need to check if anything has changed before deleting.
00210         if ( (void *)t != (void *)a )
00211         {
00212             delete a;
00213         }
00214         return t;
00215     }
00216 public:
00218     DatabaseInterpreter(PilotDatabase *d) : fDB(d) { } ;
00219 
00221     kdetype *readRecordById(recordid_t id)
00222     {
00223         return interpret(fDB->readRecordById(id));
00224     }
00225 
00227     kdetype *readRecordByIndex(int index)
00228     {
00229         return interpret(fDB->readRecordByIndex(index));
00230     }
00231 
00233     kdetype *readNextRecInCategory(int category)
00234     {
00235         return interpret(fDB->readNextRecInCategory(category));
00236     }
00237 
00244     kdetype *readNextModifiedRec(int *ind=NULL)
00245     {
00246         return interpret(fDB->readNextModifiedRec(ind));
00247     }
00248 
00249 
00254     PilotDatabase *db() const { return fDB; }
00255 
00256 protected:
00257     PilotDatabase *fDB;
00258 } ;
00259 
00264 template <class T>
00265 class NullMapper
00266 {
00267 public:
00269     static T *convert(T *t) { return t; }
00270 } ;
00271 
00272 #endif