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/TransferJob>
13 #include <KLocalizedString>
14 
15 #include <QFileInfo>
16 #include <QMimeDatabase>
17 #include <QUrlQuery>
18 
19 using namespace MessageCore;
20 
21 class MessageCore::AttachmentFromUrlJob::AttachmentLoadJobPrivate
22 {
23 public:
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 
33 AttachmentFromUrlJob::AttachmentLoadJobPrivate::AttachmentLoadJobPrivate(AttachmentFromUrlJob *qq)
34  : q(qq)
35 {
36 }
37 
38 void AttachmentFromUrlJob::AttachmentLoadJobPrivate::transferJobData(KIO::Job *job, const QByteArray &jobData)
39 {
40  Q_UNUSED(job)
41  mData += jobData;
42 }
43 
44 void 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()) {
65  QMimeDatabase db;
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 
90 AttachmentFromUrlJob::AttachmentFromUrlJob(const QUrl &url, QObject *parent)
91  : AttachmentFromUrlBaseJob(url, parent)
92  , d(new AttachmentLoadJobPrivate(this))
93 {
94 }
95 
97 
98 void 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"
std::optional< QSqlQuery > query(const QString &queryStatement)
void result(KJob *job)
A class that encapsulates an attachment.
QByteArray toLatin1() const const
KCALUTILS_EXPORT QString mimeType()
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-2023 The KDE developers.
Generated on Wed Dec 6 2023 03:56:39 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.