File indexing completed on 2024-04-28 05:19:25

0001 /*
0002     ktnefmessage.cpp
0003 
0004     SPDX-FileCopyrightText: 2002 Michael Goffioul <kdeprint@swing.be>
0005 
0006     This file is part of KTNEF, the KDE TNEF support library/program.
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009  */
0010 /**
0011  * @file
0012  * This file is part of the API for handling TNEF data and
0013  * defines the KTNEFMessage class.
0014  *
0015  * @author Michael Goffioul
0016  */
0017 
0018 #include "ktnefmessage.h"
0019 #include "ktnefattach.h"
0020 #include "lzfu.h"
0021 
0022 #include <QBuffer>
0023 #include <QDebug>
0024 
0025 using namespace KTnef;
0026 
0027 /**
0028  * Private class that helps to provide binary compatibility between releases.
0029  * @internal
0030  */
0031 //@cond PRIVATE
0032 class KTnef::KTNEFMessage::MessagePrivate
0033 {
0034 public:
0035     MessagePrivate() = default;
0036     ~MessagePrivate();
0037 
0038     void clearAttachments();
0039 
0040     QList<KTNEFAttach *> attachments_;
0041 };
0042 
0043 KTNEFMessage::MessagePrivate::~MessagePrivate()
0044 {
0045     clearAttachments();
0046 }
0047 
0048 void KTNEFMessage::MessagePrivate::clearAttachments()
0049 {
0050     while (!attachments_.isEmpty()) {
0051         delete attachments_.takeFirst();
0052     }
0053 }
0054 //@endcond
0055 
0056 KTNEFMessage::KTNEFMessage()
0057     : d(new KTnef::KTNEFMessage::MessagePrivate)
0058 {
0059 }
0060 
0061 KTNEFMessage::~KTNEFMessage() = default;
0062 
0063 const QList<KTNEFAttach *> &KTNEFMessage::attachmentList() const
0064 {
0065     return d->attachments_;
0066 }
0067 
0068 KTNEFAttach *KTNEFMessage::attachment(const QString &filename) const
0069 {
0070     QList<KTNEFAttach *>::const_iterator it = d->attachments_.constBegin();
0071     const QList<KTNEFAttach *>::const_iterator end = d->attachments_.constEnd();
0072     for (; it != end; ++it) {
0073         if ((*it)->name() == filename) {
0074             return *it;
0075         }
0076     }
0077     return nullptr;
0078 }
0079 
0080 void KTNEFMessage::addAttachment(KTNEFAttach *attach)
0081 {
0082     d->attachments_.append(attach);
0083 }
0084 
0085 void KTNEFMessage::clearAttachments()
0086 {
0087     d->clearAttachments();
0088 }
0089 
0090 QString KTNEFMessage::rtfString() const
0091 {
0092     QVariant prop = property(0x1009);
0093     if (prop.isNull() || prop.metaType().id() != QMetaType::QByteArray) {
0094         return {};
0095     } else {
0096         QByteArray rtf;
0097         QByteArray propArray(prop.toByteArray());
0098         QBuffer input(&propArray);
0099         QBuffer output(&rtf);
0100         if (input.open(QIODevice::ReadOnly) && output.open(QIODevice::WriteOnly)) {
0101             if (KTnef::lzfu_decompress(&input, &output) == 0) {
0102                 qWarning() << "Error when decompress data";
0103             }
0104         }
0105         return QString::fromLatin1(rtf);
0106     }
0107 }