kresources
idmapper.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00034 #include "idmapper.h"
00035
00036 #include <kstandarddirs.h>
00037 #include <kdebug.h>
00038
00039 #include <QtCore/QFile>
00040 #include <QtCore/QTextStream>
00041 #include <QtCore/QVariant>
00042
00043 namespace KRES {
00044
00045 class IdMapperPrivate
00046 {
00047 public:
00048 QMap<QString, QVariant> idMap;
00049 QMap<QString, QString> fingerprintMap;
00050
00051 QString path;
00052 QString identifier;
00053 };
00054
00055 IdMapper::IdMapper()
00056 : d( new IdMapperPrivate )
00057 {
00058 }
00059
00060 IdMapper::IdMapper( const QString &path, const QString &identifier )
00061 : d( new IdMapperPrivate )
00062 {
00063 d->path = path;
00064 d->identifier = identifier;
00065 }
00066
00067 IdMapper::~IdMapper()
00068 {
00069 delete d;
00070 }
00071
00072 void IdMapper::setPath( const QString &path )
00073 {
00074 d->path = path;
00075 }
00076
00077 QString IdMapper::path() const
00078 {
00079 return d->path;
00080 }
00081
00082 void IdMapper::setIdentifier( const QString &identifier )
00083 {
00084 d->identifier = identifier;
00085 }
00086
00087 QString IdMapper::identifier() const
00088 {
00089 return d->identifier;
00090 }
00091
00092 QString IdMapper::filename()
00093 {
00094 QString file = d->path;
00095 if ( !file.endsWith( '/' ) ) {
00096 file += '/';
00097 }
00098 file += d->identifier;
00099
00100 return KStandardDirs::locateLocal( "data", file );
00101 }
00102
00103 bool IdMapper::load()
00104 {
00105 QFile file( filename() );
00106 if ( !file.open( QIODevice::ReadOnly ) ) {
00107 kError(5800) << "Cannot read uid map file '" << filename() << "'";
00108 return false;
00109 }
00110
00111 clear();
00112
00113 QTextStream ts( &file );
00114 QString line;
00115 while ( !ts.atEnd() ) {
00116 line = ts.readLine( 1024 );
00117 QStringList parts = line.split( "\x02\x02", QString::KeepEmptyParts );
00118
00119
00120 if ( parts.count() == 3 ) {
00121 d->idMap.insert( parts[ 0 ], parts[ 1 ] );
00122 d->fingerprintMap.insert( parts[ 0 ], parts[ 2 ] );
00123 }
00124 }
00125
00126 file.close();
00127
00128 return true;
00129 }
00130
00131 bool IdMapper::save()
00132 {
00133 QFile file( filename() );
00134 if ( !file.open( QIODevice::WriteOnly ) ) {
00135 kError(5800) << "Can't write uid map file '" << filename() << "'";
00136 return false;
00137 }
00138
00139 QString content;
00140
00141 QMap<QString, QVariant>::Iterator it;
00142 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00143 QString fingerprint;
00144 if ( d->fingerprintMap.contains( it.key() ) ) {
00145 fingerprint = d->fingerprintMap[ it.key() ];
00146 }
00147 content += it.key() + "\x02\x02" + it.value().toString() + "\x02\x02" + fingerprint + "\r\n";
00148 }
00149 QTextStream ts( &file );
00150 ts << content;
00151 file.close();
00152
00153 return true;
00154 }
00155
00156 void IdMapper::clear()
00157 {
00158 d->idMap.clear();
00159 d->fingerprintMap.clear();
00160 }
00161
00162 void IdMapper::setRemoteId( const QString &localId, const QString &remoteId )
00163 {
00164 if ( !( localId.isEmpty() || remoteId.isEmpty() ) ) {
00165 d->idMap.insert( localId, remoteId );
00166 }
00167 }
00168
00169 void IdMapper::removeRemoteId( const QString &remoteId )
00170 {
00171 if ( !remoteId.isEmpty( ) ) {
00172 QMap<QString, QVariant>::Iterator it;
00173 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00174 if ( it.value().toString() == remoteId ) {
00175
00176 QString key = it.key();
00177
00178 d->idMap.remove( key );
00179 d->fingerprintMap.remove( key );
00180 return;
00181 }
00182 }
00183 }
00184 }
00185
00186 QString IdMapper::remoteId( const QString &localId ) const
00187 {
00188 QMap<QString, QVariant>::ConstIterator it;
00189 it = d->idMap.constFind( localId );
00190
00191 if ( it != d->idMap.constEnd() ) {
00192 return it.value().toString();
00193 } else {
00194 return QString();
00195 }
00196 }
00197
00198 QString IdMapper::localId( const QString &remoteId ) const
00199 {
00200 QMap<QString, QVariant>::ConstIterator it;
00201 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
00202 if ( it.value().toString() == remoteId ) {
00203 return it.key();
00204 }
00205 }
00206
00207 return QString();
00208 }
00209
00210 QString IdMapper::asString() const
00211 {
00212 QString content;
00213
00214 QMap<QString, QVariant>::ConstIterator it;
00215 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
00216 QString fp;
00217 if ( d->fingerprintMap.contains( it.key() ) ) {
00218 fp = d->fingerprintMap[ it.key() ];
00219 }
00220 content += it.key() + '\t' + it.value().toString() + '\t' + fp + "\r\n";
00221 }
00222
00223 return content;
00224 }
00225
00226 void IdMapper::setFingerprint( const QString &localId, const QString &fingerprint )
00227 {
00228 if ( !( localId.isEmpty() || fingerprint.isEmpty() ) ) {
00229 d->fingerprintMap.insert( localId, fingerprint );
00230 }
00231 }
00232
00233 QString IdMapper::fingerprint( const QString &localId ) const
00234 {
00235 if ( d->fingerprintMap.contains( localId ) ) {
00236 return d->fingerprintMap[ localId ];
00237 } else {
00238 return QString();
00239 }
00240 }
00241
00242 QMap<QString, QString> IdMapper::remoteIdMap() const
00243 {
00244 QMap<QString, QString> reverseMap;
00245 QMap<QString, QVariant>::ConstIterator it;
00246 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
00247 reverseMap.insert( it.value().toString(), it.key() );
00248 }
00249 return reverseMap;
00250 }
00251
00252 }