36 #include <kstandarddirs.h>
39 #include <QtCore/QFile>
40 #include <QtCore/QTextStream>
41 #include <QtCore/QVariant>
48 QMap<QString, QVariant> idMap;
49 QMap<QString, QString> fingerprintMap;
56 : d( new IdMapperPrivate )
61 : d( new IdMapperPrivate )
94 QString file = d->path;
95 if ( !file.endsWith(
'/' ) ) {
98 file += d->identifier;
100 return KStandardDirs::locateLocal(
"data", file );
106 if ( !file.open( QIODevice::ReadOnly ) ) {
107 kError( 5800 ) <<
"Cannot read uid map file '" <<
filename() <<
"'";
113 QTextStream ts( &file );
115 while ( !ts.atEnd() ) {
116 line = ts.readLine( 1024 );
117 QStringList parts = line.split(
"\x02\x02", QString::KeepEmptyParts );
120 if ( parts.count() == 3 ) {
121 d->idMap.insert( parts[ 0 ], parts[ 1 ] );
122 d->fingerprintMap.insert( parts[ 0 ], parts[ 2 ] );
134 if ( !file.open( QIODevice::WriteOnly ) ) {
135 kError( 5800 ) <<
"Can't write uid map file '" <<
filename() <<
"'";
141 QMap<QString, QVariant>::Iterator it;
142 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
144 if ( d->fingerprintMap.contains( it.key() ) ) {
145 fingerprint = d->fingerprintMap[ it.key() ];
147 content += it.key() +
"\x02\x02" + it.value().toString() +
"\x02\x02" + fingerprint +
"\r\n";
149 QTextStream ts( &file );
159 d->fingerprintMap.clear();
164 if ( !( localId.isEmpty() || remoteId.isEmpty() ) ) {
165 d->idMap.insert( localId, remoteId );
171 if ( !remoteId.isEmpty( ) ) {
172 QMap<QString, QVariant>::Iterator it;
173 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
174 if ( it.value().toString() ==
remoteId ) {
176 QString key = it.key();
178 d->idMap.remove( key );
179 d->fingerprintMap.remove( key );
188 QMap<QString, QVariant>::ConstIterator it;
189 it = d->idMap.constFind( localId );
191 if ( it != d->idMap.constEnd() ) {
192 return it.value().toString();
200 QMap<QString, QVariant>::ConstIterator it;
201 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
202 if ( it.value().toString() ==
remoteId ) {
214 QMap<QString, QVariant>::ConstIterator it;
215 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
217 if ( d->fingerprintMap.contains( it.key() ) ) {
218 fp = d->fingerprintMap[ it.key() ];
220 content += it.key() +
'\t' + it.value().toString() +
'\t' + fp +
"\r\n";
228 if ( !( localId.isEmpty() || fingerprint.isEmpty() ) ) {
229 d->fingerprintMap.insert( localId, fingerprint );
235 if ( d->fingerprintMap.contains( localId ) ) {
236 return d->fingerprintMap[
localId ];
244 QMap<QString, QString> reverseMap;
245 QMap<QString, QVariant>::ConstIterator it;
246 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
247 reverseMap.insert( it.value().toString(), it.key() );
bool save()
Saves the map.
QString identifier() const
Return id map identifier.
IdMapper()
Create Id mapper.
void setPath(const QString &path)
Set id map path.
QString localId(const QString &remoteId) const
Returns the local id for the given remote id.
QString remoteId(const QString &localId) const
Returns the remote id of the given local id.
QMap< QString, QString > remoteIdMap() const
Returns the entire map of local-to-remote Ids.
void clear()
Clears the map.
bool load()
Loads the map.
QString fingerprint(const QString &localId) const
Returns the fingerprint for the map.
QString filename()
Filename of the map when stored on disk.
void setIdentifier(const QString &identifier)
Set id map identifier.
void setRemoteId(const QString &localId, const QString &remoteId)
Stores the remote id for the given local id.
QString asString() const
Returns a string representation of the id pairs, that's useful for debugging.
This file is part of the KDE resource framework and defines the IdMapper class.
void setFingerprint(const QString &localId, const QString &fingerprint)
Stores a fingerprint for an id which can be used to detect if the locally held version differs from w...
QString path() const
Return id map path.
void removeRemoteId(const QString &remoteId)
Removes the remote id.