Purpose

nextcloudjob.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "nextcloudjob.h"
8#include <KAccounts/Core>
9#include <KAccounts/GetCredentialsJob>
10#include <KIO/CopyJob>
11#include <QDebug>
12
13QList<QUrl> arrayToList(const QJsonArray &array)
14{
15 QList<QUrl> ret;
16 for (const QJsonValue &val : array) {
17 ret += val.toVariant().toUrl();
18 }
19 return ret;
20}
21
22void NextcloudJob::start()
23{
24 const Accounts::AccountId id = data().value(QLatin1String("accountId")).toInt();
25 auto credentialsJob = new KAccounts::GetCredentialsJob(id, this);
26
27 connect(credentialsJob, &KAccounts::GetCredentialsJob::finished, this, &NextcloudJob::gotCredentials);
28
29 credentialsJob->start();
30}
31
32void NextcloudJob::gotCredentials(KJob *job)
33{
34 if (job->error()) {
35 setError(job->error());
36 setErrorText(job->errorText());
37 emitResult();
38 return;
39 }
40
41 const Accounts::AccountId id = data().value(QLatin1String("accountId")).toInt();
42 Accounts::Account *acc = Accounts::Account::fromId(KAccounts::accountsManager(), id);
43
44 const auto services = acc->services();
45 for (const Accounts::Service &service : services) {
46 if (service.name() == QStringLiteral("dav-storage")) {
47 acc->selectService(service);
48 }
49 }
50
51 KAccounts::GetCredentialsJob *credentialsJob = qobject_cast<KAccounts::GetCredentialsJob *>(job);
52 Q_ASSERT(credentialsJob);
53 const QString folder = data().value(QLatin1String("folder")).toString();
54
55 QUrl destUrl;
56 destUrl.setHost(acc->valueAsString(QStringLiteral("dav/host")));
57 destUrl.setScheme(QStringLiteral("webdav"));
58 destUrl.setPath(acc->valueAsString(QStringLiteral("dav/storagePath")) + folder);
59 destUrl.setUserName(credentialsJob->credentialsData().value(QStringLiteral("UserName")).toString());
60 destUrl.setPassword(credentialsJob->credentialsData().value(QStringLiteral("Secret")).toString());
61
62 const QList<QUrl> sourceUrls = arrayToList(data().value(QLatin1String("urls")).toArray());
63
64 KIO::CopyJob *copyJob = KIO::copy(sourceUrls, destUrl);
65
66 connect(copyJob, &KIO::CopyJob::finished, this, [this, copyJob] {
67 if (copyJob->error()) {
68 setError(copyJob->error());
69 setErrorText(copyJob->errorText());
70 }
71 emitResult();
72 });
73
74 copyJob->start();
75}
76
77#include "moc_nextcloudjob.cpp"
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void finished(KJob *job)
void setError(int errorCode)
virtual Q_SCRIPTABLE void start()=0
QString errorText() const
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
char * toString(const EngineQuery &query)
KIOCORE_EXPORT CopyJob * copy(const QList< QUrl > &src, const QUrl &dest, JobFlags flags=DefaultFlags)
QJsonValue value(QLatin1StringView key) const const
int toInt(int defaultValue) const const
QString toString() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
void setHost(const QString &host, ParsingMode mode)
void setPassword(const QString &password, ParsingMode mode)
void setPath(const QString &path, ParsingMode mode)
void setScheme(const QString &scheme)
void setUserName(const QString &userName, ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.