From 892de92aa2371718fb3a0bb0847d43de2d7d2423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bernhard=20Rosenkr=C3=A4nzer?= Date: Thu, 29 Aug 2024 13:21:00 +0200 Subject: [PATCH] Transform JSON objects into native objects --- CMakeLists.txt | 3 ++- Configurator.cpp | 26 ++++++-------------------- Configurator.h | 5 +++-- lib/CMakeLists.txt | 2 ++ lib/Service.cpp | 22 ++++++++++++++++++++++ lib/Service.h | 24 ++++++++++++++++++++++++ 6 files changed, 59 insertions(+), 23 deletions(-) create mode 100644 lib/CMakeLists.txt create mode 100644 lib/Service.cpp create mode 100644 lib/Service.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 57ca388..a4cf913 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,7 +9,8 @@ set(QT_VERSION 6 CACHE STRING "Major version of Qt to use (6 will work, anything find_package(Qt${QT_VERSION} COMPONENTS Core Gui Widgets WebEngineWidgets REQUIRED) add_subdirectory(dialogs) +add_subdirectory(lib) add_executable(configurator main.cpp Configurator.cpp) -target_link_libraries(configurator Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::WebEngineWidgets Qt${QT_VERSION}::Network dialogs) +target_link_libraries(configurator Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets Qt${QT_VERSION}::WebEngineWidgets Qt${QT_VERSION}::Network dialogs fed) install(TARGETS configurator DESTINATION bin) diff --git a/Configurator.cpp b/Configurator.cpp index 9fea781..9f3133b 100644 --- a/Configurator.cpp +++ b/Configurator.cpp @@ -1,6 +1,6 @@ #include "Configurator.h" #include -#include +#include #include Configurator::Configurator(int &argc, char **&argv):QApplication(argc, argv),_nam(this) { @@ -25,27 +25,13 @@ void Configurator::appListReceived(QNetworkReply *reply) { _loginDialog->show(); return; } - _services=QJsonDocument::fromJson(reply->readAll()); - if(!_services.isArray()) { + QJsonDocument services = QJsonDocument::fromJson(reply->readAll()); + if(!services.isArray()) { _loginDialog->setMessage(tr("App list did not return an array. Is the domain name correct?")); _loginDialog->show(); return; } - QJsonArray a=_services.array(); - for(auto i : a) { - if(!i.isObject()) { - std::cerr << "Non-object in JSON array" << std::endl; - continue; - } - QJsonObject o = i.toObject(); - // We can assume the object has: - // o["Title"].toString() - // o["Url"].toString() - // o["DocumentationUrl"].toString() - // o["Image"].toString() - // o["Description"].toString() - // o["SpecialNote"].toString() - // o["LDAP"].toBool() - std::cerr << qPrintable(o["Title"].toString()) << " " << o["LDAP"].toBool() << std::endl; - } + _services = services.array(); + for(auto i : _services) + std::cerr << qPrintable(i.title) << std::endl; } diff --git a/Configurator.h b/Configurator.h index f21adf1..3f7dd10 100644 --- a/Configurator.h +++ b/Configurator.h @@ -3,8 +3,9 @@ #include #include #include -#include + #include +#include class Configurator:public QApplication { Q_OBJECT @@ -16,5 +17,5 @@ protected Q_SLOTS: private: QNetworkAccessManager _nam; FederatedLogin * _loginDialog; - QJsonDocument _services; + Services _services; }; diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt new file mode 100644 index 0000000..30f2eb7 --- /dev/null +++ b/lib/CMakeLists.txt @@ -0,0 +1,2 @@ +add_library(fed STATIC Service.cpp) +target_link_libraries(fed Qt${QT_VERSION}::Core Qt${QT_VERSION}::Gui Qt${QT_VERSION}::Widgets) diff --git a/lib/Service.cpp b/lib/Service.cpp new file mode 100644 index 0000000..ab6e6c6 --- /dev/null +++ b/lib/Service.cpp @@ -0,0 +1,22 @@ +#include "Service.h" +#include + +Service::Service(QJsonObject const &o) { + title = o["Title"].toString(); + url = o["Url"].toString(); + documentationUrl = o["DocumentationUrl"].toString(); + image = o["Image"].toString(); + description = o["Description"].toString(); + specialNote = o["SpecialNote"].toString(); + LDAP = o["LDAP"].toBool(); +} + +Services::Services(QJsonArray const &a):QList() { + for(auto i : a) { + if(!i.isObject()) { + std::cerr << "Non-object in JSON array, ignoring" << std::endl; + continue; + } + append(Service{i.toObject()}); + } +} diff --git a/lib/Service.h b/lib/Service.h new file mode 100644 index 0000000..6738d40 --- /dev/null +++ b/lib/Service.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include +#include +#include + +struct Service { + Service(QJsonObject const &o); + + QString title; + QUrl url; + QUrl documentationUrl; + QUrl image; + QString description; + QString specialNote; + bool LDAP; +}; + +class Services:public QList { +public: + Services():QList() {} + Services(QJsonArray const &a); +};