ksync Library API Documentation

ksyncer.cpp

00001 /*
00002     This file is part of ksync.
00003 
00004     Copyright (c) 2001 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 <qregexp.h>
00023 
00024 #include <kdebug.h>
00025 #include <ksimpleconfig.h>
00026 #include <kstandarddirs.h>
00027 
00028 #include "ksyncui.h"
00029 
00030 #include "ksyncer.h"
00031 
00032 KSyncEntry::KSyncEntry() :
00033   mSyncee(0)
00034 {
00035 }
00036 
00037 KSyncEntry::~KSyncEntry()
00038 {
00039 }
00040 
00041 void KSyncEntry::setSyncee(KSyncee *syncee)
00042 {
00043   mSyncee = syncee;
00044 }
00045 
00046 KSyncee *KSyncEntry::syncee()
00047 {
00048   return mSyncee;
00049 }
00050 
00051 
00052 KSyncee::KSyncee() :
00053   mStatusLog(0)
00054 {
00055 }
00056 
00057 KSyncee::~KSyncee()
00058 {
00059   delete mStatusLog;
00060 }
00061 
00062 void KSyncee::setFilename(const QString &filename)
00063 {
00064   mFilename = filename;
00065 }
00066 
00067 QString KSyncee::filename()
00068 {
00069   return mFilename;
00070 }
00071 
00072 KSyncEntry *KSyncee::findEntry(const QString &id)
00073 {
00074   kdDebug() << "KSyncee::findEntry() '" << id << "'" << endl;
00075 
00076   KSyncEntry *entry = firstEntry();
00077   while (entry) {
00078     if (entry->id() == id) return entry;
00079     entry = nextEntry();
00080   }
00081 
00082   return 0;
00083 }
00084 
00085 void KSyncee::replaceEntry(KSyncEntry *oldEntry,KSyncEntry *newEntry)
00086 {
00087   removeEntry(oldEntry);
00088   addEntry(newEntry);
00089 }
00090 
00091 bool KSyncee::hasChanged(KSyncEntry *entry)
00092 {
00093   if ( entry->timestamp().isEmpty() ) return true;
00094 
00095   mStatusLog->setGroup(entry->id());
00096   QString timestamp = mStatusLog->readEntry("Timestamp");
00097 
00098   return (timestamp != entry->timestamp());
00099 }
00100 
00101 bool KSyncee::load()
00102 {
00103   delete mStatusLog;
00104   mStatusLog = new KSimpleConfig(locateLocal("appdata",statusLogName()));
00105 
00106   return read();
00107 }
00108 
00109 bool KSyncee::save()
00110 {
00111   bool success = write();
00112   if (success) {
00113     writeLog();
00114     return true;
00115   } else {
00116     return false;
00117   }
00118 }
00119 
00120 void KSyncee::writeLog()
00121 {  
00122   for (KSyncEntry *entry = firstEntry();entry;entry = nextEntry()) {
00123     mStatusLog->setGroup(entry->id());
00124     mStatusLog->writeEntry("Name",entry->name());
00125     mStatusLog->writeEntry("Timestamp",entry->timestamp());
00126   }
00127   
00128   mStatusLog->sync();
00129 }
00130 
00131 QString KSyncee::statusLogName()
00132 {
00133   QString name = filename();
00134 
00135   name.replace(QRegExp("/"),"_");
00136   name.replace(QRegExp(":"),"_");
00137 
00138   name += ".syncee";
00139   
00140   return name;
00141 }
00142 
00143 
00144 KSyncer::KSyncer(KSyncUi *ui)
00145 {
00146   mSyncees.setAutoDelete(true);
00147   if (!ui) {
00148     mUi = new KSyncUi();
00149   } else {
00150     mUi = ui;
00151   }
00152 }
00153 
00154 KSyncer::~KSyncer()
00155 {
00156 }
00157 
00158 void KSyncer::addSyncee(KSyncee *syncee)
00159 {
00160   mSyncees.append(syncee);
00161 }
00162 
00163 void KSyncer::sync()
00164 {
00165   KSyncee *target = mSyncees.last();
00166   KSyncee *syncee = mSyncees.first();
00167   while (syncee != target) {
00168     syncToTarget(syncee,target);
00169     syncee = mSyncees.next();
00170   }
00171   target->save();
00172   syncee = mSyncees.first();
00173   while (syncee != target) {
00174     syncToTarget(target,syncee,true);
00175     syncee->save();
00176     syncee = mSyncees.next();
00177   }
00178 }
00179 
00180 void KSyncer::syncAllToTarget(KSyncee *target, bool writeback)
00181 {
00182   KSyncee *syncee = mSyncees.first();
00183   while(syncee) {
00184     syncToTarget(syncee,target);
00185     syncee = mSyncees.next();
00186   }
00187 
00188   target->writeLog();
00189 
00190   if (writeback) {
00191     for (KSyncee *syncee=mSyncees.first();syncee;syncee = mSyncees.next()) {
00192       syncToTarget(target,syncee,true);
00193     }
00194   }
00195 }
00196 
00197 void KSyncer::syncToTarget(KSyncee *source, KSyncee *target, bool override)
00198 {
00199   kdDebug() << "KSyncer::syncToTarget(): from: " << source->filename()
00200             << " to: " << target->filename() << "  override: "
00201             << (override ? "true" : "false") << endl;
00202 
00203   KSyncEntry *sourceEntry = source->firstEntry();
00204   while (sourceEntry) {
00205     KSyncEntry *targetEntry = target->findEntry(sourceEntry->id());
00206     if (targetEntry) {
00207       // Entry already exists in target
00208       if (sourceEntry->equals(targetEntry)) {
00209         // Entries are equal, no action required
00210       } else {
00211         // Entries are different, resolve conflict
00212         if (override) {
00213           // Force override
00214           target->replaceEntry(targetEntry,sourceEntry);
00215         } else {
00216           if (source->hasChanged(sourceEntry) &&
00217               target->hasChanged(targetEntry)) {
00218             // Both entries have changed
00219             KSyncEntry *result = mUi->deconflict(sourceEntry,targetEntry);
00220             if (result == sourceEntry) {
00221               target->replaceEntry(targetEntry,sourceEntry);
00222             }
00223           } else if (source->hasChanged(sourceEntry) &&
00224                      !target->hasChanged(targetEntry)) {
00225             // take source entry
00226             target->replaceEntry(targetEntry,sourceEntry);
00227           } else if (!source->hasChanged(sourceEntry) &&
00228                      target->hasChanged(targetEntry)) {
00229             // take target entry, no action required
00230           }
00231         }
00232       }
00233     } else {
00234       // New entry
00235       target->addEntry(sourceEntry);
00236     }
00237 
00238     sourceEntry = source->nextEntry();
00239   }
00240 
00241   source->writeLog();
00242 }
KDE Logo
This file is part of the documentation for ksync Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:37 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003