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 #include "options.h"
00030
00031 static const char *syncStack_id = "$Id: syncStack.cc,v 1.22 2003/12/04 21:10:13 adridg Exp $";
00032
00033 #include <unistd.h>
00034
00035 #include <qtimer.h>
00036 #include <qfile.h>
00037
00038 #include <kservice.h>
00039 #include <kservicetype.h>
00040 #include <kuserprofile.h>
00041 #include <klibloader.h>
00042
00043 #include "pilotUser.h"
00044 #include "hotSync.h"
00045 #include "interactiveSync.h"
00046 #include "fileInstaller.h"
00047
00048 #include "syncStack.moc"
00049
00050
00051
00052 WelcomeAction::WelcomeAction(KPilotDeviceLink *p) :
00053 SyncAction(p,"welcomeAction")
00054 {
00055 FUNCTIONSETUP;
00056
00057 (void) syncStack_id;
00058 }
00059
00060 bool WelcomeAction::exec()
00061 {
00062 FUNCTIONSETUP;
00063
00064 addSyncLogEntry(i18n("KPilot %1 HotSync starting...\n")
00065 .arg(QString::fromLatin1(KPILOT_VERSION)));
00066 emit syncDone(this);
00067 return true;
00068 }
00069
00070 SorryAction::SorryAction(KPilotDeviceLink *p) :
00071 SyncAction(p,"sorryAction")
00072 {
00073 }
00074
00075 bool SorryAction::exec()
00076 {
00077 FUNCTIONSETUP;
00078
00079 addSyncLogEntry(i18n("KPilot is busy and cannot process the "
00080 "HotSync right now."));
00081 return delayDone();
00082 }
00083
00084 ConduitProxy::ConduitProxy(KPilotDeviceLink *p,
00085 const QString &name,
00086 int m) :
00087 ConduitAction(p,name.latin1()),
00088 fDesktopName(name),
00089 fMode(m)
00090 {
00091 FUNCTIONSETUP;
00092 }
00093
00094 bool ConduitProxy::exec()
00095 {
00096 FUNCTIONSETUP;
00097
00098
00099 KSharedPtr < KService > o = KService::serviceByDesktopName(fDesktopName);
00100 if (!o)
00101 {
00102 kdWarning() << k_funcinfo
00103 << ": Can't find desktop file for conduit "
00104 << fDesktopName
00105 << endl;
00106 addSyncLogEntry(i18n("Couldn't find conduit %1.").arg(fDesktopName));
00107 emit syncDone(this);
00108 return true;
00109 }
00110
00111
00112
00113 #ifdef DEBUG
00114 DEBUGKPILOT << fname
00115 << ": Loading desktop "
00116 << fDesktopName
00117 << " with lib "
00118 << o->library()
00119 << endl;
00120 #endif
00121
00122 fLibraryName = o->library();
00123 KLibFactory *factory = KLibLoader::self()->factory(
00124 QFile::encodeName(o->library()));
00125 if (!factory)
00126 {
00127 kdWarning() << k_funcinfo
00128 << ": Can't load library "
00129 << o->library()
00130 << endl;
00131 addSyncLogEntry(i18n("Couldn't load conduit %1.").arg(fDesktopName));
00132 emit syncDone(this);
00133 return true;
00134 }
00135
00136 QStringList l;
00137 switch(fMode & ActionQueue::ActionMask)
00138 {
00139 case ActionQueue::Backup :
00140 l.append(CSL1("--backup"));
00141 break;
00142 default:
00143 ;
00144 }
00145 if (fMode & ActionQueue::FlagTest)
00146 l.append(CSL1("--test"));
00147 if (fMode & ActionQueue::FlagLocal)
00148 l.append(CSL1("--local"));
00149
00150 if ( (fMode & ActionQueue::FlagFull) ||
00151 (fHandle->getPilotUser()->getLastSyncPC()!=(unsigned long)gethostid() ) )
00152 l.append(CSL1("--full"));
00153 if (fMode & ActionQueue::FlagHHToPC)
00154 l.append(CSL1("--copyHHToPC"));
00155 if (fMode & ActionQueue::FlagPCToHH)
00156 l.append(CSL1("--copyPCToHH"));
00157
00158
00159 QObject *object = factory->create(fHandle,name(),"SyncAction",l);
00160
00161 if (!object)
00162 {
00163 kdWarning() << k_funcinfo
00164 << ": Can't create SyncAction."
00165 << endl;
00166 addSyncLogEntry(i18n("Couldn't create conduit %1.").arg(fDesktopName));
00167 emit syncDone(this);
00168 return true;
00169 }
00170
00171 fConduit = dynamic_cast<ConduitAction *>(object);
00172
00173 if (!fConduit)
00174 {
00175 kdWarning() << k_funcinfo
00176 << ": Can't cast to ConduitAction."
00177 << endl;
00178 addSyncLogEntry(i18n("Couldn't create conduit %1.").arg(fDesktopName));
00179 emit syncDone(this);
00180 return true;
00181 }
00182 fConduit->setConfig(fConfig);
00183
00184 addSyncLogEntry(i18n("[Conduit %1]").arg(fDesktopName));
00185
00186 QString conduitFlags = TODO_I18N("Running with flags: ");
00187 for (QStringList::ConstIterator i = l.begin() ; i!=l.end(); ++i)
00188 {
00189 conduitFlags.append(*i);
00190 conduitFlags.append(CSL1(" "));
00191 }
00192
00193 logMessage(conduitFlags);
00194 #ifdef DEBUG
00195 DEBUGKPILOT<<conduitFlags<<endl;
00196 #endif
00197
00198 QObject::connect(fConduit,SIGNAL(syncDone(SyncAction *)),
00199 this,SLOT(execDone(SyncAction *)));
00200
00201 QObject::connect(fConduit,SIGNAL(logMessage(const QString &)),
00202 this,SIGNAL(logMessage(const QString &)));
00203 QObject::connect(fConduit,SIGNAL(logError(const QString &)),
00204 this,SIGNAL(logError(const QString &)));
00205 QObject::connect(fConduit,SIGNAL(logProgress(const QString &,int)),
00206 this,SIGNAL(logProgress(const QString &,int)));
00207
00208 QTimer::singleShot(0,fConduit,SLOT(execConduit()));
00209 return true;
00210 }
00211
00212 void ConduitProxy::execDone(SyncAction *p)
00213 {
00214 FUNCTIONSETUP;
00215
00216 if (p!=fConduit)
00217 {
00218 kdError() << k_funcinfo
00219 << ": Unknown conduit @"
00220 << (int) p
00221 << " finished."
00222 << endl;
00223 emit syncDone(this);
00224 return;
00225 }
00226
00227 delete p;
00228 emit syncDone(this);
00229 }
00230
00231 #if 0
00232 ActionQueue::ActionQueue(KPilotDeviceLink *d,
00233 KConfig *config,
00234 const QStringList &conduits,
00235 const QString &dir,
00236 const QStringList &files) :
00237 SyncAction(d,"ActionQueue"),
00238 fReady(false),
00239 fConfig(config),
00240 fInstallerDir(dir),
00241 fInstallerFiles(files),
00242 fConduits(conduits)
00243 {
00244 FUNCTIONSETUP;
00245
00246 #ifdef DEBUG
00247 if (!conduits.count())
00248 {
00249 DEBUGCONDUIT << fname << ": No conduits." << endl;
00250 }
00251 else
00252 {
00253 DEBUGCONDUIT << fname << ": Conduits : " << conduits.join(CSL1(" + ")) << endl;
00254 }
00255 #endif
00256
00257 kdWarning() << "SyncStack usage is deprecated." << endl;
00258 }
00259 #endif
00260
00261 ActionQueue::ActionQueue(KPilotDeviceLink *d) :
00262 SyncAction(d,"ActionQueue"),
00263 fReady(false),
00264 fConfig(0L)
00265
00266 {
00267 FUNCTIONSETUP;
00268 }
00269
00270 ActionQueue::~ActionQueue()
00271 {
00272 FUNCTIONSETUP;
00273 }
00274
00275 #if 0
00276 void ActionQueue::prepare(int m)
00277 {
00278 FUNCTIONSETUP;
00279
00280 #ifdef DEBUG
00281 DEBUGDAEMON << fname
00282 << ": Using sync mode " << m
00283 << endl;
00284 #endif
00285
00286 switch ( m & (Test | Backup | Restore | HotSync))
00287 {
00288 case Test:
00289 case Backup:
00290 case Restore:
00291 case HotSync:
00292 fReady=true;
00293 break;
00294 default:
00295 kdWarning() << k_funcinfo
00296 << ": Strange sync mode " << m << " set. Aborting."
00297 << endl;
00298 return;
00299 }
00300
00301 queueInit(m);
00302 if (m & WithConduits)
00303 queueConduits(fConfig,fConduits,m);
00304
00305 switch ( m & (Test | Backup | Restore | HotSync))
00306 {
00307 case Test:
00308 addAction(new TestLink(fHandle));
00309 break;
00310 case Backup:
00311 addAction(new BackupAction(fHandle));
00312 break;
00313 case Restore:
00314 addAction(new RestoreAction(fHandle));
00315 break;
00316 case HotSync:
00317 break;
00318 default:
00319
00320 fReady=false;
00321 return;
00322 }
00323
00324 if (m & WithInstaller)
00325 queueInstaller(fInstallerDir,fInstallerFiles);
00326
00327 queueCleanup();
00328 }
00329 #endif
00330
00331 void ActionQueue::queueInit(int m)
00332 {
00333 FUNCTIONSETUP;
00334
00335 addAction(new WelcomeAction(fHandle));
00336
00337 if (m & WithUserCheck)
00338 {
00339 addAction(new CheckUser(fHandle));
00340 }
00341 }
00342
00343 void ActionQueue::queueConduits(KConfig *config,const QStringList &l,int m)
00344 {
00345 FUNCTIONSETUP;
00346
00347
00348
00349
00350 for (QStringList::ConstIterator it = l.begin();
00351 it != l.end();
00352 ++it)
00353 {
00354 if ((*it).startsWith(CSL1("internal_")))
00355 {
00356 #ifdef DEBUG
00357 DEBUGDAEMON << k_funcinfo <<
00358 "Ignoring conduit " << *it << endl;
00359 #endif
00360 continue;
00361 }
00362 ConduitProxy *cp = new ConduitProxy(fHandle,*it,m);
00363 cp->setConfig(config);
00364 addAction(cp);
00365 }
00366 }
00367
00368 void ActionQueue::queueInstaller(const QString &dir, const QStringList &files)
00369 {
00370 addAction(new FileInstallAction(fHandle,dir,files));
00371 }
00372
00373 void ActionQueue::queueCleanup()
00374 {
00375 addAction(new CleanupAction(fHandle));
00376 }
00377
00378 bool ActionQueue::exec()
00379 {
00380 actionCompleted(0L);
00381 return true;
00382 }
00383
00384 void ActionQueue::actionCompleted(SyncAction *b)
00385 {
00386 FUNCTIONSETUP;
00387
00388 if (b)
00389 {
00390 #ifdef DEBUG
00391 DEBUGDAEMON << fname
00392 << ": Completed action "
00393 << b->name()
00394 << endl;
00395 #endif
00396 delete b;
00397 }
00398
00399 if (isEmpty())
00400 {
00401 emit syncDone(this);
00402 return;
00403 }
00404
00405 SyncAction *a = nextAction();
00406
00407 if (!a)
00408 {
00409 kdWarning() << k_funcinfo
00410 << ": NULL action on stack."
00411 << endl;
00412 return;
00413 }
00414
00415 #ifdef DEBUG
00416 DEBUGDAEMON << fname
00417 << ": Will run action "
00418 << a->name()
00419 << endl;
00420 #endif
00421
00422 QObject::connect(a, SIGNAL(logMessage(const QString &)),
00423 this, SIGNAL(logMessage(const QString &)));
00424 QObject::connect(a, SIGNAL(logError(const QString &)),
00425 this, SIGNAL(logMessage(const QString &)));
00426 QObject::connect(a, SIGNAL(logProgress(const QString &, int)),
00427 this, SIGNAL(logProgress(const QString &, int)));
00428 QObject::connect(a, SIGNAL(syncDone(SyncAction *)),
00429 this, SLOT(actionCompleted(SyncAction *)));
00430
00431 QTimer::singleShot(0,a,SLOT(execConduit()));
00432 }
00433