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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "dispatchmodeattribute.h"
0008 
0009 #include "akonadi_mime_debug.h"
0010 
0011 #include <Akonadi/AttributeFactory>
0012 
0013 using namespace Akonadi;
0014 
0015 class Akonadi::DispatchModeAttributePrivate
0016 {
0017 public:
0018     DispatchModeAttribute::DispatchMode mMode;
0019     QDateTime mDueDate;
0020 };
0021 
0022 DispatchModeAttribute::DispatchModeAttribute(DispatchMode mode)
0023     : d(new DispatchModeAttributePrivate)
0024 {
0025     d->mMode = mode;
0026 }
0027 
0028 DispatchModeAttribute::~DispatchModeAttribute() = default;
0029 
0030 DispatchModeAttribute *DispatchModeAttribute::clone() const
0031 {
0032     auto const cloned = new DispatchModeAttribute(d->mMode);
0033     cloned->setSendAfter(d->mDueDate);
0034     return cloned;
0035 }
0036 
0037 QByteArray DispatchModeAttribute::type() const
0038 {
0039     static const QByteArray sType("DispatchModeAttribute");
0040     return sType;
0041 }
0042 
0043 QByteArray DispatchModeAttribute::serialized() const
0044 {
0045     switch (d->mMode) {
0046     case Automatic:
0047         if (!d->mDueDate.isValid()) {
0048             return "immediately";
0049         } else {
0050             return "after" + d->mDueDate.toString(Qt::ISODate).toLatin1();
0051         }
0052     case Manual:
0053         return "never";
0054     }
0055 
0056     Q_ASSERT(false);
0057     return {}; // suppress control-reaches-end-of-non-void-function warning
0058 }
0059 
0060 void DispatchModeAttribute::deserialize(const QByteArray &data)
0061 {
0062     d->mDueDate = QDateTime();
0063     if (data == "immediately") {
0064         d->mMode = Automatic;
0065     } else if (data == "never") {
0066         d->mMode = Manual;
0067     } else if (data.startsWith(QByteArrayLiteral("after"))) {
0068         d->mMode = Automatic;
0069         d->mDueDate = QDateTime::fromString(QString::fromLatin1(data.mid(5)), Qt::ISODate);
0070         // NOTE: 5 is the strlen of "after".
0071     } else {
0072         qCWarning(AKONADIMIME_LOG) << "Failed to deserialize data [" << data << "]";
0073     }
0074 }
0075 
0076 DispatchModeAttribute::DispatchMode DispatchModeAttribute::dispatchMode() const
0077 {
0078     return d->mMode;
0079 }
0080 
0081 void DispatchModeAttribute::setDispatchMode(DispatchMode mode)
0082 {
0083     d->mMode = mode;
0084 }
0085 
0086 QDateTime DispatchModeAttribute::sendAfter() const
0087 {
0088     return d->mDueDate;
0089 }
0090 
0091 void DispatchModeAttribute::setSendAfter(const QDateTime &date)
0092 {
0093     d->mDueDate = date;
0094 }