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 ** Copyright (C) 2005-2006 Adriaan de Groot <groot@kde.org>
00008 **
00009 ** This is the abstract base class for databases, which is used both
00010 ** by local databases and by the serial databases held in the Pilot.
00011 */
00012 
00013 /*
00014 ** This program is free software; you can redistribute it and/or modify
00015 ** it under the terms of the GNU Lesser General Public License as published by
00016 ** the Free Software Foundation; either version 2.1 of the License, or
00017 ** (at your option) any later version.
00018 **
00019 ** This program is distributed in the hope that it will be useful,
00020 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00022 ** GNU Lesser General Public License for more details.
00023 **
00024 ** You should have received a copy of the GNU Lesser General Public License
00025 ** along with this program in a file called COPYING; if not, write to
00026 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00027 ** MA 02110-1301, USA.
00028 */
00029 
00030 /*
00031 ** Bug reports and questions can be sent to kde-pim@kde.org
00032 */
00033 
00034 
00035 #include "pilot.h"
00036 
00037 
00046 class KDE_EXPORT PilotDatabase
00047 {
00048 public:
00049     PilotDatabase(const QString &name = QString::null);
00050     virtual ~PilotDatabase();
00051 
00052 
00053     QString name() const { return fName; } ;
00054 
00059     static int count();
00060 
00061     /* -------------------- Abstract interface for subclasses ----------------- */
00062 
00068     virtual bool createDatabase(long creator=0, long type=0,
00069         int cardno=0, int flags=0, int version=0) = 0;
00070 
00076     virtual int deleteDatabase()=0;
00077 
00079     virtual int readAppBlock(unsigned char* buffer, int maxLen) = 0;
00080 
00082     virtual int writeAppBlock(unsigned char* buffer, int len) = 0;
00083 
00085     virtual int recordCount()=0;
00086 
00089     virtual Pilot::RecordIDList idList();
00090 
00093     virtual Pilot::RecordIDList modifiedIDList();
00094 
00095 
00097     virtual PilotRecord* readRecordById(recordid_t id) = 0;
00098 
00100     virtual PilotRecord* readRecordByIndex(int index) = 0;
00101 
00103     virtual PilotRecord* readNextRecInCategory(int category) = 0;
00104 
00111     virtual PilotRecord* readNextModifiedRec(int *ind=NULL) = 0;
00112 
00117     virtual recordid_t writeRecord(PilotRecord* newRecord) = 0;
00118 
00126     virtual int deleteRecord(recordid_t id, bool all=false) = 0;
00127 
00129     virtual int resetSyncFlags() = 0;
00130 
00132     virtual int resetDBIndex() = 0;
00133 
00135     virtual int cleanup() = 0;
00136 
00137     bool isOpen() const { return fDBOpen; }
00138 
00143     virtual QString dbPathName() const = 0;
00144 
00149     typedef enum { eNone=0,
00150         eLocalDB=1,
00151         eSerialDB=2 } DBType;
00152     virtual DBType dbType() const = 0;
00153 
00154     static inline bool isResource(struct DBInfo *info)
00155     {
00156         return (info->flags & dlpDBFlagResource);
00157     }
00158 
00159 protected:
00160     virtual void openDatabase() = 0;
00161     virtual void closeDatabase() = 0;
00162 
00163     void setDBOpen(bool yesno) { fDBOpen = yesno; }
00164 
00165 private:
00166     bool fDBOpen;
00167     QString fName;
00168 };
00169 
00185 template <class kdetype, class pilottype, class mapper>
00186 class DatabaseInterpreter
00187 {
00188 private:
00190     kdetype *interpret(PilotRecord *r)
00191     {
00192         // NULL records return NULL kde objects.
00193         if (!r) return 0;
00194         // Interpret the binary blob as a pilot-link object.
00195         pilottype *a = new pilottype(r);
00196         // The record is now obsolete.
00197         delete r;
00198         // Interpretation failed.
00199         if (!a) { return 0; }
00200         // Now convert to KDE type.
00201         kdetype *t = mapper::convert(a);
00202         // The NULL mapper just returns the pointer a, so we
00203         // need to check if anything has changed before deleting.
00204         if ( (void *)t != (void *)a )
00205         {
00206             delete a;
00207         }
00208         return t;
00209     }
00210 public:
00212     DatabaseInterpreter(PilotDatabase *d) : fDB(d) { } ;
00213 
00215     kdetype *readRecordById(recordid_t id)
00216     {
00217         return interpret(fDB->readRecordById(id));
00218     }
00219 
00221     kdetype *readRecordByIndex(int index)
00222     {
00223         return interpret(fDB->readRecordByIndex(index));
00224     }
00225 
00227     kdetype *readNextRecInCategory(int category)
00228     {
00229         return interpret(fDB->readNextRecInCategory(category));
00230     }
00231 
00238     kdetype *readNextModifiedRec(int *ind=NULL)
00239     {
00240         return interpret(fDB->readNextModifiedRec(ind));
00241     }
00242 
00243 
00248     PilotDatabase *db() const { return fDB; }
00249 
00250 protected:
00251     PilotDatabase *fDB;
00252 } ;
00253 
00258 template <class T>
00259 class NullMapper
00260 {
00261 public:
00263     static T *convert(T *t) { return t; }
00264 } ;
00265 
00266 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys