mirror of
https://github.com/HenkKalkwater/harbour-sailfin.git
synced 2025-09-05 10:12:46 +00:00
Models get updated when userData changes at server
The websocket now notifies the ApiClient, on which several models and items are listening, when the userData for an user has changed. The UI on the qml side may automatically updates without any extra effort. This also resolves a bug where videos didn't resume after +/- 3:40 due to an integer overflow.
This commit is contained in:
parent
1e80ceb697
commit
d81fa50715
17 changed files with 304 additions and 44 deletions
|
@ -127,4 +127,4 @@ private:
|
|||
QSettings m_settings;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif // CREDENTIAL_MANAGER_H
|
||||
|
|
|
@ -38,6 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
|
||||
#include "credentialmanager.h"
|
||||
#include "jellyfindeviceprofile.h"
|
||||
#include "jellyfinitem.h"
|
||||
#include "jellyfinwebsocket.h"
|
||||
|
||||
namespace Jellyfin {
|
||||
|
@ -144,6 +145,15 @@ signals:
|
|||
void itemFetched(const QString &itemId, const QJsonObject &result);
|
||||
void itemFetchFailed(const QString &itemId, const QNetworkReply::NetworkError error);
|
||||
|
||||
/**
|
||||
* @brief onUserDataChanged Emitted when the user data of an item is changed on the server.
|
||||
* @param itemId The id of the item being changed
|
||||
* @param userData The new user data
|
||||
*
|
||||
* Note: only Jellyfin::UserData should connect to this signal, they will update themselves!
|
||||
*/
|
||||
void userDataChanged(const QString &itemId, QSharedPointer<UserData> userData);
|
||||
|
||||
public slots:
|
||||
/**
|
||||
* @brief Tries to access credentials and connect to a server. If nothing has been configured yet,
|
||||
|
@ -172,6 +182,7 @@ public slots:
|
|||
|
||||
protected slots:
|
||||
void defaultNetworkErrorHandler(QNetworkReply::NetworkError error);
|
||||
void onUserDataChanged(const QString &itemId, QSharedPointer<UserData> newData);
|
||||
|
||||
protected:
|
||||
/**
|
||||
|
|
|
@ -129,7 +129,7 @@ public:
|
|||
* responseHasRecords should be true
|
||||
*/
|
||||
explicit ApiModel(QString path, bool responseHasRecords, bool passUserId = false, QObject *parent = nullptr);
|
||||
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient)
|
||||
Q_PROPERTY(ApiClient *apiClient MEMBER m_apiClient NOTIFY apiClientChanged)
|
||||
Q_PROPERTY(ModelStatus status READ status NOTIFY statusChanged)
|
||||
|
||||
// Query properties
|
||||
|
@ -171,6 +171,7 @@ public:
|
|||
}
|
||||
|
||||
signals:
|
||||
void apiClientChanged(ApiClient *newApiClient);
|
||||
void statusChanged(ModelStatus newStatus);
|
||||
void limitChanged(int newLimit);
|
||||
void parentIdChanged(QString newParentId);
|
||||
|
@ -242,6 +243,9 @@ private:
|
|||
*/
|
||||
void generateFields();
|
||||
QString sortByToString(SortOptions::SortBy sortBy);
|
||||
|
||||
void convertToCamelCase(QJsonValueRef val);
|
||||
QString convertToCamelCaseHelper(const QString &str);
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -253,40 +257,53 @@ public:
|
|||
: ApiModel ("/users/public", false, false, parent) { }
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Base class for each model that works with items.
|
||||
*
|
||||
* Listens for updates in the library and updates the model accordingly.
|
||||
*/
|
||||
class ItemModel : public ApiModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ItemModel (QString path, bool responseHasRecords, bool replaceUser, QObject *parent = nullptr);
|
||||
public slots:
|
||||
void onUserDataChanged(const QString &itemId, QSharedPointer<UserData> userData);
|
||||
};
|
||||
|
||||
class UserViewModel : public ApiModel {
|
||||
public:
|
||||
explicit UserViewModel (QObject *parent = nullptr)
|
||||
: ApiModel ("/Users/{{user}}/Views", true, false, parent) {}
|
||||
};
|
||||
|
||||
class UserItemModel : public ApiModel {
|
||||
class UserItemModel : public ItemModel {
|
||||
public:
|
||||
explicit UserItemModel (QObject *parent = nullptr)
|
||||
: ApiModel ("/Users/{{user}}/Items", true, false, parent) {}
|
||||
: ItemModel ("/Users/{{user}}/Items", true, false, parent) {}
|
||||
};
|
||||
|
||||
class UserItemResumeModel : public ApiModel {
|
||||
class UserItemResumeModel : public ItemModel {
|
||||
public:
|
||||
explicit UserItemResumeModel (QObject *parent = nullptr)
|
||||
: ApiModel ("/Users/{{user}}/Items/Resume", true, false, parent) {}
|
||||
: ItemModel ("/Users/{{user}}/Items/Resume", true, false, parent) {}
|
||||
};
|
||||
|
||||
class UserItemLatestModel : public ApiModel {
|
||||
class UserItemLatestModel : public ItemModel {
|
||||
public:
|
||||
explicit UserItemLatestModel (QObject *parent = nullptr)
|
||||
: ApiModel ("/Users/{{user}}/Items/Latest", false, false, parent) {}
|
||||
: ItemModel ("/Users/{{user}}/Items/Latest", false, false, parent) {}
|
||||
};
|
||||
|
||||
class ShowSeasonsModel : public ApiModel {
|
||||
class ShowSeasonsModel : public ItemModel {
|
||||
public:
|
||||
explicit ShowSeasonsModel (QObject *parent = nullptr)
|
||||
: ApiModel ("/Shows/{{show}}/Seasons", true, true, parent) {}
|
||||
: ItemModel ("/Shows/{{show}}/Seasons", true, true, parent) {}
|
||||
};
|
||||
|
||||
class ShowEpisodesModel : public ApiModel {
|
||||
class ShowEpisodesModel : public ItemModel {
|
||||
public:
|
||||
explicit ShowEpisodesModel (QObject *parent = nullptr)
|
||||
: ApiModel ("/Shows/{{show}}/Episodes", true, true, parent) {}
|
||||
: ItemModel ("/Shows/{{show}}/Episodes", true, true, parent) {}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1,5 +1,24 @@
|
|||
#ifndef JELLYFINITEM_H
|
||||
#define JELLYFINITEM_H
|
||||
/*
|
||||
Sailfin: a Jellyfin client written using Qt
|
||||
Copyright (C) 2020 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
|
||||
*/
|
||||
|
||||
#ifndef JELLYFIN_ITEM_H
|
||||
#define JELLYFIN_ITEM_H
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
|
@ -23,7 +42,7 @@
|
|||
#include "jellyfinapiclient.h"
|
||||
|
||||
namespace Jellyfin {
|
||||
|
||||
class ApiClient;
|
||||
/**
|
||||
* @brief Base class for a serializable object.
|
||||
*
|
||||
|
@ -40,7 +59,7 @@ public:
|
|||
* @param obj The data to load into this object.
|
||||
*/
|
||||
void deserialize(const QJsonObject &obj);
|
||||
QJsonObject serialize() const;
|
||||
QJsonObject serialize(bool capitalize = true) const;
|
||||
private:
|
||||
QVariant jsonToVariant(QMetaProperty prop, const QJsonValue &val, const QJsonObject &root);
|
||||
QJsonValue variantToJson(const QVariant var) const;
|
||||
|
@ -56,7 +75,7 @@ private:
|
|||
* @param str The string to modify
|
||||
* @return THe modified string
|
||||
*/
|
||||
static QString toPascalCase(QString str);
|
||||
static QString toPascalCase(QString st);
|
||||
|
||||
static const QRegularExpression m_listExpression;
|
||||
/**
|
||||
|
@ -157,6 +176,54 @@ private:
|
|||
int m_index = -1;
|
||||
};
|
||||
|
||||
class UserData : public JsonSerializable {
|
||||
Q_OBJECT
|
||||
public:
|
||||
Q_INVOKABLE explicit UserData(QObject *parent = nullptr);
|
||||
|
||||
Q_PROPERTY(double playedPercentage READ playedPercentage WRITE setPlayedPercentage RESET resetPlayedPercentage NOTIFY playedPercentageChanged)
|
||||
Q_PROPERTY(qint64 playbackPositionTicks READ playbackPositionTicks WRITE setPlaybackPositionTicks NOTIFY playbackPositionTicksChanged)
|
||||
Q_PROPERTY(bool isFavorite READ isFavorite WRITE setIsFavorite NOTIFY isFavoriteChanged)
|
||||
Q_PROPERTY(bool likes READ likes WRITE setLikes RESET resetLikes NOTIFY likesChanged)
|
||||
Q_PROPERTY(bool played READ played WRITE setPlayed NOTIFY playedChanged)
|
||||
Q_PROPERTY(QString itemId READ itemId MEMBER m_itemId);
|
||||
|
||||
double playedPercentage() const { return m_playedPercentage.value_or(0.0); }
|
||||
void setPlayedPercentage(double newPlayedPercentage) { m_playedPercentage = newPlayedPercentage; emit playedPercentageChanged(newPlayedPercentage); }
|
||||
void resetPlayedPercentage() { m_playedPercentage = std::nullopt; emit playedPercentageChanged(0.0); updateOnServer(); }
|
||||
|
||||
qint64 playbackPositionTicks() const { return m_playbackPositionTicks; }
|
||||
void setPlaybackPositionTicks(qint64 newPlaybackPositionTicks) { m_playbackPositionTicks = newPlaybackPositionTicks; emit playbackPositionTicksChanged(newPlaybackPositionTicks); }
|
||||
|
||||
bool played() const { return m_played; }
|
||||
void setPlayed(bool newPlayed) { m_played = newPlayed; emit playedChanged(newPlayed); updateOnServer(); }
|
||||
|
||||
bool likes() const { return m_likes.value_or(false); }
|
||||
void setLikes(bool newLikes) { m_likes = newLikes; emit likesChanged(newLikes); }
|
||||
void resetLikes() { m_likes = std::nullopt; emit likesChanged(false); updateOnServer(); }
|
||||
|
||||
bool isFavorite() const { return m_isFavorite; }
|
||||
void setIsFavorite(bool newIsFavorite) { m_isFavorite = newIsFavorite; emit isFavoriteChanged(newIsFavorite); updateOnServer(); }
|
||||
|
||||
const QString &itemId() const { return m_itemId; }
|
||||
signals:
|
||||
void playedPercentageChanged(double newPlayedPercentage);
|
||||
void playbackPositionTicksChanged(qint64 playbackPositionTicks);
|
||||
void isFavoriteChanged(bool newIsFavorite);
|
||||
void likesChanged(bool newLikes);
|
||||
void playedChanged(bool newPlayed);
|
||||
public slots:
|
||||
void updateOnServer();
|
||||
void onUpdated(QSharedPointer<UserData> other);
|
||||
private:
|
||||
std::optional<double> m_playedPercentage = std::nullopt;
|
||||
qint64 m_playbackPositionTicks = 0;
|
||||
bool m_isFavorite = false;
|
||||
std::optional<bool> m_likes = std::nullopt;
|
||||
bool m_played;
|
||||
QString m_itemId;
|
||||
};
|
||||
|
||||
class Item : public RemoteData {
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -206,6 +273,7 @@ public:
|
|||
Q_PROPERTY(int indexNumberEnd READ indexNumberEnd WRITE setIndexNumberEnd NOTIFY indexNumberEndChanged)
|
||||
Q_PROPERTY(bool isFolder READ isFolder WRITE setIsFolder NOTIFY isFolderChanged)
|
||||
Q_PROPERTY(QString type MEMBER m_type NOTIFY typeChanged)
|
||||
Q_PROPERTY(UserData *userData MEMBER m_userData NOTIFY userDataChanged)
|
||||
Q_PROPERTY(QString seriesName MEMBER m_seriesName NOTIFY seriesNameChanged)
|
||||
Q_PROPERTY(QString seasonName MEMBER m_seasonName NOTIFY seasonNameChanged)
|
||||
Q_PROPERTY(QList<MediaStream *> __list__mediaStreams MEMBER __list__m_mediaStreams NOTIFY mediaStreamsChanged)
|
||||
|
@ -283,6 +351,7 @@ signals:
|
|||
void indexNumberEndChanged(int newIndexNumberEnd);
|
||||
void isFolderChanged(bool newIsFolder);
|
||||
void typeChanged(const QString &newType);
|
||||
void userDataChanged(UserData *newUserData);
|
||||
void seriesNameChanged(const QString &newSeriesName);
|
||||
void seasonNameChanged(const QString &newSeasonName);
|
||||
void mediaStreamsChanged(/*const QList<MediaStream *> &newMediaStreams*/);
|
||||
|
@ -292,6 +361,7 @@ public slots:
|
|||
* @brief (Re)loads the item from the Jellyfin server.
|
||||
*/
|
||||
void reload() override;
|
||||
void onUserDataChanged(const QString &itemId, QSharedPointer<UserData> userData);
|
||||
protected:
|
||||
QString m_id;
|
||||
QString m_name;
|
||||
|
@ -327,6 +397,7 @@ protected:
|
|||
std::optional<int> m_indexNumberEnd = std::nullopt;
|
||||
std::optional<bool> m_isFolder = std::nullopt;
|
||||
QString m_type;
|
||||
UserData *m_userData = nullptr;
|
||||
QString m_seriesName;
|
||||
QString m_seasonName;
|
||||
QList<MediaStream *> __list__m_mediaStreams;
|
||||
|
@ -351,4 +422,4 @@ protected:
|
|||
void registerSerializableJsonTypes(const char* URI);
|
||||
}
|
||||
|
||||
#endif // JELLYFINITEM_H
|
||||
#endif // JELLYFIN_ITEM_H
|
||||
|
|
|
@ -32,9 +32,17 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|||
#include <QtWebSockets/QWebSocket>
|
||||
|
||||
#include "jellyfinapiclient.h"
|
||||
#include "jellyfinitem.h"
|
||||
|
||||
namespace Jellyfin {
|
||||
class ApiClient;
|
||||
class UserData;
|
||||
/**
|
||||
* @brief Keeps a connection with the Jellyfin server to receive real time updates.
|
||||
*
|
||||
* This class will parse these messages and send them to ApiClient, which will emit signals for
|
||||
* the interested classes.
|
||||
*/
|
||||
class WebSocket : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
|
@ -47,7 +55,8 @@ public:
|
|||
explicit WebSocket(ApiClient *client);
|
||||
enum MessageType {
|
||||
ForceKeepAlive,
|
||||
KeepAlive
|
||||
KeepAlive,
|
||||
UserDataChanged
|
||||
};
|
||||
Q_ENUM(MessageType)
|
||||
public slots:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue