mirror of
https://github.com/HenkKalkwater/harbour-sailfin.git
synced 2025-09-01 08:52:45 +00:00
WIP: Add playlists/queues and add support for Sailfish back
This commit is contained in:
parent
fbc154fb56
commit
86672be051
89 changed files with 1637 additions and 849 deletions
|
@ -19,159 +19,31 @@
|
|||
#ifndef JELLYFIN_SUPPORT_JSONCONV_H
|
||||
#define JELLYFIN_SUPPORT_JSONCONV_H
|
||||
|
||||
#include <QException>
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QDateTime>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QList>
|
||||
#include <QUuid>
|
||||
#include "jsonconvimpl.h"
|
||||
#include "parseexception.h"
|
||||
|
||||
namespace Jellyfin {
|
||||
namespace Support {
|
||||
|
||||
// Helper functions
|
||||
QString uuidToString(const QUuid &source);
|
||||
QUuid stringToUuid(const QString &source);
|
||||
|
||||
/**
|
||||
* @brief Thrown when JSON cannot be parsed.
|
||||
*/
|
||||
class ParseException : public QException {
|
||||
public:
|
||||
explicit ParseException(const QString &message)
|
||||
: m_message(message.toStdString()) {}
|
||||
|
||||
/*explicit ParseException(const ParseException &other)
|
||||
: m_message(other.m_message) {}*/
|
||||
|
||||
virtual const char *what() const noexcept override;
|
||||
|
||||
virtual QException *clone() const override;
|
||||
virtual void raise() const override;
|
||||
private:
|
||||
std::string m_message;
|
||||
};
|
||||
|
||||
// https://www.fluentcpp.com/2017/08/15/function-templates-partial-specialization-cpp/
|
||||
template <typename T>
|
||||
struct convertType{};
|
||||
|
||||
/**
|
||||
* Template for converting types from JSON into their respective type.
|
||||
*/
|
||||
template <typename T>
|
||||
T fromJsonValue(const QJsonValue &source, convertType<T>) {
|
||||
Q_UNUSED(source)
|
||||
Q_ASSERT_X(false, "fromJsonValue<T>", "fromJsonValue called with unimplemented type");
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const T &source, convertType<T>) {
|
||||
Q_UNUSED(source)
|
||||
std::string msg = "toJsonValue called with unimplemented type ";
|
||||
msg += typeid (T).name();
|
||||
Q_ASSERT_X(false, "toJsonValue<T>", msg.c_str());
|
||||
return QJsonValue();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T fromJsonValue(const QJsonValue &source) {
|
||||
return fromJsonValue(source, convertType<T>{});
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
QJsonValue toJsonValue(const T &source) {
|
||||
return toJsonValue(source, convertType<T>{});
|
||||
}
|
||||
|
||||
// QList
|
||||
template <typename T>
|
||||
QList<T> fromJsonValue(const QJsonValue &source, convertType<QList<T>>) {
|
||||
QList<T> result;
|
||||
QJsonArray arr = source.toArray();
|
||||
result.reserve(arr.size());
|
||||
for (auto it = arr.cbegin(); it != arr.cend(); it++) {
|
||||
result.append(fromJsonValue<T>(*it));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const QList<T> &source, convertType<QList<T>>) {
|
||||
QJsonArray result;
|
||||
for (auto it = source.cbegin(); it != source.cend(); it++) {
|
||||
result.push_back(toJsonValue<T>(*it));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Optional
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> fromJsonValue(const QJsonValue &source, convertType<std::optional<T>>) {
|
||||
if (source.isNull()) {
|
||||
return std::nullopt;
|
||||
} else {
|
||||
return fromJsonValue<T>(source, convertType<T>{});
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const std::optional<T> &source, convertType<std::optional<T>>) {
|
||||
if (source.has_value()) {
|
||||
return toJsonValue<T>(source.value(), convertType<T>{});
|
||||
} else {
|
||||
// Null
|
||||
return QJsonValue();
|
||||
}
|
||||
}
|
||||
|
||||
// QSharedPointer
|
||||
template <typename T>
|
||||
QSharedPointer<T> fromJsonValue(const QJsonValue &source, convertType<QSharedPointer<T>>) {
|
||||
if (source.isNull()) {
|
||||
return QSharedPointer<T>();
|
||||
}
|
||||
return QSharedPointer<T>::create(fromJsonValue<T>(source));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const QSharedPointer<T> &source, convertType<QSharedPointer<T>>) {
|
||||
if (source.isNull()) {
|
||||
return QJsonValue();
|
||||
}
|
||||
return toJsonValue<T>(*source);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Templates for string conversion.
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
QString toString(const T &source, convertType<T>) {
|
||||
return toJsonValue(source).toString();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QString toString(const std::optional<T> &source, convertType<std::optional<T>>) {
|
||||
if (source.has_value()) {
|
||||
return toString<T>(source.value(), convertType<T>{});
|
||||
} else {
|
||||
return QString();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QString toString(const T &source) {
|
||||
return toString(source, convertType<T>{});
|
||||
}
|
||||
extern template int fromJsonValue<int>(const QJsonValue &source, convertType<int>);
|
||||
extern template qint64 fromJsonValue<qint64>(const QJsonValue &source, convertType<qint64>);
|
||||
extern template bool fromJsonValue<bool>(const QJsonValue &source, convertType<bool>);
|
||||
extern template QString fromJsonValue<QString>(const QJsonValue &source, convertType<QString>);
|
||||
extern template QStringList fromJsonValue<QStringList>(const QJsonValue &source, convertType<QStringList>);
|
||||
extern template QJsonObject fromJsonValue<QJsonObject>(const QJsonValue &source, convertType<QJsonObject>);
|
||||
extern template double fromJsonValue<double>(const QJsonValue &source, convertType<double>);
|
||||
extern template float fromJsonValue<float>(const QJsonValue &source, convertType<float>);
|
||||
extern template QDateTime fromJsonValue<QDateTime>(const QJsonValue &source, convertType<QDateTime>);
|
||||
extern template QVariant fromJsonValue<QVariant>(const QJsonValue &source, convertType<QVariant>);
|
||||
extern template QUuid fromJsonValue<QUuid>(const QJsonValue &source, convertType<QUuid>);
|
||||
|
||||
extern template QString toString(const QUuid &source, convertType<QUuid>);
|
||||
extern template QString toString(const qint32 &source, convertType<qint32>);
|
||||
extern template QString toString(const qint64 &source, convertType<qint64>);
|
||||
extern template QString toString(const float &source, convertType<float>);
|
||||
extern template QString toString(const double &source, convertType<double>);
|
||||
extern template QString toString(const bool &source, convertType<bool>);
|
||||
extern template QString toString(const QString &source, convertType<QString>);
|
||||
|
||||
} // NS Support
|
||||
} // NS Jellyfin
|
||||
|
|
165
core/include/JellyfinQt/support/jsonconvimpl.h
Normal file
165
core/include/JellyfinQt/support/jsonconvimpl.h
Normal file
|
@ -0,0 +1,165 @@
|
|||
/*
|
||||
* 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_JSONCONVIMPL_H
|
||||
#define JELLYFIN_SUPPORT_JSONCONVIMPL_H
|
||||
|
||||
#include <QtGlobal>
|
||||
#include <QDateTime>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonObject>
|
||||
#include <QJsonValue>
|
||||
#include <QList>
|
||||
#include <QSharedPointer>
|
||||
#include <QUuid>
|
||||
#include <QVariant>
|
||||
|
||||
#include <optional>
|
||||
|
||||
namespace Jellyfin {
|
||||
namespace Support {
|
||||
|
||||
// Helper functions
|
||||
//QString uuidToString(const QUuid &source);
|
||||
//QUuid stringToUuid(const QString &source);
|
||||
|
||||
|
||||
// https://www.fluentcpp.com/2017/08/15/function-templates-partial-specialization-cpp/
|
||||
template <typename T>
|
||||
struct convertType{};
|
||||
|
||||
/**
|
||||
* Template for converting types from JSON into their respective type.
|
||||
*/
|
||||
template <typename T>
|
||||
T fromJsonValue(const QJsonValue &source, convertType<T>) {
|
||||
Q_UNUSED(source)
|
||||
Q_ASSERT_X(false, "fromJsonValue<T>", "fromJsonValue called with unimplemented type");
|
||||
}
|
||||
|
||||
/**
|
||||
* Template for converting types from their type to JSON.
|
||||
*/
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const T &source, convertType<T>) {
|
||||
Q_UNUSED(source)
|
||||
std::string msg = "toJsonValue called with unimplemented type ";
|
||||
msg += typeid (T).name();
|
||||
Q_ASSERT_X(false, "toJsonValue<T>", msg.c_str());
|
||||
return QJsonValue();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T fromJsonValue(const QJsonValue &source) {
|
||||
return fromJsonValue(source, convertType<T>{});
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
QJsonValue toJsonValue(const T &source) {
|
||||
return toJsonValue(source, convertType<T>{});
|
||||
}
|
||||
|
||||
// QList
|
||||
template <typename T>
|
||||
QList<T> fromJsonValue(const QJsonValue &source, convertType<QList<T>>) {
|
||||
QList<T> result;
|
||||
QJsonArray arr = source.toArray();
|
||||
result.reserve(arr.size());
|
||||
for (auto it = arr.constBegin(); it != arr.constEnd(); it++) {
|
||||
result.append(fromJsonValue<T>(*it));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const QList<T> &source, convertType<QList<T>>) {
|
||||
QJsonArray result;
|
||||
for (auto it = source.cbegin(); it != source.cend(); it++) {
|
||||
result.push_back(toJsonValue<T>(*it));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Optional
|
||||
|
||||
template <typename T>
|
||||
std::optional<T> fromJsonValue(const QJsonValue &source, convertType<std::optional<T>>) {
|
||||
if (source.isNull()) {
|
||||
return std::nullopt;
|
||||
} else {
|
||||
return fromJsonValue<T>(source, convertType<T>{});
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const std::optional<T> &source, convertType<std::optional<T>>) {
|
||||
if (source.has_value()) {
|
||||
return toJsonValue<T>(source.value(), convertType<T>{});
|
||||
} else {
|
||||
// Null
|
||||
return QJsonValue();
|
||||
}
|
||||
}
|
||||
|
||||
// QSharedPointer
|
||||
template <typename T>
|
||||
QSharedPointer<T> fromJsonValue(const QJsonValue &source, convertType<QSharedPointer<T>>) {
|
||||
if (source.isNull()) {
|
||||
return QSharedPointer<T>();
|
||||
}
|
||||
return QSharedPointer<T>::create(fromJsonValue<T>(source));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QJsonValue toJsonValue(const QSharedPointer<T> &source, convertType<QSharedPointer<T>>) {
|
||||
if (source.isNull()) {
|
||||
return QJsonValue();
|
||||
}
|
||||
return toJsonValue<T>(*source);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Templates for string conversion.
|
||||
*/
|
||||
|
||||
template <typename T>
|
||||
QString toString(const T &source, convertType<T>) {
|
||||
return toJsonValue(source).toString();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QString toString(const std::optional<T> &source, convertType<std::optional<T>>) {
|
||||
if (source.has_value()) {
|
||||
return toString<T>(source.value(), convertType<T>{});
|
||||
} else {
|
||||
return QString();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
QString toString(const T &source) {
|
||||
return toString(source, convertType<T>{});
|
||||
}
|
||||
|
||||
|
||||
} // NS Support
|
||||
} // NS Jellyfin
|
||||
|
||||
#endif // JELLYFIN_SUPPORT_JSONCONVIMPL_H
|
50
core/include/JellyfinQt/support/parseexception.h
Normal file
50
core/include/JellyfinQt/support/parseexception.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
/*
|
||||
* 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_PARSEEXCEPTION_H
|
||||
#define JELLYFIN_SUPPORT_PARSEEXCEPTION_H
|
||||
|
||||
#include <QException>
|
||||
#include <QString>
|
||||
|
||||
namespace Jellyfin {
|
||||
namespace Support {
|
||||
|
||||
/**
|
||||
* @brief Thrown when JSON cannot be parsed.
|
||||
*/
|
||||
class ParseException : public QException {
|
||||
public:
|
||||
explicit ParseException(const QString &message)
|
||||
: m_message(message.toStdString()) {}
|
||||
|
||||
/*explicit ParseException(const ParseException &other)
|
||||
: m_message(other.m_message) {}*/
|
||||
|
||||
virtual const char *what() const noexcept override;
|
||||
|
||||
virtual QException *clone() const override;
|
||||
virtual void raise() const override;
|
||||
private:
|
||||
std::string m_message;
|
||||
};
|
||||
|
||||
} // NS Support
|
||||
} // NS Jellyfin
|
||||
|
||||
#endif // JELLYFIN_SUPPORT_PARSEEXCEPTION_H
|
Loading…
Add table
Add a link
Reference in a new issue