diff --git a/qml/pages/itemdetails/VideoPage.qml b/qml/pages/itemdetails/VideoPage.qml index 17b7691..26e00a0 100644 --- a/qml/pages/itemdetails/VideoPage.qml +++ b/qml/pages/itemdetails/VideoPage.qml @@ -54,13 +54,13 @@ BaseDetailPage { width: parent.width imageSource: Utils.itemImageUrl(ApiClient.baseUrl, itemData, "Primary", {"maxWidth": parent.width}) imageAspectRatio: Constants.horizontalVideoAspectRatio - playProgress: itemData.UserData.PlayedPercentage / 100 + playProgress: itemData.userData.playedPercentage / 100 onPlayPressed: pageStack.push(Qt.resolvedUrl("../VideoPage.qml"), {"itemId": itemId, "itemData": itemData, "audioTrack": trackSelector.audioTrack, "subtitleTrack": trackSelector.subtitleTrack, "startTicks": startFromBeginning ? 0.0 - : itemData.UserData.PlaybackPositionTicks }) + : itemData.userData.playbackPositionTicks }) } VideoTrackSelector { diff --git a/src/jellyfinitem.cpp b/src/jellyfinitem.cpp index 51ced56..3ea6ea3 100644 --- a/src/jellyfinitem.cpp +++ b/src/jellyfinitem.cpp @@ -3,11 +3,45 @@ namespace Jellyfin { JsonSerializable::JsonSerializable(QObject *parent) : QObject(parent) {} -void JsonSerializable::deserialize(const QJsonObject &jObj) { - const QMetaObject *obj = this->metaObject(); +void JsonSerializable::deserialize(const QJsonObject &jObj, QObject *to) { + if (to == nullptr) to = this; + const QMetaObject *obj = to->metaObject(); + qDebug() << "Inside class: " << QString(obj->className()); + + for (auto it = jObj.constBegin(); it != jObj.constEnd(); it++) { + + // Hardcoded exception for the property id, since its special inside QML + if (it.key() == "Id") { + to->setProperty("jellyfinId", jsonToVariant( + obj->property(obj->indexOfProperty("jellyfinId")), it.value(), jObj)); + } else { + const char *propName = fromPascalCase(it.key()).normalized(QString::NormalizationForm_D).toLatin1(); + int propIndex = obj->indexOfProperty(propName); + if (propIndex >= 0) { + qDebug() << "Setting property " << QString(propName) << "(" << it.key() << ")"; + // We have this property! Set it. + QMetaProperty prop = obj->property(propIndex); + prop.write(to, jsonToVariant(prop, it.value(), jObj)); + } else { + qDebug() << "Setting custom property " << QString(propName) << "(" << it.key() << ")"; + QJsonValue val = it.value(); + if (val.isObject()) { + // Create a new QObject with its properties. + QObject *newObj = new QObject(to); + deserialize(val.toObject(), newObj); + to->setProperty(propName, QVariant::fromValue(newObj)); + } else if (val.isArray()) { + to->setProperty(propName, val.toArray().toVariantList()); + } else { + to->setProperty(propName, val.toVariant()); + } + } + } + } + qDebug() << "Leaving class: " << QString(obj->className()); // Loop over each property, - for (int i = 0; i < obj->propertyCount(); i++) { + /*for (int i = 0; i < obj->propertyCount(); i++) { QMetaProperty prop = obj->property(i); // Skip properties which are not stored (usually derrived of other properties) if (!prop.isStored()) continue; @@ -24,7 +58,7 @@ void JsonSerializable::deserialize(const QJsonObject &jObj) { } else { qDebug() << "Ignored " << prop.name() << " while deserializing"; } - } + }*/ } QVariant JsonSerializable::jsonToVariant(QMetaProperty prop, const QJsonValue &val, const QJsonObject &root) const { @@ -112,13 +146,15 @@ QJsonValue JsonSerializable::variantToJson(const QVariant var) const { } QString JsonSerializable::toPascalCase(QString str) { - str[0] = str[0].toUpper(); - return str; + QString copy(str); + copy[0] = copy[0].toUpper(); + return copy; } QString JsonSerializable::fromPascalCase(QString str) { - str[0] = str[0].toLower(); - return str; + QString copy(str); + copy[0] = copy[0].toLower(); + return copy; } diff --git a/src/jellyfinitem.h b/src/jellyfinitem.h index fb58587..8e45229 100644 --- a/src/jellyfinitem.h +++ b/src/jellyfinitem.h @@ -36,7 +36,7 @@ public: * @brief Sets this objects properties based on obj. * @param obj The data to load into this object. */ - void deserialize(const QJsonObject &obj); + void deserialize(const QJsonObject &obj, QObject *to = nullptr); QJsonObject serialize() const; private: QVariant jsonToVariant(QMetaProperty prop, const QJsonValue &val, const QJsonObject &root) const;