File indexing completed on 2024-04-14 03:50:39

0001 /*
0002   This file is part of the kcalcore library.
0003 
0004   SPDX-FileCopyrightText: 2002 Michael Brade <brade@kde.org>
0005 
0006   SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 /**
0009   @file
0010   This file is part of the API for handling calendar data and
0011   defines the Attachment class.
0012 
0013   @brief
0014   Represents information related to an attachment for a Calendar Incidence.
0015 
0016   @author Michael Brade \<brade@kde.org\>
0017 */
0018 
0019 #include "attachment.h"
0020 #include <QDataStream>
0021 
0022 using namespace KCalendarCore;
0023 
0024 /**
0025   Private class that helps to provide binary compatibility between releases.
0026   @internal
0027 */
0028 //@cond PRIVATE
0029 class Q_DECL_HIDDEN KCalendarCore::Attachment::Private : public QSharedData
0030 {
0031 public:
0032     Private() = default;
0033     Private(const QString &mime, bool binary)
0034         : mMimeType(mime)
0035         , mBinary(binary)
0036     {
0037     }
0038     Private(const Private &other) = default;
0039 
0040     ~Private()
0041     {
0042     }
0043 
0044     mutable uint mSize = 0;
0045     mutable QByteArray mDecodedDataCache;
0046     QString mMimeType;
0047     QString mUri;
0048     QByteArray mEncodedData;
0049     QString mLabel;
0050     bool mBinary = false;
0051     bool mLocal = false;
0052     bool mShowInline = false;
0053 };
0054 //@endcond
0055 
0056 Attachment::Attachment()
0057     : d(new Attachment::Private)
0058 {
0059 }
0060 
0061 Attachment::Attachment(const Attachment &attachment) = default;
0062 
0063 Attachment::Attachment(const QString &uri, const QString &mime)
0064     : d(new Attachment::Private(mime, false))
0065 {
0066     d->mUri = uri;
0067 }
0068 
0069 Attachment::Attachment(const QByteArray &base64, const QString &mime)
0070     : d(new Attachment::Private(mime, true))
0071 {
0072     d->mEncodedData = base64;
0073 }
0074 
0075 Attachment::~Attachment() = default;
0076 
0077 bool Attachment::isEmpty() const
0078 {
0079     return d->mMimeType.isEmpty() && d->mUri.isEmpty() && d->mEncodedData.isEmpty();
0080 }
0081 
0082 bool Attachment::isUri() const
0083 {
0084     return !d->mBinary;
0085 }
0086 
0087 QString Attachment::uri() const
0088 {
0089     if (!d->mBinary) {
0090         return d->mUri;
0091     } else {
0092         return QString();
0093     }
0094 }
0095 
0096 void Attachment::setUri(const QString &uri)
0097 {
0098     d->mUri = uri;
0099     d->mBinary = false;
0100 }
0101 
0102 bool Attachment::isBinary() const
0103 {
0104     return d->mBinary;
0105 }
0106 
0107 QByteArray Attachment::data() const
0108 {
0109     if (d->mBinary) {
0110         return d->mEncodedData;
0111     } else {
0112         return QByteArray();
0113     }
0114 }
0115 
0116 QByteArray Attachment::decodedData() const
0117 {
0118     if (d->mDecodedDataCache.isNull()) {
0119         d->mDecodedDataCache = QByteArray::fromBase64(d->mEncodedData);
0120     }
0121 
0122     return d->mDecodedDataCache;
0123 }
0124 
0125 void Attachment::setDecodedData(const QByteArray &data)
0126 {
0127     setData(data.toBase64());
0128     d->mDecodedDataCache = data;
0129     d->mSize = d->mDecodedDataCache.size();
0130 }
0131 
0132 void Attachment::setData(const QByteArray &base64)
0133 {
0134     d->mEncodedData = base64;
0135     d->mBinary = true;
0136     d->mDecodedDataCache = QByteArray();
0137     d->mSize = 0;
0138 }
0139 
0140 uint Attachment::size() const
0141 {
0142     if (isUri()) {
0143         return 0;
0144     }
0145     if (!d->mSize) {
0146         d->mSize = decodedData().size();
0147     }
0148 
0149     return d->mSize;
0150 }
0151 
0152 QString Attachment::mimeType() const
0153 {
0154     return d->mMimeType;
0155 }
0156 
0157 void Attachment::setMimeType(const QString &mime)
0158 {
0159     d->mMimeType = mime;
0160 }
0161 
0162 bool Attachment::showInline() const
0163 {
0164     return d->mShowInline;
0165 }
0166 
0167 void Attachment::setShowInline(bool showinline)
0168 {
0169     d->mShowInline = showinline;
0170 }
0171 
0172 QString Attachment::label() const
0173 {
0174     return d->mLabel;
0175 }
0176 
0177 void Attachment::setLabel(const QString &label)
0178 {
0179     d->mLabel = label;
0180 }
0181 
0182 bool Attachment::isLocal() const
0183 {
0184     return d->mLocal;
0185 }
0186 
0187 void Attachment::setLocal(bool local)
0188 {
0189     d->mLocal = local;
0190 }
0191 
0192 Attachment &Attachment::operator=(const Attachment &other) = default;
0193 
0194 bool Attachment::operator==(const Attachment &a2) const
0195 {
0196     return uri() == a2.uri() //
0197         && d->mLabel == a2.label() //
0198         && d->mLocal == a2.isLocal() //
0199         && d->mBinary == a2.isBinary() //
0200         && d->mShowInline == a2.showInline() //
0201         && size() == a2.size() //
0202         && decodedData() == a2.decodedData();
0203 }
0204 
0205 bool Attachment::operator!=(const Attachment &a2) const
0206 {
0207     return !(*this == a2);
0208 }
0209 
0210 QDataStream &KCalendarCore::operator<<(QDataStream &out, const KCalendarCore::Attachment &a)
0211 {
0212     out << a.d->mSize << a.d->mMimeType << a.d->mUri << a.d->mEncodedData << a.d->mLabel << a.d->mBinary << a.d->mLocal << a.d->mShowInline;
0213     return out;
0214 }
0215 
0216 QDataStream &KCalendarCore::operator>>(QDataStream &in, KCalendarCore::Attachment &a)
0217 {
0218     in >> a.d->mSize >> a.d->mMimeType >> a.d->mUri >> a.d->mEncodedData >> a.d->mLabel >> a.d->mBinary >> a.d->mLocal >> a.d->mShowInline;
0219     return in;
0220 }
0221 
0222 #include "moc_attachment.cpp"