idmapper.cpp
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 <tqfile.h> 00029 00030 using namespace KPIM; 00031 00032 IdMapper::IdMapper() 00033 { 00034 } 00035 00036 IdMapper::IdMapper( const TQString &path, const TQString &identifier ) 00037 : mPath( path ), mIdentifier( identifier ) 00038 { 00039 } 00040 00041 IdMapper::~IdMapper() 00042 { 00043 } 00044 00045 void IdMapper::setPath( const TQString &path ) 00046 { 00047 mPath = path; 00048 } 00049 00050 void IdMapper::setIdentifier( const TQString &identifier ) 00051 { 00052 mIdentifier = identifier; 00053 } 00054 00055 TQString IdMapper::filename() 00056 { 00057 TQString file = mPath; 00058 if ( !file.endsWith( "/" ) ) file += "/"; 00059 file += mIdentifier; 00060 00061 return locateLocal( "data", file ); 00062 } 00063 00064 bool IdMapper::load() 00065 { 00066 TQFile 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 TQString line; 00075 while ( file.readLine( line, 1024 ) != -1 ) { 00076 line.truncate( line.length() - 2 ); // strip newline 00077 00078 TQStringList parts = TQStringList::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 TQFile 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 TQString content; 00097 00098 TQMap<TQString, TQVariant>::Iterator it; 00099 for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) { 00100 TQString 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(), tqstrlen( 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 TQString &localId, const TQString &remoteId ) 00119 { 00120 mIdMap.replace( localId, remoteId ); 00121 } 00122 00123 void IdMapper::removeRemoteId( const TQString &remoteId ) 00124 { 00125 TQMap<TQString, TQVariant>::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 TQString IdMapper::remoteId( const TQString &localId ) const 00135 { 00136 TQMap<TQString, TQVariant>::ConstIterator it; 00137 it = mIdMap.find( localId ); 00138 00139 if ( it != mIdMap.end() ) 00140 return it.data().toString(); 00141 else 00142 return TQString(); 00143 } 00144 00145 TQString IdMapper::localId( const TQString &remoteId ) const 00146 { 00147 TQMap<TQString, TQVariant>::ConstIterator it; 00148 for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) 00149 if ( it.data().toString() == remoteId ) 00150 return it.key(); 00151 00152 return TQString(); 00153 } 00154 00155 TQString IdMapper::asString() const 00156 { 00157 TQString content; 00158 00159 TQMap<TQString, TQVariant>::ConstIterator it; 00160 for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) { 00161 TQString 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 TQString &localId, const TQString &fingerprint ) 00171 { 00172 mFingerprintMap.insert( localId, fingerprint ); 00173 } 00174 00175 const TQString& IdMapper::fingerprint( const TQString &localId ) const 00176 { 00177 if ( mFingerprintMap.contains( localId ) ) 00178 return mFingerprintMap[ localId ]; 00179 else 00180 return TQString::null; 00181 } 00182 00183 TQMap<TQString, TQString> IdMapper::remoteIdMap() const 00184 { 00185 TQMap<TQString, TQString> reverseMap; 00186 TQMap<TQString, TQVariant>::ConstIterator it; 00187 for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) { 00188 reverseMap.insert( it.data().toString(), it.key() ); 00189 } 00190 return reverseMap; 00191 }