File indexing completed on 2024-04-28 04:43:00
0001 /* 0002 * Copyright (C) 2014 David Faure <faure@kde.org> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library is distributed in the hope that it will be useful, 0010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "creditsjob.h" 0021 #include "jobparams_p.h" 0022 #include "configuration.h" 0023 #include "personlist.h" 0024 #include <QNetworkAccessManager> 0025 #include <QNetworkReply> 0026 #include <QDebug> 0027 #include <QJsonDocument> 0028 #include <QJsonObject> 0029 #include <QJsonArray> 0030 0031 using namespace TmdbQt; 0032 0033 class TmdbQt::CreditsJobPrivate 0034 { 0035 public: 0036 CreditsJobPrivate(const JobParams ¶ms) 0037 : m_params(params) {} 0038 QNetworkReply *m_reply; 0039 QString m_errorMessage; 0040 PersonList m_cast; 0041 PersonList m_crew; 0042 const JobParams &m_params; 0043 }; 0044 0045 CreditsJob::CreditsJob(const JobParams ¶ms, int movieId) 0046 : d(new CreditsJobPrivate(params)) 0047 { 0048 QUrl url = params.baseUrl; 0049 url.setPath(url.path() + QStringLiteral("/movie/%1/credits").arg(movieId)); 0050 init(url); 0051 } 0052 0053 CreditsJob::CreditsJob(const JobParams ¶ms, int tvShowId, int seasonNum, int episodeNum) 0054 : d(new CreditsJobPrivate(params)) 0055 { 0056 QUrl url = params.baseUrl; 0057 url.setPath(url.path() + QStringLiteral("/tv/%1/season/%2/episode/%3/credits") 0058 .arg(tvShowId).arg(seasonNum).arg(episodeNum)); 0059 init(url); 0060 } 0061 0062 void CreditsJob::init(const QUrl &url) 0063 { 0064 QNetworkRequest request(url); 0065 d->m_reply = d->m_params.qnam.get(request); 0066 connect(d->m_reply, &QNetworkReply::finished, this, &CreditsJob::requestFinished); 0067 } 0068 0069 CreditsJob::~CreditsJob() 0070 { 0071 delete d->m_reply; 0072 delete d; 0073 } 0074 0075 /** 0076 * @brief CreditsJob::hasError 0077 * @return true if the job had an error 0078 */ 0079 bool CreditsJob::hasError() const 0080 { 0081 return !errorMessage().isEmpty(); 0082 } 0083 0084 /** 0085 * @brief CreditsJob::errorMessage 0086 * @return the error message for the job 0087 */ 0088 QString CreditsJob::errorMessage() const 0089 { 0090 return d->m_errorMessage; 0091 } 0092 0093 /** 0094 * @brief CreditsJob::cast 0095 * @return first job result: the cast, i.e. the list of actors/actresses 0096 */ 0097 PersonList CreditsJob::cast() const 0098 { 0099 return d->m_cast; 0100 } 0101 0102 /** 0103 * @brief CreditsJob::crew 0104 * @return second job result: the crew, i.e. everyone else who worked on the movie/show 0105 */ 0106 PersonList CreditsJob::crew() const 0107 { 0108 return d->m_crew; 0109 } 0110 0111 void CreditsJob::requestFinished() 0112 { 0113 const QByteArray data = d->m_reply->readAll(); 0114 //qDebug() << data; 0115 QJsonDocument doc = QJsonDocument::fromJson(data); 0116 if (doc.isNull()) { 0117 d->m_errorMessage = QStringLiteral("Invalid json received\n") + QString::fromUtf8(data); 0118 } 0119 QJsonObject root = doc.object(); 0120 0121 QJsonArray castArray = root.value(QStringLiteral("cast")).toArray(); 0122 d->m_cast.load(castArray, d->m_params.configuration, Person::Cast); 0123 0124 QJsonArray crewArray = root.value(QStringLiteral("crew")).toArray(); 0125 d->m_crew.load(crewArray, d->m_params.configuration, Person::Crew); 0126 0127 d->m_reply->deleteLater(); 0128 d->m_reply = nullptr; 0129 0130 emit result(this); 0131 deleteLater(); 0132 }