00001
#ifndef OPIE_HELPER_META_TEMPLATE_NEW_H
00002
#define OPIE_HELPER_META_TEMPLATE_NEW_H
00003
00004
#include <kmdcodec.h>
00005
00006
#include <syncer.h>
00007
#include <idhelper.h>
00008
00009
00010
#include "md5map.h"
00011
00012
namespace OpieHelper {
00013
00014
template <
class Syncee = KSync::Syncee,
class Entry = KSync::SyncEntry>
00015
class MD5Template {
00016
public:
00017 MD5Template();
00018
virtual ~MD5Template();
00019
00020
void doMeta(
Syncee* newEntries,
const MD5Map& );
00021
void saveMeta(
Syncee*, MD5Map& );
00022
00023
protected:
00024
virtual QString string( Entry* ) = 0;
00025
00026
private:
00027
QString md5sum(
const QString& );
00028 };
00029
00030
template<
class Syncee,
class Entry>
00031 MD5Template<Syncee, Entry>::MD5Template() {
00032 }
00033
template<
class Syncee,
class Entry>
00034 MD5Template<Syncee, Entry>::~MD5Template() {
00035 }
00036
template<
class Syncee,
class Entry>
00037
void MD5Template<Syncee, Entry>::doMeta(
Syncee* newEntries,
const MD5Map& map) {
00038
bool found;
00039 Entry* entryNew;
00047
for ( entryNew = (Entry*)newEntries->
firstEntry();
00048 entryNew != 0l;
00049 entryNew = (Entry*)newEntries->
nextEntry() ) {
00050 found =
false;
00051
00052
00053
00054
00055
00056
00057
00058
00059
if ( map.contains( entryNew->id() ) ) {
00060 found =
true;
00061
QString str = map.md5sum( entryNew->id() );
00062
QString newStr = string( entryNew );
00063
00064
if ( str != md5sum( newStr ) ) {
00065 entryNew->setState( KSync::SyncEntry::Modified );
00066 }
00067 }
00068
if (!found ) {
00069 entryNew->setState( KSync::SyncEntry::Added );
00070 }
00071 }
00072
00073
00074
00075 MD5Map::Iterator it;
00076
MD5Map::Map ma = map.map();
00077
for ( it = ma.begin(); it != ma.end(); ++it ) {
00078 entryNew = (Entry*)newEntries->
findEntry( it.key() );
00085
if (!entryNew) {
00086 entryNew =
new Entry();
00087 entryNew->setId( it.key() );
00088
00089
00090 newEntries->
addEntry( entryNew );
00091 entryNew->setState( KSync::SyncEntry::Removed );
00092 }
00093 }
00094
00095 }
00096
template<
class Syncee,
class Entry>
00097
void MD5Template<Syncee, Entry>::saveMeta(
Syncee* syncee, MD5Map& map) {
00098 map.clear();
00099
for ( Entry* entry = (Entry*)syncee->
firstEntry();
00100 entry != 0l; entry = (Entry*)syncee->
nextEntry() ) {
00101
00102
00103
if ( entry->state() != KSync::SyncEntry::Removed ) {
00104 map.insert( entry->id(), md5sum( string( entry ) ) );
00105 }
00106 }
00107 }
00108
template<
class Syncee,
class Entry>
00109
QString MD5Template<Syncee, Entry>::md5sum(
const QString& base ) {
00110 KMD5 sum(base);
00111
QString str = QString::fromLatin1( sum.hexDigest().data() );
00112
00113
return str;
00114 }
00115 };
00116
00117
00118
#endif