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