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

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