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 instanceCount();
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 
00087     virtual unsigned int recordCount() const=0;
00088 
00091     virtual Pilot::RecordIDList idList();
00092 
00095     virtual Pilot::RecordIDList modifiedIDList();
00096 
00097 
00099     virtual PilotRecord* readRecordById(recordid_t id) = 0;
00100 
00102     virtual PilotRecord* readRecordByIndex(int index) = 0;
00103 
00105     virtual PilotRecord* readNextRecInCategory(int category) = 0;
00106 
00113     virtual PilotRecord* readNextModifiedRec(int *ind=NULL) = 0;
00114 
00119     virtual recordid_t writeRecord(PilotRecord* newRecord) = 0;
00120 
00128     virtual int deleteRecord(recordid_t id, bool all=false) = 0;
00129 
00131     virtual int resetSyncFlags() = 0;
00132 
00134     virtual int resetDBIndex() = 0;
00135 
00137     virtual int cleanup() = 0;
00138 
00139     bool isOpen() const { return fDBOpen; }
00140 
00145     virtual QString dbPathName() const = 0;
00146 
00151     typedef enum { eNone=0,
00152         eLocalDB=1,
00153         eSerialDB=2 } DBType;
00154     virtual DBType dbType() const = 0;
00155 
00156     static inline bool isResource(struct DBInfo *info)
00157     {
00158         return (info->flags & dlpDBFlagResource);
00159     }
00160 
00161 protected:
00162     virtual void openDatabase() = 0;
00163     virtual void closeDatabase() = 0;
00164 
00165     void setDBOpen(bool yesno) { fDBOpen = yesno; }
00166 
00167 private:
00168     bool fDBOpen;
00169     QString fName;
00170 };
00171 
00187 template <class kdetype, class pilottype, class mapper>
00188 class DatabaseInterpreter
00189 {
00190 private:
00192     kdetype *interpret(PilotRecord *r)
00193     {
00194         // NULL records return NULL kde objects.
00195         if (!r) return 0;
00196         // Interpret the binary blob as a pilot-link object.
00197         pilottype *a = new pilottype(r);
00198         // The record is now obsolete.
00199         delete r;
00200         // Interpretation failed.
00201         if (!a) { return 0; }
00202         // Now convert to KDE type.
00203         kdetype *t = mapper::convert(a);
00204         // The NULL mapper just returns the pointer a, so we
00205         // need to check if anything has changed before deleting.
00206         if ( (void *)t != (void *)a )
00207         {
00208             delete a;
00209         }
00210         return t;
00211     }
00212 public:
00214     DatabaseInterpreter(PilotDatabase *d) : fDB(d) { } ;
00215 
00217     kdetype *readRecordById(recordid_t id)
00218     {
00219         return interpret(fDB->readRecordById(id));
00220     }
00221 
00223     kdetype *readRecordByIndex(int index)
00224     {
00225         return interpret(fDB->readRecordByIndex(index));
00226     }
00227 
00229     kdetype *readNextRecInCategory(int category)
00230     {
00231         return interpret(fDB->readNextRecInCategory(category));
00232     }
00233 
00240     kdetype *readNextModifiedRec(int *ind=NULL)
00241     {
00242         return interpret(fDB->readNextModifiedRec(ind));
00243     }
00244 
00245 
00250     PilotDatabase *db() const { return fDB; }
00251 
00252 protected:
00253     PilotDatabase *fDB;
00254 } ;
00255 
00260 template <class T>
00261 class NullMapper
00262 {
00263 public:
00265     static T *convert(T *t) { return t; }
00266 } ;
00267 
00268 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys