Akonadi

entityannotationsattribute.cpp
1/*
2 * SPDX-FileCopyrightText: 2008 Omat Holding B.V. <info@omat.nl>
3 * SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
4 *
5 * SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7
8#include "entityannotationsattribute.h"
9
10#include <QByteArray>
11#include <QString>
12
13using namespace Akonadi;
14
15EntityAnnotationsAttribute::EntityAnnotationsAttribute(const QMap<QByteArray, QByteArray> &annotations)
16 : mAnnotations(annotations)
17{
18}
19
20void EntityAnnotationsAttribute::setAnnotations(const QMap<QByteArray, QByteArray> &annotations)
21{
22 mAnnotations = annotations;
23}
24
25QMap<QByteArray, QByteArray> EntityAnnotationsAttribute::annotations() const
26{
27 return mAnnotations;
28}
29
30void EntityAnnotationsAttribute::insert(const QByteArray &key, const QString &value)
31{
32 mAnnotations.insert(key, value.toUtf8());
33}
34
35QString EntityAnnotationsAttribute::value(const QByteArray &key) const
36{
37 return QString::fromUtf8(mAnnotations.value(key).data());
38}
39
40bool EntityAnnotationsAttribute::contains(const QByteArray &key) const
41{
42 return mAnnotations.contains(key);
43}
44
46{
47 static const QByteArray sType("entityannotations");
48 return sType;
49}
50
55
57{
58 QByteArray result = "";
59
60 for (auto it = mAnnotations.cbegin(), e = mAnnotations.cend(); it != e; ++it) {
61 result += it.key();
62 result += ' ';
63 result += it.value();
64 result += " % "; // We use this separator as '%' is not allowed in keys or values
65 }
66 result.chop(3);
67
68 return result;
69}
70
72{
73 mAnnotations.clear();
74 const QList<QByteArray> lines = data.split('%');
75
76 for (int i = 0; i < lines.size(); ++i) {
77 QByteArray line = lines[i];
78 if (i != 0 && line.startsWith(' ')) {
79 line = line.mid(1);
80 }
81 if (i != lines.size() - 1 && line.endsWith(' ')) {
82 line.chop(1);
83 }
84 if (line.trimmed().isEmpty()) {
85 continue;
86 }
87 const int wsIndex = line.indexOf(' ');
88 if (wsIndex > 0) {
89 const QByteArray key = line.mid(0, wsIndex);
90 const QByteArray value = line.mid(wsIndex + 1);
91 mAnnotations[key] = value;
92 } else {
93 mAnnotations.insert(line, QByteArray());
94 }
95 }
96}
Provides interface for custom attributes for Entity.
Definition attribute.h:132
QByteArray serialized() const override
Returns a QByteArray representation of the attribute which will be storaged.
Attribute * clone() const override
Creates a copy of this attribute.
void deserialize(const QByteArray &data) override
Sets the data of this attribute, using the same encoding as returned by toByteArray().
QByteArray type() const override
Returns the type of the attribute.
Helper integration between Akonadi and Qt.
void chop(qsizetype n)
bool endsWith(QByteArrayView bv) const const
qsizetype indexOf(QByteArrayView bv, qsizetype from) const const
bool isEmpty() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
QList< QByteArray > split(char sep) const const
bool startsWith(QByteArrayView bv) const const
QByteArray trimmed() const const
qsizetype size() const const
const_iterator cbegin() const const
const_iterator cend() const const
void clear()
bool contains(const Key &key) const const
iterator insert(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
QString fromUtf8(QByteArrayView str)
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.