1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtOrganizer module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
17
** GNU Lesser General Public License Usage
18
** Alternatively, this file may be used under the terms of the GNU Lesser
19
** General Public License version 2.1 as published by the Free Software
20
** Foundation and appearing in the file LICENSE.LGPL included in the
21
** packaging of this file. Please review the following information to
22
** ensure the GNU Lesser General Public License version 2.1 requirements
23
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
** In addition, as a special exception, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include <qorganizercollectionid.h>
43
#include <qorganizercollectionengineid.h>
44
#include <private/qorganizermanager_p.h>
46
#include <QtCore/qstringlist.h>
48
#ifndef QT_NO_DEBUG_STREAM
49
#include <QtCore/qdebug.h>
50
#endif // QT_NO_DEBUG_STREAM
52
#if !defined(Q_CC_MWERKS)
54
template<> QTORGANIZER_PREPEND_NAMESPACE(QOrganizerCollectionEngineId) *QSharedDataPointer<QTORGANIZER_PREPEND_NAMESPACE(QOrganizerCollectionEngineId)>::clone()
56
return d ? d->clone() : 0;
61
QT_BEGIN_NAMESPACE_ORGANIZER
64
\class QOrganizerCollectionId
65
\brief The QOrganizerCollectionId class provides information that uniquely identifies a collection
66
in a particular manager.
68
\ingroup organizer-main
70
It consists of a manager URI which identifies the manager which manages the collection, and the
71
ID of the collection in that manager.
75
Constructs a new, null collection ID.
77
QOrganizerCollectionId::QOrganizerCollectionId()
83
Cleans up the memory in use by the collection ID.
85
QOrganizerCollectionId::~QOrganizerCollectionId()
90
Constructs a manager-unique ID which wraps the given engine-unique \a engineId.
92
This ID takes ownership of the \a engineId and will delete it when this ID is destructed. Engine
93
implementors must not delete the \a engineId, otherwise undefined behaviour will occur.
95
QOrganizerCollectionId::QOrganizerCollectionId(QOrganizerCollectionEngineId *engineId)
101
Constructs a new collection ID as a copy of \a other.
103
QOrganizerCollectionId::QOrganizerCollectionId(const QOrganizerCollectionId &other)
109
Assigns the collection ID to be equal to \a other.
111
QOrganizerCollectionId &QOrganizerCollectionId::operator=(const QOrganizerCollectionId &other)
118
Returns true if it has the same manager URI and ID as \a other.
120
bool QOrganizerCollectionId::operator==(const QOrganizerCollectionId &other) const
122
// if both ids are null then they are equal.
123
if (d == 0 && other.d == 0)
127
// ensure they're of the same type (and therefore comparable)
128
if (d->managerUri() == other.d->managerUri())
129
return d->isEqualTo(other.d);
135
Returns true if either the manager URI or ID of it is different to that of \a other.
137
bool QOrganizerCollectionId::operator!=(const QOrganizerCollectionId &other) const
139
return !(*this == other);
143
Returns true if this ID is less than the \a other.
145
This ID will be considered less than the \a other if the manager URI of this ID is alphabetically
146
less than the manager URI of the \a other. If both IDs have the same manager URI, this ID will
147
be considered less than the \a other if the engine ID is less than the engine ID of the \a other.
149
The invalid, empty ID consists of an empty manager URI and a null engine ID, and hence will be
150
less than any non-invalid ID.
152
This operator is provided primarily to allow use of a QOrganizerCollectionId as a key in a QMap.
154
bool QOrganizerCollectionId::operator<(const QOrganizerCollectionId &other) const
156
// a null id is always less than a non-null id.
157
if (d == 0 && other.d != 0)
161
// ensure they're of the same type (and therefore comparable)
162
if (d->managerUri() == other.d->managerUri())
163
return d->isLessThan(other.d);
165
// not the same type? just compare the manager uri.
166
return d->managerUri() < other.d->managerUri();
173
Returns true if the engine ID part of this ID is null (default constructed); otherwise, returns false.
175
bool QOrganizerCollectionId::isNull() const
181
\relates QOrganizerCollectionId
182
Returns the hash value for \a key.
184
uint qHash(const QOrganizerCollectionId &key)
187
return key.d->hash();
191
#ifndef QT_NO_DEBUG_STREAM
193
\relates QOrganizerCollectionId
194
Outputs \a id to the debug stream \a dbg.
196
QDebug operator<<(QDebug dbg, const QOrganizerCollectionId &id)
198
dbg.nospace() << "QOrganizerCollectionId(";
200
dbg.nospace() << "(null))";
202
id.d->debugStreamOut(dbg) << ")";
203
return dbg.maybeSpace();
205
#endif // QT_NO_DEBUG_STREAM
207
#ifndef QT_NO_DATASTREAM
209
\relates QOrganizerCollectionId
210
Streams \a collectionId to the data stream \a out.
212
QDataStream &operator<<(QDataStream &out, const QOrganizerCollectionId &collectionId)
214
out << (collectionId.toString());
219
\relates QOrganizerCollectionId
220
Streams \a collectionId in from the data stream \a in.
222
QDataStream &operator>>(QDataStream &in, QOrganizerCollectionId &collectionId)
226
collectionId = QOrganizerCollectionId::fromString(idString);
229
#endif // QT_NO_DATASTREAM
232
Returns the URI of the manager which contains the collection identified by this ID.
234
QString QOrganizerCollectionId::managerUri() const
236
return d ? d->managerUri() : QString::null;
242
Builds a string from the given \a managerName, \a params and \a engineId.
244
inline QString buildIdString(const QString &managerName, const QMap<QString, QString> ¶ms, const QString &engineId)
246
// the constructed id string will be of the form: "qtorganizer:managerName:param1=value1¶m2=value2:
247
QString ret(QStringLiteral("qtorganizer:%1:%2:%3"));
249
// we have to escape each param
250
QStringList escapedParams;
251
QStringList keys = params.keys();
252
for (int i = 0; i < keys.size(); ++i) {
253
QString key = keys.at(i);
254
QString arg = params.value(key);
255
arg = arg.replace(QLatin1Char('&'), QStringLiteral("&"));
256
arg = arg.replace(QLatin1Char('='), QStringLiteral("&equ;"));
257
arg = arg.replace(QLatin1Char(':'), QStringLiteral(":"));
258
key = key.replace(QLatin1Char('&'), QStringLiteral("&"));
259
key = key.replace(QLatin1Char('='), QStringLiteral("&equ;"));
260
key = key.replace(QLatin1Char(':'), QStringLiteral(":"));
261
key = key + QLatin1Char('=') + arg;
262
escapedParams.append(key);
265
// and we escape the engine id string.
266
QString escapedEngineId = engineId;
267
escapedEngineId.replace(QLatin1Char('&'), QStringLiteral("&"));
268
escapedEngineId.replace(QLatin1Char(':'), QStringLiteral(":"));
270
return ret.arg(managerName, escapedParams.join(QStringLiteral("&")), escapedEngineId);
274
Serializes the ID to a string. The format of the string will be:
275
"qtorganizer:managerName:constructionParams:serializedEngineLocalItemId".
277
QString QOrganizerCollectionId::toString() const
280
QMap<QString, QString> params;
284
QOrganizerManager::parseUri(d->managerUri(), &mgrName, ¶ms);
285
engineId = d->toString();
288
// having extracted the params the name, we now need to build a new string.
289
return buildIdString(mgrName, params, engineId);
295
Parses the individual components of the given \a idString and fills the \a managerName, \a params
296
and \a engineIdString.
298
Returns true if the parts could be parsed successfully, false otherwise.
300
inline bool parseIdString(const QString &idString, QString *managerName, QMap<QString, QString> *params, QString *engineIdString)
302
QStringList colonSplit = idString.split(QLatin1Char(':'));
304
QString prefix = colonSplit.value(0);
305
if (prefix != QStringLiteral("qtorganizer") || colonSplit.size() != 4)
306
return false; // invalid serialized string. we cannot continue.
308
QString mgrName = colonSplit.value(1);
309
QString paramString = colonSplit.value(2);
310
QString engIdString = colonSplit.value(3);
312
// Now we have to decode each parameter
313
QMap<QString, QString> outParams;
314
if (!paramString.isEmpty()) {
315
QStringList params = paramString.split(QRegExp(QStringLiteral("&(?!(amp;|equ;))")), QString::KeepEmptyParts);
316
// If we have an empty string for paramstring, we get one entry in params,
317
// so skip that case.
318
for (int i = 0; i < params.count(); ++i) {
319
/* This should be something like "foo&bar&equ;=grob&" */
320
QStringList paramChunk = params.value(i).split(QStringLiteral("="), QString::KeepEmptyParts);
322
if (paramChunk.count() != 2)
325
QString arg = paramChunk.value(0);
326
QString param = paramChunk.value(1);
328
arg.replace(QStringLiteral(":"), QStringLiteral(":"));
329
arg.replace(QStringLiteral("&equ;"), QStringLiteral("="));
330
arg.replace(QStringLiteral("&"), QStringLiteral("&"));
331
param.replace(QStringLiteral(":"), QStringLiteral(":"));
332
param.replace(QStringLiteral("&equ;"), QStringLiteral("="));
333
param.replace(QStringLiteral("&"), QStringLiteral("&"));
336
outParams.insert(arg, param);
340
// and unescape the engine id string.
341
engIdString.replace(QStringLiteral(":"), QStringLiteral(":"));
342
engIdString.replace(QStringLiteral("&"), QStringLiteral("&"));
344
// now fill the return values.
346
*managerName = mgrName;
350
*engineIdString = engIdString;
357
Deserializes the given \a idString. Returns a default-constructed (null) collection ID if the
358
given \a idString is not a valid, serialized collection ID, or if the manager engine from which
359
the ID refers to could not be found.
361
QOrganizerCollectionId QOrganizerCollectionId::fromString(const QString &idString)
364
QMap<QString, QString> params;
365
QString engineIdString;
367
if (!parseIdString(idString, &managerName, ¶ms, &engineIdString))
368
return QOrganizerCollectionId(); // invalid idString given.
370
QOrganizerCollectionEngineId *engineId = QOrganizerManagerData::createEngineCollectionId(managerName, params, engineIdString);
371
return QOrganizerCollectionId(engineId);
374
QT_END_NAMESPACE_ORGANIZER