File indexing completed on 2024-04-28 04:43:01

0001 /*
0002  * Copyright (C) 2014 Vishesh Handa <me@vhanda.in>
0003  * Copyright (C) 2014 David Faure <faure@kde.org>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Lesser General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2.1 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Lesser General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Lesser General Public
0016  * License along with this library; if not, write to the Free Software
0017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
0018  *
0019  */
0020 
0021 #include "tvshowinfojob.h"
0022 #include "jobparams_p.h"
0023 #include "configuration.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::TvShowInfoJobPrivate
0034 {
0035 public:
0036     TvShowInfoJobPrivate(int id, const JobParams &params)
0037         : m_tvShowId(id), m_result(params.configuration), m_params(params) {}
0038     int m_tvShowId;
0039     QNetworkReply *m_reply;
0040     QString m_errorMessage;
0041     TvShowDb m_result;
0042     const JobParams &m_params;
0043 };
0044 
0045 TvShowInfoJob::TvShowInfoJob(const JobParams &params, int tvid)
0046     : d(new TvShowInfoJobPrivate(tvid, params))
0047 {
0048     QUrl url = params.baseUrl;
0049     url.setPath(url.path() + QStringLiteral("/tv/%1").arg(tvid));
0050 
0051     QNetworkRequest request(url);
0052     d->m_reply = params.qnam.get(request);
0053     connect(d->m_reply, &QNetworkReply::finished, this, &TvShowInfoJob::requestFinished);
0054 }
0055 
0056 TvShowInfoJob::~TvShowInfoJob()
0057 {
0058     delete d->m_reply;
0059     delete d;
0060 }
0061 
0062 /**
0063  * @brief TvShowInfoJob::tvShowId
0064  * @return the ID of the TV show, as passed to TheMovieDbApi::getTvShowInfo
0065  */
0066 int TvShowInfoJob::tvShowId() const
0067 {
0068     return d->m_tvShowId;
0069 }
0070 
0071 /**
0072  * @brief TvShowInfoJob::hasError
0073  * @return true if the job had an error
0074  */
0075 bool TvShowInfoJob::hasError() const
0076 {
0077     return !errorMessage().isEmpty();
0078 }
0079 
0080 /**
0081  * @brief TvShowInfoJob::errorMessage
0082  * @return the error message for the job
0083  */
0084 QString TvShowInfoJob::errorMessage() const
0085 {
0086     return d->m_errorMessage;
0087 }
0088 
0089 /**
0090  * @brief TvShowInfoJob::searchResult
0091  * @return the job result: information about a TV show, including a list of seasons, but
0092  * without details for the reasons.
0093  */
0094 TvShowDb TvShowInfoJob::searchResult() const
0095 {
0096     return d->m_result;
0097 }
0098 
0099 void TvShowInfoJob::requestFinished()
0100 {
0101     const QByteArray data = d->m_reply->readAll();
0102     QJsonDocument doc = QJsonDocument::fromJson(data);
0103     if (doc.isNull()) {
0104         d->m_errorMessage = QStringLiteral("Invalid json received");
0105     }
0106     QJsonObject root = doc.object();
0107     d->m_result.load(root);
0108 
0109     d->m_reply->deleteLater();
0110     d->m_reply = nullptr;
0111 
0112     emit result(this);
0113     deleteLater();
0114 }