Messagelib

attachmentupdatejob.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "attachmentupdatejob.h"
8 #include "MessageCore/AttachmentFromUrlBaseJob"
9 #include "attachmentfromfolderjob.h"
10 #include "attachmentfromurljob.h"
11 #include "attachmentfromurlutils.h"
12 #include "messagecore_debug.h"
13 #include <KLocalizedString>
14 #include <QTimer>
15 
16 using namespace MessageCore;
17 
18 class Q_DECL_HIDDEN MessageCore::AttachmentUpdateJob::AttachmentUpdateJobPrivate
19 {
20 public:
21  AttachmentUpdateJobPrivate(AttachmentUpdateJob *qq);
22 
23  void doStart(); // slot
24  void loadJobResult(KJob *);
25 
26  AttachmentUpdateJob *const q;
27  AttachmentPart::Ptr mOriginalPart;
28  AttachmentPart::Ptr mUpdatedPart;
29 };
30 
31 AttachmentUpdateJob::AttachmentUpdateJobPrivate::AttachmentUpdateJobPrivate(AttachmentUpdateJob *qq)
32  : q(qq)
33 {
34 }
35 
36 void AttachmentUpdateJob::AttachmentUpdateJobPrivate::doStart()
37 {
38  Q_ASSERT(mOriginalPart);
39  if (mOriginalPart->url().isEmpty()) {
40  qCDebug(MESSAGECORE_LOG) << " url is empty. We can't update file";
41  q->setError(KJob::UserDefinedError);
42  q->setErrorText(i18n("URL is empty."));
43  q->emitResult();
44  return;
45  }
46  MessageCore::AttachmentFromUrlBaseJob *job = MessageCore::AttachmentFromUrlUtils::createAttachmentJob(mOriginalPart->url(), q);
47  connect(job, &AttachmentFromUrlBaseJob::result, q, [this](KJob *job) {
48  loadJobResult(job);
49  });
50  job->start();
51 }
52 
53 void AttachmentUpdateJob::AttachmentUpdateJobPrivate::loadJobResult(KJob *job)
54 {
55  if (job->error()) {
56  q->setError(KJob::UserDefinedError);
57  q->setErrorText(job->errorString());
58  q->emitResult();
59  return;
60  }
61 
62  Q_ASSERT(dynamic_cast<AttachmentLoadJob *>(job));
63  auto ajob = static_cast<AttachmentLoadJob *>(job);
64  mUpdatedPart = ajob->attachmentPart();
65  mUpdatedPart->setName(q->originalPart()->name());
66  mUpdatedPart->setFileName(q->originalPart()->fileName());
67  mUpdatedPart->setDescription(q->originalPart()->description());
68  mUpdatedPart->setSigned(q->originalPart()->isSigned());
69  mUpdatedPart->setEncrypted(q->originalPart()->isEncrypted());
70  mUpdatedPart->setEncoding(q->originalPart()->encoding());
71  mUpdatedPart->setMimeType(q->originalPart()->mimeType());
72  mUpdatedPart->setInline(q->originalPart()->isInline());
73  q->emitResult(); // Success.
74 }
75 
76 AttachmentUpdateJob::AttachmentUpdateJob(const AttachmentPart::Ptr &part, QObject *parent)
77  : KJob(parent)
78  , d(new AttachmentUpdateJobPrivate(this))
79 {
80  d->mOriginalPart = part;
81 }
82 
83 AttachmentUpdateJob::~AttachmentUpdateJob() = default;
84 
85 void AttachmentUpdateJob::start()
86 {
87  QTimer::singleShot(0, this, [this]() {
88  d->doStart();
89  });
90 }
91 
92 AttachmentPart::Ptr AttachmentUpdateJob::originalPart() const
93 {
94  return d->mOriginalPart;
95 }
96 
97 AttachmentPart::Ptr AttachmentUpdateJob::updatedPart() const
98 {
99  return d->mUpdatedPart;
100 }
101 
102 #include "moc_attachmentupdatejob.cpp"
A base class for jobs to load attachments from different sources.
virtual QString errorString() const
QString i18n(const char *text, const TYPE &arg...)
virtual Q_SCRIPTABLE void start()=0
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
AttachmentPart::Ptr attachmentPart() const
Returns the loaded attachment.
int error() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.