Messagelib

attachmentcompressjob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 Based on KMail code by various authors.
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "attachmentcompressjob.h"
10
11#include <KLocalizedString>
12#include <KZip>
13
14#include <QBuffer>
15#include <QDateTime>
16#include <QSharedPointer>
17#include <QTimer>
18
19using namespace MessageCore;
20static const mode_t archivePerms = S_IFREG | 0644;
21
22class MessageCore::AttachmentCompressJob::AttachmentCompressJobPrivate
23{
24public:
25 AttachmentCompressJobPrivate(AttachmentCompressJob *qq);
26
27 void doStart(); // slot
28
29 AttachmentCompressJob *const q;
30 AttachmentPart::Ptr mOriginalPart;
31 AttachmentPart::Ptr mCompressedPart;
32 bool mCompressedPartLarger = false;
33};
34
35AttachmentCompressJob::AttachmentCompressJobPrivate::AttachmentCompressJobPrivate(AttachmentCompressJob *qq)
36 : q(qq)
37{
38}
39
40void AttachmentCompressJob::AttachmentCompressJobPrivate::doStart()
41{
42 Q_ASSERT(mOriginalPart);
43 const QByteArray decoded = mOriginalPart->data();
44
45 QByteArray array;
46 QBuffer dev(&array);
47 KZip zip(&dev);
48 if (!zip.open(QIODevice::WriteOnly)) {
49 q->setError(KJob::UserDefinedError);
50 q->setErrorText(i18n("Could not initiate attachment compression."));
51 q->emitResult();
52 return;
53 }
54
55 // Compress.
56 zip.setCompression(KZip::DeflateCompression);
58 if (!zip.writeFile(mOriginalPart->name(), decoded, archivePerms, QString(/*user*/), QString(/*group*/), zipTime, zipTime, zipTime)) {
59 q->setError(KJob::UserDefinedError);
60 q->setErrorText(i18n("Could not compress the attachment."));
61 q->emitResult();
62 return;
63 }
64 zip.close();
65 mCompressedPartLarger = (array.size() >= decoded.size());
66
67 // Create new part.
68 Q_ASSERT(mCompressedPart == nullptr);
69 mCompressedPart = AttachmentPart::Ptr(new AttachmentPart);
70 mCompressedPart->setName(mOriginalPart->name() + QLatin1StringView(".zip")); // TODO not sure name should be .zipped too
71 mCompressedPart->setFileName(mOriginalPart->fileName() + QLatin1StringView(".zip"));
72 mCompressedPart->setDescription(mOriginalPart->description());
73 mCompressedPart->setInline(mOriginalPart->isInline());
74 mCompressedPart->setMimeType("application/zip");
75 mCompressedPart->setCompressed(true);
76 mCompressedPart->setEncrypted(mOriginalPart->isEncrypted());
77 mCompressedPart->setSigned(mOriginalPart->isSigned());
78 mCompressedPart->setData(array);
79 q->emitResult(); // Success.
80
81 // TODO consider adding a copy constructor to AttachmentPart.
82}
83
85 : KJob(parent)
86 , d(new AttachmentCompressJobPrivate(this))
87{
88 d->mOriginalPart = part;
89}
90
92
94{
95 QTimer::singleShot(0, this, [this]() {
96 d->doStart();
97 });
98}
99
101{
102 return d->mOriginalPart;
103}
104
106{
107 d->mOriginalPart = part;
108}
109
111{
112 return d->mCompressedPart;
113}
114
116{
117 return d->mCompressedPartLarger;
118}
119
120#include "moc_attachmentcompressjob.cpp"
void setErrorText(const QString &errorText)
void emitResult()
void setError(int errorCode)
DeflateCompression
A job to compress the attachment of an email.
const AttachmentPart::Ptr originalPart() const
Returns the original part of the compressed attachment.
void start() override
Starts the attachment compress job.
AttachmentCompressJob(const AttachmentPart::Ptr &part, QObject *parent=nullptr)
Creates a new attachment compress job.
AttachmentPart::Ptr compressedPart() const
Returns the compressed part of the attachment.
void setOriginalPart(const AttachmentPart::Ptr &part)
Sets the original part of the compressed attachment.
~AttachmentCompressJob() override
Destroys the attachment compress job.
bool isCompressedPartLarger() const
Returns whether the compressed part is larger than the original part.
A class that encapsulates an attachment.
QSharedPointer< AttachmentPart > Ptr
Defines a pointer to an attachment object.
QString i18n(const char *text, const TYPE &arg...)
qsizetype size() const const
QDateTime currentDateTime()
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.