00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include "options.h"
00032
00033 #if QT_VERSION < 300
00034 #include <qmsgbox.h>
00035 #else
00036 #include <qmessagebox.h>
00037 #endif
00038
00039 #include <unistd.h>
00040 #include <qtimer.h>
00041 #include <kconfig.h>
00042 #include <kmessagebox.h>
00043
00044 #include <pilotUser.h>
00045 #include "pilotRecord.h"
00046 #include "pilotSerialDatabase.h"
00047 #include "MultiDB-conduit.h"
00048 #include "DatabaseAction.h"
00049
00050 using namespace KCal;
00051
00052 static const char *MultiDB_conduit_id = "$Id: MultiDB-conduit.cc,v 1.5.4.1 2003/03/12 23:31:10 adridg Exp $";
00053
00054
00055
00056 MultiDBConduit::MultiDBConduit(KPilotDeviceLink *d, const char *n, const QStringList &l, SyncTypeList_t *tps) : ConduitAction(d,n,l), dbases(), fCurrentDatabase(), fBackupDatabase() {
00057 FUNCTIONSETUP;
00058 (void)MultiDB_conduit_id;
00059 dbnr=0;
00060 synctypes=tps;
00061 }
00062
00063 void MultiDBConduit::cleanup() {
00064 FUNCTIONSETUP;
00065
00066 if (!fConfig) return;
00067
00068 KConfigGroupSaver cfgs(fConfig, conduitSettingsGroup());
00069 fConfig->writeEntry(conduitSettingsGroup(), dbases);
00070 cleanupDB();
00071 }
00072
00073 void MultiDBConduit::cleanupDB() {
00074 FUNCTIONSETUP;
00075 KPILOT_DELETE(fCurrentDatabase);
00076 KPILOT_DELETE(fBackupDatabase);
00077 }
00078
00079
00080 bool MultiDBConduit::preSyncAction(DBSyncInfo*dbinfo) {
00081 FUNCTIONSETUP;
00082 switch (dbinfo->syncaction) {
00083 case st_ask:
00084 case st_ignore:
00085 case st_backup:
00086 return false;
00087 break;
00088 }
00089 return true;
00090 }
00091
00092 void MultiDBConduit::finishedDB() {
00093 QTimer::singleShot(0, this, SLOT(syncNextDB()));
00094 };
00095 void MultiDBConduit::syncNextRecord() {
00096 QTimer::singleShot(0, this, SLOT(finishedDB()));
00097 };
00098
00099
00100 void MultiDBConduit::syncNextDB() {
00101 FUNCTIONSETUP;
00102 DBInfo dbinfo;
00103 KPilotUser*usr;
00104
00105 cleanupDB();
00106 if (fHandle->getNextDatabase(++dbnr, &dbinfo)<=0) {
00107
00108 QTimer::singleShot(0, this, SLOT(cleanup()));
00109 return;
00110 }
00111
00112 KConfigGroupSaver cfgs(fConfig, conduitSettingsGroup());
00113
00114 if (!fConfig) {
00115 kdWarning() << k_funcinfo <<": No configuration set for conduit. Skipping database "<<dbinfo.name<<"..."<<endl;
00116 goto skip;
00117 }
00118
00119
00120 if (!isCorrectDBTypeCreator(dbinfo)) goto skip;
00121
00122 if (!GetSyncType(dbinfo, &syncinfo)) goto skip;
00123
00124 dbases.append(dbinfo.name);
00125 #ifdef DEBUG
00126 DEBUGCONDUIT << fname << ": Using file " << syncinfo.filename << " to sync the palm database "<< dbinfo.name << endl;
00127 #endif
00128
00129
00130 if (!preSyncAction(&syncinfo)) {
00131 #ifdef DEBUG
00132 DEBUGCONDUIT << "Error initializing the databases for "<<dbinfo.name<<endl;
00133 #endif
00134 emit logError(i18n("Unable to initialize the database: ")+dbinfo.name);
00135 goto error;
00136 }
00137
00138 usr=fHandle->getPilotUser();
00139 fFullSync |= (syncinfo.synctype==SYNC_FULL) || ( (usr->getLastSyncPC()!=(unsigned long)gethostid()) && fConfig->readBoolEntry(MultiDBConduitFactory::fullSyncOnPCChange, true));
00140
00141
00142
00143 fBackupDatabase = new PilotLocalDatabase(dbinfo.name);
00144 if (!fBackupDatabase) goto error;
00145
00146 if (!fBackupDatabase->isDBOpen()) {
00147 #ifdef DEBUG
00148 DEBUGCONDUIT<<"Backup databse "<<fBackupDatabase->dbPathName()<<" could not be opened. Will fetch a copy from the palm and do a full sync"<<endl;
00149 #endif
00150 struct DBInfo dbinfo;
00151 char nm[50];
00152 strncpy(&nm[0], &dbinfo.name[0], sizeof(nm));
00153 if (fHandle->findDatabase(&nm[0], &dbinfo)<0 ){
00154 #ifdef DEBUG
00155 DEBUGCONDUIT<<fname<<": Could not get DBInfo for "<<nm<<"! Skipping databse"<<endl;
00156 #endif
00157 goto error;
00158 }
00159 dbinfo.flags &= ~dlpDBFlagOpen;
00160 if (!fHandle->retrieveDatabase(fBackupDatabase->dbPathName(), &dbinfo) ) goto error;
00161 KPILOT_DELETE(fBackupDatabase);
00162 fBackupDatabase=new PilotLocalDatabase(dbinfo.name);
00163 if (!fBackupDatabase || ! fBackupDatabase->isDBOpen() ) goto error;
00164 fFullSync=true;
00165 }
00166
00167 fCurrentDatabase = new PilotSerialDatabase(pilotSocket(), dbinfo.name, this, dbinfo.name );
00168
00169
00170 if (!fCurrentDatabase || !fBackupDatabase || !fCurrentDatabase->isDBOpen() || !fBackupDatabase->isDBOpen()) {
00171 emit logError(i18n("Couldn't open the calendar databases."));
00172 goto skip;
00173 }
00174
00175
00176
00177 QTimer::singleShot(0,this,SLOT(syncNextRecord()));
00178 return;
00179
00180 error:
00181 emit logError(i18n("Sync failed for database: ")+dbinfo.name);
00182 skip:
00183 QTimer::singleShot(0, this, SLOT(syncNextDB()));
00184 return;
00185 }
00186
00187
00188 bool MultiDBConduit::GetSyncType(DBInfo dbinfo, DBSyncInfo*syncinfo) {
00189 fConfig->setGroup(dbinfo.name);
00190 syncinfo->set(dbinfo.name,
00191 fConfig->readNumEntry(getSyncTypeEntry(), st_ask),
00192 fConfig->readEntry(getSyncFileEntry()));
00193 if (syncinfo->syncaction!=st_ask) return true;
00194
00195 while (true) {
00196
00197 DBSettings*actiondlg=new DBSettings(0, "dbsettings", syncinfo, synctypes, false, false);
00198 int dlgres=actiondlg->exec();
00199
00200
00201 delete actiondlg;
00202 if (dlgres==QDialog::Accepted ) {
00203
00204 fConfig->writeEntry(getSyncTypeEntry(), syncinfo->syncaction);
00205 fConfig->writeEntry(getSyncFileEntry(), syncinfo->filename);
00206 if (syncinfo->syncaction!=st_ask) return true;
00207 }
00208
00209 if (KMessageBox::warningContinueCancel (0, i18n("You did not select a "
00210 "sync method for the database. If you continue, the database will be "
00211 "skipped during this sync. If you cancel, you will be returned to the "
00212 "previous dialog."),
00213 i18n("&Continue"), KStdGuiItem::cont(), "noEmptySyncSettingWarning") ) {
00214 return false;
00215 }
00216 }
00217 return false;
00218 }
00219
00220
00221
00222 bool MultiDBConduit::exec() {
00223 FUNCTIONSETUP;
00224
00225 if (!fConfig) {
00226 kdWarning() << k_funcinfo <<": No configuration set for conduit. SKIPPING..."<<endl;
00227 return false;
00228 }
00229
00230 KConfigGroupSaver cfgs(fConfig, conduitSettingsGroup());
00231
00232 dbases=fConfig->readListEntry(settingsFileList());
00233 conflictResolution = fConfig->readNumEntry(MultiDBConduitFactory::conflictResolution);
00234 archive = fConfig->readBoolEntry(MultiDBConduitFactory::archive);
00235
00236 #ifdef DEBUG
00237 DEBUGCONDUIT<<k_funcinfo<<": starting sync with syncNextDB "<<endl;
00238 #endif
00239 QTimer::singleShot(0, this, SLOT(syncNextDB()));
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 return true;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331