File indexing completed on 2024-11-10 04:40:27

0001 /*
0002     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "entitydisplayattribute.h"
0008 
0009 #include "private/imapparser_p.h"
0010 
0011 using namespace Akonadi;
0012 
0013 class Akonadi::EntityDisplayAttributePrivate
0014 {
0015 public:
0016     QString name;
0017     QString icon;
0018     QString activeIcon;
0019     QColor backgroundColor;
0020 };
0021 
0022 EntityDisplayAttribute::EntityDisplayAttribute()
0023     : d(new EntityDisplayAttributePrivate())
0024 {
0025 }
0026 
0027 EntityDisplayAttribute::~EntityDisplayAttribute() = default;
0028 
0029 QString EntityDisplayAttribute::displayName() const
0030 {
0031     return d->name;
0032 }
0033 
0034 void EntityDisplayAttribute::setDisplayName(const QString &name)
0035 {
0036     d->name = name;
0037 }
0038 
0039 QIcon EntityDisplayAttribute::icon() const
0040 {
0041     return QIcon::fromTheme(d->icon);
0042 }
0043 
0044 QString EntityDisplayAttribute::iconName() const
0045 {
0046     return d->icon;
0047 }
0048 
0049 void EntityDisplayAttribute::setIconName(const QString &icon)
0050 {
0051     d->icon = icon;
0052 }
0053 
0054 QByteArray EntityDisplayAttribute::type() const
0055 {
0056     static const QByteArray sType("ENTITYDISPLAY");
0057     return sType;
0058 }
0059 
0060 EntityDisplayAttribute *EntityDisplayAttribute::clone() const
0061 {
0062     auto attr = new EntityDisplayAttribute();
0063     attr->d->name = d->name;
0064     attr->d->icon = d->icon;
0065     attr->d->activeIcon = d->activeIcon;
0066     attr->d->backgroundColor = d->backgroundColor;
0067     return attr;
0068 }
0069 
0070 QByteArray EntityDisplayAttribute::serialized() const
0071 {
0072     QList<QByteArray> l;
0073     l.reserve(4);
0074     l << ImapParser::quote(d->name.toUtf8());
0075     l << ImapParser::quote(d->icon.toUtf8());
0076     l << ImapParser::quote(d->activeIcon.toUtf8());
0077     QList<QByteArray> components;
0078     if (d->backgroundColor.isValid()) {
0079         components = QList<QByteArray>() << QByteArray::number(d->backgroundColor.red()) << QByteArray::number(d->backgroundColor.green())
0080                                          << QByteArray::number(d->backgroundColor.blue()) << QByteArray::number(d->backgroundColor.alpha());
0081     }
0082     l << '(' + ImapParser::join(components, " ") + ')';
0083     return '(' + ImapParser::join(l, " ") + ')';
0084 }
0085 
0086 void EntityDisplayAttribute::deserialize(const QByteArray &data)
0087 {
0088     QList<QByteArray> l;
0089     ImapParser::parseParenthesizedList(data, l);
0090     int size = l.size();
0091     Q_ASSERT(size >= 2);
0092     d->name = QString::fromUtf8(l[0]);
0093     d->icon = QString::fromUtf8(l[1]);
0094     if (size >= 3) {
0095         d->activeIcon = QString::fromUtf8(l[2]);
0096     }
0097     if (size >= 4) {
0098         if (!l[3].isEmpty()) {
0099             QList<QByteArray> componentData;
0100             ImapParser::parseParenthesizedList(l[3], componentData);
0101             if (componentData.size() != 4) {
0102                 return;
0103             }
0104             QList<int> components;
0105             components.reserve(4);
0106 
0107             bool ok;
0108             for (int i = 0; i <= 3; ++i) {
0109                 components << componentData.at(i).toInt(&ok);
0110                 if (!ok) {
0111                     return;
0112                 }
0113             }
0114             d->backgroundColor = QColor(components.at(0), components.at(1), components.at(2), components.at(3));
0115         }
0116     }
0117 }
0118 
0119 void EntityDisplayAttribute::setActiveIconName(const QString &name)
0120 {
0121     d->activeIcon = name;
0122 }
0123 
0124 QIcon EntityDisplayAttribute::activeIcon() const
0125 {
0126     return QIcon::fromTheme(d->activeIcon);
0127 }
0128 
0129 QString EntityDisplayAttribute::activeIconName() const
0130 {
0131     return d->activeIcon;
0132 }
0133 
0134 QColor EntityDisplayAttribute::backgroundColor() const
0135 {
0136     return d->backgroundColor;
0137 }
0138 
0139 void EntityDisplayAttribute::setBackgroundColor(const QColor &color)
0140 {
0141     d->backgroundColor = color;
0142 }