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 "tvshowdb.h"
0022 #include "configuration.h"
0023 #include "tvseasondblist.h"
0024 
0025 #include <QJsonArray>
0026 #include <QJsonObject>
0027 #include <QDebug>
0028 
0029 using namespace TmdbQt;
0030 
0031 class TmdbQt::TvDbPrivate : public QSharedData
0032 {
0033 public:
0034     TvDbPrivate(const Configuration &config)
0035         : m_configuration(config)
0036         , m_id(-1)
0037     {}
0038 
0039     Configuration m_configuration;
0040 
0041     int m_id;
0042     QString m_name;
0043     QString m_originalName;
0044 
0045     QDate m_firstAired;
0046     QString m_posterPath;
0047     QString m_backdropPath;
0048 
0049     QString m_overview;
0050     TvSeasonDbList m_seasons;
0051 };
0052 
0053 
0054 TvShowDb::TvShowDb(const Configuration& config)
0055     : d(new TvDbPrivate(config))
0056 {
0057 }
0058 
0059 TvShowDb::TvShowDb(const TvShowDb &other)
0060     : d(other.d)
0061 {
0062 }
0063 
0064 TvShowDb::~TvShowDb()
0065 {
0066 }
0067 
0068 TvShowDb &TvShowDb::operator=(const TvShowDb &other)
0069 {
0070     d = other.d;
0071     return *this;
0072 }
0073 
0074 int TvShowDb::id() const
0075 {
0076     return d->m_id;
0077 }
0078 
0079 QString TvShowDb::name() const
0080 {
0081     return d->m_name;
0082 }
0083 
0084 QString TvShowDb::originalName() const
0085 {
0086     return d->m_originalName;
0087 }
0088 
0089 QDate TvShowDb::firstAiredDate() const
0090 {
0091     return d->m_firstAired;
0092 }
0093 
0094 QString TvShowDb::backdropPath() const
0095 {
0096     return d->m_backdropPath;
0097 }
0098 
0099 QString TvShowDb::posterPath() const
0100 {
0101     return d->m_posterPath;
0102 }
0103 
0104 QUrl TvShowDb::backdropUrl(const QString &size) const
0105 {
0106     QUrl url = d->m_configuration.imageBaseUrl();
0107     if (url.isEmpty())
0108         qWarning() << "No image base URL, did you wait for the initialized() signal before starting jobs?";
0109     url.setPath(url.path() + size + d->m_backdropPath);
0110     return url;
0111 }
0112 
0113 QUrl TvShowDb::posterUrl(const QString &size) const
0114 {
0115     QUrl url = d->m_configuration.imageBaseUrl();
0116     if (url.isEmpty())
0117         qWarning() << "No image base URL, did you wait for the initialized() signal before starting jobs?";
0118     url.setPath(url.path() + size + d->m_posterPath);
0119     return url;
0120 
0121 }
0122 
0123 QString TvShowDb::overview() const
0124 {
0125     return d->m_overview;
0126 }
0127 
0128 TvSeasonDbList TvShowDb::seasons() const
0129 {
0130     return d->m_seasons;
0131 }
0132 
0133 void TvShowDb::load(const QJsonObject& json)
0134 {
0135     d->m_backdropPath = json.value(QStringLiteral("backdrop_path")).toString();
0136     d->m_id = json.value(QStringLiteral("id")).toInt();
0137     d->m_originalName = json.value(QStringLiteral("original_name")).toString();
0138     d->m_name = json.value(QStringLiteral("name")).toString();
0139     d->m_overview = json.value(QStringLiteral("overview")).toString();
0140 
0141     const QString firstAiredDate = json.value(QStringLiteral("first_air_date")).toString();
0142     d->m_firstAired = QDate::fromString(firstAiredDate, Qt::ISODate);
0143     d->m_posterPath = json.value(QStringLiteral("poster_path")).toString();
0144 
0145     QJsonArray arr = json.value(QStringLiteral("seasons")).toArray();
0146     d->m_seasons.load(arr, d->m_configuration);
0147 }
0148