Purpose

saveasplugin.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include <purpose/job.h>
8#include <purpose/pluginbase.h>
9
10#include <KIO/CopyJob>
11#include <KLocalizedString>
12#include <KPluginFactory>
13#include <QFile>
14#include <QFileInfo>
15#include <QJsonArray>
16#include <QStandardPaths>
17
18static QUrl addPathToUrl(const QUrl &url, const QString &relPath)
19{
20 QString path = url.path();
21 if (!path.endsWith(QLatin1Char('/'))) {
22 path += QLatin1Char('/');
23 }
24 path += relPath;
25 QUrl u(url);
26 u.setPath(path);
27 return u;
28}
29
30class SaveAsShareJob : public Purpose::Job
31{
33public:
34 SaveAsShareJob(QObject *parent)
35 : Purpose::Job(parent)
36 {
38 }
39
40 void start() override
41 {
42 const QJsonArray inputUrls = data().value(QLatin1String("urls")).toArray();
43
44 if (inputUrls.isEmpty()) {
45 setErrorText(i18n("No URLs to save"));
46 setError(1);
47 emitResult();
48 return;
49 }
50
51 QList<QUrl> urls;
52 bool containsData = false;
53 for (const QJsonValue &val : inputUrls) {
54 urls.append(QUrl(val.toString()));
55 containsData |= urls.last().scheme() == QLatin1String("data");
56 }
57
58 m_dest = QUrl(data().value(QLatin1String("destinationPath")).toString());
59 if (containsData && !(urls.count() == 1 && m_dest.isLocalFile() && !QFileInfo(m_dest.toLocalFile()).isDir())) {
60 for (const QUrl &url : urls) {
61 QUrl dest = addPathToUrl(m_dest, QStringLiteral("data"));
62 auto job = KIO::copy(url, dest);
63 connect(job, &KJob::finished, this, &SaveAsShareJob::fileCopied);
64 m_jobs.insert(job);
65 }
66 } else {
67 auto job = KIO::copy(urls, m_dest);
68 connect(job, &KJob::finished, this, &SaveAsShareJob::fileCopied);
69 m_jobs.insert(job);
70 }
71 }
72
73 bool doKill() override
74 {
75 bool killed = true;
76 for (KJob *job : m_jobs) {
77 killed &= job->kill();
78 }
79 return killed;
80 }
81
82 void fileCopied(KJob *job)
83 {
84 auto r = m_jobs.remove(job);
85 Q_ASSERT(r);
86
87 setError(error() + job->error());
89 if (job->error() == 0 && m_jobs.isEmpty()) {
90 setOutput({{QStringLiteral("url"), m_dest.toString()}});
91 }
92 emitResult();
93 }
94
95private:
96 QUrl m_dest;
97 QSet<KJob *> m_jobs;
98};
99
100class SaveAsPlugin : public Purpose::PluginBase
101{
103public:
104 using PluginBase::PluginBase;
105 Purpose::Job *createJob() const override
106 {
107 return new SaveAsShareJob(nullptr);
108 }
109};
110
111K_PLUGIN_CLASS_WITH_JSON(SaveAsPlugin, "saveasplugin.json")
112
113#include "saveasplugin.moc"
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void finished(KJob *job)
void setError(int errorCode)
void setCapabilities(Capabilities capabilities)
QString errorText() const
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Job that will actually perform the sharing.
Definition job.h:34
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
Base class to implement by plugins.
Definition pluginbase.h:29
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QString path(const QString &relativePath)
bool isDir() const const
bool isEmpty() const const
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
void append(QList< T > &&value)
qsizetype count() const const
T & last()
Q_OBJECTQ_OBJECT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
iterator insert(const T &value)
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isLocalFile() const const
QString path(ComponentFormattingOptions options) const const
QString toLocalFile() const const
QString toString(FormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.