File indexing completed on 2024-05-12 05:11:12

0001 /*
0002   SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.net>
0003   SPDX-FileContributor: Tobias Koenig <tokoe@kdab.com>
0004 
0005   SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "sentactionattribute.h"
0009 
0010 #include <QDataStream>
0011 #include <QIODevice>
0012 #include <QSharedData>
0013 
0014 using namespace Akonadi;
0015 
0016 class Akonadi::SentActionAttributeActionPrivate : public QSharedData
0017 {
0018 public:
0019     SentActionAttributeActionPrivate() = default;
0020 
0021     SentActionAttributeActionPrivate(const SentActionAttributeActionPrivate &other)
0022 
0023         = default;
0024 
0025     SentActionAttribute::Action::Type mType = SentActionAttribute::Action::Invalid;
0026     QVariant mValue;
0027 };
0028 
0029 SentActionAttribute::Action::Action()
0030     : d(new SentActionAttributeActionPrivate)
0031 {
0032 }
0033 
0034 SentActionAttribute::Action::Action(Type type, const QVariant &value)
0035     : d(new SentActionAttributeActionPrivate)
0036 {
0037     d->mType = type;
0038     d->mValue = value;
0039 }
0040 
0041 SentActionAttribute::Action::Action(const Action &other)
0042 
0043     = default;
0044 
0045 SentActionAttribute::Action::~Action() = default;
0046 
0047 SentActionAttribute::Action::Type SentActionAttribute::Action::type() const
0048 {
0049     return d->mType;
0050 }
0051 
0052 QVariant SentActionAttribute::Action::value() const
0053 {
0054     return d->mValue;
0055 }
0056 
0057 SentActionAttribute::Action &SentActionAttribute::Action::operator=(const Action &other)
0058 {
0059     if (this != &other) {
0060         d = other.d;
0061     }
0062 
0063     return *this;
0064 }
0065 
0066 bool SentActionAttribute::Action::operator==(const Action &other) const
0067 {
0068     return (d->mType == other.d->mType) && (d->mValue == other.d->mValue);
0069 }
0070 
0071 class Akonadi::SentActionAttributePrivate
0072 {
0073 public:
0074     SentActionAttribute::Action::List mActions;
0075 };
0076 
0077 SentActionAttribute::SentActionAttribute()
0078     : d(new SentActionAttributePrivate)
0079 {
0080 }
0081 
0082 SentActionAttribute::~SentActionAttribute() = default;
0083 
0084 void SentActionAttribute::addAction(Action::Type type, const QVariant &value)
0085 {
0086     d->mActions.append(Action(type, value));
0087 }
0088 
0089 SentActionAttribute::Action::List SentActionAttribute::actions() const
0090 {
0091     return d->mActions;
0092 }
0093 
0094 SentActionAttribute *SentActionAttribute::clone() const
0095 {
0096     auto attribute = new SentActionAttribute;
0097     attribute->d->mActions = d->mActions;
0098 
0099     return attribute;
0100 }
0101 
0102 QByteArray SentActionAttribute::type() const
0103 {
0104     static const QByteArray sType("SentActionAttribute");
0105     return sType;
0106 }
0107 
0108 QByteArray SentActionAttribute::serialized() const
0109 {
0110     QVariantList list;
0111     list.reserve(d->mActions.count());
0112     for (const Action &action : std::as_const(d->mActions)) {
0113         QVariantMap map;
0114         map.insert(QString::number(action.type()), action.value());
0115 
0116         list << QVariant(map);
0117     }
0118 
0119     QByteArray data;
0120     QDataStream stream(&data, QIODevice::WriteOnly);
0121     stream.setVersion(QDataStream::Qt_4_6);
0122     stream << list;
0123 
0124     return data;
0125 }
0126 
0127 void SentActionAttribute::deserialize(const QByteArray &data)
0128 {
0129     d->mActions.clear();
0130 
0131     QDataStream stream(data);
0132     stream.setVersion(QDataStream::Qt_4_6);
0133 
0134     QVariantList list;
0135     stream >> list;
0136 
0137     for (const QVariant &variant : std::as_const(list)) {
0138         const QVariantMap map = variant.toMap();
0139         QMap<QString, QVariant>::const_iterator it = map.cbegin();
0140         const QMap<QString, QVariant>::const_iterator itEnd = map.cend();
0141         for (; it != itEnd; ++it) {
0142             d->mActions << Action(static_cast<Action::Type>(it.key().toInt()), it.value());
0143         }
0144     }
0145 }