2020-09-27 18:38:33 +00:00
|
|
|
/*
|
|
|
|
Sailfin: a Jellyfin client written using Qt
|
2021-02-17 18:42:10 +00:00
|
|
|
Copyright (C) 2021 Chris Josten
|
2020-09-27 18:38:33 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
#ifndef JELLYFIN_API_MODEL
|
|
|
|
#define JELLYFIN_API_MODEL
|
|
|
|
|
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QFlags>
|
|
|
|
#include <QMetaEnum>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2020-10-08 01:00:08 +00:00
|
|
|
#include <QtQml>
|
2021-02-20 22:20:39 +00:00
|
|
|
#include <QQmlParserStatus>
|
2020-09-15 14:53:13 +00:00
|
|
|
#include <QVariant>
|
|
|
|
|
2021-02-17 18:42:10 +00:00
|
|
|
#include "apiclient.h"
|
2020-10-10 12:30:49 +00:00
|
|
|
#include "jsonhelper.h"
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
namespace Jellyfin {
|
2021-02-17 18:42:10 +00:00
|
|
|
|
|
|
|
namespace DTO {
|
2021-02-20 22:20:39 +00:00
|
|
|
class Item;
|
2021-02-17 18:42:10 +00:00
|
|
|
class JsonSerializable;
|
2021-02-20 22:20:39 +00:00
|
|
|
class User;
|
2021-02-17 18:42:10 +00:00
|
|
|
}
|
2021-02-20 22:20:39 +00:00
|
|
|
class SortOptions : public QObject {
|
2020-10-01 09:56:02 +00:00
|
|
|
Q_OBJECT
|
2020-09-15 14:53:13 +00:00
|
|
|
public:
|
2020-10-01 09:56:02 +00:00
|
|
|
explicit SortOptions (QObject *parent = nullptr) : QObject(parent) {}
|
2020-09-15 14:53:13 +00:00
|
|
|
enum SortBy {
|
|
|
|
Album,
|
|
|
|
AlbumArtist,
|
|
|
|
Artist,
|
|
|
|
Budget,
|
|
|
|
CommunityRating,
|
|
|
|
CriticRating,
|
|
|
|
DateCreated,
|
|
|
|
DatePlayed,
|
|
|
|
PlayCount,
|
|
|
|
PremiereDate,
|
|
|
|
ProductionYear,
|
|
|
|
SortName,
|
|
|
|
Random,
|
|
|
|
Revenue,
|
|
|
|
Runtime
|
|
|
|
};
|
|
|
|
Q_ENUM(SortBy)
|
|
|
|
};
|
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
/**
|
|
|
|
* Q_OBJECT does not support template classes. This base class declares the
|
|
|
|
* Q_OBJECT related properties and signals.
|
|
|
|
*/
|
|
|
|
class BaseApiModel : public QAbstractListModel, public QQmlParserStatus {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit BaseApiModel(QString path, bool hasRecordResponse, bool addUserId, QObject *parent = nullptr);
|
|
|
|
enum ModelStatus {
|
|
|
|
Uninitialised,
|
|
|
|
Loading,
|
|
|
|
Ready,
|
|
|
|
Error,
|
|
|
|
LoadingMore
|
|
|
|
};
|
|
|
|
Q_ENUM(ModelStatus)
|
|
|
|
|
|
|
|
enum SortOrder {
|
|
|
|
Unspecified,
|
|
|
|
Ascending,
|
|
|
|
Descending
|
|
|
|
};
|
|
|
|
Q_ENUM(SortOrder)
|
|
|
|
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient NOTIFY apiClientChanged)
|
|
|
|
Q_PROPERTY(ModelStatus status READ status NOTIFY statusChanged)
|
|
|
|
|
|
|
|
// Query properties
|
|
|
|
Q_PROPERTY(int limit MEMBER m_limit NOTIFY limitChanged)
|
|
|
|
Q_PROPERTY(QList<QString> sortBy MEMBER m_sortBy NOTIFY sortByChanged)
|
|
|
|
Q_PROPERTY(QList<QString> fields MEMBER m_fields NOTIFY fieldsChanged)
|
|
|
|
Q_PROPERTY(SortOrder sortOrder MEMBER m_sortOrder NOTIFY sortOrderChanged)
|
|
|
|
|
|
|
|
ModelStatus status() const { return m_status; }
|
|
|
|
void setApiClient(ApiClient *newApiClient);
|
|
|
|
void setLimit(int newLimit);
|
|
|
|
|
|
|
|
// From AbstractListModel, gets implemented in ApiModel<T>
|
|
|
|
virtual int rowCount(const QModelIndex &index) const override = 0;
|
|
|
|
virtual QHash<int, QByteArray> roleNames() const override = 0;
|
|
|
|
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override = 0;
|
|
|
|
virtual bool canFetchMore(const QModelIndex &parent) const override = 0;
|
|
|
|
virtual void fetchMore(const QModelIndex &parent) override = 0;
|
|
|
|
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void ready();
|
|
|
|
void apiClientChanged(ApiClient *newApiClient);
|
|
|
|
void statusChanged(ModelStatus newStatus);
|
|
|
|
void limitChanged(int newLimit);
|
|
|
|
void sortByChanged(QList<QString> newSortOrder);
|
|
|
|
void sortOrderChanged(SortOrder newSortOrder);
|
|
|
|
void fieldsChanged(QList<QString> newFields);
|
|
|
|
|
|
|
|
public slots:
|
|
|
|
/**
|
|
|
|
* @brief (Re)loads the data into this model. This might make a network request.
|
|
|
|
*/
|
|
|
|
void reload();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
enum LoadType {
|
|
|
|
RELOAD,
|
|
|
|
LOAD_MORE
|
|
|
|
};
|
|
|
|
|
|
|
|
ApiClient *m_apiClient = nullptr;
|
|
|
|
bool m_isBeingParsed = false;
|
|
|
|
// Per-model specific settings.
|
|
|
|
QString m_path;
|
|
|
|
bool m_hasRecordResponse;
|
|
|
|
bool m_addUserId;
|
|
|
|
bool padding; bool padding2;
|
|
|
|
|
|
|
|
// Query/record controlling properties
|
|
|
|
int m_limit = -1;
|
|
|
|
int m_startIndex = 0;
|
|
|
|
int m_totalRecordCount = 0;
|
|
|
|
const int DEFAULT_LIMIT = 100;
|
|
|
|
|
|
|
|
// Query properties
|
|
|
|
QList<QString> m_fields = {};
|
|
|
|
QList<QString> m_sortBy = {};
|
|
|
|
SortOrder m_sortOrder = Unspecified;
|
|
|
|
|
|
|
|
// State properties.
|
|
|
|
ModelStatus m_status = Uninitialised;
|
|
|
|
|
|
|
|
void setStatus(ModelStatus newStatus) {
|
|
|
|
if (this->m_status != newStatus) {
|
|
|
|
this->m_status = newStatus;
|
|
|
|
emit this->statusChanged(newStatus);
|
|
|
|
if (m_status == Ready) {
|
|
|
|
emit ready();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void load(LoadType loadType);
|
|
|
|
virtual void setModelData(QJsonArray &data) = 0;
|
|
|
|
virtual void appendModelData(QJsonArray &data) = 0;
|
|
|
|
/**
|
|
|
|
* @brief Adds parameters to the query
|
|
|
|
* @param query The query to add parameters to
|
|
|
|
*
|
|
|
|
* This method is intended to be overrided by subclasses. It gets called
|
|
|
|
* before a request is made to the server and can be used to enable
|
|
|
|
* query types specific for a certain model to be available.
|
|
|
|
*
|
|
|
|
* Make sure to call the method in the superclass as well!
|
|
|
|
*/
|
|
|
|
virtual void addQueryParameters(QUrlQuery &query);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Replaces placeholders in an URL.
|
|
|
|
* @param path The path in which placeholders should be replaced.
|
|
|
|
*
|
|
|
|
* This method is intended to be overrided by subclasses. It gets called
|
|
|
|
* before a request is made to the server and can be used to enable
|
|
|
|
* query types specific for a certain model to be available.
|
|
|
|
*
|
|
|
|
* Make sure to call the method in the superclass as well!
|
|
|
|
*/
|
|
|
|
virtual void replacePathPlaceholders(QString &path);
|
|
|
|
|
|
|
|
virtual void classBegin() override;
|
|
|
|
virtual void componentComplete() override;
|
|
|
|
};
|
|
|
|
|
2020-10-01 09:56:02 +00:00
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
/**
|
|
|
|
* @brief Abstract model for displaying a REST JSON collection. Role names will be based on the fields encountered in the
|
|
|
|
* first record.
|
|
|
|
*
|
|
|
|
* To create a new model, extend this class and create an QObject-parent constructor.
|
|
|
|
* Call the right super constructor with the right values, depending which path should be queried and
|
|
|
|
* how the result should be interpreted.
|
|
|
|
*
|
|
|
|
* Register the model in QML and create an instance. Don't forget to set the apiClient attribute or else
|
|
|
|
* the model you've created will be useless!
|
|
|
|
*
|
|
|
|
* Rolenames are based on the fields in the first object within the array of results, with the first letter
|
|
|
|
* lowercased, to accomodate for QML style guidelines. (This ain't C# here).
|
|
|
|
*
|
|
|
|
* If a call to /cats/new results in
|
|
|
|
* @code{.json}
|
|
|
|
* [
|
|
|
|
* {"Name": "meow", "Id": 432},
|
|
|
|
* {"Name": "miew", "Id": 323}
|
|
|
|
* ]
|
|
|
|
* @endcode
|
|
|
|
* The model will have roleNames for "name" and "id".
|
|
|
|
*
|
|
|
|
*/
|
2021-02-20 22:20:39 +00:00
|
|
|
template <typename T>
|
|
|
|
class ApiModel : public BaseApiModel {
|
2020-09-15 14:53:13 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* @brief Creates a new basemodel
|
|
|
|
* @param path The path (relative to the baseUrl of JellyfinApiClient) to make the call to.
|
|
|
|
* @param subfield Leave empty if the root of the result is the array with results. Otherwise, set to the key name in the
|
|
|
|
* root object which contains the data.
|
|
|
|
* @param parent Parent (Standard QObject stuff)
|
|
|
|
*
|
|
|
|
* If the response looks something like this:
|
|
|
|
* @code{.json}
|
|
|
|
* [{...}, {...}, {...}]
|
|
|
|
* @endcode
|
2020-09-27 01:14:05 +00:00
|
|
|
*
|
|
|
|
* or
|
|
|
|
* @code{.json}
|
|
|
|
* {...}
|
|
|
|
* @endcode
|
|
|
|
* responseHasRecords should be false
|
2020-09-15 14:53:13 +00:00
|
|
|
*
|
|
|
|
* If the response looks something like this:
|
|
|
|
* @code{.json}
|
|
|
|
* {
|
2020-09-27 01:14:05 +00:00
|
|
|
* "Offset": 0,
|
|
|
|
* "Count": 20,
|
|
|
|
* "Items": [{...}, {...}, {...}, ..., {...}]
|
2020-09-15 14:53:13 +00:00
|
|
|
* }
|
|
|
|
* @endcode
|
2020-09-27 01:14:05 +00:00
|
|
|
* responseHasRecords should be true
|
2020-09-15 14:53:13 +00:00
|
|
|
*/
|
2020-09-27 01:14:05 +00:00
|
|
|
explicit ApiModel(QString path, bool responseHasRecords, bool passUserId = false, QObject *parent = nullptr);
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2020-09-27 01:14:05 +00:00
|
|
|
// Standard QAbstractItemModel overrides
|
2020-09-15 14:53:13 +00:00
|
|
|
int rowCount(const QModelIndex &index) const override {
|
|
|
|
if (!index.isValid()) return m_array.size();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
QHash<int, QByteArray> roleNames() const override { return m_roles; }
|
|
|
|
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
2020-09-27 01:14:05 +00:00
|
|
|
bool canFetchMore(const QModelIndex &parent) const override;
|
|
|
|
void fetchMore(const QModelIndex &parent) override;
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2020-09-27 01:14:05 +00:00
|
|
|
// Helper methods
|
2020-09-15 14:53:13 +00:00
|
|
|
template<typename QEnum>
|
|
|
|
QString enumToString (const QEnum anEnum) { return QVariant::fromValue(anEnum).toString(); }
|
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
// QList-like API
|
|
|
|
T* at(int index) { return m_array.at(index); }
|
|
|
|
int size() { return rowCount(QModelIndex()); }
|
|
|
|
void insert(int index, T* object);
|
|
|
|
void append(T* object) { insert(size(), object); }
|
|
|
|
void removeAt(int index);
|
|
|
|
void removeOne(T* object);
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
template<typename QEnum>
|
|
|
|
QString enumListToString (const QList<QEnum> enumList) {
|
|
|
|
QString result;
|
|
|
|
for (QEnum e : enumList) {
|
|
|
|
result += QVariant::fromValue(e).toString() + ",";
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2020-09-27 01:14:05 +00:00
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
protected:
|
2021-02-20 22:20:39 +00:00
|
|
|
// AbstractItemModel bookkeeping
|
|
|
|
QHash<int, QByteArray> m_roles;
|
2020-09-27 01:14:05 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
// Helper methods.
|
|
|
|
T *deserializeResult(QJsonValueRef source);
|
|
|
|
virtual void addQueryParameters(QUrlQuery &query) override;
|
|
|
|
virtual void replacePathPlaceholders(QString &path) override;
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
virtual void setModelData(QJsonArray &data) override;
|
|
|
|
virtual void appendModelData(QJsonArray &data) override;
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
// Model-specific properties.
|
|
|
|
QList<T*> m_array;
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* @brief Generates roleNames based on the first record in m_array.
|
|
|
|
*/
|
|
|
|
void generateFields();
|
2020-10-01 09:56:02 +00:00
|
|
|
QString sortByToString(SortOptions::SortBy sortBy);
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief List of the public users on the server.
|
|
|
|
*/
|
2021-02-20 22:20:39 +00:00
|
|
|
class PublicUserModel : public ApiModel<User> {
|
2020-09-15 14:53:13 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit PublicUserModel (QObject *parent = nullptr);
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 00:33:08 +00:00
|
|
|
/**
|
|
|
|
* @brief Base class for each model that works with items.
|
|
|
|
*
|
|
|
|
* Listens for updates in the library and updates the model accordingly.
|
|
|
|
*/
|
2021-02-20 22:20:39 +00:00
|
|
|
class ItemModel : public ApiModel<Item> {
|
2020-10-09 00:33:08 +00:00
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit ItemModel (QString path, bool responseHasRecords, bool replaceUser, QObject *parent = nullptr);
|
2021-02-20 22:20:39 +00:00
|
|
|
// Query parameters
|
|
|
|
Q_PROPERTY(QString parentId MEMBER m_parentId WRITE setParentId NOTIFY parentIdChanged)
|
|
|
|
Q_PROPERTY(QString seasonId MEMBER m_seasonId NOTIFY seasonIdChanged)
|
|
|
|
Q_PROPERTY(QList<QString> imageTypes MEMBER m_imageTypes NOTIFY imageTypesChanged)
|
|
|
|
Q_PROPERTY(QList<QString> includeItemTypes MEMBER m_includeItemTypes NOTIFY includeItemTypesChanged)
|
|
|
|
Q_PROPERTY(bool recursive MEMBER m_recursive)
|
|
|
|
QList<QString> m_includeItemTypes = {};
|
|
|
|
|
|
|
|
// Path properties
|
|
|
|
Q_PROPERTY(QString show MEMBER m_show NOTIFY showChanged)
|
|
|
|
|
|
|
|
void setParentId(const QString &parentId) {
|
|
|
|
m_parentId = parentId;
|
|
|
|
emit parentIdChanged(m_parentId);
|
|
|
|
}
|
|
|
|
signals:
|
|
|
|
// Query property signals
|
|
|
|
void parentIdChanged(QString newParentId);
|
|
|
|
void seasonIdChanged(QString newSeasonId);
|
|
|
|
void imageTypesChanged(QList<QString> newImageTypes);
|
|
|
|
void includeItemTypesChanged(const QList<QString> &newIncludeItemTypes);
|
|
|
|
|
|
|
|
// Path property signals
|
|
|
|
void showChanged(QString newShow);
|
2020-10-09 00:33:08 +00:00
|
|
|
public slots:
|
2021-02-20 22:20:39 +00:00
|
|
|
void onUserDataChanged(const QString &itemId, DTO::UserData *userData);
|
|
|
|
protected:
|
|
|
|
virtual void addQueryParameters(QUrlQuery &query) override;
|
|
|
|
virtual void replacePathPlaceholders(QString &path) override;
|
|
|
|
private:
|
|
|
|
// Path properties
|
|
|
|
QString m_show;
|
|
|
|
|
|
|
|
// Query parameters
|
|
|
|
QString m_parentId;
|
|
|
|
QString m_seasonId;
|
|
|
|
QList<QString> m_imageTypes = {};
|
|
|
|
bool m_recursive = false;
|
2020-10-09 00:33:08 +00:00
|
|
|
};
|
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
//template<>
|
|
|
|
//void ApiModel<Item>::apiClientChanged();
|
|
|
|
|
|
|
|
class UserViewModel : public ItemModel {
|
2020-09-15 14:53:13 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit UserViewModel (QObject *parent = nullptr);
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 00:33:08 +00:00
|
|
|
class UserItemModel : public ItemModel {
|
2020-09-15 14:53:13 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit UserItemModel (QObject *parent = nullptr);
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
2020-09-27 14:54:45 +00:00
|
|
|
|
2020-10-09 00:33:08 +00:00
|
|
|
class UserItemResumeModel : public ItemModel {
|
2020-09-27 14:54:45 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit UserItemResumeModel (QObject *parent = nullptr);
|
2020-09-27 14:54:45 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 00:33:08 +00:00
|
|
|
class UserItemLatestModel : public ItemModel {
|
2020-09-25 15:14:44 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit UserItemLatestModel (QObject *parent = nullptr);
|
2020-09-26 00:51:37 +00:00
|
|
|
};
|
|
|
|
|
2020-10-10 14:26:08 +00:00
|
|
|
class ShowNextUpModel : public ItemModel {
|
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit ShowNextUpModel (QObject *parent = nullptr);
|
2020-10-10 14:26:08 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 00:33:08 +00:00
|
|
|
class ShowSeasonsModel : public ItemModel {
|
2020-09-26 00:51:37 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit ShowSeasonsModel (QObject *parent = nullptr);
|
2020-09-26 00:51:37 +00:00
|
|
|
};
|
|
|
|
|
2020-10-09 00:33:08 +00:00
|
|
|
class ShowEpisodesModel : public ItemModel {
|
2020-09-26 00:51:37 +00:00
|
|
|
public:
|
2020-10-25 18:58:02 +00:00
|
|
|
explicit ShowEpisodesModel (QObject *parent = nullptr);
|
2020-09-25 15:14:44 +00:00
|
|
|
};
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
void registerModels(const char *URI);
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif //JELLYFIN_API_MODEL
|