File indexing completed on 2024-11-10 04:40:27

0001 /*
0002   SPDX-FileCopyrightText: 2008 Omat Holding B.V. <info@omat.nl>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "collectionannotationsattribute.h"
0008 
0009 #include <QByteArray>
0010 
0011 using namespace Akonadi;
0012 
0013 CollectionAnnotationsAttribute::CollectionAnnotationsAttribute() = default;
0014 
0015 CollectionAnnotationsAttribute::CollectionAnnotationsAttribute(const QMap<QByteArray, QByteArray> &annotations)
0016     : mAnnotations(annotations)
0017 {
0018 }
0019 
0020 void CollectionAnnotationsAttribute::setAnnotations(const QMap<QByteArray, QByteArray> &annotations)
0021 {
0022     mAnnotations = annotations;
0023 }
0024 
0025 QMap<QByteArray, QByteArray> CollectionAnnotationsAttribute::annotations() const
0026 {
0027     return mAnnotations;
0028 }
0029 
0030 QByteArray CollectionAnnotationsAttribute::type() const
0031 {
0032     static const QByteArray sType("collectionannotations");
0033     return sType;
0034 }
0035 
0036 CollectionAnnotationsAttribute *CollectionAnnotationsAttribute::clone() const
0037 {
0038     return new CollectionAnnotationsAttribute(mAnnotations);
0039 }
0040 
0041 QByteArray CollectionAnnotationsAttribute::serialized() const
0042 {
0043     QByteArray result;
0044     for (auto it = mAnnotations.cbegin(), end = mAnnotations.cend(); it != end; ++it) {
0045         result += it.key();
0046         result += ' ';
0047         result += it.value();
0048         result += " % "; // We use this separator as '%' is not allowed in keys or values
0049     }
0050     result.chop(3);
0051 
0052     return result;
0053 }
0054 
0055 void CollectionAnnotationsAttribute::deserialize(const QByteArray &data)
0056 {
0057     mAnnotations.clear();
0058     const QList<QByteArray> lines = data.split('%');
0059 
0060     for (int i = 0; i < lines.size(); ++i) {
0061         QByteArray line = lines[i];
0062         if (i != 0 && line.startsWith(' ')) {
0063             line.remove(0, 1);
0064         }
0065         if (i != lines.size() - 1 && line.endsWith(' ')) {
0066             line.chop(1);
0067         }
0068         if (line.trimmed().isEmpty()) {
0069             continue;
0070         }
0071         int wsIndex = line.indexOf(' ');
0072         if (wsIndex > 0) {
0073             const QByteArray key = line.mid(0, wsIndex);
0074             const QByteArray value = line.mid(wsIndex + 1);
0075             mAnnotations[key] = value;
0076         } else {
0077             mAnnotations.insert(line, QByteArray());
0078         }
0079     }
0080 }
0081 
0082 bool CollectionAnnotationsAttribute::operator==(const CollectionAnnotationsAttribute &other) const
0083 {
0084     return mAnnotations == other.annotations();
0085 }