Messagelib

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

KDE's Doxygen guidelines are available online.