kpilot Library API Documentation

syncStack.cc

00001 /* syncStack.cc                       KPilot
00002 **
00003 ** Copyright (C) 1998-2001 by Dan Pilone
00004 **
00005 ** This defines the "ActionQueue", which is the pile of actions
00006 ** that will occur during a HotSync.
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., 59 Temple Place, Suite 330, Boston,
00023 ** MA 02111-1307, USA.
00024 */
00025 
00026 /*
00027 ** Bug reports and questions can be sent to kde-pim@kde.org
00028 */
00029 #include "options.h"
00030 
00031 static const char *syncStack_id = "$Id: syncStack.cc,v 1.9.4.8 2003/03/15 10:29:01 adridg Exp $";
00032 
00033 #include <qtimer.h>
00034 #include <qfile.h>
00035 
00036 #include <kservice.h>
00037 #include <kservicetype.h>
00038 #include <kuserprofile.h>
00039 #include <klibloader.h>
00040 
00041 #include "hotSync.h"
00042 #include "interactiveSync.h"
00043 #include "fileInstaller.h"
00044 
00045 #include "syncStack.moc"
00046 
00047 
00048 
00049 WelcomeAction::WelcomeAction(KPilotDeviceLink *p) :
00050         SyncAction(p,"welcomeAction")
00051 {
00052         FUNCTIONSETUP;
00053 
00054         (void) syncStack_id;
00055 }
00056 
00057 /* virtual */ bool WelcomeAction::exec()
00058 {
00059         FUNCTIONSETUP;
00060 
00061         addSyncLogEntry(i18n("KPilot %1 HotSync starting...\n")
00062                 .arg(QString::fromLatin1(KPILOT_VERSION)));
00063         emit syncDone(this);
00064         return true;
00065 }
00066 
00067 ConduitProxy::ConduitProxy(KPilotDeviceLink *p,
00068         const QString &name,
00069         int m) :
00070         ConduitAction(p,name.latin1()),
00071         fDesktopName(name),
00072         fMode(m)
00073 {
00074         FUNCTIONSETUP;
00075 }
00076 
00077 /* virtual */ bool ConduitProxy::exec()
00078 {
00079         FUNCTIONSETUP;
00080 
00081         // query that service
00082         KSharedPtr < KService > o = KService::serviceByDesktopName(fDesktopName);
00083         if (!o)
00084         {
00085                 kdWarning() << k_funcinfo
00086                         << ": Can't find desktop file for conduit "
00087                         << fDesktopName
00088                         << endl;
00089                 addSyncLogEntry(i18n("Couldn't find conduit %1.").arg(fDesktopName));
00090                 emit syncDone(this);
00091                 return true;
00092         }
00093 
00094 
00095         // load the lib
00096 #ifdef DEBUG
00097         DEBUGKPILOT << fname
00098                 << ": Loading desktop "
00099                 << fDesktopName
00100                 << " with lib "
00101                 << o->library()
00102                 << endl;
00103 #endif
00104 
00105         fLibraryName = o->library();
00106         KLibFactory *factory = KLibLoader::self()->factory(
00107                 QFile::encodeName(o->library()));
00108         if (!factory)
00109         {
00110                 kdWarning() << k_funcinfo
00111                         << ": Can't load library "
00112                         << o->library()
00113                         << endl;
00114                 addSyncLogEntry(i18n("Couldn't load conduit %1.").arg(fDesktopName));
00115                 emit syncDone(this);
00116                 return true;
00117         }
00118 
00119         QStringList l;
00120         switch(fMode & ActionQueue::ActionMask)
00121         {
00122         case ActionQueue::Backup :
00123                 l.append(CSL1("--backup"));
00124                 break;
00125         default:
00126                 ;
00127         }
00128         if (fMode & ActionQueue::FlagTest)
00129         {
00130                 l.append(CSL1("--test"));
00131         }
00132         if (fMode & ActionQueue::FlagLocal)
00133         {
00134                 l.append(CSL1("--local"));
00135         }
00136 
00137 
00138         QObject *object = factory->create(fHandle,name(),"SyncAction",l);
00139 
00140         if (!object)
00141         {
00142                 kdWarning() << k_funcinfo
00143                         << ": Can't create SyncAction."
00144                         << endl;
00145                 addSyncLogEntry(i18n("Couldn't create conduit %1.").arg(fDesktopName));
00146                 emit syncDone(this);
00147                 return true;
00148         }
00149 
00150         fConduit = dynamic_cast<ConduitAction *>(object);
00151 
00152         if (!fConduit)
00153         {
00154                 kdWarning() << k_funcinfo
00155                         << ": Can't cast to ConduitAction."
00156                         << endl;
00157                 addSyncLogEntry(i18n("Couldn't create conduit %1.").arg(fDesktopName));
00158                 emit syncDone(this);
00159                 return true;
00160         }
00161         fConduit->setConfig(fConfig);
00162 
00163         logMessage(i18n("[Conduit %1]").arg(fDesktopName));
00164 
00165         QString conduitFlags = TODO_I18N("Running with flags: ");
00166         for (QStringList::ConstIterator i = l.begin() ; i!=l.end(); ++i)
00167         {
00168                 conduitFlags.append(*i);
00169                 conduitFlags.append(CSL1("  "));
00170         }
00171         
00172         logMessage(conduitFlags);
00173 
00174         // Handle the syncDone signal properly & unload the conduit.
00175         QObject::connect(fConduit,SIGNAL(syncDone(SyncAction *)),
00176                 this,SLOT(execDone(SyncAction *)));
00177         // Proxy all the log and error messages.
00178         QObject::connect(fConduit,SIGNAL(logMessage(const QString &)),
00179                 this,SIGNAL(logMessage(const QString &)));
00180         QObject::connect(fConduit,SIGNAL(logError(const QString &)),
00181                 this,SIGNAL(logError(const QString &)));
00182         QObject::connect(fConduit,SIGNAL(logProgress(const QString &,int)),
00183                 this,SIGNAL(logProgress(const QString &,int)));
00184 
00185         QTimer::singleShot(0,fConduit,SLOT(execConduit()));
00186         return true;
00187 }
00188 
00189 void ConduitProxy::execDone(SyncAction *p)
00190 {
00191         FUNCTIONSETUP;
00192 
00193         if (p!=fConduit)
00194         {
00195                 kdError() << k_funcinfo
00196                         << ": Unknown conduit @"
00197                         << (int) p
00198                         << " finished."
00199                         << endl;
00200                 emit syncDone(this);
00201                 return;
00202         }
00203 
00204         delete p;
00205         emit syncDone(this);
00206 }
00207 
00208 ActionQueue::ActionQueue(KPilotDeviceLink *d,
00209         KConfig *config,
00210         const QStringList &conduits,
00211         const QString &dir,
00212         const QStringList &files) :
00213         SyncAction(d,"ActionQueue"),
00214         fReady(false),
00215         fConfig(config),
00216         fInstallerDir(dir),
00217         fInstallerFiles(files),
00218         fConduits(conduits)
00219 {
00220         FUNCTIONSETUP;
00221 
00222 #ifdef DEBUG
00223         if (!conduits.count())
00224         {
00225                 DEBUGCONDUIT << fname << ": No conduits." << endl;
00226         }
00227         else
00228         {
00229                 DEBUGCONDUIT << fname << ": Conduits : " << conduits.join(CSL1(" + ")) << endl;
00230         }
00231 #endif
00232 
00233         kdWarning() << "SyncStack usage is deprecated." << endl;
00234 }
00235 
00236 ActionQueue::ActionQueue(KPilotDeviceLink *d) :
00237         SyncAction(d,"ActionQueue"),
00238         fReady(false),
00239         fConfig(0L)
00240         // The string lists have default constructors
00241 {
00242         FUNCTIONSETUP;
00243 }
00244 
00245 ActionQueue::~ActionQueue()
00246 {
00247         FUNCTIONSETUP;
00248 }
00249 
00250 void ActionQueue::prepare(int m)
00251 {
00252         FUNCTIONSETUP;
00253 
00254 #ifdef DEBUG
00255         DEBUGDAEMON << fname
00256                 << ": Using sync mode " << m
00257                 << endl;
00258 #endif
00259         
00260         switch ( m & (Test | Backup | Restore | HotSync))
00261         {
00262         case Test:
00263         case Backup:
00264         case Restore:
00265         case HotSync:
00266                 fReady=true;
00267                 break;
00268         default:
00269                 kdWarning() << k_funcinfo
00270                         << ": Strange sync mode " << m << " set. Aborting."
00271                         << endl;
00272                 return;
00273         }
00274 
00275         queueInit(m);
00276         if (m & WithConduits)
00277                 queueConduits(fConfig,fConduits,m);
00278         
00279         switch ( m & (Test | Backup | Restore | HotSync))
00280         {
00281         case Test:
00282                 addAction(new TestLink(fHandle));
00283                 break;
00284         case Backup:
00285                 addAction(new BackupAction(fHandle));
00286                 break;
00287         case Restore:
00288                 addAction(new RestoreAction(fHandle));
00289                 break;
00290         case HotSync:
00291                 break;
00292         default:
00293                 // We already checked for this case!
00294                 fReady=false;
00295                 return;
00296         }
00297 
00298         if (m & WithInstaller)
00299                 queueInstaller(fInstallerDir,fInstallerFiles);
00300 
00301         queueCleanup();
00302 }
00303 
00304 void ActionQueue::queueInit(int m)
00305 {
00306         FUNCTIONSETUP;
00307 
00308         addAction(new WelcomeAction(fHandle));
00309 
00310         if (m & WithUserCheck)
00311         {
00312                 addAction(new CheckUser(fHandle));
00313         }
00314 }
00315 
00316 void ActionQueue::queueConduits(KConfig *config,const QStringList &l,int m)
00317 {
00318         FUNCTIONSETUP;
00319 
00320         // Add conduits here ...
00321         //
00322         //
00323         for (QStringList::ConstIterator it = l.begin();
00324                 it != l.end();
00325                 ++it)
00326         {
00327                 ConduitProxy *cp = new ConduitProxy(fHandle,*it,m);
00328                 cp->setConfig(config);
00329                 addAction(cp);
00330         }
00331 }
00332 
00333 void ActionQueue::queueInstaller(const QString &dir, const QStringList &files)
00334 {
00335         addAction(new FileInstallAction(fHandle,dir,files));
00336 }
00337 
00338 void ActionQueue::queueCleanup()
00339 {
00340         addAction(new CleanupAction(fHandle));
00341 }
00342 
00343 bool ActionQueue::exec()
00344 {
00345         actionCompleted(0L);
00346         return true;
00347 }
00348 
00349 void ActionQueue::actionCompleted(SyncAction *b)
00350 {
00351         FUNCTIONSETUP;
00352 
00353         if (b)
00354         {
00355 #ifdef DEBUG
00356                 DEBUGDAEMON << fname
00357                         << ": Completed action "
00358                         << b->name()
00359                         << endl;
00360 #endif
00361                 delete b;
00362         }
00363 
00364         if (isEmpty())
00365         {
00366                 emit syncDone(this);
00367                 return;
00368         }
00369 
00370         SyncAction *a = nextAction();
00371 
00372         if (!a)
00373         {
00374                 kdWarning() << k_funcinfo
00375                         << ": NULL action on stack."
00376                         << endl;
00377                 return;
00378         }
00379 
00380 #ifdef DEBUG
00381         DEBUGDAEMON << fname
00382                 << ": Will run action "
00383                 << a->name()
00384                 << endl;
00385 #endif
00386 
00387         QObject::connect(a, SIGNAL(logMessage(const QString &)),
00388                 this, SIGNAL(logMessage(const QString &)));
00389         QObject::connect(a, SIGNAL(logError(const QString &)),
00390                 this, SIGNAL(logMessage(const QString &)));
00391         QObject::connect(a, SIGNAL(logProgress(const QString &, int)),
00392                 this, SIGNAL(logProgress(const QString &, int)));
00393         QObject::connect(a, SIGNAL(syncDone(SyncAction *)),
00394                 this, SLOT(actionCompleted(SyncAction *)));
00395 
00396         QTimer::singleShot(0,a,SLOT(execConduit()));
00397 }
00398 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Sun Feb 15 11:40:44 2004 by doxygen 1.3.5 written by Dimitri van Heesch, © 1997-2001