Messagelib

attachmentfromurljob.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 Parts based on KMail code by various authors.
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "attachmentfromurljob.h"
10
11#include "messagecore_debug.h"
12#include <KIO/TransferJob>
13#include <KLocalizedString>
14
15#include <QFileInfo>
16#include <QMimeDatabase>
17#include <QUrlQuery>
18
19using namespace MessageCore;
20
21class MessageCore::AttachmentFromUrlJob::AttachmentLoadJobPrivate
22{
23public:
24 explicit AttachmentLoadJobPrivate(AttachmentFromUrlJob *qq);
25
26 void transferJobData(KIO::Job *job, const QByteArray &jobData);
27 void transferJobResult(KJob *job);
28
29 AttachmentFromUrlJob *const q;
30 QByteArray mData;
31};
32
33AttachmentFromUrlJob::AttachmentLoadJobPrivate::AttachmentLoadJobPrivate(AttachmentFromUrlJob *qq)
34 : q(qq)
35{
36}
37
38void AttachmentFromUrlJob::AttachmentLoadJobPrivate::transferJobData(KIO::Job *job, const QByteArray &jobData)
39{
40 Q_UNUSED(job)
41 mData += jobData;
42}
43
44void AttachmentFromUrlJob::AttachmentLoadJobPrivate::transferJobResult(KJob *job)
45{
46 if (job->error()) {
47 // TODO this loses useful stuff from KIO, like detailed error descriptions, causes+solutions,
48 // ... use UiDelegate somehow?
49 q->setError(job->error());
50 q->setErrorText(job->errorString());
51 q->emitResult();
52 return;
53 }
54
55 Q_ASSERT(dynamic_cast<KIO::TransferJob *>(job));
56 auto transferJob = static_cast<KIO::TransferJob *>(job);
57
58 // Determine the MIME type and filename of the attachment.
59 const QString mimeTypeName = transferJob->mimetype();
60 qCDebug(MESSAGECORE_LOG) << "Mimetype is" << mimeTypeName;
61
62 QString fileName = q->url().fileName();
63 fileName.replace(QLatin1Char('\n'), QLatin1Char('_'));
64 if (fileName.isEmpty()) {
66 const auto mimeType = db.mimeTypeForName(mimeTypeName);
67 if (mimeType.isValid()) {
68 fileName = i18nc("a file called 'unknown.ext'", "unknown%1", mimeType.preferredSuffix());
69 } else {
70 fileName = i18nc("a file called 'unknown'", "unknown");
71 }
72 }
73
74 // Create the AttachmentPart.
75 Q_ASSERT(q->attachmentPart() == nullptr); // Not created before.
76
78 QUrlQuery query(q->url());
79 const QString value = query.queryItemValue(QStringLiteral("charset"));
80 part->setCharset(value.toLatin1());
81 part->setMimeType(mimeTypeName.toLatin1());
82 part->setName(fileName);
83 part->setFileName(fileName);
84 part->setData(mData);
85 part->setUrl(q->url());
86 q->setAttachmentPart(part);
87 q->emitResult(); // Success.
88}
89
91 : AttachmentFromUrlBaseJob(url, parent)
92 , d(new AttachmentLoadJobPrivate(this))
93{
94}
95
97
98void AttachmentFromUrlJob::doStart()
99{
100 if (!url().isValid()) {
101 setError(KJob::UserDefinedError);
102 setErrorText(i18n("\"%1\" not found. Please specify the full path.", url().toDisplayString()));
103 emitResult();
104 return;
105 }
106
107 if (maximumAllowedSize() != -1 && url().isLocalFile()) {
108 const qint64 size = QFileInfo(url().toLocalFile()).size();
109 if (size > maximumAllowedSize()) {
110 setError(KJob::UserDefinedError);
111 setErrorText(i18n("You may not attach files bigger than %1. Share it with storage service.", KIO::convertSize(maximumAllowedSize())));
112 emitResult();
113 return;
114 }
115 }
116
117 Q_ASSERT(d->mData.isEmpty()); // Not started twice.
118
119 KIO::TransferJob *job = KIO::get(url(), KIO::NoReload, (uiDelegate() ? KIO::DefaultFlags : KIO::HideProgressInfo));
120 connect(job, &KIO::TransferJob::result, this, [this](KJob *job) {
121 d->transferJobResult(job);
122 });
123 connect(job, &KIO::TransferJob::data, this, [this](KIO::Job *job, const QByteArray &ba) {
124 d->transferJobData(job, ba);
125 });
126}
127
128#include "moc_attachmentfromurljob.cpp"
void data(KIO::Job *job, const QByteArray &data)
virtual QString errorString() const
int error() const
void result(KJob *job)
A job to load an attachment from an url.
~AttachmentFromUrlJob() override
Destroys the job.
AttachmentFromUrlJob(const QUrl &url=QUrl(), QObject *parent=nullptr)
Creates a new job.
A class that encapsulates an attachment.
QSharedPointer< AttachmentPart > Ptr
Defines a pointer to an attachment object.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
std::optional< QSqlQuery > query(const QString &queryStatement)
KCALUTILS_EXPORT QString mimeType()
KIOCORE_EXPORT QString convertSize(KIO::filesize_t size)
KIOCORE_EXPORT TransferJob * get(const QUrl &url, LoadType reload=NoReload, JobFlags flags=DefaultFlags)
DefaultFlags
HideProgressInfo
qint64 size() const const
QMimeType mimeTypeForName(const QString &nameOrAlias) const const
bool isEmpty() const const
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QByteArray toLatin1() const const
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.