00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 *s = mSyncees.first();
00066
while( s ) {
00067
if ( !s->
loadLog() ) {
00068 kdError() <<
"Syncer::sync(): Unable to load sync log for Syncee '"
00069 << s->
identifier() <<
"'" << endl;
00070 }
00071 s = mSyncees.next();
00072 }
00073
00074
Syncee *target = mSyncees.last();
00075
00076
if ( !target ) {
00077 kdWarning() <<
"Syncer::sync(): No Syncees set." << endl;
00078
return;
00079 }
00080
00081
Syncee *syncee = mSyncees.first();
00082
while ( syncee != target ) {
00083
syncToTarget( syncee, target );
00084 syncee = mSyncees.next();
00085 }
00086
if ( !target->
saveLog() ) {
00087 kdDebug() <<
"Syncer::sync() failed to save target log." << endl;
00088 }
00089 syncee = mSyncees.first();
00090
while ( syncee != target ) {
00091
syncToTarget( target, syncee,
true );
00092
if ( !syncee->
saveLog() ) {
00093 kdDebug() <<
"Syncer::sync() failed to save syncee log." << endl;
00094 }
00095 syncee = mSyncees.next();
00096 }
00097 }
00098
00099 void Syncer::syncAllToTarget(
Syncee *target,
bool writeback )
00100 {
00101
Syncee *syncee = mSyncees.first();
00102
while ( syncee ) {
00103
syncToTarget(syncee,target);
00104 syncee = mSyncees.next();
00105 }
00106
00107
if ( !target->
saveLog() ) {
00108 kdDebug() <<
"Syncer::syncAllToTarget() failed to save target log." << endl;
00109 }
00110
00111
if ( writeback ) {
00112
for (
Syncee *syncee = mSyncees.first(); syncee;
00113 syncee = mSyncees.next() ) {
00114
syncToTarget( target, syncee,
true );
00115 }
00116 }
00117 }
00118
00119 void Syncer::syncToTarget(
Syncee *source,
Syncee *target,
bool override )
00120 {
00121 mAlgorithm->
syncToTarget( source, target, override );
00122 }
00123
00124 void Syncer::setSyncAlgorithm(
SyncAlgorithm *algorithm )
00125 {
00126
if ( mOwnAlgorithm )
delete mAlgorithm;
00127 mOwnAlgorithm =
false;
00128 mAlgorithm = algorithm;
00129 }
00130
00131 void Syncer::setSyncUi(
SyncUi *ui )
00132 {
00133
if ( mOwnUi )
delete mUi;
00134 mOwnUi =
false;
00135 mUi = ui;
00136 mAlgorithm->
setUi( ui );
00137 }