1/*
2 * Copyright (C) 2008 Omat Holding B.V. <info@omat.nl>
3 * Copyright (C) 2014 Christian Mollekopf <mollekopf@kolabsys.com>
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
26using namespace Akonadi;
27
28EntityAnnotationsAttribute::EntityAnnotationsAttribute()
29{
30}
31
32EntityAnnotationsAttribute::EntityAnnotationsAttribute(const QMap<QByteArray, QByteArray> &annotations)
33 : mAnnotations(annotations)
34{
35}
36
37void EntityAnnotationsAttribute::setAnnotations(const QMap<QByteArray, QByteArray> &annotations)
38{
39 mAnnotations = annotations;
40}
41
42QMap<QByteArray, QByteArray> EntityAnnotationsAttribute::annotations() const
43{
44 return mAnnotations;
45}
46
47void EntityAnnotationsAttribute::insert(const QByteArray &key, const QString &value)
48{
49 mAnnotations.insert(key, value.toUtf8());
50}
51
52QString EntityAnnotationsAttribute::value(const QByteArray &key)
53{
54 return QString::fromUtf8(mAnnotations.value(key).data());
55}
56
57bool EntityAnnotationsAttribute::contains(const QByteArray &key) const
58{
59 return mAnnotations.contains(key);
60}
61
62QByteArray EntityAnnotationsAttribute::type() const
63{
64 return "entityannotations";
65}
66
67Akonadi::Attribute *EntityAnnotationsAttribute::clone() const
68{
69 return new EntityAnnotationsAttribute(mAnnotations);
70}
71
72QByteArray EntityAnnotationsAttribute::serialized() const
73{
74 QByteArray result = "";
75
76 Q_FOREACH (const QByteArray &key, mAnnotations.keys()) {
77 result += key;
78 result += ' ';
79 result += mAnnotations[key];
80 result += " % "; // We use this separator as '%' is not allowed in keys or values
81 }
82 result.chop(3);
83
84 return result;
85}
86
87void EntityAnnotationsAttribute::deserialize(const QByteArray &data)
88{
89 mAnnotations.clear();
90 const QList<QByteArray> lines = data.split('%');
91
92 for (int i = 0; i < lines.size(); ++i) {
93 QByteArray line = lines[i];
94 if (i != 0 && line.startsWith(' ')) {
95 line = line.mid(1);
96 }
97 if (i != lines.size() - 1 && line.endsWith(' ')) {
98 line.chop(1);
99 }
100 if (line.trimmed().isEmpty()) {
101 continue;
102 }
103 int wsIndex = line.indexOf(' ');
104 if (wsIndex > 0) {
105 const QByteArray key = line.mid(0, wsIndex);
106 const QByteArray value = line.mid(wsIndex + 1);
107 mAnnotations[key] = value;
108 } else {
109 mAnnotations.insert(line, QByteArray());
110 }
111 }
112}
113