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 "tagattribute.h"
21
22#include "imapparser_p.h"
23
24#include <KIcon>
25
26using namespace Akonadi;
27
28class TagAttribute::Private
29{
30public:
31 Private()
32 : inToolbar(false)
33 , priority(-1)
34 {}
35
36 QString name;
37 QString icon;
38 QColor backgroundColor;
39 QColor textColor;
40 QString font;
41 bool inToolbar;
42 QString shortcut;
43 int priority;
44};
45
46TagAttribute::TagAttribute()
47 : d(new Private)
48{
49}
50
51TagAttribute::~TagAttribute()
52{
53 delete d;
54}
55
56QString TagAttribute::displayName() const
57{
58 return d->name;
59}
60
61void TagAttribute::setDisplayName(const QString &name)
62{
63 d->name = name;
64}
65
66QString TagAttribute::iconName() const
67{
68 return d->icon;
69}
70
71void TagAttribute::setIconName(const QString &icon)
72{
73 d->icon = icon;
74}
75
76QByteArray Akonadi::TagAttribute::type() const
77{
78 return "TAG";
79}
80
81TagAttribute *TagAttribute::clone() const
82{
83 TagAttribute *attr = new TagAttribute();
84 attr->d->name = d->name;
85 attr->d->icon = d->icon;
86 attr->d->backgroundColor = d->backgroundColor;
87 attr->d->textColor = d->textColor;
88 attr->d->font = d->font;
89 attr->d->inToolbar = d->inToolbar;
90 attr->d->shortcut = d->shortcut;
91 attr->d->priority = d->priority;
92 return attr;
93}
94
95QByteArray TagAttribute::serialized() const
96{
97 QList<QByteArray> l;
98 l << ImapParser::quote(d->name.toUtf8());
99 l << ImapParser::quote(d->icon.toUtf8());
100 l << ImapParser::quote(d->font.toUtf8());
101 l << ImapParser::quote(d->shortcut.toUtf8());
102 l << ImapParser::quote(QString::number(d->inToolbar).toUtf8());
103 {
104 QList<QByteArray> components;
105 if (d->backgroundColor.isValid()) {
106 components = QList<QByteArray>() << QByteArray::number(d->backgroundColor.red())
107 << QByteArray::number(d->backgroundColor.green())
108 << QByteArray::number(d->backgroundColor.blue())
109 << QByteArray::number(d->backgroundColor.alpha());
110 }
111 l << '(' + ImapParser::join(components, " ") + ')';
112 }
113 {
114 QList<QByteArray> components;
115 if (d->textColor.isValid()) {
116 components = QList<QByteArray>() << QByteArray::number(d->textColor.red())
117 << QByteArray::number(d->textColor.green())
118 << QByteArray::number(d->textColor.blue())
119 << QByteArray::number(d->textColor.alpha());
120 }
121 l << '(' + ImapParser::join(components, " ") + ')';
122 }
123 l << ImapParser::quote(QString::number(d->priority).toUtf8());
124 return '(' + ImapParser::join(l, " ") + ')';
125}
126
127static QColor parseColor(const QByteArray &data)
128{
129 QList<QByteArray> componentData;
130 ImapParser::parseParenthesizedList(data, componentData);
131 if (componentData.size() != 4) {
132 return QColor();
133 }
134 QList<int> components;
135 bool ok;
136 for (int i = 0; i <= 3; ++i) {
137 components << componentData.at(i).toInt(&ok);
138 if (!ok) {
139 return QColor();
140 }
141 }
142 return QColor(components.at(0), components.at(1), components.at(2), components.at(3));
143}
144
145void TagAttribute::deserialize(const QByteArray &data)
146{
147 QList<QByteArray> l;
148 ImapParser::parseParenthesizedList(data, l);
149 int size = l.size();
150 Q_ASSERT(size >= 7);
151 d->name = QString::fromUtf8(l[0]);
152 d->icon = QString::fromUtf8(l[1]);
153 d->font = QString::fromUtf8(l[2]);
154 d->shortcut = QString::fromUtf8(l[3]);
155 d->inToolbar = QString::fromUtf8(l[4]).toInt();
156 if (!l[5].isEmpty()) {
157 d->backgroundColor = parseColor(l[5]);
158 }
159 if (!l[6].isEmpty()) {
160 d->textColor = parseColor(l[6]);
161 }
162 if (l.size() >= 8) {
163 d->priority = QString::fromUtf8(l[7]).toInt();
164 }
165}
166
167QColor TagAttribute::backgroundColor() const
168{
169 return d->backgroundColor;
170}
171
172void TagAttribute::setBackgroundColor(const QColor &color)
173{
174 d->backgroundColor = color;
175}
176
177void TagAttribute::setTextColor(const QColor &color)
178{
179 d->textColor = color;
180}
181
182QColor TagAttribute::textColor() const
183{
184 return d->textColor;
185}
186
187void TagAttribute::setFont(const QString &font)
188{
189 d->font = font;
190}
191
192QString TagAttribute::font() const
193{
194 return d->font;
195}
196
197void TagAttribute::setInToolbar(bool inToolbar)
198{
199 d->inToolbar = inToolbar;
200}
201
202bool TagAttribute::inToolbar() const
203{
204 return d->inToolbar;
205}
206
207void TagAttribute::setShortcut(const QString &shortcut)
208{
209 d->shortcut = shortcut;
210}
211
212QString TagAttribute::shortcut() const
213{
214 return d->shortcut;
215}
216
217void TagAttribute::setPriority(int priority)
218{
219 d->priority = priority;
220}
221
222int TagAttribute::priority() const
223{
224 return d->priority;
225}
226