1
0
Fork 0
mirror of https://github.com/HenkKalkwater/harbour-sailfin.git synced 2025-09-05 18:22:46 +00:00

Big C++ refractor

- Removed "jellyfin" prefix from files, as they are already in a
  directory named Jellyfin
- Split the former "jellyfinitem.{h,cpp}" into multiple files in the DTO
  directory, one for each class. The jellyfinitem files started to
  become enormous.
- Use forward declarations in headers instead of including files
  wherever possible.
- Updated copyright headers
This commit is contained in:
Chris Josten 2021-02-17 19:42:10 +01:00
parent b345a1c650
commit 895731ae38
34 changed files with 914 additions and 463 deletions

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -17,12 +17,15 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/jellyfinitem.h"
#include "JellyfinQt/DTO/dto.h"
#include "JellyfinQt/apiclient.h"
namespace Jellyfin {
namespace DTO {
const QRegularExpression JsonSerializable::m_listExpression = QRegularExpression("^QList<\\s*([a-zA-Z0-9]*)\\s*\\*?\\s*>$");
JsonSerializable::JsonSerializable(QObject *parent) : QObject(parent) {
}
JsonSerializable::JsonSerializable(QObject *parent) : QObject(parent) { }
void JsonSerializable::deserialize(const QJsonObject &jObj) {
const QMetaObject *obj = this->metaObject();
@ -107,7 +110,7 @@ QVariant JsonSerializable::deserializeQobject(const QJsonObject &innerObj, const
if (match.hasMatch()) {
// It is a qList! Now extract the inner type
// There should be an easier way, shouldn't there?
QString listType = match.captured(1).prepend("Jellyfin::").append("*");
QString listType = match.captured(1).prepend("Jellyfin::DTO::").append("*");
// UGLY CODE HERE WE COME
typeNo = QMetaType::type(listType.toUtf8());
if (typeNo == QMetaType::UnknownType) {
@ -254,116 +257,5 @@ void RemoteData::reload() {
});
}
NameGuidPair::NameGuidPair(QObject *parent) : JsonSerializable (parent) {}
// User
User::User(QObject *parent) : RemoteData (parent) {}
QString User::getDataUrl() const {
return QString("/Users/") + m_apiClient->userId();
}
bool User::canReload() const {
return true;
}
// MediaStream
MediaStream::MediaStream(QObject *parent) : JsonSerializable (parent) {}
MediaStream::MediaStream(const MediaStream &other)
: JsonSerializable (other.parent()),
m_codec(other.m_codec),
m_codecTag(other.m_codecTag),
m_language(other.m_language),
m_displayTitle(other.m_displayTitle),
m_type(other.m_type),
m_index(other.m_index){
}
bool MediaStream::operator==(const MediaStream &other) {
// displayTitle is explicitly left out, since it's generated based on other properties
// in the Jellyfin source code.
return m_codec == other.m_codec && m_codecTag == other.m_codecTag
&& m_language == other.m_language && m_type == other.m_type
&& m_index == other.m_index;
}
// UserData
UserData::UserData(QObject *parent) : JsonSerializable (parent) {}
void UserData::updateOnServer() {
//TODO: implement
}
void UserData::onUpdated(QSharedPointer<UserData> other) {
// The reason I'm not using setLikes and similar is that they don't work with std::nullopt,
// since QML does not like it.
// THe other reason is that the setLikes method will send a post request to the server, to update the contents
// we don't want that to happen, obviously, since the application could end in an infinite loop.
if (this->m_playedPercentage != other->m_playedPercentage) {
this->m_playedPercentage = other->m_playedPercentage;
emit playedPercentageChanged(playedPercentage());
}
if (m_playbackPositionTicks!= other->m_playbackPositionTicks) {
this->m_playbackPositionTicks = other->m_playbackPositionTicks;
emit playbackPositionTicksChanged(this->m_playbackPositionTicks);
}
if (m_isFavorite != other->m_isFavorite) {
this->m_isFavorite = other->m_isFavorite;
emit isFavoriteChanged(this->m_isFavorite);
}
if (this->m_likes != other->m_likes) {
this->m_likes = other->m_likes;
emit likesChanged(likes());
}
if (this->m_played != other->m_played) {
this->m_played = other->m_played;
emit playedChanged(this->m_played);
}
}
// Item
Item::Item(QObject *parent) : RemoteData(parent) {
connect(this, &RemoteData::apiClientChanged, this, [this](ApiClient *newApiClient) {
connect(newApiClient, &ApiClient::userDataChanged, this, &Item::onUserDataChanged);
});
}
Item::Item(QString id, ApiClient *apiClient, QObject *parent)
: RemoteData(parent), m_id(id) {
connect(this, &RemoteData::apiClientChanged, this, [this](ApiClient *newApiClient) {
connect(newApiClient, &ApiClient::userDataChanged, this, &Item::onUserDataChanged);
});
setApiClient(apiClient);
}
QString Item::getDataUrl() const {
return QString("/Users/") + m_apiClient->userId() + "/Items/" + m_id;
}
bool Item::canReload() const {
return !m_id.isNull();
}
void Item::setJellyfinId(QString newId) {
m_id = newId.trimmed();
if (m_id != newId) {
emit jellyfinIdChanged(m_id);
reload();
}
}
void Item::onUserDataChanged(const QString &itemId, QSharedPointer<UserData> userData) {
if (itemId != m_id || m_userData == nullptr) return;
m_userData->onUpdated(userData);
}
void registerSerializableJsonTypes(const char* URI) {
qmlRegisterType<MediaStream>(URI, 1, 0, "MediaStream");
qmlRegisterType<NameGuidPair>(URI, 1, 0, "NameGuidPair");
qmlRegisterType<User>(URI, 1, 0, "User");
qmlRegisterType<UserData>(URI, 1, 0, "UserData");
qmlRegisterType<Item>(URI, 1, 0, "JellyfinItem");
}
}
} // NS DTO
} // NS Jellyfin

63
core/src/DTO/item.cpp Normal file
View file

@ -0,0 +1,63 @@
/*
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
*/
#include "JellyfinQt/DTO/item.h"
#include "JellyfinQt/apiclient.h"
namespace Jellyfin {
namespace DTO {
Item::Item(QObject *parent) : RemoteData(parent) {
connect(this, &RemoteData::apiClientChanged, this, [this](ApiClient *newApiClient) {
connect(newApiClient, &ApiClient::userDataChanged, this, &Item::onUserDataChanged);
});
}
Item::Item(QString id, ApiClient *apiClient, QObject *parent)
: RemoteData(parent), m_id(id) {
connect(this, &RemoteData::apiClientChanged, this, [this](ApiClient *newApiClient) {
connect(newApiClient, &ApiClient::userDataChanged, this, &Item::onUserDataChanged);
});
setApiClient(apiClient);
}
QString Item::getDataUrl() const {
return QString("/Users/") + m_apiClient->userId() + "/Items/" + m_id;
}
bool Item::canReload() const {
return !m_id.isNull();
}
void Item::setJellyfinId(QString newId) {
m_id = newId.trimmed();
if (m_id != newId) {
emit jellyfinIdChanged(m_id);
reload();
}
}
void Item::onUserDataChanged(const QString &itemId, QSharedPointer<UserData> userData) {
if (itemId != m_id || m_userData == nullptr) return;
m_userData->onUpdated(userData);
}
} // NS DTO
} // NS Jellyfin

View file

@ -0,0 +1,43 @@
/*
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
*/
#include "JellyfinQt/DTO/mediastream.h"
namespace Jellyfin {
namespace DTO {
MediaStream::MediaStream(QObject *parent) : JsonSerializable (parent) {}
MediaStream::MediaStream(const MediaStream &other)
: JsonSerializable (other.parent()),
m_codec(other.m_codec),
m_codecTag(other.m_codecTag),
m_language(other.m_language),
m_displayTitle(other.m_displayTitle),
m_type(other.m_type),
m_index(other.m_index){
}
bool MediaStream::operator==(const MediaStream &other) {
// displayTitle is explicitly left out, since it's generated based on other properties
// in the Jellyfin source code.
return m_codec == other.m_codec && m_codecTag == other.m_codecTag
&& m_language == other.m_language && m_type == other.m_type
&& m_index == other.m_index;
}
} // NS DTO
} // NS Jellyfin

View file

@ -0,0 +1,27 @@
/*
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
*/
#include "JellyfinQt/DTO/namedguidpair.h"
namespace Jellyfin {
namespace DTO {
NameGuidPair::NameGuidPair(QObject *parent) : JsonSerializable (parent) {}
} // NS DTO
} // NS Jellyfin

36
core/src/DTO/types.cpp Normal file
View file

@ -0,0 +1,36 @@
/*
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
*/
#include "JellyfinQt/DTO/types.h"
#include <QtQml>
namespace Jellyfin {
namespace DTO {
void registerTypes(const char *uri) {
qmlRegisterType<MediaStream>(uri, 1, 0, "MediaStream");
qmlRegisterType<NameGuidPair>(uri, 1, 0, "NameGuidPair");
qmlRegisterType<User>(uri, 1, 0, "User");
qmlRegisterType<UserData>(uri, 1, 0, "UserData");
qmlRegisterType<Item>(uri, 1, 0, "JellyfinItem");
}
} // NS DTO
} // NS Jellyfin

36
core/src/DTO/user.cpp Normal file
View file

@ -0,0 +1,36 @@
/*
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
*/
#include "JellyfinQt/DTO/user.h"
#include "JellyfinQt/apiclient.h"
namespace Jellyfin {
namespace DTO {
User::User(QObject *parent) : RemoteData (parent) {}
QString User::getDataUrl() const {
return QString("/Users/") + m_apiClient->userId();
}
bool User::canReload() const {
return true;
}
} // NS DTO
} // NS Jellyfin

58
core/src/DTO/userdata.cpp Normal file
View file

@ -0,0 +1,58 @@
/*
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
*/
#include "JellyfinQt/DTO/userdata.h"
namespace Jellyfin {
namespace DTO {
UserData::UserData(QObject *parent) : JsonSerializable (parent) {}
void UserData::updateOnServer() {
//TODO: implement
}
void UserData::onUpdated(QSharedPointer<UserData> other) {
// The reason I'm not using setLikes and similar is that they don't work with std::nullopt,
// since QML does not like it.
// THe other reason is that the setLikes method will send a post request to the server, to update the contents
// we don't want that to happen, obviously, since the application could end in an infinite loop.
if (this->m_playedPercentage != other->m_playedPercentage) {
this->m_playedPercentage = other->m_playedPercentage;
emit playedPercentageChanged(playedPercentage());
}
if (m_playbackPositionTicks!= other->m_playbackPositionTicks) {
this->m_playbackPositionTicks = other->m_playbackPositionTicks;
emit playbackPositionTicksChanged(this->m_playbackPositionTicks);
}
if (m_isFavorite != other->m_isFavorite) {
this->m_isFavorite = other->m_isFavorite;
emit isFavoriteChanged(this->m_isFavorite);
}
if (this->m_likes != other->m_likes) {
this->m_likes = other->m_likes;
emit likesChanged(likes());
}
if (this->m_played != other->m_played) {
this->m_played = other->m_played;
emit playedChanged(this->m_played);
}
}
} // NS DTO
} // NS Jellyfin

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -17,7 +17,7 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/jellyfinapiclient.h"
#include "JellyfinQt/apiclient.h"
namespace Jellyfin {
@ -264,7 +264,7 @@ void ApiClient::defaultNetworkErrorHandler(QNetworkReply::NetworkError error) {
}
void ApiClient::onUserDataChanged(const QString &itemId, QSharedPointer<UserData> userData) {
userDataChanged(itemId, userData);
emit userDataChanged(itemId, userData);
}
void ApiClient::setAuthenticated(bool authenticated) {

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -17,7 +17,9 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/jellyfinapimodel.h"
#include "JellyfinQt/apimodel.h"
#include "JellyfinQt/DTO/userdata.h"
namespace Jellyfin {
ApiModel::ApiModel(QString path, bool hasRecordResponse, bool addUserId, QObject *parent)
@ -229,7 +231,7 @@ ItemModel::ItemModel(QString path, bool hasRecordFields, bool replaceUser, QObje
});
}
void ItemModel::onUserDataChanged(const QString &itemId, QSharedPointer<UserData> userData) {
void ItemModel::onUserDataChanged(const QString &itemId, QSharedPointer<DTO::UserData> userData) {
int i = 0;
for (QJsonValueRef val: m_array) {
QJsonObject item = val.toObject();

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -17,7 +17,7 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/jellyfindeviceprofile.h"
#include "JellyfinQt/deviceprofile.h"
namespace Jellyfin {

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -31,6 +31,6 @@ void registerTypes(const char *uri) {
// API models
Jellyfin::registerModels(uri);
Jellyfin::registerSerializableJsonTypes(uri);
Jellyfin::DTO::registerTypes(uri);
}
}

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -19,7 +19,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#include "JellyfinQt/jsonhelper.h"
namespace Jellyfin {
namespace JsonHelper {
void convertToCamelCase(QJsonValueRef val) {

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -17,7 +17,10 @@ License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "JellyfinQt/jellyfinplaybackmanager.h"
#include "JellyfinQt/playbackmanager.h"
#include "JellyfinQt/DTO/dto.h"
#include "JellyfinQt/DTO/userdata.h"
namespace Jellyfin {
@ -213,6 +216,14 @@ void PlaybackManager::playItem(const QString &itemId) {
setItem(newItem);
}
void PlaybackManager::next() {
Q_UNIMPLEMENTED();
}
void PlaybackManager::previous() {
Q_UNIMPLEMENTED();
}
void PlaybackManager::postPlaybackInfo(PlaybackInfoType type) {
QJsonObject root;

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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

View file

@ -1,6 +1,6 @@
/*
Sailfin: a Jellyfin client written using Qt
Copyright (C) 2020 Chris Josten
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
@ -16,7 +16,10 @@ 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
*/
#include "JellyfinQt/jellyfinwebsocket.h"
#include "JellyfinQt/websocket.h"
#include "JellyfinQt/DTO/userdata.h"
namespace Jellyfin {
WebSocket::WebSocket(ApiClient *client)
@ -102,7 +105,7 @@ void WebSocket::textMessageReceived(const QString &message) {
}
QJsonArray userDataList = data2["UserDataList"].toArray();
for (QJsonValue val: userDataList) {
QSharedPointer<UserData> userData(new UserData, &QObject::deleteLater);
QSharedPointer<DTO::UserData> userData(new DTO::UserData, &QObject::deleteLater);
userData->deserialize(val.toObject());
m_apiClient->onUserDataChanged(userData->itemId(), userData);
}