1/*
2 This file is part of kdepim.
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 @file
24 This file is part of the KDE resource framework and defines the
25 IdMapper class.
26
27 @brief
28 Keeps a map of paths and identifiers.
29
30 @author Tobias Koenig
31 @author Cornelius Schumacher
32*/
33
34#include "idmapper.h"
35
36#include <kstandarddirs.h>
37#include <kdebug.h>
38
39#include <QtCore/QFile>
40#include <QtCore/QTextStream>
41#include <QtCore/QVariant>
42
43namespace KRES {
44
45class IdMapperPrivate
46{
47 public:
48 QMap<QString, QVariant> idMap;
49 QMap<QString, QString> fingerprintMap;
50
51 QString path;
52 QString identifier;
53};
54
55IdMapper::IdMapper()
56 : d( new IdMapperPrivate )
57{
58}
59
60IdMapper::IdMapper( const QString &path, const QString &identifier )
61 : d( new IdMapperPrivate )
62{
63 d->path = path;
64 d->identifier = identifier;
65}
66
67IdMapper::~IdMapper()
68{
69 delete d;
70}
71
72void IdMapper::setPath( const QString &path )
73{
74 d->path = path;
75}
76
77QString IdMapper::path() const
78{
79 return d->path;
80}
81
82void IdMapper::setIdentifier( const QString &identifier )
83{
84 d->identifier = identifier;
85}
86
87QString IdMapper::identifier() const
88{
89 return d->identifier;
90}
91
92QString IdMapper::filename()
93{
94 QString file = d->path;
95 if ( !file.endsWith( QLatin1Char('/') ) ) {
96 file += QLatin1Char('/');
97 }
98 file += d->identifier;
99
100 return KStandardDirs::locateLocal( "data", file );
101}
102
103bool IdMapper::load()
104{
105 QFile file( filename() );
106 if ( !file.open( QIODevice::ReadOnly ) ) {
107 kError( 5800 ) << "Cannot read uid map file '" << filename() << "'";
108 return false;
109 }
110
111 clear();
112
113 QTextStream ts( &file );
114 QString line;
115 while ( !ts.atEnd() ) {
116 line = ts.readLine( 1024 );
117 QStringList parts = line.split( QLatin1String("\x02\x02"), QString::KeepEmptyParts );
118 // sanity check; the uidmap file could be corrupted and
119 // QList doesn't like accessing invalid indexes
120 if ( parts.count() == 3 ) {
121 d->idMap.insert( parts[ 0 ], parts[ 1 ] );
122 d->fingerprintMap.insert( parts[ 0 ], parts[ 2 ] );
123 }
124 }
125
126 file.close();
127
128 return true;
129}
130
131bool IdMapper::save()
132{
133 QFile file( filename() );
134 if ( !file.open( QIODevice::WriteOnly ) ) {
135 kError( 5800 ) << "Can't write uid map file '" << filename() << "'";
136 return false;
137 }
138
139 QString content;
140
141 QMap<QString, QVariant>::Iterator it;
142 for ( it = d->idMap.begin(); it != d->idMap.end(); ++it ) {
143 QString fingerprint;
144 if ( d->fingerprintMap.contains( it.key() ) ) {
145 fingerprint = d->fingerprintMap[ it.key() ];
146 }
147 content += it.key() + QLatin1String("\x02\x02") + it.value().toString() + QLatin1String("\x02\x02") + fingerprint + QLatin1String("\r\n");
148 }
149 QTextStream ts( &file );
150 ts << content;
151 file.close();
152
153 return true;
154}
155
156void IdMapper::clear()
157{
158 d->idMap.clear();
159 d->fingerprintMap.clear();
160}
161
162void IdMapper::setRemoteId( const QString &localId, const QString &remoteId )
163{
164 if ( !( localId.isEmpty() || remoteId.isEmpty() ) ) {
165 d->idMap.insert( localId, remoteId );
166 }
167}
168
169void IdMapper::removeRemoteId( const QString &remoteId )
170{
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 ) {
175
176 QString key = it.key();
177
178 d->idMap.remove( key );
179 d->fingerprintMap.remove( key );
180 return;
181 }
182 }
183 }
184}
185
186QString IdMapper::remoteId( const QString &localId ) const
187{
188 QMap<QString, QVariant>::ConstIterator it;
189 it = d->idMap.constFind( localId );
190
191 if ( it != d->idMap.constEnd() ) {
192 return it.value().toString();
193 } else {
194 return QString();
195 }
196}
197
198QString IdMapper::localId( const QString &remoteId ) const
199{
200 QMap<QString, QVariant>::ConstIterator it;
201 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
202 if ( it.value().toString() == remoteId ) {
203 return it.key();
204 }
205 }
206
207 return QString();
208}
209
210QString IdMapper::asString() const
211{
212 QString content;
213
214 QMap<QString, QVariant>::ConstIterator it;
215 for ( it = d->idMap.constBegin(); it != d->idMap.constEnd(); ++it ) {
216 QString fp;
217 if ( d->fingerprintMap.contains( it.key() ) ) {
218 fp = d->fingerprintMap[ it.key() ];
219 }
220 content += it.key() + QLatin1Char('\t') + it.value().toString() + QLatin1Char('\t') + fp + QLatin1String("\r\n");
221 }
222
223 return content;
224}
225
226void IdMapper::setFingerprint( const QString &localId, const QString &fingerprint )
227{
228 if ( !( localId.isEmpty() || fingerprint.isEmpty() ) ) {
229 d->fingerprintMap.insert( localId, fingerprint );
230 }
231}
232
233QString IdMapper::fingerprint( const QString &localId ) const
234{
235 if ( d->fingerprintMap.contains( localId ) ) {
236 return d->fingerprintMap[ localId ];
237 } else {
238 return QString();
239 }
240}
241
242QMap<QString, QString> IdMapper::remoteIdMap() const
243{
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() );
248 }
249 return reverseMap;
250}
251
252}
253