• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepimlibs API Reference
  • KDE Home
  • Contact Us
 

kresources

  • sources
  • kde-4.12
  • kdepimlibs
  • kresources
idmapper.cpp
Go to the documentation of this file.
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 */
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 
43 namespace KRES {
44 
45 class IdMapperPrivate
46 {
47  public:
48  QMap<QString, QVariant> idMap;
49  QMap<QString, QString> fingerprintMap;
50 
51  QString path;
52  QString identifier;
53 };
54 
55 IdMapper::IdMapper()
56  : d( new IdMapperPrivate )
57 {
58 }
59 
60 IdMapper::IdMapper( const QString &path, const QString &identifier )
61  : d( new IdMapperPrivate )
62 {
63  d->path = path;
64  d->identifier = identifier;
65 }
66 
67 IdMapper::~IdMapper()
68 {
69  delete d;
70 }
71 
72 void IdMapper::setPath( const QString &path )
73 {
74  d->path = path;
75 }
76 
77 QString IdMapper::path() const
78 {
79  return d->path;
80 }
81 
82 void IdMapper::setIdentifier( const QString &identifier )
83 {
84  d->identifier = identifier;
85 }
86 
87 QString IdMapper::identifier() const
88 {
89  return d->identifier;
90 }
91 
92 QString 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 
103 bool 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 
131 bool 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 
156 void IdMapper::clear()
157 {
158  d->idMap.clear();
159  d->fingerprintMap.clear();
160 }
161 
162 void IdMapper::setRemoteId( const QString &localId, const QString &remoteId )
163 {
164  if ( !( localId.isEmpty() || remoteId.isEmpty() ) ) {
165  d->idMap.insert( localId, remoteId );
166  }
167 }
168 
169 void 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 
186 QString 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 
198 QString 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 
210 QString 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 
226 void IdMapper::setFingerprint( const QString &localId, const QString &fingerprint )
227 {
228  if ( !( localId.isEmpty() || fingerprint.isEmpty() ) ) {
229  d->fingerprintMap.insert( localId, fingerprint );
230  }
231 }
232 
233 QString 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 
242 QMap<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 }
KRES::IdMapper::save
bool save()
Saves the map.
Definition: idmapper.cpp:131
KRES::IdMapper::identifier
QString identifier() const
Return id map identifier.
Definition: idmapper.cpp:87
KRES::IdMapper::IdMapper
IdMapper()
Create Id mapper.
Definition: idmapper.cpp:55
KRES::IdMapper::setPath
void setPath(const QString &path)
Set id map path.
Definition: idmapper.cpp:72
KRES::IdMapper::~IdMapper
~IdMapper()
Destructor.
Definition: idmapper.cpp:67
KRES::IdMapper::localId
QString localId(const QString &remoteId) const
Returns the local id for the given remote id.
Definition: idmapper.cpp:198
KRES::IdMapper::remoteId
QString remoteId(const QString &localId) const
Returns the remote id of the given local id.
Definition: idmapper.cpp:186
KRES::IdMapper::remoteIdMap
QMap< QString, QString > remoteIdMap() const
Returns the entire map of local-to-remote Ids.
Definition: idmapper.cpp:242
KRES::IdMapper::clear
void clear()
Clears the map.
Definition: idmapper.cpp:156
KRES::IdMapper::load
bool load()
Loads the map.
Definition: idmapper.cpp:103
KRES::IdMapper::fingerprint
QString fingerprint(const QString &localId) const
Returns the fingerprint for the map.
Definition: idmapper.cpp:233
KRES::IdMapper::filename
QString filename()
Filename of the map when stored on disk.
Definition: idmapper.cpp:92
KRES::IdMapper::setIdentifier
void setIdentifier(const QString &identifier)
Set id map identifier.
Definition: idmapper.cpp:82
KRES::IdMapper::setRemoteId
void setRemoteId(const QString &localId, const QString &remoteId)
Stores the remote id for the given local id.
Definition: idmapper.cpp:162
KRES::IdMapper::asString
QString asString() const
Returns a string representation of the id pairs, that's useful for debugging.
Definition: idmapper.cpp:210
idmapper.h
This file is part of the KDE resource framework and defines the IdMapper class.
KRES::IdMapper::setFingerprint
void setFingerprint(const QString &localId, const QString &fingerprint)
Stores a fingerprint for an id which can be used to detect if the locally held version differs from w...
Definition: idmapper.cpp:226
KRES::IdMapper::path
QString path() const
Return id map path.
Definition: idmapper.cpp:77
KRES::IdMapper::removeRemoteId
void removeRemoteId(const QString &remoteId)
Removes the remote id.
Definition: idmapper.cpp:169
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:00:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kresources

Skip menu "kresources"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs API Reference

Skip menu "kdepimlibs API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kldap
  • kmbox
  • kmime
  • kpimidentities
  • kpimtextedit
  • kresources
  • ktnef
  • kxmlrpcclient
  • microblog

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal