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

0001 /*
0002  * <one line to give the library's name and an idea of what it does.>
0003  * Copyright (C) 2014  Vishesh Handa <me@vhanda.in>
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 "tvseasoninfojob.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::TvSeasonInfoJobPrivate
0034 {
0035 public:
0036     TvSeasonInfoJobPrivate(const JobParams &params)
0037         : m_result(params.configuration), m_params(params) {}
0038     QNetworkReply *m_reply;
0039     QString m_errorMessage;
0040     TvSeasonDb m_result;
0041     const JobParams &m_params;
0042 };
0043 
0044 TvSeasonInfoJob::TvSeasonInfoJob(const JobParams &params, int tvid, int seasonNum)
0045     : d(new TvSeasonInfoJobPrivate(params))
0046 {
0047     QUrl url = params.baseUrl;
0048     url.setPath(url.path() + QStringLiteral("/tv/%1/season/%2").arg(tvid).arg(seasonNum));
0049 
0050     QNetworkRequest request(url);
0051     d->m_reply = params.qnam.get(request);
0052     connect(d->m_reply, &QNetworkReply::finished, this, &TvSeasonInfoJob::requestFinished);
0053 }
0054 
0055 TvSeasonInfoJob::~TvSeasonInfoJob()
0056 {
0057     delete d->m_reply;
0058     delete d;
0059 }
0060 
0061 /**
0062  * @brief TvSeasonInfoJob::hasError
0063  * @return true if the job had an error
0064  */
0065 bool TvSeasonInfoJob::hasError() const
0066 {
0067     return !errorMessage().isEmpty();
0068 }
0069 
0070 /**
0071  * @brief TvSeasonInfoJob::errorMessage
0072  * @return the error message for the job
0073  */
0074 QString TvSeasonInfoJob::errorMessage() const
0075 {
0076     return d->m_errorMessage;
0077 }
0078 
0079 /**
0080  * @brief TvSeasonInfoJob::searchResult
0081  * @return the job result: detailed information about a season
0082  */
0083 TvSeasonDb TvSeasonInfoJob::searchResult() const
0084 {
0085     return d->m_result;
0086 }
0087 
0088 void TvSeasonInfoJob::requestFinished()
0089 {
0090     const QByteArray data = d->m_reply->readAll();
0091     QJsonDocument doc = QJsonDocument::fromJson(data);
0092     if (doc.isNull()) {
0093         d->m_errorMessage = QStringLiteral("Invalid json received");
0094     }
0095     QJsonObject root = doc.object();
0096     d->m_result.load(root);
0097 
0098     d->m_reply->deleteLater();
0099     d->m_reply = nullptr;
0100 
0101     emit result(this);
0102     deleteLater();
0103 }