Purpose

youtubejobcomposite.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 "youtubejobcomposite.h"
8#include "youtubejob.h"
9#include <Accounts/Application>
10#include <Accounts/Manager>
11#include <KAccounts/Core>
12#include <KAccounts/GetCredentialsJob>
13#include <KLocalizedString>
14#include <QDebug>
15#include <QJsonArray>
16#include <QJsonValue>
17#include <QStandardPaths>
18
19QDebug operator<<(QDebug s, const Accounts::Service &service)
20{
21 s.nospace() << qPrintable(service.displayName()) << ',' << qPrintable(service.name()) << '\n';
22 return s;
23}
24QDebug operator<<(QDebug s, const Accounts::Provider &provider)
25{
26 s.nospace() << "Provider(" << qPrintable(provider.displayName()) << ',' << qPrintable(provider.name()) << ")\n";
27 return s;
28}
29
30YoutubeJobComposite::YoutubeJobComposite()
31 : Purpose::Job()
32{
33}
34
35void YoutubeJobComposite::start()
36{
37 const QJsonValue jsonId = data().value(QLatin1String("accountId"));
38 if (jsonId.isNull() || jsonId.isUndefined()) {
39 setError(1);
40 setErrorText(i18n("No YouTube account configured in your accounts."));
41 emitResult();
42 return;
43 }
44 const Accounts::AccountId id = jsonId.toInt();
45
46 // TODO: make async
47 QByteArray accessToken;
48 {
49 auto job = new KAccounts::GetCredentialsJob(id, this);
50 bool b = job->exec();
51 if (!b) {
52 qWarning() << "Couldn't fetch credentials";
53 setError(job->error());
54 setErrorText(job->errorText());
55 emitResult();
56 return;
57 }
58 accessToken = job->credentialsData().value(QStringLiteral("AccessToken")).toByteArray();
59 }
60
61 m_pendingJobs = 0;
62 const QJsonArray urls = data().value(QLatin1String("urls")).toArray();
63 for (const QJsonValue &url : urls) {
64 YoutubeJob *job = new YoutubeJob(QUrl(url.toString()),
65 accessToken,
66 data().value(QLatin1String("videoTitle")).toString(),
67 data().value(QLatin1String("videoTags")).toString().split(QLatin1Char(',')),
68 data().value(QLatin1String("videoDesc")).toString(),
69 this);
70 connect(job, &KJob::finished, this, &YoutubeJobComposite::subjobFinished);
71 job->start();
72 m_pendingJobs++;
73 }
74}
75
76void YoutubeJobComposite::subjobFinished(KJob *subjob)
77{
78 m_pendingJobs--;
79 if (subjob->error()) {
80 setError(subjob->error());
81 setErrorText(subjob->errorText());
82 emitResult();
83 return;
84 }
85 if (m_pendingJobs == 0) {
86 if (!error()) {
87 const QJsonValue url = qobject_cast<YoutubeJob *>(subjob)->outputUrl();
88 setOutput({{QStringLiteral("url"), url.toString()}});
89 }
90 emitResult();
91 }
92}
void setErrorText(const QString &errorText)
void emitResult()
int error() const
void finished(KJob *job)
void setError(int errorCode)
QString errorText() const
QJsonObject data
Represents the data the job will have available to perform its task.
Definition job.h:39
QString i18n(const char *text, const TYPE &arg...)
char * toString(const EngineQuery &query)
QDebug operator<<(QDebug dbg, const DcrawInfoContainer &c)
QDebug & nospace()
QJsonValue value(QLatin1StringView key) const const
bool isNull() const const
bool isUndefined() const const
QJsonArray toArray() 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)
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.