libemailfunctions

idmapper.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of kdepim.
00003 
00004     Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
00005     Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "idmapper.h"
00024 
00025 #include <kstandarddirs.h>
00026 #include <kdebug.h>
00027 
00028 #include <qfile.h>
00029 
00030 using namespace KPIM;
00031 
00032 IdMapper::IdMapper()
00033 {
00034 }
00035 
00036 IdMapper::IdMapper( const QString &path, const QString &identifier )
00037   : mPath( path ), mIdentifier( identifier )
00038 {
00039 }
00040 
00041 IdMapper::~IdMapper()
00042 {
00043 }
00044 
00045 void IdMapper::setPath( const QString &path )
00046 {
00047   mPath = path;
00048 }
00049 
00050 void IdMapper::setIdentifier( const QString &identifier )
00051 {
00052   mIdentifier = identifier;
00053 }
00054 
00055 QString IdMapper::filename()
00056 {
00057   QString file = mPath;
00058   if ( !file.endsWith( "/" ) ) file += "/";
00059   file += mIdentifier;
00060 
00061   return locateLocal( "data", file );
00062 }
00063 
00064 bool IdMapper::load()
00065 {
00066   QFile file( filename() );
00067   if ( !file.open( IO_ReadOnly ) ) {
00068     kdError(5800) << "Can't read uid map file '" << filename() << "'" << endl;
00069     return false;
00070   }
00071 
00072   clear();
00073 
00074   QString line;
00075   while ( file.readLine( line, 1024 ) != -1 ) {
00076     line.truncate( line.length() - 2 ); // strip newline
00077 
00078     QStringList parts = QStringList::split( "\x02\x02", line, true );
00079     mIdMap.insert( parts[ 0 ], parts[ 1 ] );
00080     mFingerprintMap.insert( parts[ 0 ], parts[ 2 ] );
00081   }
00082 
00083   file.close();
00084 
00085   return true;
00086 }
00087 
00088 bool IdMapper::save()
00089 {
00090   QFile file( filename() );
00091   if ( !file.open( IO_WriteOnly ) ) {
00092     kdError(5800) << "Can't write uid map file '" << filename() << "'" << endl;
00093     return false;
00094   }
00095 
00096   QString content;
00097 
00098   QMap<QString, QVariant>::Iterator it;
00099   for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) {
00100     QString fingerprint( "" );
00101     if ( mFingerprintMap.contains( it.key() ) )
00102       fingerprint = mFingerprintMap[ it.key() ];
00103     content += it.key() + "\x02\x02" + it.data().toString() + "\x02\x02" + fingerprint + "\r\n";
00104   }
00105 
00106   file.writeBlock( content.latin1(), qstrlen( content.latin1() ) );
00107   file.close();
00108 
00109   return true;
00110 }
00111 
00112 void IdMapper::clear()
00113 {
00114   mIdMap.clear();
00115   mFingerprintMap.clear();
00116 }
00117 
00118 void IdMapper::setRemoteId( const QString &localId, const QString &remoteId )
00119 {
00120   mIdMap.replace( localId, remoteId );
00121 }
00122 
00123 void IdMapper::removeRemoteId( const QString &remoteId )
00124 {
00125   QMap<QString, QVariant>::Iterator it;
00126   for ( it = mIdMap.begin(); it != mIdMap.end(); ++it )
00127     if ( it.data().toString() == remoteId ) {
00128       mIdMap.remove( it );
00129       mFingerprintMap.remove( it.key() );
00130       return;
00131     }
00132 }
00133 
00134 QString IdMapper::remoteId( const QString &localId ) const
00135 {
00136   QMap<QString, QVariant>::ConstIterator it;
00137   it = mIdMap.find( localId );
00138 
00139   if ( it != mIdMap.end() )
00140     return it.data().toString();
00141   else
00142     return QString::null;
00143 }
00144 
00145 QString IdMapper::localId( const QString &remoteId ) const
00146 {
00147   QMap<QString, QVariant>::ConstIterator it;
00148   for ( it = mIdMap.begin(); it != mIdMap.end(); ++it )
00149     if ( it.data().toString() == remoteId )
00150       return it.key();
00151 
00152   return QString::null;
00153 }
00154 
00155 QString IdMapper::asString() const
00156 {
00157   QString content;
00158 
00159   QMap<QString, QVariant>::ConstIterator it;
00160   for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) {
00161     QString fp;
00162     if ( mFingerprintMap.contains( it.key() ) )
00163       fp = mFingerprintMap[ it.key() ];
00164     content += it.key() + "\t" + it.data().toString() + "\t" + fp + "\r\n";
00165   }
00166 
00167   return content;
00168 }
00169 
00170 void IdMapper::setFingerprint( const QString &localId, const QString &fingerprint )
00171 {
00172   mFingerprintMap.insert( localId, fingerprint );
00173 }
00174 
00175 const QString& IdMapper::fingerprint( const QString &localId ) const
00176 {
00177   if ( mFingerprintMap.contains( localId ) )
00178     return mFingerprintMap[ localId ];
00179   else 
00180     return QString::null;
00181 }
00182 
00183 QMap<QString, QString> IdMapper::remoteIdMap() const
00184 {
00185   QMap<QString, QString> reverseMap;
00186   QMap<QString, QVariant>::ConstIterator it;
00187   for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) {
00188     reverseMap.insert( it.data().toString(), it.key() );
00189   }
00190   return reverseMap;
00191 }