kitchensync

syncer.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (c) 2002 Holger Freyther <zecke@handhelds.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include <kdebug.h>
00024 
00025 #include "standardsync.h"
00026 #include "syncui.h"
00027 #include "syncee.h"
00028 
00029 #include "syncer.h"
00030 
00031 using namespace KSync;
00032 
00033 Syncer::Syncer( SyncUi *ui, SyncAlgorithm *algorithm )
00034   : mOwnUi( false ), mOwnAlgorithm( false )
00035 {
00036   if ( !ui ) {
00037     mUi = new SyncUi();
00038     mOwnUi = true;
00039   } else mUi = ui;
00040 
00041   if ( !algorithm ) {
00042     mAlgorithm = new StandardSync( mUi );
00043     mOwnAlgorithm = true;
00044   } else mAlgorithm = algorithm;
00045 }
00046 
00047 Syncer::~Syncer()
00048 {
00049   if ( mOwnUi ) delete mUi;
00050   if ( mOwnAlgorithm ) delete mAlgorithm;
00051 }
00052 
00053 void Syncer::addSyncee( Syncee *syncee )
00054 {
00055   mSyncees.append( syncee );
00056 }
00057 
00058 void Syncer::clear()
00059 {
00060   mSyncees.clear();
00061 }
00062 
00063 void Syncer::sync()
00064 {
00065   Syncee *target = mSyncees.last();
00066 
00067   if ( !target ) {
00068     kdWarning() << "Syncer::sync(): No Syncees set." << endl;
00069     return;
00070   }
00071 
00072   Syncee *syncee = mSyncees.first();
00073   while ( syncee != target ) {
00074     syncToTarget( syncee, target );
00075     syncee = mSyncees.next();
00076   }
00077 
00078   syncee = mSyncees.first();
00079   while ( syncee != target ) {
00080     syncToTarget( target, syncee, true );
00081 
00082     syncee = mSyncees.next();
00083   }
00084 }
00085 
00086 void Syncer::syncAllToTarget( Syncee *target, bool writeback )
00087 {
00088   Syncee *syncee = mSyncees.first();
00089   while ( syncee ) {
00090     syncToTarget(syncee,target);
00091     syncee = mSyncees.next();
00092   }
00093 
00094 
00095   if ( writeback ) {
00096     for ( Syncee *syncee = mSyncees.first(); syncee;
00097           syncee = mSyncees.next() ) {
00098       syncToTarget( target, syncee, true );
00099     }
00100   }
00101 }
00102 
00103 void Syncer::syncToTarget( Syncee *source, Syncee *target, bool override )
00104 {
00105   mAlgorithm->syncToTarget( source, target, override );
00106 }
00107 
00108 void Syncer::setSyncAlgorithm( SyncAlgorithm *algorithm )
00109 {
00110   if ( mOwnAlgorithm ) delete mAlgorithm;
00111   mOwnAlgorithm = false;
00112   mAlgorithm = algorithm;
00113 }
00114 
00115 void Syncer::setSyncUi( SyncUi *ui )
00116 {
00117   if ( mOwnUi ) delete mUi;
00118   mOwnUi = false;
00119   mUi = ui;
00120   mAlgorithm->setUi( ui );
00121 }
KDE Home | KDE Accessibility Home | Description of Access Keys