mirror of
https://github.com/HenkKalkwater/harbour-sailfin.git
synced 2025-09-01 08:52:45 +00:00
WIP: logic rewrite
WIP: adding loaders
This commit is contained in:
parent
b9b08ab384
commit
2360b261f7
1769 changed files with 124903 additions and 1963 deletions
|
@ -1,23 +1,48 @@
|
|||
/*
|
||||
* Sailfin: a Jellyfin client written using Qt
|
||||
* Copyright (C) 2021 Chris Josten and the Sailfin Contributors.
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
#ifndef JELLYFIN_SUPPORT_LOADER_H
|
||||
#define JELLYFIN_SUPPORT_LOADER_H
|
||||
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <initializer_list>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
#include <QException>
|
||||
#include <QJsonDocument>
|
||||
#include <QUrlQuery>
|
||||
#include <QString>
|
||||
|
||||
#include <JellyfinQt/apiclient.h>
|
||||
#include "../apiclient.h"
|
||||
#include "jsonconv.h"
|
||||
|
||||
namespace Jellfyin {
|
||||
namespace Jellyfin {
|
||||
namespace Support {
|
||||
|
||||
|
||||
class LoadException : public std::runtime_error {
|
||||
class LoadException : public QException {
|
||||
public:
|
||||
explicit LoadException(const char *message)
|
||||
: std::runtime_error(message) {}
|
||||
explicit LoadException(const QString &message)
|
||||
: m_message(message.toStdString()) {}
|
||||
virtual const char *what() const noexcept override;
|
||||
private:
|
||||
std::string m_message;
|
||||
};
|
||||
|
||||
static const int HTTP_TIMEOUT = 30000; // 30 seconds;
|
||||
|
@ -33,14 +58,17 @@ static const int HTTP_TIMEOUT = 30000; // 30 seconds;
|
|||
template <typename R, typename P>
|
||||
class Loader {
|
||||
public:
|
||||
explicit Loader(Jellyfin::ApiClient *apiClient)
|
||||
explicit Loader(ApiClient *apiClient)
|
||||
: m_apiClient(apiClient) {}
|
||||
/**
|
||||
* @brief load Loads the given resource.
|
||||
* @param parameters Parameters to determine which resource should be loaded.
|
||||
* @return The resource if successfull.
|
||||
*/
|
||||
virtual R load(const P ¶meters) const;
|
||||
virtual std::optional<R> load(const P ¶meters) const {
|
||||
Q_UNUSED(parameters)
|
||||
throw new LoadException(QStringLiteral("Loader not set"));
|
||||
}
|
||||
/**
|
||||
* @brief Heuristic to determine if this resource can be loaded via this loaded.
|
||||
*
|
||||
|
@ -53,6 +81,36 @@ protected:
|
|||
Jellyfin::ApiClient *m_apiClient;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implementation of a Loader that tries multiple loaders.
|
||||
*/
|
||||
template <typename R, typename P>
|
||||
class MultipleChoiceLoader : public Loader<R, P> {
|
||||
using L = Loader<R, P>;
|
||||
public:
|
||||
explicit MultipleChoiceLoader(ApiClient *apiClient,
|
||||
std::initializer_list<L> loaders)
|
||||
: L(apiClient), m_loaders(loaders) {}
|
||||
|
||||
virtual std::optional<R> load(const P ¶meters) const override {
|
||||
for(auto it = m_loaders.begin(); it != m_loaders.end(); it++) {
|
||||
if (it->isAvailable()) {
|
||||
try {
|
||||
std::optional<R> res = it->load(parameters);
|
||||
if (res.has_value()) return res;
|
||||
} catch (LoadException e) {
|
||||
qDebug() << "Error while loading: " << e.what();
|
||||
}
|
||||
}
|
||||
}
|
||||
qDebug() << "No loaders were able to fulfill the request";
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
QList<L> m_loaders;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implementation of Loader that loads Items over HTTP
|
||||
*/
|
||||
|
@ -62,17 +120,19 @@ public:
|
|||
explicit HttpLoader(Jellyfin::ApiClient *apiClient)
|
||||
: Loader<R, P> (apiClient) {}
|
||||
|
||||
virtual R load(const P ¶meters) const override {
|
||||
virtual std::optional<R> load(const P ¶meters) const override {
|
||||
QNetworkReply *reply = this->m_apiClient->get(url(parameters), query(parameters));
|
||||
QByteArray array;
|
||||
while (!reply->atEnd()) {
|
||||
if (!reply->waitForReadyRead(HTTP_TIMEOUT)) {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
reply->deleteLater();
|
||||
throw LoadException("HTTP timeout");
|
||||
//: Loading from a resource failed due to the server taking too long to respond
|
||||
throw LoadException(QObject::tr("Network timeout"));
|
||||
}
|
||||
reply->deleteLater();
|
||||
throw LoadException("HTTP error");
|
||||
//: An HTTP has occurred. First argument is replaced by QNetworkReply->errorString()
|
||||
throw LoadException(QObject::tr("HTTP error: %1").arg(reply->errorString()));
|
||||
}
|
||||
array.append(reply->readAll());
|
||||
}
|
||||
|
@ -82,12 +142,11 @@ public:
|
|||
if (error.error != QJsonParseError::NoError) {
|
||||
throw LoadException(error.errorString().toLocal8Bit().constData());
|
||||
}
|
||||
return parse(document);
|
||||
return fromJsonValue<R>(QJsonValue(document.object()));
|
||||
}
|
||||
protected:
|
||||
virtual const QString url(const P ¶meters) const;
|
||||
virtual const QUrlQuery query(const P ¶meters) const;
|
||||
virtual R parse(const QJsonObject &object) const;
|
||||
virtual QString url(const P ¶meters) const;
|
||||
virtual QUrlQuery query(const P ¶meters) const;
|
||||
};
|
||||
|
||||
} // NS Support
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue