Akonadi

entityannotationsattribute.cpp
1 /*
2  * Copyright (C) 2008 Omat Holding B.V. <[email protected]>
3  * Copyright (C) 2014 Christian Mollekopf <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Library General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20 
21 #include "entityannotationsattribute.h"
22 
23 #include <QByteArray>
24 #include <QString>
25 
26 using namespace Akonadi;
27 
28 EntityAnnotationsAttribute::EntityAnnotationsAttribute()
29 {
30 }
31 
32 EntityAnnotationsAttribute::EntityAnnotationsAttribute(const QMap<QByteArray, QByteArray> &annotations)
33  : mAnnotations(annotations)
34 {
35 }
36 
37 void EntityAnnotationsAttribute::setAnnotations(const QMap<QByteArray, QByteArray> &annotations)
38 {
39  mAnnotations = annotations;
40 }
41 
42 QMap<QByteArray, QByteArray> EntityAnnotationsAttribute::annotations() const
43 {
44  return mAnnotations;
45 }
46 
47 void EntityAnnotationsAttribute::insert(const QByteArray &key, const QString &value)
48 {
49  mAnnotations.insert(key, value.toUtf8());
50 }
51 
52 QString EntityAnnotationsAttribute::value(const QByteArray &key) const
53 {
54  return QString::fromUtf8(mAnnotations.value(key).data());
55 }
56 
57 bool EntityAnnotationsAttribute::contains(const QByteArray &key) const
58 {
59  return mAnnotations.contains(key);
60 }
61 
63 {
64  static const QByteArray sType("entityannotations");
65  return sType;
66 }
67 
69 {
70  return new EntityAnnotationsAttribute(mAnnotations);
71 }
72 
74 {
75  QByteArray result = "";
76 
77  for (auto it = mAnnotations.cbegin(), e = mAnnotations.cend(); it != e; ++it) {
78  result += it.key();
79  result += ' ';
80  result += it.value();
81  result += " % "; // We use this separator as '%' is not allowed in keys or values
82  }
83  result.chop(3);
84 
85  return result;
86 }
87 
89 {
90  mAnnotations.clear();
91  const QList<QByteArray> lines = data.split('%');
92 
93  for (int i = 0; i < lines.size(); ++i) {
94  QByteArray line = lines[i];
95  if (i != 0 && line.startsWith(' ')) {
96  line = line.mid(1);
97  }
98  if (i != lines.size() - 1 && line.endsWith(' ')) {
99  line.chop(1);
100  }
101  if (line.trimmed().isEmpty()) {
102  continue;
103  }
104  const int wsIndex = line.indexOf(' ');
105  if (wsIndex > 0) {
106  const QByteArray key = line.mid(0, wsIndex);
107  const QByteArray value = line.mid(wsIndex + 1);
108  mAnnotations[key] = value;
109  } else {
110  mAnnotations.insert(line, QByteArray());
111  }
112  }
113 }
void deserialize(const QByteArray &data) override
Sets the data of this attribute, using the same encoding as returned by toByteArray().
QList< QByteArray > split(char sep) const const
QByteArray trimmed() const const
Attribute * clone() const override
Creates a copy of this attribute.
void chop(int n)
bool isEmpty() const const
bool startsWith(const QByteArray &ba) const const
QByteArray & insert(int i, char ch)
QByteArray serialized() const override
Returns a QByteArray representation of the attribute which will be storaged.
Provides interface for custom attributes for Entity.
Definition: attribute.h:139
int size() const const
int indexOf(char ch, int from) const const
QString fromUtf8(const char *str, int size)
QByteArray mid(int pos, int len) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QByteArray type() const override
Returns the type of the attribute.
Helper integration between Akonadi and Qt.
QMap::iterator insert(const Key &key, const T &value)
bool endsWith(const QByteArray &ba) const const
QByteArray toUtf8() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:08:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.