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 <KLocalizedString>
12 #include <KZip>
13 
14 #include <QBuffer>
15 #include <QDateTime>
16 #include <QSharedPointer>
17 #include <QTimer>
18 
19 using namespace MessageCore;
20 static const mode_t archivePerms = S_IFREG | 0644;
21 
22 class MessageCore::AttachmentCompressJob::AttachmentCompressJobPrivate
23 {
24 public:
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 
35 AttachmentCompressJob::AttachmentCompressJobPrivate::AttachmentCompressJobPrivate(AttachmentCompressJob *qq)
36  : q(qq)
37 {
38 }
39 
40 void 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() + QLatin1String(".zip")); // TODO not sure name should be .zipped too
71  mCompressedPart->setFileName(mOriginalPart->fileName() + QLatin1String(".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 
84 AttachmentCompressJob::AttachmentCompressJob(const AttachmentPart::Ptr &part, QObject *parent)
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"
const AttachmentPart::Ptr originalPart() const
Returns the original part of the compressed attachment.
void setOriginalPart(const AttachmentPart::Ptr &part)
Sets the original part of the compressed attachment.
~AttachmentCompressJob() override
Destroys the attachment compress job.
A job to compress the attachment of an email.
QDateTime currentDateTime()
A class that encapsulates an attachment.
DeflateCompression
QString i18n(const char *text, const TYPE &arg...)
bool isCompressedPartLarger() const
Returns whether the compressed part is larger than the original part.
AttachmentPart::Ptr compressedPart() const
Returns the compressed part of the attachment.
void start() override
Starts the attachment compress job.
int size() const const
char * data()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Mar 22 2023 04:07:13 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.