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

0001 /*
0002  SPDX-FileCopyrightText: 2011 Christian Mollekopf <chrigi_1@fastmail.fm>
0003 
0004  SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "entitydeletedattribute.h"
0008 
0009 #include "private/imapparser_p.h"
0010 
0011 #include "akonadicore_debug.h"
0012 
0013 #include <QByteArray>
0014 #include <QString>
0015 
0016 using namespace Akonadi;
0017 
0018 class Akonadi::EntityDeletedAttributePrivate
0019 {
0020 public:
0021     Collection restoreCollection;
0022     QString restoreResource;
0023 };
0024 
0025 EntityDeletedAttribute::EntityDeletedAttribute()
0026     : d(new EntityDeletedAttributePrivate())
0027 {
0028 }
0029 
0030 EntityDeletedAttribute::~EntityDeletedAttribute() = default;
0031 
0032 void EntityDeletedAttribute::setRestoreCollection(const Akonadi::Collection &collection)
0033 {
0034     if (!collection.isValid()) {
0035         qCWarning(AKONADICORE_LOG) << "invalid collection" << collection;
0036     }
0037     Q_ASSERT(collection.isValid());
0038     d->restoreCollection = collection;
0039     if (collection.resource().isEmpty()) {
0040         qCWarning(AKONADICORE_LOG) << "no resource set";
0041     }
0042     d->restoreResource = collection.resource();
0043 }
0044 
0045 Collection EntityDeletedAttribute::restoreCollection() const
0046 {
0047     return d->restoreCollection;
0048 }
0049 
0050 QString EntityDeletedAttribute::restoreResource() const
0051 {
0052     return d->restoreResource;
0053 }
0054 
0055 QByteArray Akonadi::EntityDeletedAttribute::type() const
0056 {
0057     return QByteArrayLiteral("DELETED");
0058 }
0059 
0060 EntityDeletedAttribute *EntityDeletedAttribute::clone() const
0061 {
0062     auto attr = new EntityDeletedAttribute();
0063     attr->d->restoreCollection = d->restoreCollection;
0064     attr->d->restoreResource = d->restoreResource;
0065     return attr;
0066 }
0067 
0068 QByteArray EntityDeletedAttribute::serialized() const
0069 {
0070     QList<QByteArray> l;
0071     l << ImapParser::quote(d->restoreResource.toUtf8());
0072     QList<QByteArray> components;
0073     components << QByteArray::number(d->restoreCollection.id());
0074 
0075     l << '(' + ImapParser::join(components, " ") + ')';
0076     return '(' + ImapParser::join(l, " ") + ')';
0077 }
0078 
0079 void EntityDeletedAttribute::deserialize(const QByteArray &data)
0080 {
0081     QList<QByteArray> l;
0082     ImapParser::parseParenthesizedList(data, l);
0083     if (l.size() != 2) {
0084         qCWarning(AKONADICORE_LOG) << "invalid size";
0085         return;
0086     }
0087     d->restoreResource = QString::fromUtf8(l[0]);
0088 
0089     if (!l[1].isEmpty()) {
0090         QList<QByteArray> componentData;
0091         ImapParser::parseParenthesizedList(l[1], componentData);
0092         if (componentData.size() != 1) {
0093             return;
0094         }
0095         bool ok;
0096         const int components = componentData.at(0).toInt(&ok);
0097         if (!ok) {
0098             return;
0099         }
0100         d->restoreCollection = Collection(components);
0101     }
0102 }