Akonadi

entityannotationsattribute.cpp
1 /*
2  * SPDX-FileCopyrightText: 2008 Omat Holding B.V. <[email protected]>
3  * SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]>
4  *
5  * SPDX-License-Identifier: LGPL-2.0-or-later
6  */
7 
8 #include "entityannotationsattribute.h"
9 
10 #include <QByteArray>
11 #include <QString>
12 
13 using namespace Akonadi;
14 
15 EntityAnnotationsAttribute::EntityAnnotationsAttribute(const QMap<QByteArray, QByteArray> &annotations)
16  : mAnnotations(annotations)
17 {
18 }
19 
20 void EntityAnnotationsAttribute::setAnnotations(const QMap<QByteArray, QByteArray> &annotations)
21 {
22  mAnnotations = annotations;
23 }
24 
25 QMap<QByteArray, QByteArray> EntityAnnotationsAttribute::annotations() const
26 {
27  return mAnnotations;
28 }
29 
30 void EntityAnnotationsAttribute::insert(const QByteArray &key, const QString &value)
31 {
32  mAnnotations.insert(key, value.toUtf8());
33 }
34 
35 QString EntityAnnotationsAttribute::value(const QByteArray &key) const
36 {
37  return QString::fromUtf8(mAnnotations.value(key).data());
38 }
39 
40 bool 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 
52 {
53  return new EntityAnnotationsAttribute(mAnnotations);
54 }
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 }
QString fromUtf8(const char *str, int size)
int indexOf(char ch, int from) const const
QList< QByteArray > split(char sep) const const
QByteArray trimmed() const const
Provides interface for custom attributes for Entity.
Definition: attribute.h:124
QMap::iterator insert(const Key &key, const T &value)
int size() const const
void chop(int n)
Attribute * clone() const override
Creates a copy of this attribute.
QByteArray mid(int pos, int len) const const
QByteArray serialized() const override
Returns a QByteArray representation of the attribute which will be storaged.
QByteArray toUtf8() const const
bool startsWith(const QByteArray &ba) const const
bool isEmpty() const const
void deserialize(const QByteArray &data) override
Sets the data of this attribute, using the same encoding as returned by toByteArray().
bool endsWith(const QByteArray &ba) const const
QByteArray type() const override
Returns the type of the attribute.
Helper integration between Akonadi and Qt.
QByteArray & insert(int i, char ch)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 03:52:16 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.