kitchensync

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., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, 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::logMessage( const QString &message )
00045 {
00046   QString text = QTime::currentTime().toString() + ": ";
00047   text += message;
00048 
00049   kdDebug() << "LOG: " << text << endl;
00050 }
00051 
00052 Konnector::List Engine::konnectors()
00053 {
00054   return mKonnectors;
00055 }
00056 
00057 void Engine::go()
00058 {
00059   kdDebug() << "Engine::go():" << endl;
00060 
00061   logMessage( i18n("Sync Action triggered") );
00062 
00063   mOpenedKonnectors.clear();
00064   mProcessedKonnectors.clear();
00065   mKonnectorCount = 0;
00066 
00067   mKonnectors.clear();
00068 
00069   KRES::Manager<Konnector> *manager = KonnectorManager::self();
00070   KRES::Manager<Konnector>::ActiveIterator it;
00071   for( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00072     kdDebug() << "  Engine::go(): Konnector: id: " << (*it)->identifier()
00073               << " name: " << (*it)->resourceName() << endl;
00074     mKonnectors.append( *it );
00075   }
00076 
00077   bool needsRead = false;
00078 
00079   ActionPart *part;
00080   for ( part = mParts.first(); part; part = mParts.next() ) {
00081     part->filterKonnectors( mKonnectors );
00082     if ( part->needsKonnectorRead() ) needsRead = true;
00083   }
00084 
00085   if ( needsRead ) {
00086     Konnector *k;
00087     for( k = mKonnectors.first(); k; k = mKonnectors.next() ) {
00088       logMessage( i18n("Connecting '%1'").arg( k->resourceName() ) );
00089       if ( !k->connectDevice() ) {
00090         logMessage( i18n("Error connecting device.") );
00091       } else {
00092         mOpenedKonnectors.append( k );
00093         ++mKonnectorCount;
00094       }
00095     }
00096 
00097     for ( k = mOpenedKonnectors.first(); k; k = mOpenedKonnectors.next() ) {
00098       logMessage( i18n("Request Syncees") );
00099       if ( !k->readSyncees() ) {
00100         logMessage( i18n("Request failed.") );
00101       }
00102     }
00103   } else {
00104     executeActions();
00105   }
00106 }
00107 
00108 void Engine::slotSynceesRead( Konnector *k )
00109 {
00110   logMessage( i18n("Syncees read from '%1'").arg( k->resourceName() ) );
00111 
00112   mProcessedKonnectors.append( k );
00113 
00114   SynceeList syncees = k->syncees();
00115 
00116   if ( syncees.count() == 0 ) {
00117     logMessage( i18n("Syncee list is empty.") );
00118     return;
00119   }
00120 
00121   tryExecuteActions();
00122 }
00123 
00124 void Engine::tryExecuteActions()
00125 {
00126   kdDebug() << "Engine::tryExecuteActions()" << endl;
00127 
00128   kdDebug() << "  konnectorCount: " << mKonnectorCount << endl;
00129   kdDebug() << "  processedKonnectorsCount: " << mProcessedKonnectors.count()
00130             << endl;
00131 
00132   if ( mKonnectorCount == mProcessedKonnectors.count() ) {
00133     executeActions();
00134   }
00135 }
00136 
00137 void Engine::executeActions()
00138 {
00139   logMessage( i18n("Execute Actions") );
00140 
00141   /*
00142    * Apply filters before sync
00143    */
00144   Konnector *konnector;
00145   for ( konnector = mOpenedKonnectors.first(); konnector;
00146         konnector = mOpenedKonnectors.next() )
00147     konnector->applyFilters( KSync::Konnector::FilterBeforeSync );
00148 
00149   bool needsWrite = false;
00150 
00151   ActionPart *part;
00152   for ( part = mParts.first(); part; part = mParts.next() ) {
00153     part->executeAction();
00154     if ( part->needsKonnectorWrite() ) needsWrite = true;
00155   }
00156 
00157   if ( needsWrite ) {
00158     mProcessedKonnectors.clear();
00159 
00160     for( konnector = mOpenedKonnectors.first(); konnector;
00161          konnector = mOpenedKonnectors.next() ) {
00162       konnector->applyFilters( KSync::Konnector::FilterAfterSync );
00163       if ( konnector->writeSyncees() ) {
00164         kdDebug() << "writeSyncees(): " << konnector->resourceName() << endl;
00165       } else {
00166         kdError() << "Error requesting to write Syncee: "
00167                   << konnector->resourceName() << endl;
00168       }
00169     }
00170   } else {
00171     finish();
00172   }
00173 }
00174 
00175 void Engine::slotSynceeReadError( Konnector *k )
00176 {
00177   logMessage( i18n("Error reading Syncees from '%1'")
00178               .arg( k->resourceName() ) );
00179 
00180   --mKonnectorCount;
00181 
00182   tryExecuteActions();
00183 }
00184 
00185 void Engine::slotSynceesWritten( Konnector *k )
00186 {
00187   logMessage( i18n("Syncees written to '%1'").arg( k->resourceName() ) );
00188 
00189   mProcessedKonnectors.append( k );
00190 
00191   disconnectDevice( k );
00192 
00193   tryFinish();
00194 }
00195 
00196 void Engine::slotSynceeWriteError( Konnector *k )
00197 {
00198   logMessage( i18n("Error writing Syncees to '%1'")
00199               .arg( k->resourceName() ) );
00200 
00201   --mKonnectorCount;
00202 
00203   disconnectDevice( k );
00204 
00205   tryFinish();
00206 }
00207 
00208 void Engine::disconnectDevice( Konnector *k )
00209 {
00210   if ( !k->disconnectDevice() ) {
00211     logMessage( i18n("Error disconnecting device") );
00212   }
00213 }
00214 
00215 void Engine::tryFinish()
00216 {
00217   if ( mKonnectorCount == mProcessedKonnectors.count() ) {
00218     finish();
00219   }
00220 }
00221 
00222 void Engine::finish()
00223 {
00224   logMessage( i18n("Synchronization finished.") );
00225   emit doneSync();
00226 }
00227 
00228 #include "engine.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys