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 "searchjob.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 #include <QUrlQuery> 0030 0031 using namespace TmdbQt; 0032 0033 class TmdbQt::SearchJobPrivate 0034 { 0035 public: 0036 SearchJobPrivate(const JobParams ¶ms) 0037 : m_params(params) {} 0038 QNetworkReply *m_reply; 0039 QString m_errorMessage; 0040 MovieDbList m_result; 0041 const JobParams &m_params; 0042 }; 0043 0044 SearchJob::SearchJob(const JobParams ¶ms, const QString &movieName, int searchYear, const QString &language) 0045 : d(new SearchJobPrivate(params)) 0046 { 0047 QUrl url = params.baseUrl; 0048 url.setPath(url.path() + QStringLiteral("/search/movie")); 0049 QUrlQuery query(url); 0050 query.addQueryItem(QStringLiteral("query"), movieName); 0051 if (searchYear > 0) 0052 query.addQueryItem(QStringLiteral("year"), QString::number(searchYear)); 0053 if (!language.isEmpty()) 0054 query.addQueryItem(QStringLiteral("language"), language); 0055 url.setQuery(query); 0056 0057 QNetworkRequest request(url); 0058 d->m_reply = params.qnam.get(request); 0059 connect(d->m_reply, &QNetworkReply::finished, this, &SearchJob::requestFinished); 0060 } 0061 0062 SearchJob::~SearchJob() 0063 { 0064 delete d->m_reply; 0065 delete d; 0066 } 0067 0068 /** 0069 * @brief SearchJob::hasError 0070 * @return true if the job had an error 0071 */ 0072 bool SearchJob::hasError() const 0073 { 0074 return !errorMessage().isEmpty(); 0075 } 0076 0077 /** 0078 * @brief SearchJob::errorMessage 0079 * @return the error message for the job 0080 */ 0081 QString SearchJob::errorMessage() const 0082 { 0083 return d->m_errorMessage; 0084 } 0085 0086 /** 0087 * @brief SearchJob::searchResult 0088 * @return the job result: list of movies matching the search criteria, without details for each movie 0089 */ 0090 MovieDbList SearchJob::searchResult() const 0091 { 0092 return d->m_result; 0093 } 0094 0095 void SearchJob::requestFinished() 0096 { 0097 const QByteArray data = d->m_reply->readAll(); 0098 //qDebug() << data; 0099 QJsonDocument doc = QJsonDocument::fromJson(data); 0100 if (doc.isNull()) { 0101 d->m_errorMessage = QStringLiteral("Invalid json received"); 0102 } 0103 QJsonObject root = doc.object(); 0104 QJsonArray results = root.value(QStringLiteral("results")).toArray(); 0105 d->m_result.load(results, d->m_params.configuration); 0106 0107 d->m_reply->deleteLater(); 0108 d->m_reply = nullptr; 0109 0110 emit result(this); 0111 deleteLater(); 0112 }