Commit dd7a0570 authored by Leander Schulten's avatar Leander Schulten

Application Data: Improve code and API. Report errors to the user when something went wrong.

parent 0bd79a59
#include "applicationdata.h"
#include <QJsonDocument>
#include "errornotifier.h"
#include "idbase.h"
#include <QJsonArray>
#include "usermanagment.h"
#include "dmx/channel.h"
#include "dmx/deviceprototype.h"
#include "dmx/device.h"
#include "dmx/programmprototype.h"
#include "dmx/deviceprototype.h"
#include "dmx/programm.h"
#include "usermanagment.h"
#include "dmx/programmprototype.h"
#include "gui/controlpanel.h"
#include "gui/mapview.h"
#include <QCryptographicHash>
#include "modules/modulemanager.h"
#include "modules/programblock.h"
#include "spotify/spotify.h"
#include <QCryptographicHash>
#include <QJsonArray>
#include <QJsonDocument>
namespace ApplicationData{
QByteArray password;
namespace ApplicationData {
bool saveData(QFile &file){
if(!file.open(QIODevice::WriteOnly))return false;
bool saveData(const QString &filename) {
QFile file(filename);
if (!file.open(QIODevice::WriteOnly)) {
return false;
}
auto data = saveData();
file.write(data);
file.close();
return true;
}
template<typename Vector>
void saveIDBaseObjects(QJsonObject &o,const Vector & vector, QString entryName ){
template <typename Vector>
void saveIDBaseObjects(QJsonObject &o, const Vector &vector, const QString &entryName) {
QJsonArray array;
for(const auto & p : vector){
for (const auto &p : vector) {
QJsonObject o;
p->writeJsonObject(o);
array.append(o);
}
o.insert(entryName,array);
o.insert(entryName, array);
}
QByteArray saveData(){
qDebug()<<"SAVE DATA";
QByteArray saveData() {
qDebug() << "SAVE DATA";
QJsonObject o;
saveIDBaseObjects(o,ModelManager::get().getDevicePrototypes(),"DevicePrototypes");
saveIDBaseObjects(o,ModelManager::get().getDevices(),"Devices");
saveIDBaseObjects(o,ModelManager::get().getProgramPrototypes(),"ProgrammPrototypes");
saveIDBaseObjects(o,ModelManager::get().getPrograms(),"Programms");
saveIDBaseObjects(o,UserManagment::get()->getUsers(),"Users");
o.insert("password",QString::fromLatin1(password.toBase64()));
saveIDBaseObjects(o, ModelManager::get().getDevicePrototypes(), QStringLiteral("DevicePrototypes"));
saveIDBaseObjects(o, ModelManager::get().getDevices(), QStringLiteral("Devices"));
saveIDBaseObjects(o, ModelManager::get().getProgramPrototypes(), QStringLiteral("ProgrammPrototypes"));
saveIDBaseObjects(o, ModelManager::get().getPrograms(), QStringLiteral("Programms"));
saveIDBaseObjects(o, UserManagment::get()->getUsers(), QStringLiteral("Users"));
if (GUI::ControlPanel::getLastCreated()) {
QJsonObject u;
GUI::ControlPanel::getLastCreated()->writeJsonObject(u);
o.insert("ControlPanel",u);
o.insert(QStringLiteral("ControlPanel"), u);
}
if (GUI::MapView::getLastCreated()) {
QJsonObject u;
GUI::MapView::getLastCreated()->writeJsonObject(u);
o.insert("MapView",u);
o.insert(QStringLiteral("MapView"), u);
}
{
QJsonObject u;
Modules::ModuleManager::singletone()->writeJsonObject(u);
o.insert("ModuleManager",u);
o.insert(QStringLiteral("ModuleManager"), u);
}
{
QJsonObject u;
Modules::ProgramBlockManager::writeToJsonObject(u);
o.insert("ProgramBlockManager",u);
o.insert(QStringLiteral("ProgramBlockManager"), u);
}
{
QJsonObject u;
Spotify::get().writeToJsonObject(u);
o.insert("Spotify",u);
o.insert(QStringLiteral("Spotify"), u);
}
return QJsonDocument(o).toJson();
}
std::function<void()> loadData(QFile &file){
if(!file.open(QIODevice::ReadOnly)){
return [](){qDebug()<<"Error: can not open file";};
std::pair<std::function<void()>, QString> loadData(const QString &filename) {
QFile file(filename);
if (!file.open(QIODevice::ReadOnly)) {
return {{}, "Failed to open settings file " + file.fileName() + ". Error : " + file.errorString()};
}
auto r = loadData(file.readAll());
file.close();
return r;
}
std::function<void()> loadData(QByteArray data){
const auto o = QJsonDocument::fromJson(data).object();
for(const auto e : o["DevicePrototypes"].toArray()){
std::pair<std::function<void()>, QString> loadData(const QByteArray &data) {
QJsonParseError error{};
auto doc = QJsonDocument::fromJson(data, &error);
if (error.error != QJsonParseError::NoError) {
return {{}, "Failed to parse settings file: " + error.errorString() + " at position " + QString::number(error.offset)};
}
const auto o = doc.object();
for (const auto e : o[QStringLiteral("DevicePrototypes")].toArray()) {
ModelManager::get().addNewDevicePrototype(e.toObject());
}
for(const auto e : o["Devices"].toArray()){
for (const auto e : o[QStringLiteral("Devices")].toArray()) {
ModelManager::get().addNewDevice(e.toObject());
}
for(const auto e : o["ProgrammPrototypes"].toArray()){
for (const auto e : o[QStringLiteral("ProgrammPrototypes")].toArray()) {
ModelManager::get().addNewProgramPrototype(e.toObject());
}
for(const auto e : o["Programms"].toArray()){
for (const auto e : o[QStringLiteral("Programms")].toArray()) {
ModelManager::get().addNewProgram(e.toObject());
}
password = QByteArray::fromBase64(o["password"].toString().toLatin1());
{//USERS
for(const auto e : o["Users"].toArray()){
{ // USERS
for (const auto e : o[QStringLiteral("Users")].toArray()) {
User::createUser(e.toObject());
}
}
if(password==QCryptographicHash::hash(QString("admin").toLatin1(),QCryptographicHash::Sha3_256)){
qDebug()<<"Erfolgreich";
}else{
qDebug()<<"password : "<<password;
qDebug()<<"hash : "<<QCryptographicHash::hash(QString("admin").toLatin1(),QCryptographicHash::Sha3_256);
}
{
QJsonObject ob = o["ModuleManager"].toObject();
QJsonObject ob = o[QStringLiteral("ModuleManager")].toObject();
Modules::ModuleManager::singletone()->loadModules(ob);
}
Spotify::get().loadFromJsonObject(o["Spotify"].toObject());
password=QCryptographicHash::hash(QString("admin").toLatin1(),QCryptographicHash::Sha3_256);
return [=]() {
if (GUI::MapView::getLastCreated()) GUI::MapView::getLastCreated()->loadFromJsonObject(o["MapView"].toObject());
Modules::ProgramBlockManager::readFromJsonObject(o["ProgramBlockManager"].toObject());
if (GUI::ControlPanel::getLastCreated()) GUI::ControlPanel::getLastCreated()->loadFromJsonObject(o["ControlPanel"].toObject());
};
}
Spotify::get().loadFromJsonObject(o[QStringLiteral("Spotify")].toObject());
return {[=]() {
if (GUI::MapView::getLastCreated()) {
GUI::MapView::getLastCreated()->loadFromJsonObject(o[QStringLiteral("MapView")].toObject());
}
Modules::ProgramBlockManager::readFromJsonObject(o[QStringLiteral("ProgramBlockManager")].toObject());
if (GUI::ControlPanel::getLastCreated()) {
GUI::ControlPanel::getLastCreated()->loadFromJsonObject(o[QStringLiteral("ControlPanel")].toObject());
}
},
{}};
}
} // namespace ApplicationData
......@@ -6,12 +6,32 @@
namespace ApplicationData{
bool saveData(QFile &file);
std::function<void()> loadData(QFile &file);
/**
* @brief saveData saves all application data into the given file
* @param file The file in which the data should be written
* @return true on success, false on failure
*/
bool saveData(const QString &file);
/**
* @brief saveData saves all application data into a QByteArray
* @return The application data as data array
*/
QByteArray saveData();
std::function<void()> loadData(QByteArray data);
/**
* @brief loadData loads the application data from the file and returns a callback
* that should be called, after the QML UI is loaded
* @param file The file with the application data
* @return a callback and a error message. An error occurred, when the error message is not empty
*/
std::pair<std::function<void()>, QString> loadData(const QString &file);
/**
* @brief loadData loads the application data from the byte array and returns a callback
* that should be called, after the QML UI is loaded
* @param data the application data
* @return a callback and a error message. An error occurred, when the error message is not empty
*/
std::pair<std::function<void()>, QString> loadData(const QByteArray &data);
}
} // namespace ApplicationData
#endif // APPLICATIONDATA_H
......@@ -262,16 +262,23 @@ int main(int argc, char *argv[]) {
// Load Settings and ApplicationData
Settings::setLocalSettingFile(QFileInfo(QStringLiteral("settings.ini")));
Settings settings;
QFile file(settings.getJsonSettingsFilePath());
if(!file.exists()){
file.setFileName(QStringLiteral("QTJSONFile.json"));
QString file(settings.getJsonSettingsFilePath());
if (!QFile::exists(file)) {
file = QStringLiteral("QTJSONFile.json");
}
if(file.exists()){
file.copy(file.fileName()+"_"+QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy HH.mm.ss")));
std::function<void()> after;
if (QFile::exists(file)) {
QFile::copy(file, file + "_" + QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy HH.mm.ss")));
// load application data
QString error;
std::tie(after, error) = ApplicationData::loadData(file);
if (!error.isEmpty()) {
ErrorNotifier::showError(error);
}
} else {
ErrorNotifier::showError(QStringLiteral("No settings file found! The Lichtsteuerung starts wihout content."));
}
auto after = ApplicationData::loadData(file);
// nachdem die Benutzer geladen wurden, auto login durchführen
UserManagment::get()->autoLoginUser();
......@@ -312,8 +319,7 @@ int main(int argc, char *argv[]) {
CatchingErrorApplication::connect(&app, &QGuiApplication::aboutToQuit, [&]() {
Modules::ModuleManager::singletone()->controller().stop();
Audio::AudioCaptureManager::get().stopCapturingAndWait();
QFile savePath(settings.getJsonSettingsFileSavePath());
ApplicationData::saveData(savePath);
ApplicationData::saveData(settings.getJsonSettingsFileSavePath());
Driver::stopAndUnloadDriver();
if (updater.getState() == Updater::ReadyToInstall) {
updater.runUpdateInstaller();
......@@ -328,10 +334,7 @@ int main(int argc, char *argv[]) {
Driver::getCurrentDriver()->setWaitTime(std::chrono::milliseconds(settings.getUpdatePauseInMs()));
}
});
Settings::connect(&settings, &Settings::saveAs, [&](const auto &path) {
QFile saveFile(settings.getJsonSettingsFileSavePath());
ApplicationData::saveData(saveFile);
});
Settings::connect(&settings, &Settings::saveAs, [&](const auto &path) { ApplicationData::saveData(path); });
Modules::ModuleManager::singletone()->loadAllModulesInDir(settings.getModuleDirPath());
Settings::connect(&settings, &Settings::moduleDirPathChanged, [&]() { Modules::ModuleManager::singletone()->loadAllModulesInDir(settings.getModuleDirPath()); });
......@@ -364,8 +367,9 @@ int main(int argc, char *argv[]) {
// laden erst nach dem laden des qml ausführen
after();
if (after) {
after();
}
// Treiber laden
#define USE_DUMMY_DRIVER
......
......@@ -149,9 +149,9 @@ public:
*/
Q_INVOKABLE void save(){
if(settings != nullptr){
QFile savePath(settings->getJsonSettingsFilePath());
if(savePath.exists()){
savePath.copy(savePath.fileName()+"_"+QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy HH.mm.ss")));
QString savePath(settings->getJsonSettingsFilePath());
if (QFile::exists(savePath)) {
QFile::copy(savePath, savePath + "_" + QDateTime::currentDateTime().toString(QStringLiteral("dd.MM.yyyy HH.mm.ss")));
}
ApplicationData::saveData(savePath);
}else {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment