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
00032
00033 #include "options.h"
00034
00035 #include <stdlib.h>
00036
00037 #include <qstringlist.h>
00038 #include <qfileinfo.h>
00039 #include <qdir.h>
00040 #include <qregexp.h>
00041
00042 #include <dcopclient.h>
00043 #include <kapplication.h>
00044 #include <kmessagebox.h>
00045 #include <kstandarddirs.h>
00046
00047 #include "pilotSerialDatabase.h"
00048 #include "pilotLocalDatabase.h"
00049
00050 #include "plugin.moc"
00051
00052 ConduitConfigBase::ConduitConfigBase(QWidget *parent,
00053 const char *name) :
00054 QObject(parent,name),
00055 fModified(false),
00056 fWidget(0L),
00057 fConduitName(i18n("Unnamed"))
00058 {
00059 FUNCTIONSETUP;
00060 }
00061
00062 ConduitConfigBase::~ConduitConfigBase()
00063 {
00064 FUNCTIONSETUP;
00065 }
00066
00067 void ConduitConfigBase::modified()
00068 {
00069 fModified=true;
00070 }
00071
00072 QString ConduitConfigBase::maybeSaveText() const
00073 {
00074 FUNCTIONSETUP;
00075
00076 return i18n("<qt>The <i>%1</i> conduit's settings have been changed. Do you "
00077 "want to save the changes before continuing?</qt>").arg(this->conduitName());
00078 }
00079
00080 bool ConduitConfigBase::maybeSave(KConfig *c)
00081 {
00082 FUNCTIONSETUP;
00083
00084 if (!isModified()) return true;
00085
00086 int r = KMessageBox::questionYesNoCancel(fWidget,
00087 maybeSaveText(),
00088 i18n("%1 Conduit").arg(this->conduitName()));
00089 if (r == KMessageBox::Cancel) return false;
00090 if (r == KMessageBox::Yes) commit(c);
00091 return true;
00092 }
00093
00094 ConduitConfig::ConduitConfig(QWidget *parent,
00095 const char *name,
00096 const QStringList &args) :
00097 UIDialog(parent,name,PluginUtility::isModal(args)),
00098 fConfig(0L),
00099 fConduitName(i18n("Unnamed"))
00100 {
00101 FUNCTIONSETUP;
00102 }
00103
00104
00105 ConduitConfig::~ConduitConfig()
00106 {
00107 FUNCTIONSETUP;
00108 }
00109
00110 ConduitAction::ConduitAction(KPilotDeviceLink *p,
00111 const char *name,
00112 const QStringList &args) :
00113 SyncAction(p,name),
00114 fConfig(0L),
00115 fDatabase(0L),
00116 fLocalDatabase(0L),
00117 fTest(args.contains(CSL1("--test"))),
00118 fBackup(args.contains(CSL1("--backup"))),
00119 fLocal(args.contains(CSL1("--local"))),
00120 fConflictResolution(SyncAction::eAskUser),
00121 fFirstSync(false)
00122 {
00123 FUNCTIONSETUP;
00124 if (args.contains(CSL1("--copyPCToHH"))) fSyncDirection=SyncAction::eCopyPCToHH;
00125 else if (args.contains(CSL1("--copyHHToPC"))) fSyncDirection=SyncAction::eCopyHHToPC;
00126 else if (args.contains(CSL1("--full"))) fSyncDirection=SyncAction::eFullSync;
00127 else fSyncDirection=SyncAction::eFastSync;
00128
00129 QString cResolution(args.grep(QRegExp("--conflictResolution \\d*")).first());
00130 if (cResolution.isEmpty())
00131 {
00132 fConflictResolution=(SyncAction::eConflictResolution)
00133 cResolution.replace(QRegExp("--conflictResolution (\\d*)"), "\\1").toInt();
00134 }
00135
00136 #ifdef DEBUG
00137 for (QStringList::ConstIterator it = args.begin();
00138 it != args.end();
00139 ++it)
00140 {
00141 DEBUGCONDUIT << fname << ": " << *it << endl;
00142 }
00143 #endif
00144 }
00145
00146 ConduitAction::~ConduitAction()
00147 {
00148 FUNCTIONSETUP;
00149 KPILOT_DELETE(fDatabase);
00150 KPILOT_DELETE(fLocalDatabase);
00151 }
00152
00153 bool ConduitAction::openDatabases_(const QString &name, bool *retrieved)
00154 {
00155 FUNCTIONSETUP;
00156
00157 #ifdef DEBUG
00158 DEBUGCONDUIT << fname
00159 << ": Trying to open database "
00160 << name << endl;
00161 #endif
00162
00163 fLocalDatabase = new PilotLocalDatabase(name, true);
00164
00165 if (!fLocalDatabase)
00166 {
00167 kdWarning() << k_funcinfo
00168 << ": Could not initialize object for local copy of database \""
00169 << name
00170 << "\"" << endl;
00171 return false;
00172 }
00173
00174
00175 if (!fLocalDatabase->isDBOpen() )
00176 {
00177 QString dbpath(dynamic_cast<PilotLocalDatabase*>(fLocalDatabase)->dbPathName());
00178 KPILOT_DELETE(fLocalDatabase);
00179 #ifdef DEBUG
00180 DEBUGCONDUIT << fname
00181 << ": Backup database "<< dbpath <<" could not be opened. Will fetch a copy from the palm and do a full sync"<<endl;
00182 #endif
00183 struct DBInfo dbinfo;
00184 if (fHandle->findDatabase(name.latin1(), &dbinfo)<0 )
00185 {
00186 #ifdef DEBUG
00187 DEBUGCONDUIT << fname
00188 << ": Could not get DBInfo for "<<name<<"! "<<endl;
00189 #endif
00190 return false;
00191 }
00192 #ifdef DEBUG
00193 DEBUGCONDUIT << fname
00194 << ": Found Palm database: "<<dbinfo.name<<endl
00195 <<"type = "<< dbinfo.type<<endl
00196 <<"creator = "<< dbinfo.creator<<endl
00197 <<"version = "<< dbinfo.version<<endl
00198 <<"index = "<< dbinfo.index<<endl;
00199 #endif
00200 dbinfo.flags &= ~dlpDBFlagOpen;
00201
00202
00203 QFileInfo fi(dbpath);
00204 QString path(QFileInfo(dbpath).dir(TRUE).absPath());
00205 if (!KStandardDirs::exists(path)) {
00206 KStandardDirs::makeDir(path);
00207 }
00208 if (!fHandle->retrieveDatabase(dbpath, &dbinfo) )
00209 {
00210 #ifdef DEBUG
00211 DEBUGCONDUIT << fname << ": Could not retrieve database "<<name<<" from the handheld."<<endl;
00212 #endif
00213 return false;
00214 }
00215 fLocalDatabase = new PilotLocalDatabase(name, true);
00216 if (!fLocalDatabase || !fLocalDatabase->isDBOpen())
00217 {
00218 #ifdef DEBUG
00219 DEBUGCONDUIT << fname << ": local backup of database "<<name<<" could not be initialized."<<endl;
00220 #endif
00221 return false;
00222 }
00223 if (retrieved) *retrieved=true;
00224 }
00225
00226
00227 fDatabase = new PilotSerialDatabase(pilotSocket(), name ,
00228 this, name.latin1() );
00229
00230 if (!fDatabase)
00231 {
00232 kdWarning() << k_funcinfo
00233 << ": Could not open database \""
00234 << name
00235 << "\" on the pilot."
00236 << endl;
00237 }
00238
00239 return (fDatabase && fDatabase->isDBOpen() &&
00240 fLocalDatabase && fLocalDatabase->isDBOpen() );
00241 }
00242
00243
00244 bool ConduitAction::openDatabases_(const QString &dbName,const QString &localPath)
00245 {
00246 FUNCTIONSETUP;
00247 #ifdef DEBUG
00248 DEBUGCONDUIT << fname << ": Doing local test mode for " << dbName << endl;
00249 #endif
00250 if (localPath.isNull())
00251 {
00252 #ifdef DEBUG
00253 DEBUGCONDUIT << fname
00254 << ": local mode test for one database only."
00255 << endl;
00256 #endif
00257 fDatabase = new PilotLocalDatabase(dbName);
00258 fLocalDatabase = 0L;
00259 return false;
00260 }
00261
00262 fDatabase = new PilotLocalDatabase(localPath,dbName);
00263 fLocalDatabase= new PilotLocalDatabase(dbName, true);
00264 if (!fLocalDatabase || !fDatabase)
00265 {
00266 #ifdef DEBUG
00267 const QString *where2 = PilotLocalDatabase::getDBPath();
00268
00269 QString none = CSL1("<null>");
00270 DEBUGCONDUIT << fname
00271 << ": Could not open both local copies of \""
00272 << dbName
00273 << "\"" << endl
00274 << "Using \""
00275 << (where2 ? *where2 : none)
00276 << "\" and \""
00277 << (localPath.isEmpty() ? localPath : none)
00278 << "\""
00279 << endl;
00280 #endif
00281 }
00282 #ifdef DEBUG
00283 if (fLocalDatabase)
00284 {
00285 DEBUGCONDUIT << fname
00286 << ": Opened local database "
00287 << fLocalDatabase->dbPathName()
00288 << (fLocalDatabase->isDBOpen() ? " OK" : "")
00289 << endl;
00290 }
00291 if (fDatabase)
00292 {
00293 DEBUGCONDUIT << fname
00294 << ": Opened database "
00295 << fDatabase->dbPathName()
00296 << (fDatabase->isDBOpen() ? " OK" : "")
00297 << endl;
00298 }
00299 #endif
00300
00301 return (fDatabase && fLocalDatabase);
00302 }
00303
00304 bool ConduitAction::openDatabases(const QString &dbName, bool*retrieved)
00305 {
00306 FUNCTIONSETUP;
00307
00308
00309
00310
00311
00312
00313 #ifdef DEBUG
00314 DEBUGCONDUIT << fname
00315 << ": Mode="
00316 << (isTest() ? "test " : "")
00317 << (isLocal() ? "local " : "")
00318 << endl ;
00319 #endif
00320
00321 if (isTest() && isLocal())
00322 {
00323 return openDatabases_(dbName,CSL1("/tmp/"));
00324 }
00325 else
00326 {
00327 return openDatabases_(dbName, retrieved);
00328 }
00329 }
00330
00331 int PluginUtility::findHandle(const QStringList &a)
00332 {
00333 FUNCTIONSETUP;
00334
00335 int handle = -1;
00336 for (QStringList::ConstIterator i = a.begin();
00337 i != a.end(); ++i)
00338 {
00339 if ((*i).left(7) == CSL1("handle="))
00340 {
00341 QString s = (*i).mid(7);
00342 if (s.isEmpty()) continue;
00343
00344 handle = s.toInt();
00345 #ifdef DEBUG
00346 DEBUGCONDUIT << fname
00347 << ": Got handle "
00348 << handle
00349 << endl;
00350 #endif
00351 if (handle<1)
00352 {
00353 kdWarning() << k_funcinfo
00354 << ": Improbable handle value found."
00355 << endl;
00356 }
00357 return handle;
00358 }
00359 }
00360
00361 #ifdef DEBUG
00362 DEBUGCONDUIT << fname
00363 << ": No handle= parameter found."
00364 << endl;
00365 #endif
00366
00367 return -1;
00368 }
00369
00370 bool PluginUtility::isModal(const QStringList &a)
00371 {
00372 return a.contains(CSL1("modal"));
00373 }
00374
00375 bool PluginUtility::isRunning(const QCString &n)
00376 {
00377 FUNCTIONSETUP;
00378
00379 DCOPClient *dcop = KApplication::kApplication()->dcopClient();
00380 QCStringList apps = dcop->registeredApplications();
00381 return apps.contains(n);
00382 }
00383
00384 ConduitConfigImplementation::ConduitConfigImplementation(QWidget *w, const char *n,
00385 const QStringList &a,
00386 ConduitConfigBase *(*f)(QWidget *, const char *)) :
00387 ConduitConfig(w,n,a)
00388 {
00389 FUNCTIONSETUP;
00390 fConfigWidget=f(widget(),n);
00391 fConfigWidget->widget()->adjustSize();
00392 fConfigWidget->widget()->show();
00393 fConduitName=fConfigWidget->conduitName();
00394 #ifdef DEBUG
00395 DEBUGCONDUIT << fname << ": Created conduit " << fConduitName << endl;
00396 #endif
00397 widget()->adjustSize();
00398 adjustSize();
00399 }
00400
00401 ConduitConfigImplementation::~ConduitConfigImplementation()
00402 {
00403 FUNCTIONSETUP;
00404 }
00405
00406 void ConduitConfigImplementation::readSettings()
00407 {
00408 if (fConfig) fConfigWidget->load(fConfig);
00409 }
00410
00411 void ConduitConfigImplementation::commitChanges()
00412 {
00413 if (fConfig) fConfigWidget->commit(fConfig);
00414 }
00415