Messagelib

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

KDE's Doxygen guidelines are available online.