1/*
2 Copyright (c) 2008 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#include "entitydisplayattribute.h"
21
22#include "imapparser_p.h"
23
24#include <KIcon>
25
26using namespace Akonadi;
27
28class EntityDisplayAttribute::Private
29{
30public:
31 Private()
32 : hidden(false)
33 {
34 }
35 QString name;
36 QString icon;
37 QString activeIcon;
38 QColor backgroundColor;
39 bool hidden;
40};
41
42EntityDisplayAttribute::EntityDisplayAttribute()
43 : d(new Private)
44{
45}
46
47EntityDisplayAttribute::~ EntityDisplayAttribute()
48{
49 delete d;
50}
51
52QString EntityDisplayAttribute::displayName() const
53{
54 return d->name;
55}
56
57void EntityDisplayAttribute::setDisplayName(const QString &name)
58{
59 d->name = name;
60}
61
62KIcon EntityDisplayAttribute::icon() const
63{
64 return KIcon(d->icon);
65}
66
67QString EntityDisplayAttribute::iconName() const
68{
69 return d->icon;
70}
71
72void EntityDisplayAttribute::setIconName(const QString &icon)
73{
74 d->icon = icon;
75}
76
77QByteArray Akonadi::EntityDisplayAttribute::type() const
78{
79 return "ENTITYDISPLAY";
80}
81
82EntityDisplayAttribute *EntityDisplayAttribute::clone() const
83{
84 EntityDisplayAttribute *attr = new EntityDisplayAttribute();
85 attr->d->name = d->name;
86 attr->d->icon = d->icon;
87 attr->d->activeIcon = d->activeIcon;
88 attr->d->backgroundColor = d->backgroundColor;
89 return attr;
90}
91
92QByteArray EntityDisplayAttribute::serialized() const
93{
94 QList<QByteArray> l;
95 l << ImapParser::quote(d->name.toUtf8());
96 l << ImapParser::quote(d->icon.toUtf8());
97 l << ImapParser::quote(d->activeIcon.toUtf8());
98 QList<QByteArray> components;
99 if (d->backgroundColor.isValid()) {
100 components = QList<QByteArray>() << QByteArray::number(d->backgroundColor.red())
101 << QByteArray::number(d->backgroundColor.green())
102 << QByteArray::number(d->backgroundColor.blue())
103 << QByteArray::number(d->backgroundColor.alpha());
104 }
105 l << '(' + ImapParser::join(components, " ") + ')';
106 return '(' + ImapParser::join(l, " ") + ')';
107}
108
109void EntityDisplayAttribute::deserialize(const QByteArray &data)
110{
111 QList<QByteArray> l;
112 ImapParser::parseParenthesizedList(data, l);
113 int size = l.size();
114 Q_ASSERT(size >= 2);
115 d->name = QString::fromUtf8(l[0]);
116 d->icon = QString::fromUtf8(l[1]);
117 if (size >= 3) {
118 d->activeIcon = QString::fromUtf8(l[2]);
119 }
120 if (size >= 4) {
121 if (!l[3].isEmpty()) {
122 QList<QByteArray> componentData;
123 ImapParser::parseParenthesizedList(l[3], componentData);
124 if (componentData.size() != 4) {
125 return;
126 }
127 QList<int> components;
128
129 bool ok;
130 for (int i = 0; i <= 3; ++i) {
131 components << componentData.at(i).toInt(&ok);
132 if (!ok) {
133 return;
134 }
135 }
136 d->backgroundColor = QColor(components.at(0), components.at(1), components.at(2), components.at(3));
137 }
138 }
139}
140
141void EntityDisplayAttribute::setActiveIconName(const QString &name)
142{
143 d->activeIcon = name;
144}
145
146KIcon EntityDisplayAttribute::activeIcon() const
147{
148 return KIcon(d->activeIcon);
149}
150
151QString EntityDisplayAttribute::activeIconName() const
152{
153 return d->activeIcon;
154}
155
156QColor EntityDisplayAttribute::backgroundColor() const
157{
158 return d->backgroundColor;
159}
160
161void EntityDisplayAttribute::setBackgroundColor(const QColor &color)
162{
163 d->backgroundColor = color;
164}
165