File indexing completed on 2024-05-19 04:03:03
0001 /* 0002 SPDX-FileCopyrightText: 2020 Nicolas Fella <nicolas.fella@gmx.de> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "nextcloudjob.h" 0008 #include <KAccounts/Core> 0009 #include <KAccounts/GetCredentialsJob> 0010 #include <KIO/CopyJob> 0011 #include <QDebug> 0012 0013 QList<QUrl> arrayToList(const QJsonArray &array) 0014 { 0015 QList<QUrl> ret; 0016 for (const QJsonValue &val : array) { 0017 ret += val.toVariant().toUrl(); 0018 } 0019 return ret; 0020 } 0021 0022 void NextcloudJob::start() 0023 { 0024 const Accounts::AccountId id = data().value(QLatin1String("accountId")).toInt(); 0025 auto credentialsJob = new KAccounts::GetCredentialsJob(id, this); 0026 0027 connect(credentialsJob, &KAccounts::GetCredentialsJob::finished, this, &NextcloudJob::gotCredentials); 0028 0029 credentialsJob->start(); 0030 } 0031 0032 void NextcloudJob::gotCredentials(KJob *job) 0033 { 0034 if (job->error()) { 0035 setError(job->error()); 0036 setErrorText(job->errorText()); 0037 emitResult(); 0038 return; 0039 } 0040 0041 const Accounts::AccountId id = data().value(QLatin1String("accountId")).toInt(); 0042 Accounts::Account *acc = Accounts::Account::fromId(KAccounts::accountsManager(), id); 0043 0044 const auto services = acc->services(); 0045 for (const Accounts::Service &service : services) { 0046 if (service.name() == QStringLiteral("dav-storage")) { 0047 acc->selectService(service); 0048 } 0049 } 0050 0051 KAccounts::GetCredentialsJob *credentialsJob = qobject_cast<KAccounts::GetCredentialsJob *>(job); 0052 Q_ASSERT(credentialsJob); 0053 const QString folder = data().value(QLatin1String("folder")).toString(); 0054 0055 QUrl destUrl; 0056 destUrl.setHost(acc->valueAsString(QStringLiteral("dav/host"))); 0057 destUrl.setScheme(QStringLiteral("webdav")); 0058 destUrl.setPath(acc->valueAsString(QStringLiteral("dav/storagePath")) + folder); 0059 destUrl.setUserName(credentialsJob->credentialsData().value(QStringLiteral("UserName")).toString()); 0060 destUrl.setPassword(credentialsJob->credentialsData().value(QStringLiteral("Secret")).toString()); 0061 0062 const QList<QUrl> sourceUrls = arrayToList(data().value(QLatin1String("urls")).toArray()); 0063 0064 KIO::CopyJob *copyJob = KIO::copy(sourceUrls, destUrl); 0065 0066 connect(copyJob, &KIO::CopyJob::finished, this, [this, copyJob] { 0067 if (copyJob->error()) { 0068 setError(copyJob->error()); 0069 setErrorText(copyJob->errorText()); 0070 } 0071 emitResult(); 0072 }); 0073 0074 copyJob->start(); 0075 } 0076 0077 #include "moc_nextcloudjob.cpp"