Messagelib

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

KDE's Doxygen guidelines are available online.