kpilot/lib

recordConduit.h

Go to the documentation of this file.
00001 #ifndef _RECORD_CONDUIT_H
00002 #define _RECORD_CONDUIT_H
00003 /* record-conduit.h                           KPilot
00004 **
00005 ** Copyright (C) 2005 by Adriaan de Groot
00006 **
00007 */
00008 
00009 /*
00010 ** This program is free software; you can redistribute it and/or modify
00011 ** it under the terms of the GNU General Public License as published by
00012 ** the Free Software Foundation; either version 2 of the License, or
00013 ** (at your option) any later version.
00014 **
00015 ** This program is distributed in the hope that it will be useful,
00016 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00018 ** GNU General Public License for more details.
00019 **
00020 ** You should have received a copy of the GNU General Public License
00021 ** along with this program in a file called COPYING; if not, write to
00022 ** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 ** MA 02110-1301, USA.
00024 */
00025 
00026 /*
00027 ** Bug reports and questions can be sent to kde-pim@kde.org
00028 */
00029 
00030 #include <qtimer.h>
00031 #include <klocale.h>
00032 
00033 #include "plugin.h"
00034 #include "pilotAppCategory.h"
00035 #include "pilotDatabase.h"
00036 
00037 
00054 class RecordConduitBase : public ConduitAction
00055 {
00056 Q_OBJECT
00057 public:
00060     RecordConduitBase(KPilotDeviceLink *o,
00061         const char *n,
00062         const QStringList a = QStringList()) :
00063         ConduitAction(o,n,a),
00064         fTimer(0L)
00065     {
00066     } ;
00068     virtual ~RecordConduitBase()
00069     {
00070         // delete fTimer; // Timer is a child object
00071     } ;
00072 
00078     typedef enum { HHtoPC=0, PCtoHH=1, Both=2 } SyncDirection;
00079 
00085     typedef enum { NotDone=0, Done=1, Error=2 } SyncProgress;
00086 
00087 protected:
00092     virtual SyncProgress loadPC() = 0;
00093 
00100     virtual SyncProgress palmRecToPC() = 0;
00101 
00108     virtual SyncProgress cleanup() = 0;
00109 
00110 protected slots:
00114     void process();
00115 
00116 protected:
00117     virtual bool exec();
00118 
00119 private:
00121     QTimer *fTimer;
00122 
00124     enum { Initialize, PalmToPC, Cleanup } fState;
00125 
00126     QMap<recordid_t,QString> fUIDMap;
00127     RecordIDList fIDList;
00128     RecordIDList::Iterator fIDListIterator;
00129 
00130     QString fDBName;
00131 } ;
00132 
00133 template <class PCEntry, class PCContainer, class HHEntry, class HHAppInfo, class Syncer>
00134 class RecordConduit : public RecordConduitBase
00135 {
00136 public:
00138     RecordConduit(const QString &name ,
00139         KPilotDeviceLink *o ,
00140         const char *n ,
00141         const QStringList a = QStringList() ) :
00142         RecordConduitBase(o,n,a)
00143     {
00144         fConduitName=name;
00145     } ;
00146     virtual ~RecordConduit()
00147     {
00148     } ;
00149 
00150     virtual SyncProgress loadPC()
00151     {
00152         fAppInfo = new HHAppInfo(fDatabase) ;
00153         fContainer = new PCContainer();
00154         if (!fContainer->load())
00155         {
00156             emit logError(i18n("Unable to load the %1 database on the PC.").arg(fConduitName));
00157             return Error;
00158         }
00159         if (fContainer->isEmpty()) setFirstSync(true); /* And leave UID map empty */
00160         else fContainer->mapToRecords(fUIDMap);
00161 
00162         return Done;
00163     } ;
00164 
00165     virtual SyncProgress palmRecToPC()
00166     {
00167         if ( fIDListIterator == fIDList.end() )
00168         {
00169             return Done;
00170         }
00171 
00172         recordid_t currentID = *fIDListIterator++;
00173         PilotRecord *rec = fDatabase->readRecordById(currentID);
00174         HHEntry *currentHH = 0;
00175         PCEntry *currentPC = 0;
00176         Q_ASSERT(rec);
00177         if (fUIDMap.contains(currentID))
00178         {
00179             QString currentUID = fUIDMap[currentID];
00180             // This is a modified entry or it is deleted on the HH
00181             if (rec->isDeleted())
00182             {
00183                 fContainer->remove(currentUID);
00184             }
00185             else
00186             {
00187                 currentHH = new HHEntry(rec);
00188                 currentPC = fContainer->get(currentUID);
00189                 Syncer::sync(currentPC,currentHH,fAppInfo,HHtoPC);
00190             }
00191         }
00192         else
00193         {
00194             // Deleted on HH, unknown on PC -> Ignore it.
00195             // Not deleted, unknown -> New record.
00196             if (!rec->isDeleted())
00197             {
00198                 currentHH = new HHEntry(rec);
00199                 currentPC = new PCEntry();
00200                 Syncer::sync(currentPC,currentHH,fAppInfo,HHtoPC);
00201                 fContainer->insert(currentPC);
00202             }
00203         }
00204         delete rec;
00205         delete currentHH;
00206         // delete currentPC; Ownership passed to the container
00207 
00208         return NotDone;
00209     }
00210 
00211     virtual SyncProgress cleanup()
00212     {
00213         delete fAppInfo;
00214         fContainer->save();
00215         delete fContainer;
00216         return Done;
00217     }
00218 
00219     virtual bool getAppInfo( unsigned char *buffer, int appLen ) { return true; } ;
00220 
00221 protected:
00222     HHAppInfo *fAppInfo;
00223     PCContainer *fContainer;
00224 } ;
00225 
00226 
00227 #endif
00228 
KDE Home | KDE Accessibility Home | Description of Access Keys