applicationdata.cpp 2.77 KB
Newer Older
1
#include "applicationdata.h"
2
3
4
5
#include <QJsonDocument>
#include "idbase.h"
#include <QJsonArray>

Leander Schulten's avatar
Leander Schulten committed
6
7
8
9
10
#include "channel.h"
#include "deviceprototype.h"
#include "device.h"
#include "programmprototype.h"
#include "programm.h"
11
#include "usermanagment.h"
Leander Schulten's avatar
Leander Schulten committed
12
13
#include "controlpanel.h"
#include <QCryptographicHash>
14
15
16

namespace ApplicationData{

Leander Schulten's avatar
Leander Schulten committed
17
18
19

QByteArray password;

Leander Schulten's avatar
Leander Schulten committed
20
bool saveData(QFile &file){
21
    if(!file.open(QIODevice::WriteOnly))return false;
Leander Schulten's avatar
Leander Schulten committed
22
    auto data = saveData();
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    file.write(data);
    file.close();
    return true;
}

template<typename Subclass>
void saveIDBaseObjects(QJsonObject &o,QString entryName ){
    QJsonArray array;
    for(const auto p : IDBase<Subclass>::getAllIDBases()){
        QJsonObject o;
        p->writeJsonObject(o);
        array.append(o);
    }
    o.insert(entryName,array);
}

QByteArray saveData(){
Leander Schulten's avatar
Leander Schulten committed
40
    qDebug()<<"SAVE DATA";
41
    QJsonObject o;
Leander Schulten's avatar
Leander Schulten committed
42
43
44
45
    saveIDBaseObjects<DevicePrototype>(o,"DevicePrototypes");
    saveIDBaseObjects<Device>(o,"Devices");
    saveIDBaseObjects<ProgrammPrototype>(o,"ProgrammPrototypes");
    saveIDBaseObjects<Programm>(o,"Programms");
Leander Schulten's avatar
Leander Schulten committed
46
47
    saveIDBaseObjects<User>(o,"Users");
    o.insert("password",QString::fromLatin1(password.toBase64()));
48
49
    {
        QJsonObject u;
Leander Schulten's avatar
Leander Schulten committed
50
51
        ControlPanel::getLastCreated()->writeJsonObject(u);
        o.insert("ControlPanel",u);
52
    }
Leander Schulten's avatar
Leander Schulten committed
53
54
55
56
57
58
59
60
61
62
    return QJsonDocument(o).toJson();
}

template<typename Subclass>
void loadIDBaseObjects(const QJsonObject &o,QString entryName ){
    for(const auto e : o[entryName].toArray()){
        new Subclass(e.toObject());
    }
}

Leander Schulten's avatar
Leander Schulten committed
63
std::function<void()> loadData(QFile &file){
Leander Schulten's avatar
Leander Schulten committed
64
    if(!file.open(QIODevice::ReadOnly)){
Leander Schulten's avatar
Leander Schulten committed
65
        return [](){qDebug()<<"Error: can not open file";};
Leander Schulten's avatar
Leander Schulten committed
66
    }
Leander Schulten's avatar
Leander Schulten committed
67
    auto r = loadData(file.readAll());
Leander Schulten's avatar
Leander Schulten committed
68
    file.close();
Leander Schulten's avatar
Leander Schulten committed
69
    return r;
Leander Schulten's avatar
Leander Schulten committed
70
71
}

Leander Schulten's avatar
Leander Schulten committed
72
73

std::function<void()> loadData(QByteArray data){
Leander Schulten's avatar
Leander Schulten committed
74
75
76
77
78
    const auto o = QJsonDocument::fromJson(data).object();
    loadIDBaseObjects<DevicePrototype>(o,"DevicePrototypes");
    loadIDBaseObjects<Device>(o,"Devices");
    loadIDBaseObjects<ProgrammPrototype>(o,"ProgrammPrototypes");
    loadIDBaseObjects<Programm>(o,"Programms");
Leander Schulten's avatar
Leander Schulten committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    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());};

94
}
95
96
97


}