File indexing completed on 2024-05-19 04:03:04
0001 /* 0002 SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <aleixpol@blue-systems.com> 0003 0004 SPDX-License-Identifier: LGPL-2.1-or-later 0005 */ 0006 0007 #include "youtubejobcomposite.h" 0008 #include "youtubejob.h" 0009 #include <Accounts/Application> 0010 #include <Accounts/Manager> 0011 #include <KAccounts/Core> 0012 #include <KAccounts/GetCredentialsJob> 0013 #include <KLocalizedString> 0014 #include <QDebug> 0015 #include <QJsonArray> 0016 #include <QJsonValue> 0017 #include <QStandardPaths> 0018 0019 QDebug operator<<(QDebug s, const Accounts::Service &service) 0020 { 0021 s.nospace() << qPrintable(service.displayName()) << ',' << qPrintable(service.name()) << '\n'; 0022 return s; 0023 } 0024 QDebug operator<<(QDebug s, const Accounts::Provider &provider) 0025 { 0026 s.nospace() << "Provider(" << qPrintable(provider.displayName()) << ',' << qPrintable(provider.name()) << ")\n"; 0027 return s; 0028 } 0029 0030 YoutubeJobComposite::YoutubeJobComposite() 0031 : Purpose::Job() 0032 { 0033 } 0034 0035 void YoutubeJobComposite::start() 0036 { 0037 const QJsonValue jsonId = data().value(QLatin1String("accountId")); 0038 if (jsonId.isNull() || jsonId.isUndefined()) { 0039 setError(1); 0040 setErrorText(i18n("No YouTube account configured in your accounts.")); 0041 emitResult(); 0042 return; 0043 } 0044 const Accounts::AccountId id = jsonId.toInt(); 0045 0046 // TODO: make async 0047 QByteArray accessToken; 0048 { 0049 auto job = new KAccounts::GetCredentialsJob(id, this); 0050 bool b = job->exec(); 0051 if (!b) { 0052 qWarning() << "Couldn't fetch credentials"; 0053 setError(job->error()); 0054 setErrorText(job->errorText()); 0055 emitResult(); 0056 return; 0057 } 0058 accessToken = job->credentialsData().value(QStringLiteral("AccessToken")).toByteArray(); 0059 } 0060 0061 m_pendingJobs = 0; 0062 const QJsonArray urls = data().value(QLatin1String("urls")).toArray(); 0063 for (const QJsonValue &url : urls) { 0064 YoutubeJob *job = new YoutubeJob(QUrl(url.toString()), 0065 accessToken, 0066 data().value(QLatin1String("videoTitle")).toString(), 0067 data().value(QLatin1String("videoTags")).toString().split(QLatin1Char(',')), 0068 data().value(QLatin1String("videoDesc")).toString(), 0069 this); 0070 connect(job, &KJob::finished, this, &YoutubeJobComposite::subjobFinished); 0071 job->start(); 0072 m_pendingJobs++; 0073 } 0074 } 0075 0076 void YoutubeJobComposite::subjobFinished(KJob *subjob) 0077 { 0078 m_pendingJobs--; 0079 if (subjob->error()) { 0080 setError(subjob->error()); 0081 setErrorText(subjob->errorText()); 0082 emitResult(); 0083 return; 0084 } 0085 if (m_pendingJobs == 0) { 0086 if (!error()) { 0087 const QJsonValue url = qobject_cast<YoutubeJob *>(subjob)->outputUrl(); 0088 setOutput({{QStringLiteral("url"), url.toString()}}); 0089 } 0090 emitResult(); 0091 } 0092 }