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 d->idMap.insert( parts[ 0 ], parts[ 1 ] );
00119 d->fingerprintMap.insert( parts[ 0 ], parts[ 2 ] );
00120 }
00121
00122 file.close();
00123
00124 return true;
00125 }
00126
00127 bool IdMapper::save()
00128 {
00129 QFile file( filename() );
00130 if ( !file.open( QIODevice::WriteOnly ) ) {
00131 kError(5800) << "Can't write uid map file '" << filename() << "'";
00132 return false;
00133 }
00134
00135 QString content;
00136
00137 QMap<QString, QVariant>::Iterator it;
00138 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00139 QString fingerprint;
00140 if ( d->fingerprintMap.contains( it.key() ) ) {
00141 fingerprint = d->fingerprintMap[ it.key() ];
00142 }
00143 content += it.key() + "\x02\x02" + it.value().toString() + "\x02\x02" + fingerprint + "\r\n";
00144 }
00145 QTextStream ts( &file );
00146 ts << content;
00147 file.close();
00148
00149 return true;
00150 }
00151
00152 void IdMapper::clear()
00153 {
00154 d->idMap.clear();
00155 d->fingerprintMap.clear();
00156 }
00157
00158 void IdMapper::setRemoteId( const QString &localId, const QString &remoteId )
00159 {
00160 d->idMap.insert( localId, remoteId );
00161 }
00162
00163 void IdMapper::removeRemoteId( const QString &remoteId )
00164 {
00165 QMap<QString, QVariant>::Iterator it;
00166 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00167 if ( it.value().toString() == remoteId ) {
00168 d->idMap.erase( it );
00169 d->fingerprintMap.remove( it.key() );
00170 return;
00171 }
00172 }
00173 }
00174
00175 QString IdMapper::remoteId( const QString &localId ) const
00176 {
00177 QMap<QString, QVariant>::ConstIterator it;
00178 it = d->idMap.find( localId );
00179
00180 if ( it != d->idMap.end() ) {
00181 return it.value().toString();
00182 } else {
00183 return QString();
00184 }
00185 }
00186
00187 QString IdMapper::localId( const QString &remoteId ) const
00188 {
00189 QMap<QString, QVariant>::ConstIterator it;
00190 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00191 if ( it.value().toString() == remoteId ) {
00192 return it.key();
00193 }
00194 }
00195
00196 return QString();
00197 }
00198
00199 QString IdMapper::asString() const
00200 {
00201 QString content;
00202
00203 QMap<QString, QVariant>::ConstIterator it;
00204 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00205 QString fp;
00206 if ( d->fingerprintMap.contains( it.key() ) ) {
00207 fp = d->fingerprintMap[ it.key() ];
00208 }
00209 content += it.key() + '\t' + it.value().toString() + '\t' + fp + "\r\n";
00210 }
00211
00212 return content;
00213 }
00214
00215 void IdMapper::setFingerprint( const QString &localId, const QString &fingerprint )
00216 {
00217 d->fingerprintMap.insert( localId, fingerprint );
00218 }
00219
00220 QString IdMapper::fingerprint( const QString &localId ) const
00221 {
00222 if ( d->fingerprintMap.contains( localId ) ) {
00223 return d->fingerprintMap[ localId ];
00224 } else {
00225 return QString();
00226 }
00227 }
00228
00229 QMap<QString, QString> IdMapper::remoteIdMap() const
00230 {
00231 QMap<QString, QString> reverseMap;
00232 QMap<QString, QVariant>::ConstIterator it;
00233 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
00234 reverseMap.insert( it.value().toString(), it.key() );
00235 }
00236 return reverseMap;
00237 }
00238
00239 }