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>
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
|
|
#include "jellyfinapiclient.h"
|
|
|
|
|
|
|
|
namespace Jellyfin {
|
|
|
|
class SortOrder {
|
|
|
|
Q_GADGET
|
|
|
|
public:
|
|
|
|
enum SortBy {
|
|
|
|
Album,
|
|
|
|
AlbumArtist,
|
|
|
|
Artist,
|
|
|
|
Budget,
|
|
|
|
CommunityRating,
|
|
|
|
CriticRating,
|
|
|
|
DateCreated,
|
|
|
|
DatePlayed,
|
|
|
|
PlayCount,
|
|
|
|
PremiereDate,
|
|
|
|
ProductionYear,
|
|
|
|
SortName,
|
|
|
|
Random,
|
|
|
|
Revenue,
|
|
|
|
Runtime
|
|
|
|
};
|
|
|
|
Q_ENUM(SortBy)
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
class ApiModel : public QAbstractListModel {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
enum ModelStatus {
|
|
|
|
Uninitialised,
|
|
|
|
Loading,
|
|
|
|
Ready,
|
|
|
|
Error
|
|
|
|
};
|
|
|
|
Q_ENUM(ModelStatus)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
* subfield should be left empty
|
|
|
|
*
|
|
|
|
* If the response looks something like this:
|
|
|
|
* @code{.json}
|
|
|
|
* {
|
|
|
|
* "offset": 0,
|
|
|
|
* "count": 20,
|
|
|
|
* "data": [{...}, {...}, {...}, ..., {...}]
|
|
|
|
* }
|
|
|
|
* @endcode
|
|
|
|
* Subfield should be set to "data" in this example.
|
|
|
|
*/
|
2020-09-26 00:51:37 +00:00
|
|
|
explicit ApiModel(QString path, QString subfield, bool passUserId = false, QObject *parent = nullptr);
|
2020-09-25 12:46:39 +00:00
|
|
|
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient)
|
2020-09-15 14:53:13 +00:00
|
|
|
Q_PROPERTY(ModelStatus status READ status NOTIFY statusChanged)
|
2020-09-26 00:51:37 +00:00
|
|
|
|
|
|
|
// Query properties
|
2020-09-15 14:53:13 +00:00
|
|
|
Q_PROPERTY(int limit MEMBER m_limit NOTIFY limitChanged)
|
|
|
|
Q_PROPERTY(QString parentId MEMBER m_parentId NOTIFY parentIdChanged)
|
2020-09-26 00:51:37 +00:00
|
|
|
Q_PROPERTY(QList<QString> sortBy MEMBER m_sortBy NOTIFY sortByChanged)
|
|
|
|
Q_PROPERTY(QList<QString> fields MEMBER m_fields NOTIFY fieldsChanged)
|
|
|
|
Q_PROPERTY(QString seasonId MEMBER m_seasonId NOTIFY seasonIdChanged)
|
|
|
|
Q_PROPERTY(QList<QString> imageTypes MEMBER m_imageTypes NOTIFY imageTypesChanged)
|
|
|
|
Q_PROPERTY(bool recursive MEMBER m_recursive)
|
|
|
|
|
|
|
|
// Path properties
|
|
|
|
Q_PROPERTY(QString show MEMBER m_show NOTIFY showChanged)
|
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;
|
|
|
|
|
|
|
|
ModelStatus status() const { return m_status; }
|
|
|
|
|
|
|
|
template<typename QEnum>
|
|
|
|
QString enumToString (const QEnum anEnum) { return QVariant::fromValue(anEnum).toString(); }
|
|
|
|
|
|
|
|
template<typename QEnum>
|
|
|
|
QString enumListToString (const QList<QEnum> enumList) {
|
|
|
|
QString result;
|
|
|
|
for (QEnum e : enumList) {
|
|
|
|
result += QVariant::fromValue(e).toString() + ",";
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
signals:
|
|
|
|
void statusChanged(ModelStatus newStatus);
|
|
|
|
void limitChanged(int newLimit);
|
|
|
|
void parentIdChanged(QString newParentId);
|
2020-09-26 00:51:37 +00:00
|
|
|
void sortByChanged(QList<QString> newSortOrder);
|
|
|
|
void showChanged(QString newShow);
|
|
|
|
void seasonIdChanged(QString newSeasonId);
|
|
|
|
void fieldsChanged(QList<QString> newFields);
|
|
|
|
void imageTypesChanged(QList<QString> newImageTypes);
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
public slots:
|
|
|
|
/**
|
|
|
|
* @brief (Re)loads the data into this model. This might make a network request.
|
|
|
|
*/
|
|
|
|
void reload();
|
|
|
|
protected:
|
2020-09-25 12:46:39 +00:00
|
|
|
ApiClient *m_apiClient = nullptr;
|
2020-09-15 14:53:13 +00:00
|
|
|
ModelStatus m_status = Uninitialised;
|
|
|
|
|
|
|
|
QString m_path;
|
|
|
|
QString m_subfield;
|
|
|
|
QJsonArray m_array;
|
|
|
|
|
2020-09-26 00:51:37 +00:00
|
|
|
// Path properties
|
|
|
|
QString m_show;
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
// Query properties
|
|
|
|
int m_limit = -1;
|
2020-09-26 00:51:37 +00:00
|
|
|
bool m_addUserId = false;
|
2020-09-15 14:53:13 +00:00
|
|
|
QString m_parentId;
|
2020-09-26 00:51:37 +00:00
|
|
|
QString m_seasonId;
|
|
|
|
QList<QString> m_fields;
|
|
|
|
QList<QString> m_imageTypes;
|
|
|
|
QList<QString> m_sortBy = {};
|
|
|
|
bool m_recursive;
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
QHash<int, QByteArray> m_roles;
|
|
|
|
//QHash<QByteArray, int> m_reverseRoles;
|
|
|
|
|
|
|
|
void setStatus(ModelStatus newStatus) {
|
|
|
|
this->m_status = newStatus;
|
|
|
|
emit this->statusChanged(newStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* @brief Generates roleNames based on the first record in m_array.
|
|
|
|
*/
|
|
|
|
void generateFields();
|
|
|
|
QString sortByToString(SortOrder::SortBy sortBy);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief List of the public users on the server.
|
|
|
|
*/
|
|
|
|
class PublicUserModel : public ApiModel {
|
|
|
|
public:
|
|
|
|
explicit PublicUserModel (QObject *parent = nullptr)
|
2020-09-26 00:51:37 +00:00
|
|
|
: ApiModel ("/users/public", "", false, parent) { }
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UserViewModel : public ApiModel {
|
|
|
|
public:
|
|
|
|
explicit UserViewModel (QObject *parent = nullptr)
|
2020-09-26 00:51:37 +00:00
|
|
|
: ApiModel ("/Users/{{user}}/Views", "Items", false, parent) {}
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class UserItemModel : public ApiModel {
|
|
|
|
public:
|
|
|
|
explicit UserItemModel (QObject *parent = nullptr)
|
2020-09-26 00:51:37 +00:00
|
|
|
: ApiModel ("/Users/{{user}}/Items", "Items", false, parent) {}
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
2020-09-25 15:14:44 +00:00
|
|
|
class UserItemLatestModel : public ApiModel {
|
|
|
|
public:
|
|
|
|
explicit UserItemLatestModel (QObject *parent = nullptr)
|
2020-09-26 00:51:37 +00:00
|
|
|
: ApiModel ("/Users/{{user}}/Items/Latest", "", false, parent) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ShowSeasonsModel : public ApiModel {
|
|
|
|
public:
|
|
|
|
explicit ShowSeasonsModel (QObject *parent = nullptr)
|
|
|
|
: ApiModel ("/Shows/{{show}}/Seasons", "Items", true, parent) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ShowEpisodesModel : public ApiModel {
|
|
|
|
public:
|
|
|
|
explicit ShowEpisodesModel (QObject *parent = nullptr)
|
|
|
|
: ApiModel ("/Shows/{{show}}/Episodes", "Items", true, parent) {}
|
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
|