TDE personal information management applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

192 lines
4.6 KiB

  1. /*
  2. This file is part of tdepim.
  3. Copyright (c) 2004 Tobias Koenig <tokoe@kde.org>
  4. Copyright (c) 2004 Cornelius Schumacher <schumacher@kde.org>
  5. This library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. This library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public License
  14. along with this library; see the file COPYING.LIB. If not, write to
  15. the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. Boston, MA 02110-1301, USA.
  17. */
  18. #include "idmapper.h"
  19. #include <kstandarddirs.h>
  20. #include <kdebug.h>
  21. #include <tqfile.h>
  22. using namespace KPIM;
  23. IdMapper::IdMapper()
  24. {
  25. }
  26. IdMapper::IdMapper( const TQString &path, const TQString &identifier )
  27. : mPath( path ), mIdentifier( identifier )
  28. {
  29. }
  30. IdMapper::~IdMapper()
  31. {
  32. }
  33. void IdMapper::setPath( const TQString &path )
  34. {
  35. mPath = path;
  36. }
  37. void IdMapper::setIdentifier( const TQString &identifier )
  38. {
  39. mIdentifier = identifier;
  40. }
  41. TQString IdMapper::filename()
  42. {
  43. TQString file = mPath;
  44. if ( !file.endsWith( "/" ) ) file += "/";
  45. file += mIdentifier;
  46. return locateLocal( "data", file );
  47. }
  48. bool IdMapper::load()
  49. {
  50. TQFile file( filename() );
  51. if ( !file.open( IO_ReadOnly ) ) {
  52. kdError(5800) << "Can't read uid map file '" << filename() << "'" << endl;
  53. return false;
  54. }
  55. clear();
  56. TQString line;
  57. while ( file.readLine( line, 1024 ) != -1 ) {
  58. line.truncate( line.length() - 2 ); // strip newline
  59. TQStringList parts = TQStringList::split( "\x02\x02", line, true );
  60. mIdMap.insert( parts[ 0 ], parts[ 1 ] );
  61. mFingerprintMap.insert( parts[ 0 ], parts[ 2 ] );
  62. }
  63. file.close();
  64. return true;
  65. }
  66. bool IdMapper::save()
  67. {
  68. TQFile file( filename() );
  69. if ( !file.open( IO_WriteOnly ) ) {
  70. kdError(5800) << "Can't write uid map file '" << filename() << "'" << endl;
  71. return false;
  72. }
  73. TQString content;
  74. TQStringVariantMap::Iterator it;
  75. for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) {
  76. TQString fingerprint( "" );
  77. if ( mFingerprintMap.contains( it.key() ) )
  78. fingerprint = mFingerprintMap[ it.key() ];
  79. content += it.key() + "\x02\x02" + it.data().toString() + "\x02\x02" + fingerprint + "\r\n";
  80. }
  81. file.writeBlock( content.latin1(), tqstrlen( content.latin1() ) );
  82. file.close();
  83. return true;
  84. }
  85. void IdMapper::clear()
  86. {
  87. mIdMap.clear();
  88. mFingerprintMap.clear();
  89. }
  90. void IdMapper::setRemoteId( const TQString &localId, const TQString &remoteId )
  91. {
  92. mIdMap.replace( localId, remoteId );
  93. }
  94. void IdMapper::removeRemoteId( const TQString &remoteId )
  95. {
  96. TQStringVariantMap::Iterator it;
  97. for ( it = mIdMap.begin(); it != mIdMap.end(); ++it )
  98. if ( it.data().toString() == remoteId ) {
  99. mIdMap.remove( it );
  100. mFingerprintMap.remove( it.key() );
  101. return;
  102. }
  103. }
  104. TQString IdMapper::remoteId( const TQString &localId ) const
  105. {
  106. TQStringVariantMap::ConstIterator it;
  107. it = mIdMap.find( localId );
  108. if ( it != mIdMap.end() )
  109. return it.data().toString();
  110. else
  111. return TQString();
  112. }
  113. TQString IdMapper::localId( const TQString &remoteId ) const
  114. {
  115. TQStringVariantMap::ConstIterator it;
  116. for ( it = mIdMap.begin(); it != mIdMap.end(); ++it )
  117. if ( it.data().toString() == remoteId )
  118. return it.key();
  119. return TQString();
  120. }
  121. TQString IdMapper::asString() const
  122. {
  123. TQString content;
  124. TQStringVariantMap::ConstIterator it;
  125. for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) {
  126. TQString fp;
  127. if ( mFingerprintMap.contains( it.key() ) )
  128. fp = mFingerprintMap[ it.key() ];
  129. content += it.key() + "\t" + it.data().toString() + "\t" + fp + "\r\n";
  130. }
  131. return content;
  132. }
  133. void IdMapper::setFingerprint( const TQString &localId, const TQString &fingerprint )
  134. {
  135. mFingerprintMap.insert( localId, fingerprint );
  136. }
  137. const TQString& IdMapper::fingerprint( const TQString &localId ) const
  138. {
  139. if ( mFingerprintMap.contains( localId ) )
  140. return mFingerprintMap[ localId ];
  141. else
  142. return TQString::null;
  143. }
  144. TQMap<TQString, TQString> IdMapper::remoteIdMap() const
  145. {
  146. TQMap<TQString, TQString> reverseMap;
  147. TQStringVariantMap::ConstIterator it;
  148. for ( it = mIdMap.begin(); it != mIdMap.end(); ++it ) {
  149. reverseMap.insert( it.data().toString(), it.key() );
  150. }
  151. return reverseMap;
  152. }