kitchensync Library API Documentation

engine.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include "engine.h"
00023 
00024 #include "actionpart.h"
00025 
00026 #include <konnector.h>
00027 #include <konnectormanager.h>
00028 #include <konnectorinfo.h>
00029 #include <klocale.h>
00030 
00031 #include <qdatetime.h>
00032 
00033 using namespace KSync;
00034 
00035 Engine::Engine( QPtrList<ActionPart> &parts )
00036   : mParts( parts )
00037 {
00038 }
00039 
00040 Engine::~Engine()
00041 {
00042 }
00043 
00044 void Engine::slotProgress( Konnector *k, const Progress &p )
00045 {
00046   logMessage( i18n("Got Progress from Konnector at address %1: %2").arg( (long)k ).arg( p.text() ) );
00047 }
00048 
00049 void Engine::slotError( Konnector *k, const Error &e )
00050 {
00051   logMessage( i18n("Got Progress from Konnector at address %1: %2").arg( (long)k ).arg( e.text() ) );
00052 }
00053 
00054 void Engine::logMessage( const QString &message )
00055 {
00056   QString text = QTime::currentTime().toString() + ": ";
00057   text += message;
00058 
00059   kdDebug() << "LOG: " << text << endl;
00060 }
00061 
00062 Konnector::List Engine::konnectors()
00063 {
00064   return mKonnectors;
00065 }
00066 
00067 void Engine::go()
00068 {
00069   kdDebug() << "Engine::go():" << endl;
00070 
00071   logMessage( i18n("Sync Action triggered") );
00072 
00073   mOpenedKonnectors.clear();
00074   mProcessedKonnectors.clear();
00075   mKonnectorCount = 0;
00076 
00077   mKonnectors.clear();
00078 
00079   KRES::Manager<Konnector> *manager = KonnectorManager::self();  
00080   KRES::Manager<Konnector>::ActiveIterator it;
00081   for( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00082     kdDebug() << "  Engine::go(): Konnector: id: " << (*it)->identifier()
00083               << " name: " << (*it)->resourceName() << endl;
00084     mKonnectors.append( *it );
00085   }
00086 
00087   bool needsRead = false;
00088 
00089   ActionPart *part;
00090   for ( part = mParts.first(); part; part = mParts.next() ) {
00091     part->filterKonnectors( mKonnectors );
00092     if ( part->needsKonnectorRead() ) needsRead = true;
00093   }
00094 
00095   if ( needsRead ) {
00096     Konnector *k;
00097     for( k = mKonnectors.first(); k; k = mKonnectors.next() ) {
00098       logMessage( i18n("Connecting '%1'").arg( k->resourceName() ) );
00099       if ( !k->connectDevice() ) {
00100         logMessage( i18n("Error connecting device.") );
00101       } else {
00102         mOpenedKonnectors.append( k );
00103         ++mKonnectorCount;
00104       }
00105     }
00106 
00107     for ( k = mOpenedKonnectors.first(); k; k = mOpenedKonnectors.next() ) {
00108       logMessage( i18n("Request Syncees") );
00109       if ( !k->readSyncees() ) {
00110         logMessage( i18n("Request failed.") );
00111       }
00112     }
00113   } else {
00114     executeActions();
00115   }
00116 }
00117 
00118 void Engine::slotSynceesRead( Konnector *k )
00119 {
00120   logMessage( i18n("Syncees read from '%1'").arg( k->resourceName() ) );
00121 
00122   mProcessedKonnectors.append( k );
00123 
00124   SynceeList syncees = k->syncees();
00125 
00126   if ( syncees.count() == 0 ) {
00127     logMessage( i18n("Syncee list is empty.") );
00128     return;
00129   }
00130 
00131   tryExecuteActions();
00132 }
00133 
00134 void Engine::tryExecuteActions()
00135 {
00136   kdDebug() << "Engine::tryExecuteActions()" << endl;
00137 
00138   kdDebug() << "  konnectorCount: " << mKonnectorCount << endl;
00139   kdDebug() << "  processedKonnectorsCount: " << mProcessedKonnectors.count()
00140             << endl;
00141 
00142   if ( mKonnectorCount == mProcessedKonnectors.count() ) {
00143     executeActions();
00144   }
00145 }
00146 
00147 void Engine::executeActions()
00148 {
00149   logMessage( i18n("Execute Actions") );
00150 
00151   bool needsWrite = false;
00152 
00153   ActionPart *part;
00154   for ( part = mParts.first(); part; part = mParts.next() ) {
00155     part->executeAction();
00156     if ( part->needsKonnectorWrite() ) needsWrite = true;
00157   }
00158 
00159   if ( needsWrite ) {
00160     mProcessedKonnectors.clear();
00161 
00162     Konnector *konnector;
00163     for( konnector = mOpenedKonnectors.first(); konnector;
00164          konnector = mOpenedKonnectors.next() ) {
00165       if ( konnector->writeSyncees() ) {
00166         kdDebug() << "writeSyncees(): " << konnector->resourceName() << endl;
00167       } else {
00168         kdError() << "Error requesting to write Syncee: "
00169                   << konnector->resourceName() << endl;
00170       }
00171     }
00172   } else {
00173     finish();
00174   }
00175 }
00176 
00177 void Engine::slotSynceeReadError( Konnector *k )
00178 {
00179   logMessage( i18n("Error reading Syncees from '%1'")
00180               .arg( k->resourceName() ) );
00181   
00182   --mKonnectorCount;
00183 
00184   tryExecuteActions();
00185 }
00186 
00187 void Engine::slotSynceesWritten( Konnector *k )
00188 {
00189   logMessage( i18n("Syncees written to '%1'").arg( k->resourceName() ) );
00190 
00191   mProcessedKonnectors.append( k );
00192 
00193   disconnectDevice( k );
00194 
00195   tryFinish();
00196 }
00197 
00198 void Engine::slotSynceeWriteError( Konnector *k )
00199 {
00200   logMessage( i18n("Error writing Syncees to '%1'")
00201               .arg( k->resourceName() ) );
00202 
00203   --mKonnectorCount;
00204 
00205   disconnectDevice( k );
00206 
00207   tryFinish();
00208 }
00209 
00210 void Engine::disconnectDevice( Konnector *k )
00211 {
00212   if ( !k->disconnectDevice() ) {
00213     logMessage( i18n("Error disconnecting device") );
00214   }
00215 }
00216 
00217 void Engine::tryFinish()
00218 {
00219   if ( mKonnectorCount == mProcessedKonnectors.count() ) {
00220     finish();
00221   }
00222 }
00223 
00224 void Engine::finish()
00225 {
00226   logMessage( i18n("Synchronization finished.") );
00227 }
00228 
00229 #include "engine.moc"
KDE Logo
This file is part of the documentation for kitchensync Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Dec 21 14:23:40 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003