1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2024-05-17 03:22:42 +00:00
harbour-sailfin/core/src/apimodel.cpp

160 lines
4.3 KiB
C++
Raw Normal View History

/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2021 Chris Josten
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
*/
#define JELLYFIN_APIMODEL_CPP
#include "JellyfinQt/apimodel.h"
#include "JellyfinQt/dto/baseitemdto.h"
#include "JellyfinQt/dto/useritemdatadto.h"
#include "JellyfinQt/dto/userdto.h"
namespace Jellyfin {
// BaseApiModel
namespace DTO {
using UserData = DTO::UserItemDataDto;
using User = DTO::UserDto;
}
using User = DTO::UserDto;
2021-03-26 20:27:35 +00:00
BaseModelLoader::BaseModelLoader(QObject *parent)
: QObject(parent) { }
2021-03-26 20:27:35 +00:00
void BaseModelLoader::classBegin() {
m_isBeingParsed = true;
}
2021-03-26 20:27:35 +00:00
void BaseModelLoader::componentComplete() {
m_isBeingParsed = false;
2021-03-26 20:27:35 +00:00
autoReloadIfNeeded();
}
2021-03-26 20:27:35 +00:00
void BaseModelLoader::autoReloadIfNeeded() {
if (m_autoReload && canReload()) {
qDebug() << "reloading due to 'autoReloadIfNeeded()'";
2021-03-26 20:27:35 +00:00
emit reloadWanted();
}
}
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-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);
}
}
2021-03-26 20:27:35 +00:00
void BaseModelLoader::setAutoReload(bool newAutoReload) {
if (m_autoReload != newAutoReload) {
m_autoReload = newAutoReload;
emit autoReloadChanged(newAutoReload);
if (canReload()) {
reload();
}
2020-09-27 01:14:05 +00:00
}
}
bool BaseModelLoader::canReload() const {
return m_apiClient != nullptr
2021-03-29 21:48:16 +00:00
&& !m_isBeingParsed
// 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-26 20:27:35 +00:00
void BaseApiModel::reload() {
qWarning() << " BaseApiModel slot called instead of overloaded method";
}
2021-03-29 21:48:16 +00:00
// Parameters injectors and result extractors
template <>
bool setRequestStartIndex(Loader::GetUserViewsParams &params, int startIndex) {
2021-03-26 20:27:35 +00:00
// Not supported
Q_UNUSED(params)
Q_UNUSED(startIndex)
return false;
}
template <>
void setRequestLimit(Loader::GetUserViewsParams &params, int limit) {
2021-03-26 20:27:35 +00:00
Q_UNUSED(params)
Q_UNUSED(limit)
}
template <>
2021-03-26 20:27:35 +00:00
QList<DTO::BaseItemDto> extractRecords(const DTO::BaseItemDtoQueryResult &result) {
return result.items();
}
template <>
2021-03-26 20:27:35 +00:00
int extractTotalRecordCount(const DTO::BaseItemDtoQueryResult &result) {
return result.totalRecordCount();
}
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 &params, int limit) {
params.setLimit(limit);
}
template<>
bool setRequestStartIndex(Loader::GetLatestMediaParams &params, int offset) {
Q_UNUSED(params)
Q_UNUSED(offset)
return false;
}
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
void registerModels(const char *URI) {
2021-03-26 20:27:35 +00:00
Q_UNUSED(URI)
//qmlRegisterUncreatableType<ApiModel>(URI, 1, 0, "ApiModel", "Is enum and base class");
//qmlRegisterType<PublicUserModel>(URI, 1, 0, "PublicUserModel");
}
}