libemailfunctions

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