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

KDE's Doxygen guidelines are available online.