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
|
|
|
|
*/
|
2021-07-31 13:06:17 +00:00
|
|
|
#define JELLYFIN_APIMODEL_CPP
|
2021-02-17 18:42:10 +00:00
|
|
|
#include "JellyfinQt/apimodel.h"
|
|
|
|
|
2021-03-29 12:27:37 +00:00
|
|
|
#include "JellyfinQt/dto/baseitemdto.h"
|
|
|
|
#include "JellyfinQt/dto/useritemdatadto.h"
|
|
|
|
#include "JellyfinQt/dto/userdto.h"
|
2020-09-15 14:53:13 +00:00
|
|
|
|
|
|
|
namespace Jellyfin {
|
2021-02-20 22:20:39 +00:00
|
|
|
|
|
|
|
// BaseApiModel
|
2021-02-21 04:02:05 +00:00
|
|
|
namespace DTO {
|
|
|
|
using UserData = DTO::UserItemDataDto;
|
|
|
|
using User = DTO::UserDto;
|
|
|
|
}
|
|
|
|
using User = DTO::UserDto;
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
BaseModelLoader::BaseModelLoader(QObject *parent)
|
|
|
|
: QObject(parent) { }
|
2021-02-20 22:20:39 +00:00
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseModelLoader::classBegin() {
|
2021-02-20 22:20:39 +00:00
|
|
|
m_isBeingParsed = true;
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseModelLoader::componentComplete() {
|
2021-02-20 22:20:39 +00:00
|
|
|
m_isBeingParsed = false;
|
2021-03-26 20:27:35 +00:00
|
|
|
autoReloadIfNeeded();
|
2021-02-20 22:20:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseModelLoader::autoReloadIfNeeded() {
|
2021-03-28 02:00:00 +00:00
|
|
|
if (m_autoReload && canReload()) {
|
2021-03-29 15:10:25 +00:00
|
|
|
qDebug() << "reloading due to 'autoReloadIfNeeded()'";
|
2021-03-26 20:27:35 +00:00
|
|
|
emit reloadWanted();
|
2021-02-20 22:20:39 +00:00
|
|
|
}
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseModelLoader::setApiClient(ApiClient *newApiClient) {
|
|
|
|
bool changed = this->m_apiClient != newApiClient;
|
|
|
|
m_apiClient = newApiClient;
|
|
|
|
if (changed) {
|
|
|
|
emit apiClientChanged(newApiClient);
|
2021-02-20 22:20:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseModelLoader::setLimit(int newLimit) {
|
|
|
|
int oldLimit = this->m_limit;
|
|
|
|
m_limit = newLimit;
|
2021-03-29 21:48:16 +00:00
|
|
|
if (oldLimit != newLimit) {
|
2021-03-26 20:27:35 +00:00
|
|
|
emit limitChanged(this->m_limit);
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseModelLoader::setAutoReload(bool newAutoReload) {
|
|
|
|
if (m_autoReload != newAutoReload) {
|
|
|
|
m_autoReload = newAutoReload;
|
|
|
|
emit autoReloadChanged(newAutoReload);
|
2021-08-11 21:35:33 +00:00
|
|
|
|
|
|
|
if (canReload()) {
|
|
|
|
reload();
|
|
|
|
}
|
2020-09-27 01:14:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-28 02:00:00 +00:00
|
|
|
bool BaseModelLoader::canReload() const {
|
2021-03-29 15:10:25 +00:00
|
|
|
return m_apiClient != nullptr
|
2021-03-29 21:48:16 +00:00
|
|
|
&& !m_isBeingParsed
|
2021-03-29 15:10:25 +00:00
|
|
|
// If the loader for this model needs authentication (almost every one does)
|
|
|
|
// block if the ApiClient is not authenticated yet.
|
|
|
|
&& (!m_needsAuthentication || m_apiClient->authenticated())
|
|
|
|
// Only allow for a reload if this model is ready or uninitialised.
|
|
|
|
&& (m_status == ViewModel::ModelStatus::Ready
|
|
|
|
|| m_status == ViewModel::ModelStatus::Uninitialised);
|
2021-03-28 02:00:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 20:27:35 +00:00
|
|
|
void BaseApiModel::reload() {
|
|
|
|
qWarning() << " BaseApiModel slot called instead of overloaded method";
|
2021-02-20 22:20:39 +00:00
|
|
|
}
|
2020-10-09 00:33:08 +00:00
|
|
|
|
2021-03-29 21:48:16 +00:00
|
|
|
// Parameters injectors and result extractors
|
|
|
|
|
2021-03-29 15:10:25 +00:00
|
|
|
template <>
|
|
|
|
bool setRequestStartIndex(Loader::GetUserViewsParams ¶ms, int startIndex) {
|
2021-03-26 20:27:35 +00:00
|
|
|
// Not supported
|
|
|
|
Q_UNUSED(params)
|
|
|
|
Q_UNUSED(startIndex)
|
2021-03-29 15:10:25 +00:00
|
|
|
return false;
|
2020-10-09 00:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:10:25 +00:00
|
|
|
template <>
|
|
|
|
void setRequestLimit(Loader::GetUserViewsParams ¶ms, int limit) {
|
2021-03-26 20:27:35 +00:00
|
|
|
Q_UNUSED(params)
|
|
|
|
Q_UNUSED(limit)
|
2020-10-09 00:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:10:25 +00:00
|
|
|
template <>
|
2021-03-26 20:27:35 +00:00
|
|
|
QList<DTO::BaseItemDto> extractRecords(const DTO::BaseItemDtoQueryResult &result) {
|
|
|
|
return result.items();
|
2021-02-20 22:20:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 15:10:25 +00:00
|
|
|
template <>
|
2021-03-26 20:27:35 +00:00
|
|
|
int extractTotalRecordCount(const DTO::BaseItemDtoQueryResult &result) {
|
|
|
|
return result.totalRecordCount();
|
2021-02-20 22:20:39 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:48:16 +00:00
|
|
|
template <>
|
|
|
|
QList<DTO::BaseItemDto> extractRecords(const QList<DTO::BaseItemDto> &result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
int extractTotalRecordCount(const QList<DTO::BaseItemDto> &result) {
|
|
|
|
return result.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void setRequestLimit(Loader::GetLatestMediaParams ¶ms, int limit) {
|
|
|
|
params.setLimit(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
bool setRequestStartIndex(Loader::GetLatestMediaParams ¶ms, int offset) {
|
|
|
|
Q_UNUSED(params)
|
|
|
|
Q_UNUSED(offset)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-22 23:48:35 +00:00
|
|
|
template<>
|
|
|
|
void setRequestLimit(Loader::GetItemsByUserIdParams ¶ms, int limit) {
|
|
|
|
params.setLimit(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
bool setRequestStartIndex(Loader::GetItemsByUserIdParams ¶ms, int index) {
|
|
|
|
params.setLimit(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
void setRequestLimit(Loader::GetResumeItemsParams ¶ms, int limit) {
|
|
|
|
params.setLimit(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
bool setRequestStartIndex(Loader::GetResumeItemsParams ¶ms, int index) {
|
|
|
|
params.setLimit(index);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-17 22:04:27 +00:00
|
|
|
template<>
|
|
|
|
QList<DTO::UserDto> extractRecords(const QList<DTO::UserDto> &result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
|
|
|
int extractTotalRecordCount(const QList<DTO::UserDto> &result) {
|
|
|
|
return result.size();
|
|
|
|
}
|
2020-09-27 01:14:05 +00:00
|
|
|
|
2020-09-15 14:53:13 +00:00
|
|
|
}
|