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
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
#include <optional>
|
2021-08-22 23:48:35 +00:00
|
|
|
#include <typeinfo>
|
2021-03-26 20:27:35 +00:00
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
#include <QAbstractListModel>
|
|
|
|
#include <QFlags>
|
2021-03-26 20:27:35 +00:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureWatcher>
|
2020-09-15 14:53:13 +00:00
|
|
|
#include <QMetaEnum>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonDocument>
|
2021-03-20 15:29:31 +00:00
|
|
|
#include <QJsonValue>
|
2021-03-26 20:27:35 +00:00
|
|
|
#include <QtConcurrent/QtConcurrent>
|
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
|
|
|
|
2021-03-24 19:04:03 +00:00
|
|
|
#include "dto/baseitemdto.h"
|
|
|
|
#include "dto/userdto.h"
|
|
|
|
#include "dto/useritemdatadto.h"
|
2021-03-26 20:27:35 +00:00
|
|
|
#include "dto/baseitemdtoqueryresult.h"
|
|
|
|
#include "loader/requesttypes.h"
|
|
|
|
#include "support/loader.h"
|
2021-03-29 12:27:37 +00:00
|
|
|
#include "viewmodel/modelstatus.h"
|
2021-02-21 04:02:05 +00:00
|
|
|
|
2021-09-09 03:57:41 +00:00
|
|
|
Q_DECLARE_LOGGING_CATEGORY(jellyfinApiModel)
|
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
namespace Jellyfin {
|
2021-02-17 18:42:10 +00:00
|
|
|
|
2021-03-29 15:10:25 +00:00
|
|
|
/*
|
|
|
|
* A brief description of this file:
|
|
|
|
*
|
|
|
|
* The reason why all of this is this complex is because Qt MOC's lack of support for template classes
|
|
|
|
* with Q_OBJECT. To work around this, "base classes", such as BaseModelLoader, are created which contain
|
|
|
|
* all functionallity required by Q_OBJECT. This class is extended by the templated class, which in turn
|
|
|
|
* is extended once more, so it can be registered for the QML engine.
|
|
|
|
*
|
|
|
|
* The loading of the data has beens separated from the QAbstractModels into ViewModel::Loaders
|
|
|
|
* to allow for loading over the network, loading from the cache and so on, without the QAbstractModel
|
|
|
|
* knowing anything about how it's done, except for the parameters it can pass to the loader and the result.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
class BaseModelLoader : public QObject, public QQmlParserStatus {
|
|
|
|
Q_INTERFACES(QQmlParserStatus)
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
explicit BaseModelLoader(QObject *parent = nullptr);
|
|
|
|
Q_PROPERTY(ApiClient *apiClient READ apiClient WRITE setApiClient NOTIFY apiClientChanged)
|
2021-03-29 15:10:25 +00:00
|
|
|
Q_PROPERTY(Jellyfin::ViewModel::ModelStatusClass::Value status READ status NOTIFY statusChanged)
|
2021-03-26 20:27:35 +00:00
|
|
|
Q_PROPERTY(int limit READ limit WRITE setLimit NOTIFY limitChanged)
|
|
|
|
Q_PROPERTY(bool autoReload READ autoReload WRITE setAutoReload NOTIFY autoReloadChanged)
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
ApiClient *apiClient() const { return m_apiClient; }
|
2021-02-20 22:20:39 +00:00
|
|
|
void setApiClient(ApiClient *newApiClient);
|
2021-03-26 20:27:35 +00:00
|
|
|
|
|
|
|
int limit() const { return m_limit; }
|
2021-02-20 22:20:39 +00:00
|
|
|
void setLimit(int newLimit);
|
2021-03-26 20:27:35 +00:00
|
|
|
bool autoReload() const { return m_autoReload; }
|
|
|
|
void setAutoReload(bool newAutoReload);
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-29 12:27:37 +00:00
|
|
|
ViewModel::ModelStatus status() const { return m_status; }
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Clears and reloads the model
|
|
|
|
*/
|
|
|
|
Q_INVOKABLE virtual void reload() {};
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
// QQmlParserStatus interface
|
|
|
|
virtual void classBegin() override;
|
|
|
|
virtual void componentComplete() override;
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void autoReloadIfNeeded();
|
2021-02-20 22:20:39 +00:00
|
|
|
signals:
|
|
|
|
void ready();
|
|
|
|
void apiClientChanged(ApiClient *newApiClient);
|
2021-03-26 20:27:35 +00:00
|
|
|
void statusChanged();
|
2021-02-20 22:20:39 +00:00
|
|
|
void limitChanged(int newLimit);
|
2021-03-26 20:27:35 +00:00
|
|
|
void autoReloadChanged(bool newAutoReload);
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Emitted when the model should clear itself
|
|
|
|
*/
|
|
|
|
void modelShouldClear();
|
|
|
|
/**
|
|
|
|
* Emitted when new items are loaded.
|
|
|
|
*/
|
|
|
|
void itemsLoaded();
|
2021-03-26 20:27:35 +00:00
|
|
|
void reloadWanted();
|
2021-02-20 22:20:39 +00:00
|
|
|
protected:
|
2021-03-26 20:27:35 +00:00
|
|
|
// Is this object being parsed by the QML engine
|
2021-02-20 22:20:39 +00:00
|
|
|
bool m_isBeingParsed = false;
|
2021-03-26 20:27:35 +00:00
|
|
|
ApiClient *m_apiClient = nullptr;
|
|
|
|
bool m_autoReload = true;
|
2021-03-28 02:00:00 +00:00
|
|
|
bool m_needsAuthentication = true;
|
2021-08-22 23:48:35 +00:00
|
|
|
bool m_manualLimitSet = false;
|
2021-02-20 22:20:39 +00:00
|
|
|
|
|
|
|
// Query/record controlling properties
|
|
|
|
int m_limit = -1;
|
|
|
|
int m_startIndex = 0;
|
|
|
|
int m_totalRecordCount = 0;
|
2021-09-09 03:57:41 +00:00
|
|
|
bool m_explicitLimitSet = false;
|
2021-02-20 22:20:39 +00:00
|
|
|
const int DEFAULT_LIMIT = 100;
|
2021-03-28 02:00:00 +00:00
|
|
|
void emitModelShouldClear() { emit modelShouldClear(); }
|
|
|
|
void emitItemsLoaded() { emit itemsLoaded(); }
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-29 12:27:37 +00:00
|
|
|
ViewModel::ModelStatus m_status = ViewModel::ModelStatus::Uninitialised;
|
|
|
|
void setStatus(ViewModel::ModelStatus newStatus) {
|
2021-02-20 22:20:39 +00:00
|
|
|
if (this->m_status != newStatus) {
|
|
|
|
this->m_status = newStatus;
|
2021-03-26 20:27:35 +00:00
|
|
|
emit this->statusChanged();
|
2021-03-29 12:27:37 +00:00
|
|
|
if (this->m_status == ViewModel::ModelStatus::Ready) {
|
2021-02-20 22:20:39 +00:00
|
|
|
emit ready();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 15:10:25 +00:00
|
|
|
/**
|
|
|
|
* @brief Determines if this model is able to reload.
|
|
|
|
*
|
|
|
|
* The default implementation checks if the user is authenticated,
|
|
|
|
* and the model is not reloading. If overriding this method, please
|
|
|
|
* call this method as well in determining if the model should be reloadable.
|
|
|
|
*
|
|
|
|
* @return True if the model can reload, false otherwise.
|
|
|
|
*/
|
2021-03-28 02:00:00 +00:00
|
|
|
virtual bool canReload() const;
|
2021-03-26 20:27:35 +00:00
|
|
|
};
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
/**
|
|
|
|
* Base model loader that only has one template parameter,
|
|
|
|
* so it can be used within the ApiModel.
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
class ModelLoader : public BaseModelLoader {
|
|
|
|
public:
|
|
|
|
ModelLoader(QObject *parent = nullptr)
|
2021-03-28 02:00:00 +00:00
|
|
|
: BaseModelLoader(parent) {
|
|
|
|
}
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
void reload() override {
|
|
|
|
if (!canReload()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-03-26 20:27:35 +00:00
|
|
|
m_startIndex = 0;
|
|
|
|
m_totalRecordCount = -1;
|
2021-03-28 02:00:00 +00:00
|
|
|
emitModelShouldClear();
|
2021-09-09 03:57:41 +00:00
|
|
|
loadMore(ViewModel::ModelStatus::Loading);
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
void loadMore() {
|
|
|
|
if (!canReload()) {
|
|
|
|
return;
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
2021-09-09 03:57:41 +00:00
|
|
|
loadMore(ViewModel::ModelStatus::LoadingMore);
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool canLoadMore() const {
|
|
|
|
return m_totalRecordCount == -1 || m_startIndex < m_totalRecordCount;
|
|
|
|
}
|
2021-03-28 02:00:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Holds the result. Moves it result to the caller and therefore can be only called once
|
|
|
|
* when the itemsLoaded is emitted.
|
|
|
|
* @return pair containing the items loaded and the integer containing the starting offset. A starting
|
|
|
|
* offset of -1 means an error has occurred.
|
|
|
|
*/
|
2021-08-11 21:35:33 +00:00
|
|
|
std::pair<QList<T*>, int> &&result() { return std::move(m_result); }
|
2021-03-26 20:27:35 +00:00
|
|
|
protected:
|
2021-03-28 02:00:00 +00:00
|
|
|
/**
|
|
|
|
* @brief Loads data from the given offset with a maximum count of limit.
|
|
|
|
* The itemsLoaded() signal is emitted when new data is ready. Call
|
|
|
|
* getLoadedItems to retrieve the loaded items.
|
|
|
|
*
|
2021-03-29 15:10:25 +00:00
|
|
|
* @param suggestedStatus The suggested status this model should take on if it is able to load (more).
|
|
|
|
* Either LOADING or LOAD_MORE.
|
2021-03-28 02:00:00 +00:00
|
|
|
*/
|
2021-09-09 03:57:41 +00:00
|
|
|
virtual void loadMore(ViewModel::ModelStatus suggestedStatus) = 0;
|
2021-08-11 21:35:33 +00:00
|
|
|
std::pair<QList<T*>, int> m_result;
|
2021-02-20 22:20:39 +00:00
|
|
|
};
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
/**
|
|
|
|
* Template to extract records from the given result.
|
|
|
|
*/
|
|
|
|
template <class T, class R>
|
|
|
|
QList<T> extractRecords(const R &result) {
|
|
|
|
Q_UNUSED(result)
|
2021-08-22 23:48:35 +00:00
|
|
|
qDebug() << "extractRecords not implemented for type " << typeid(R).name();
|
2021-03-26 20:27:35 +00:00
|
|
|
return QList<T>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
int extractTotalRecordCount(const R &result) {
|
|
|
|
Q_UNUSED(result)
|
2021-08-22 23:48:35 +00:00
|
|
|
qDebug() << "extractTotalRecourdCount not implemented for type " << typeid(R).name();
|
2021-03-26 20:27:35 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-08-22 23:48:35 +00:00
|
|
|
template <class P>
|
|
|
|
void setRequestLimit(P ¶meters, int limit) {
|
2021-03-26 20:27:35 +00:00
|
|
|
Q_UNUSED(parameters)
|
|
|
|
Q_UNUSED(limit)
|
2021-08-22 23:48:35 +00:00
|
|
|
qDebug() << "setRequestLimit not implemented for type " << typeid(P).name();
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:10:25 +00:00
|
|
|
/**
|
|
|
|
* @return True if able to set the startIndex, false otherwise.
|
|
|
|
*/
|
2021-03-26 20:27:35 +00:00
|
|
|
template <class P>
|
2021-03-29 15:10:25 +00:00
|
|
|
bool setRequestStartIndex(P ¶meters, int startIndex) {
|
2021-03-26 20:27:35 +00:00
|
|
|
Q_UNUSED(parameters)
|
|
|
|
Q_UNUSED(startIndex)
|
2021-08-22 23:48:35 +00:00
|
|
|
qDebug() << "setRequestStartIndex not implemented for type " << typeid(P).name();
|
2021-03-29 15:10:25 +00:00
|
|
|
return false;
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 13:06:17 +00:00
|
|
|
#ifndef JELLYFIN_APIMODEL_CPP
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetUserViewsParams ¶ms, int startIndex);
|
|
|
|
extern template void setRequestLimit(Loader::GetUserViewsParams ¶ms, int limit);
|
|
|
|
extern template QList<DTO::BaseItemDto> extractRecords(const DTO::BaseItemDtoQueryResult &result);
|
|
|
|
extern template int extractTotalRecordCount(const DTO::BaseItemDtoQueryResult &result);
|
|
|
|
extern template QList<DTO::BaseItemDto> extractRecords(const QList<DTO::BaseItemDto> &result);
|
|
|
|
extern template int extractTotalRecordCount(const QList<DTO::BaseItemDto> &result);
|
|
|
|
extern template void setRequestLimit(Loader::GetLatestMediaParams ¶ms, int limit);
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetLatestMediaParams ¶ms, int offset);
|
2021-08-22 23:48:35 +00:00
|
|
|
extern template void setRequestLimit(Loader::GetItemsByUserIdParams ¶ms, int limit);
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetItemsByUserIdParams ¶ms, int offset);
|
|
|
|
extern template void setRequestLimit(Loader::GetResumeItemsParams ¶ms, int limit);
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetResumeItemsParams ¶ms, int offset);
|
2022-07-20 21:13:57 +00:00
|
|
|
extern template void setRequestLimit(Loader::GetPublicUsersParams ¶ms, int limit);
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetPublicUsersParams ¶ms, int offset);
|
2022-07-22 22:38:15 +00:00
|
|
|
extern template void setRequestLimit(Loader::GetNextUpParams ¶ms, int limit);
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetNextUpParams ¶ms, int offset);
|
2022-07-29 12:26:25 +00:00
|
|
|
extern template void setRequestLimit(Loader::GetAlbumArtistsParams ¶ms, int limit);
|
|
|
|
extern template bool setRequestStartIndex(Loader::GetAlbumArtistsParams ¶ms, int offset);
|
2021-07-31 13:06:17 +00:00
|
|
|
|
2021-08-17 22:04:27 +00:00
|
|
|
extern template QList<DTO::UserDto> extractRecords(const QList<DTO::UserDto> &result);
|
|
|
|
extern template int extractTotalRecordCount(const QList<DTO::UserDto> &result);
|
2021-07-31 13:06:17 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
/**
|
2021-03-29 15:10:25 +00:00
|
|
|
* Template for implementing a loader for the given type, response and parameters using Jellyfin::Support:Loaders.
|
|
|
|
*
|
2021-03-26 20:27:35 +00:00
|
|
|
* @tparam T type of which this loader should load a list of
|
|
|
|
* @tparam D type of the DTO which can be converted into T using T(const D&, ApiClient*);
|
|
|
|
* @tparam R type of the deserialized loader response
|
|
|
|
* @tparam P type of the deserialized loader request parameters
|
|
|
|
*/
|
|
|
|
template <class T, class D, class R, class P>
|
|
|
|
class LoaderModelLoader : public ModelLoader<T> {
|
|
|
|
public:
|
2021-03-28 02:00:00 +00:00
|
|
|
explicit LoaderModelLoader(Support::Loader<R, P> *loader, QObject *parent = nullptr)
|
|
|
|
: ModelLoader<T>(parent), m_loader(QScopedPointer<Support::Loader<R, P>>(loader)) {
|
2021-08-22 00:33:04 +00:00
|
|
|
this->connect(m_loader.data(), &Support::Loader<R, P>::ready, this, &LoaderModelLoader<T, D, R, P>::loaderReady);
|
|
|
|
this->connect(m_loader.data(), &Support::Loader<R, P>::error, this, &LoaderModelLoader<T, D, R, P>::loaderError);
|
2021-03-28 02:00:00 +00:00
|
|
|
}
|
2021-03-26 20:27:35 +00:00
|
|
|
protected:
|
2021-09-09 03:57:41 +00:00
|
|
|
void loadMore(ViewModel::ModelStatus suggestedModelStatus) override {
|
2021-03-28 02:00:00 +00:00
|
|
|
// This method should only be callable on one thread.
|
|
|
|
// If futureWatcher's future is running, this method should not be called again.
|
2021-08-22 00:33:04 +00:00
|
|
|
if (m_loader->isRunning()) {
|
|
|
|
m_loader->cancel();
|
|
|
|
}
|
2021-03-28 02:00:00 +00:00
|
|
|
// Set an invalid result.
|
2021-08-11 21:35:33 +00:00
|
|
|
this->m_result = { QList<T*>(), -1 };
|
2021-03-28 02:00:00 +00:00
|
|
|
|
2021-09-09 03:57:41 +00:00
|
|
|
if (!setRequestStartIndex<P>(this->m_parameters, this->m_startIndex)
|
2021-03-29 15:10:25 +00:00
|
|
|
&& suggestedModelStatus == ViewModel::ModelStatus::LoadingMore) {
|
|
|
|
// This loader's parameters does not setting a starting index,
|
|
|
|
// meaning loadMore is not supported.
|
|
|
|
return;
|
|
|
|
}
|
2021-09-09 03:57:41 +00:00
|
|
|
setRequestStartIndex<P>(this->m_parameters, this->m_startIndex);
|
2021-08-22 23:48:35 +00:00
|
|
|
|
2021-09-09 03:57:41 +00:00
|
|
|
if (suggestedModelStatus == ViewModel::ModelStatus::LoadingMore && this->m_explicitLimitSet) {
|
|
|
|
// If an explicit limit is set, we should load no more
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qCDebug(jellyfinApiModel) << "Explicit limit set: " << this->m_explicitLimitSet << ", " << this->m_limit;
|
|
|
|
if (this->m_explicitLimitSet) {
|
|
|
|
setRequestLimit<P>(this->m_parameters, this->m_limit);
|
2021-08-22 23:48:35 +00:00
|
|
|
} else {
|
|
|
|
setRequestLimit<P>(this->m_parameters, this->DEFAULT_LIMIT);
|
2021-03-29 21:48:16 +00:00
|
|
|
}
|
2021-03-29 15:10:25 +00:00
|
|
|
this->setStatus(suggestedModelStatus);
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
// We never want to set this while the loader is running, hence the Mutex and setting it here
|
|
|
|
// instead when Loader::setApiClient is called.
|
2021-03-28 02:00:00 +00:00
|
|
|
this->m_loader->setApiClient(this->m_apiClient);
|
|
|
|
this->m_loader->setParameters(this->m_parameters);
|
2021-08-22 00:33:04 +00:00
|
|
|
this->m_loader->load();
|
2021-03-28 02:00:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QScopedPointer<Support::Loader<R, P>> m_loader;
|
|
|
|
P m_parameters;
|
2021-03-29 15:10:25 +00:00
|
|
|
|
2021-08-22 00:33:04 +00:00
|
|
|
void loaderReady() {
|
2021-08-22 23:48:35 +00:00
|
|
|
try {
|
|
|
|
R result = m_loader->result();
|
|
|
|
QList<D> records = extractRecords<D, R>(result);
|
|
|
|
int totalRecordCount = extractTotalRecordCount<R>(result);
|
|
|
|
qDebug() << "Total record count: " << totalRecordCount << ", records in request: " << records.size();
|
|
|
|
// If totalRecordCount < 0, it is not supported for this endpoint
|
|
|
|
if (totalRecordCount < 0) {
|
|
|
|
totalRecordCount = records.size();
|
|
|
|
}
|
|
|
|
QList<T*> models;
|
|
|
|
models.reserve(records.size());
|
2021-03-26 20:27:35 +00:00
|
|
|
|
2021-08-22 23:48:35 +00:00
|
|
|
// Convert the DTOs into models
|
|
|
|
for (int i = 0; i < records.size(); i++) {
|
|
|
|
models.append(new T(records[i], m_loader->apiClient()));
|
|
|
|
}
|
|
|
|
this->setStatus(ViewModel::ModelStatus::Ready);
|
|
|
|
this->m_result = { models, this->m_startIndex};
|
|
|
|
this->m_startIndex += records.size();
|
|
|
|
this->m_totalRecordCount = totalRecordCount;
|
|
|
|
this->emitItemsLoaded();
|
|
|
|
|
|
|
|
} catch(QException &e) {
|
|
|
|
qWarning() << "Error while loading data: " << e.what();
|
|
|
|
this->setStatus(ViewModel::ModelStatus::Error);
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-28 02:00:00 +00:00
|
|
|
|
2021-08-22 00:33:04 +00:00
|
|
|
void loaderError(QString error) {
|
|
|
|
Q_UNUSED(error)
|
|
|
|
this->setStatus(ViewModel::ModelStatus::Error);
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class BaseApiModel : public QAbstractListModel {
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
|
|
|
BaseApiModel(QObject *parent = nullptr)
|
|
|
|
: QAbstractListModel (parent) {}
|
|
|
|
|
|
|
|
Q_PROPERTY(BaseModelLoader *loader READ loader WRITE setLoader NOTIFY loaderChanged)
|
|
|
|
|
|
|
|
virtual BaseModelLoader *loader() const = 0;
|
|
|
|
virtual void setLoader(BaseModelLoader *newLoader) {
|
|
|
|
connect(newLoader, &BaseModelLoader::reloadWanted, this, &BaseApiModel::reload);
|
2021-03-28 02:00:00 +00:00
|
|
|
connect(newLoader, &BaseModelLoader::modelShouldClear, this, &BaseApiModel::clear);
|
|
|
|
connect(newLoader, &BaseModelLoader::itemsLoaded, this, &BaseApiModel::loadingFinished);
|
2021-03-26 20:27:35 +00:00
|
|
|
emit loaderChanged();
|
|
|
|
};
|
2021-03-28 02:00:00 +00:00
|
|
|
void disconnectOldLoader(BaseModelLoader *oldLoader) {
|
|
|
|
if (oldLoader != nullptr) {
|
|
|
|
// Disconnect all signals
|
|
|
|
disconnect(oldLoader, nullptr, this, nullptr);
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 20:27:35 +00:00
|
|
|
public slots:
|
2021-03-28 02:00:00 +00:00
|
|
|
virtual void reload() = 0;
|
|
|
|
virtual void clear() = 0;
|
|
|
|
protected slots:
|
|
|
|
virtual void loadingFinished() = 0;
|
2021-03-26 20:27:35 +00:00
|
|
|
signals:
|
|
|
|
void loaderChanged();
|
|
|
|
};
|
2020-10-01 09:56:02 +00:00
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
/**
|
2021-08-22 00:33:04 +00:00
|
|
|
* @brief Abstract model for displaying collections.
|
2020-09-15 14:53:13 +00:00
|
|
|
*
|
2021-03-26 20:27:35 +00:00
|
|
|
* @tparam T The class of the result.
|
|
|
|
* @tparam R The class returned by the loader.
|
|
|
|
* @tparam P The class with the request parameters for the loader.
|
|
|
|
*
|
2020-09-15 14:53:13 +00:00
|
|
|
*/
|
2021-02-21 04:02:05 +00:00
|
|
|
template <class T>
|
2021-02-20 22:20:39 +00:00
|
|
|
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
|
|
|
*/
|
2021-03-26 20:27:35 +00:00
|
|
|
explicit ApiModel(QObject *parent = nullptr)
|
|
|
|
: BaseApiModel(parent) {
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2021-02-20 22:20:39 +00:00
|
|
|
// QList-like API
|
2021-08-11 21:35:33 +00:00
|
|
|
QSharedPointer<T> at(int index) const { return m_array.at(index); }
|
2021-03-26 20:27:35 +00:00
|
|
|
/**
|
|
|
|
* @return the amount of objects in this model.
|
|
|
|
*/
|
2021-07-31 13:06:17 +00:00
|
|
|
int size() const {
|
2021-03-26 20:27:35 +00:00
|
|
|
return m_array.size();
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:35:33 +00:00
|
|
|
void insert(int index, QSharedPointer<T> object) {
|
2021-03-26 20:27:35 +00:00
|
|
|
Q_ASSERT(index >= 0 && index <= size());
|
|
|
|
this->beginInsertRows(QModelIndex(), index, index);
|
|
|
|
m_array.insert(index, object);
|
|
|
|
this->endInsertRows();
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:35:33 +00:00
|
|
|
void append(QSharedPointer<T> object) { insert(size(), object); }
|
|
|
|
//void append(T &object) { insert(size(), object); }
|
|
|
|
void append(QList<QSharedPointer<T>> &objects) {
|
2021-03-26 20:27:35 +00:00
|
|
|
int index = size();
|
2021-03-29 15:10:25 +00:00
|
|
|
this->beginInsertRows(QModelIndex(), index, index + objects.size() - 1);
|
2021-03-26 20:27:35 +00:00
|
|
|
m_array.append(objects);
|
|
|
|
this->endInsertRows();
|
|
|
|
};
|
|
|
|
|
2021-08-11 21:35:33 +00:00
|
|
|
QList<T*> mid(int pos, int length = -1) {
|
2021-05-21 13:46:30 +00:00
|
|
|
return m_array.mid(pos, length);
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void removeAt(int index) {
|
2021-03-28 02:00:00 +00:00
|
|
|
Q_ASSERT(index < size());
|
2021-03-26 20:27:35 +00:00
|
|
|
this->beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
m_array.removeAt(index);
|
|
|
|
this->endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
void removeUntilEnd(int from) {
|
|
|
|
this->beginRemoveRows(QModelIndex(), from , m_array.size());
|
|
|
|
while (m_array.size() != from) {
|
|
|
|
m_array.removeLast();
|
|
|
|
}
|
|
|
|
this->endRemoveRows();
|
|
|
|
}
|
|
|
|
|
2021-08-11 21:35:33 +00:00
|
|
|
void removeOne(QSharedPointer<T> object) {
|
2021-03-26 20:27:35 +00:00
|
|
|
int idx = m_array.indexOf(object);
|
|
|
|
if (idx >= 0) {
|
|
|
|
removeAt(idx);
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 01:14:05 +00:00
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
void clear() override {
|
|
|
|
this->beginResetModel();
|
|
|
|
m_array.clear();
|
|
|
|
this->endResetModel();
|
|
|
|
}
|
|
|
|
|
core: Split PlaybackManager up into smaller parts
The PlaybackManager was a giant class that handled UI bindings, fetching
stream URLS, playback logic.
It now has been split up into:
- ViewModel::PlaybackManager, which handles UI interfacing and allowing
to swap out the Model::Playback implementation on the fly.
- Model::PlaybackManager, which is an interface for what a
PlaybackManager must do, handling queues/playlists, and controlling a
player.
- Model::LocalPlaybackManager, which is an Model::PlaybackManager
implementation for playing back Jellyfin media within the application.
- Model::PlaybackReporter, which reports the current playback state to
the Jellyfin server, for keeping track of played items.
- Model::Player, which handles playing back media from an URL and
the usual play/pause et cetera.
In a future commit, this would allow for introducing a
Model::RemoteJellyfinPlaybackManager, to control other Jellyfin
instances.
2022-01-05 20:24:52 +00:00
|
|
|
const QList<QSharedPointer<T>> &toList() {
|
|
|
|
return m_array;
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
// From AbstractListModel, gets implemented in ApiModel<T>
|
|
|
|
//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 {
|
|
|
|
if (parent.isValid()) return false;
|
|
|
|
if (m_loader == nullptr) return false;
|
|
|
|
return m_loader->canLoadMore();
|
|
|
|
}
|
|
|
|
virtual void fetchMore(const QModelIndex &parent) override {
|
|
|
|
if (parent.isValid()) return;
|
|
|
|
if (m_loader != nullptr) {
|
2021-03-28 02:00:00 +00:00
|
|
|
m_loader->loadMore();
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-27 01:14:05 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
BaseModelLoader *loader() const override {
|
|
|
|
return m_loader;
|
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void setLoader(BaseModelLoader *newLoader) {
|
|
|
|
ModelLoader<T> *castedLoader = dynamic_cast<ModelLoader<T> *>(newLoader);
|
|
|
|
if (castedLoader != nullptr) {
|
|
|
|
// Hacky way to emit a signal
|
|
|
|
BaseApiModel::setLoader(newLoader);
|
2021-03-28 02:00:00 +00:00
|
|
|
BaseApiModel::disconnectOldLoader(m_loader);
|
|
|
|
m_loader = castedLoader;
|
2021-03-26 20:27:35 +00:00
|
|
|
} else {
|
|
|
|
qWarning() << "Somehow set a BaseModelLoader on ApiModel instead of a ModelLoader<T>";
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void reload() override {
|
|
|
|
if (m_loader != nullptr) {
|
2021-03-28 02:00:00 +00:00
|
|
|
m_loader->reload();
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
protected:
|
|
|
|
// Model-specific properties.
|
2021-08-11 21:35:33 +00:00
|
|
|
QList<QSharedPointer<T>> m_array;
|
2021-03-28 02:00:00 +00:00
|
|
|
ModelLoader<T> *m_loader = nullptr;
|
|
|
|
|
|
|
|
void loadingFinished() override {
|
|
|
|
Q_ASSERT(m_loader != nullptr);
|
2021-08-11 21:35:33 +00:00
|
|
|
std::pair<QList<T*>, int> result = m_loader->result();
|
2021-03-29 15:10:25 +00:00
|
|
|
qDebug() << "Results loaded: index: " << result.second << ", count: " << result.first.size();
|
2021-03-28 02:00:00 +00:00
|
|
|
if (result.second == -1) {
|
|
|
|
clear();
|
|
|
|
} else if (result.second == m_array.size()) {
|
2021-08-11 21:35:33 +00:00
|
|
|
m_array.reserve(m_array.size() + result.second);
|
|
|
|
for (auto it = result.first.begin(); it != result.first.end(); it++) {
|
|
|
|
append(QSharedPointer<T>(*it));
|
|
|
|
}
|
2021-03-28 02:00:00 +00:00
|
|
|
} else if (result.second < m_array.size()){
|
|
|
|
removeUntilEnd(result.second);
|
2021-08-11 21:35:33 +00:00
|
|
|
m_array.reserve(m_array.size() + result.second);
|
|
|
|
for (auto it = result.first.begin(); it != result.first.end(); it++) {
|
|
|
|
append(QSharedPointer<T>(*it));
|
|
|
|
}
|
2021-03-28 02:00:00 +00:00
|
|
|
} else {
|
|
|
|
// result.second > m_array.size()
|
|
|
|
qWarning() << "Retrieved data from loader at position bigger than size()";
|
2021-03-26 20:27:35 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
private:
|
2021-03-26 20:27:35 +00:00
|
|
|
QMetaObject::Connection m_futureWatcherConnection;
|
2020-09-15 14:53:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif //JELLYFIN_API_MODEL
|