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 "movieinfojob.h"
0021 #include "jobparams_p.h"
0022 #include "configuration.h"
0023 #include <QNetworkAccessManager>
0024 #include <QNetworkReply>
0025 #include <QDebug>
0026 #include <QJsonDocument>
0027 #include <QJsonObject>
0028 #include <QJsonArray>
0029 
0030 using namespace TmdbQt;
0031 
0032 class TmdbQt::MovieInfoJobPrivate
0033 {
0034 public:
0035     MovieInfoJobPrivate(const JobParams &params)
0036         : m_result(params.configuration), m_params(params) {}
0037     QNetworkReply *m_reply;
0038     QString m_errorMessage;
0039     MovieDb m_result;
0040     const JobParams &m_params;
0041 };
0042 
0043 MovieInfoJob::MovieInfoJob(const JobParams &params, int movieId)
0044     : d(new MovieInfoJobPrivate(params))
0045 {
0046     QUrl url = params.baseUrl;
0047     url.setPath(url.path() + QStringLiteral("/movie/%1").arg(movieId));
0048 
0049     QNetworkRequest request(url);
0050     d->m_reply = params.qnam.get(request);
0051     connect(d->m_reply, &QNetworkReply::finished, this, &MovieInfoJob::requestFinished);
0052 }
0053 
0054 MovieInfoJob::~MovieInfoJob()
0055 {
0056     delete d->m_reply;
0057     delete d;
0058 }
0059 
0060 /**
0061  * @brief MovieInfoJob::hasError
0062  * @return true if the job had an error
0063  */
0064 bool MovieInfoJob::hasError() const
0065 {
0066     return !errorMessage().isEmpty();
0067 }
0068 
0069 /**
0070  * @brief MovieInfoJob::errorMessage
0071  * @return the error message for the job
0072  */
0073 QString MovieInfoJob::errorMessage() const
0074 {
0075     return d->m_errorMessage;
0076 }
0077 
0078 /**
0079  * @brief MovieInfoJob::searchResult
0080  * @return the job result: detailed information about a movie
0081  */
0082 MovieDb MovieInfoJob::searchResult() const
0083 {
0084     return d->m_result;
0085 }
0086 
0087 void MovieInfoJob::requestFinished()
0088 {
0089     const QByteArray data = d->m_reply->readAll();
0090     //qDebug() << data;
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 }