Commit ddd84f3c authored by Leander Schulten's avatar Leander Schulten
Browse files

Save new Data

parent 6a9a151d
......@@ -9,12 +9,17 @@
#include "programmprototype.h"
#include "programm.h"
#include "usermanagment.h"
#include "controlpanel.h"
#include <QCryptographicHash>
namespace ApplicationData{
QByteArray password;
bool saveData(QFile &file){
auto data = saveData();
if(!file.open(QIODevice::WriteOnly))return false;
auto data = saveData();
file.write(data);
file.close();
return true;
......@@ -32,16 +37,18 @@ void saveIDBaseObjects(QJsonObject &o,QString entryName ){
}
QByteArray saveData(){
qDebug()<<"SAVE DATA";
QJsonObject o;
saveIDBaseObjects<DevicePrototype>(o,"DevicePrototypes");
saveIDBaseObjects<Device>(o,"Devices");
saveIDBaseObjects<ProgrammPrototype>(o,"ProgrammPrototypes");
saveIDBaseObjects<Programm>(o,"Programms");
saveIDBaseObjects<Programm>(o,"Programms");
saveIDBaseObjects<User>(o,"Users");
o.insert("password",QString::fromLatin1(password.toBase64()));
{
QJsonObject u;
UserManagment::get()->writeJsonObject(u);
o.insert("usermanagment",u);
ControlPanel::getLastCreated()->writeJsonObject(u);
o.insert("ControlPanel",u);
}
return QJsonDocument(o).toJson();
}
......@@ -53,22 +60,37 @@ void loadIDBaseObjects(const QJsonObject &o,QString entryName ){
}
}
bool loadData(QFile &file){
std::function<void()> loadData(QFile &file){
if(!file.open(QIODevice::ReadOnly)){
return false;
return [](){qDebug()<<"Error: can not open file";};
}
loadData(file.readAll());
auto r = loadData(file.readAll());
file.close();
return true;
return r;
}
void loadData(QByteArray data){
std::function<void()> loadData(QByteArray data){
const auto o = QJsonDocument::fromJson(data).object();
loadIDBaseObjects<DevicePrototype>(o,"DevicePrototypes");
loadIDBaseObjects<Device>(o,"Devices");
loadIDBaseObjects<ProgrammPrototype>(o,"ProgrammPrototypes");
loadIDBaseObjects<Programm>(o,"Programms");
UserManagment::get()->load(o["usermanagment"].toObject());
password = QByteArray::fromBase64(o["password"].toString().toLatin1());
{//USERS
for(const auto e : o["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);
}
password=QCryptographicHash::hash(QString("admin").toLatin1(),QCryptographicHash::Sha3_256);
return [=](){ControlPanel::getLastCreated()->loadFromJsonObject(o["ControlPanel"].toObject());};
}
......
......@@ -6,9 +6,9 @@
namespace ApplicationData{
bool saveData(QFile &file);
bool loadData(QFile &file);
std::function<void()> loadData(QFile &file);
QByteArray saveData();
void loadData(QByteArray data);
std::function<void()> loadData(QByteArray data);
}
......
Supports Markdown
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