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 <konnector.h>
00023 #include <konnectormanager.h>
00024 #include <konnectorinfo.h>
00025 #include <klocale.h>
00026 
00027 #include <addressbooksyncee.h>
00028 #include <calendarsyncee.h>
00029 #include <synceelist.h>
00030 #include <syncuikde.h>
00031 
00032 
00033 #include <qdatetime.h>
00034 
00035 #include "konnectorpair.h"
00036 #include "stdsyncui.h"
00037 
00038 #include "engine.h"
00039 
00040 using namespace KSync;
00041 
00042 Engine::Engine()
00043   : mManager( 0 ), mSyncUi( 0 )
00044 {
00045 }
00046 
00047 Engine::~Engine()
00048 {
00049   delete mSyncUi;
00050   mSyncUi = 0;
00051 }
00052 
00053 void Engine::logMessage( const QString &message )
00054 {
00055   QString text = QTime::currentTime().toString() + ": ";
00056   text += message;
00057 
00058   kdDebug() << "LOG: " << text << endl;
00059 }
00060 
00061 void Engine::logError( const QString &message )
00062 {
00063   QString text = QTime::currentTime().toString() + ": ";
00064   text += message;
00065 
00066   kdDebug() << "ERR: " << text << endl;
00067 
00068   emit error( message );
00069 }
00070 
00071 void Engine::setResolveStrategy( int strategy )
00072 {
00073   delete mSyncUi;
00074 
00075   switch ( strategy ) {
00076     case KonnectorPair::ResolveFirst:
00077       mSyncUi = new SyncUiFirst();
00078       break;
00079     case KonnectorPair::ResolveSecond:
00080       mSyncUi = new SyncUiSecond();
00081       break;
00082     case KonnectorPair::ResolveBoth:
00083       mSyncUi = new KSync::SyncUi();
00084       break;
00085     default:
00086       mSyncUi = new SyncUiKde( 0, true, true );
00087   }
00088 
00089   mCalendarSyncer.setSyncUi( mSyncUi );
00090   mAddressBookSyncer.setSyncUi( mSyncUi );
00091 }
00092 
00093 void Engine::go( KonnectorPair *pair )
00094 {
00095   kdDebug() << "Engine::go():" << endl;
00096 
00097   logMessage( i18n("Sync Action triggered") );
00098 
00099   setResolveStrategy( pair->resolveStrategy() );
00100 
00101   mOpenedKonnectors.clear();
00102   mProcessedKonnectors.clear();
00103   mKonnectorCount = 0;
00104 
00105   mKonnectors.clear();
00106 
00107   if ( mManager )
00108     disconnect( this, SIGNAL( doneSync() ), mManager, SLOT( emitFinished() ) );
00109 
00110   mManager = pair->manager();
00111   connect( this, SIGNAL( doneSync() ), mManager, SLOT( emitFinished() ) );
00112 
00113   KonnectorManager::Iterator it;
00114   for ( it = mManager->begin(); it != mManager->end(); ++it )
00115     mKonnectors.append( *it );
00116 
00117   Konnector *k;
00118   for( k = mKonnectors.first(); k; k = mKonnectors.next() ) {
00119     logMessage( i18n("Connecting '%1'").arg( k->resourceName() ) );
00120     if ( !k->connectDevice() ) {
00121       logError( i18n("Cannot connect device '%1'.").arg( k->resourceName() ) );
00122     } else {
00123       mOpenedKonnectors.append( k );
00124       ++mKonnectorCount;
00125     }
00126   }
00127 
00128   for ( k = mOpenedKonnectors.first(); k; k = mOpenedKonnectors.next() ) {
00129     logMessage( i18n("Request Syncees") );
00130     if ( !k->readSyncees() ) {
00131       logError( i18n("Cannot read data from '%1'.").arg( k->resourceName() ) );
00132     }
00133   }
00134 }
00135 
00136 void Engine::slotSynceesRead( Konnector *k )
00137 {
00138   logMessage( i18n("Syncees read from '%1'").arg( k->resourceName() ) );
00139 
00140   mProcessedKonnectors.append( k );
00141 
00142   SynceeList syncees = k->syncees();
00143 
00144   if ( syncees.count() == 0 ) {
00145     logMessage( i18n("Syncee list is empty.") );
00146     return;
00147   }
00148 
00149   tryExecuteActions();
00150 }
00151 
00152 void Engine::tryExecuteActions()
00153 {
00154   kdDebug() << "Engine::tryExecuteActions()" << endl;
00155 
00156 
00157   kdDebug() << "  konnectorCount: " << mKonnectorCount << endl;
00158   kdDebug() << "  processedKonnectorsCount: " << mProcessedKonnectors.count()
00159             << endl;
00160 
00161   Konnector *k;
00162   for( k = mProcessedKonnectors.first(); k; k = mProcessedKonnectors.next() )
00163     logMessage( i18n("Processed '%1'").arg( k->resourceName() ) );
00164 
00165   if ( mKonnectorCount == mProcessedKonnectors.count() ) {
00166     executeActions();
00167   }
00168 }
00169 
00170 void Engine::executeActions()
00171 {
00172   logMessage( i18n("Execute Actions") );
00173 
00174   Konnector *konnector;
00175   for ( konnector = mOpenedKonnectors.first(); konnector;
00176         konnector = mOpenedKonnectors.next() )
00177     konnector->applyFilters( KSync::Konnector::FilterBeforeSync );
00178 
00179   doSync();
00180 
00181   mProcessedKonnectors.clear();
00182 
00183   for( konnector = mOpenedKonnectors.first(); konnector;
00184        konnector = mOpenedKonnectors.next() ) {
00185     konnector->applyFilters( KSync::Konnector::FilterAfterSync );
00186 
00187     if ( !konnector->writeSyncees() )
00188       logError( i18n("Cannot write data back to '%1'.").arg( konnector->resourceName() ) );
00189   }
00190 }
00191 
00192 void Engine::slotSynceeReadError( Konnector *k )
00193 {
00194   logError( i18n("Error reading Syncees from '%1'").arg( k->resourceName() ) );
00195 
00196   --mKonnectorCount;
00197 
00198   tryExecuteActions();
00199 }
00200 
00201 void Engine::slotSynceesWritten( Konnector *k )
00202 {
00203   logMessage( i18n("Syncees written to '%1'").arg( k->resourceName() ) );
00204 
00205   mProcessedKonnectors.append( k );
00206 
00207   disconnectDevice( k );
00208 
00209   tryFinish();
00210 }
00211 
00212 void Engine::slotSynceeWriteError( Konnector *k )
00213 {
00214   logError( i18n("Error writing Syncees to '%1'").arg( k->resourceName() ) );
00215 
00216   --mKonnectorCount;
00217 
00218   disconnectDevice( k );
00219 
00220   tryFinish();
00221 }
00222 
00223 void Engine::disconnectDevice( Konnector *k )
00224 {
00225   if ( !k->disconnectDevice() )
00226     logError( i18n("Error disconnecting device '%1'").arg( k->resourceName() ) );
00227 }
00228 
00229 void Engine::tryFinish()
00230 {
00231   if ( mKonnectorCount == mProcessedKonnectors.count() ) {
00232     finish();
00233   }
00234 }
00235 
00236 void Engine::finish()
00237 {
00238   logMessage( i18n("Synchronization finished.") );
00239   emit doneSync();
00240 }
00241 
00242 void Engine::doSync()
00243 {
00244   mCalendarSyncer.clear();
00245   mAddressBookSyncer.clear();
00246 
00247   Konnector *k;
00248   for( k = mKonnectors.first(); k; k = mKonnectors.next() ) {
00249     SynceeList syncees = k->syncees();
00250 
00251     if ( syncees.count() == 0 )
00252       continue;
00253 
00254     CalendarSyncee *calendarSyncee = syncees.calendarSyncee();
00255     if ( calendarSyncee )
00256       mCalendarSyncer.addSyncee( calendarSyncee );
00257 
00258     AddressBookSyncee *addressBookSyncee = syncees.addressBookSyncee();
00259     if ( addressBookSyncee )
00260       mAddressBookSyncer.addSyncee( addressBookSyncee );
00261   }
00262 
00263   mCalendarSyncer.sync();
00264   mAddressBookSyncer.sync();
00265 }
00266 
00267 #include "engine.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys