#include "mainwindow.h" // Notepad class header file that was generated by the wizard #include "ui_mainwindow.h"// UI header file that was generated by the uic tool #include "login.h" #include #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) // class definition, constructor : QMainWindow(parent) // calls qmainwindow parent constructor we have, // base class for the MainWindow class: , ui(new Ui::MainWindow) // create ui class and assign to ui member // creates the UI class instance and assigns it to the ui member: { ui->setupUi(this); // setup user interface } MainWindow::~MainWindow() { delete ui; // in the destructor, we delete the ui } QJsonObject MainWindow::readUserJsonFile() { QFile file("user.json"); file.open( QIODevice::ReadOnly); QByteArray bytes = file.readAll(); file.close(); QJsonDocument document = QJsonDocument::fromJson( bytes ); return document.object(); } void MainWindow::saveJsonFile(QJsonObject &users) const { qDebug() << QFile::exists(QStringLiteral(":/user.json")); qDebug() << QFile::exists(QStringLiteral("./user.json")); qDebug() << QFile::exists(QStringLiteral("user.json")); QFile jsonFile(QStringLiteral("user.json")); QJsonDocument document; document.setObject( users ); QByteArray bytes = document.toJson( QJsonDocument::Indented ); jsonFile.open( QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate ); QTextStream iStream( &jsonFile ); // iStream.setCodec( "utf-8" ); iStream << bytes; jsonFile.close(); } // You cannot create a user without an id void MainWindow::thisIdIsEmpty() const { QMessageBox idEmpty; idEmpty.setText("THIS ID IS EMPTY!"); idEmpty.exec(); } // You cannot create a user with id similar to an existing one void MainWindow::thisIdIsTaken() const { QMessageBox idTaken; idTaken.setText("THIS ID IS ALREADY TAKEN!"); idTaken.exec(); } void MainWindow::saveRegisteredUser(QJsonObject &users) const { QString id = ui->inputId->text(); if(id == "") { thisIdIsEmpty(); return; } if(users.find(id) == users.end()) { QString mail = ui->inputMail->text(); QString password = ui->inputPassword->text(); qDebug() << mail << " " << password; QJsonObject user; user["userId"] = id; user["email"] = mail; user["password"]=password; /* userId - unique user id - text obtained from the user during user registration email - e-mail address of the user password - password provided by the user */ users.insert(id, user); saveJsonFile(users); }else thisIdIsTaken(); } void MainWindow::on_pushButton_clicked() { QJsonObject users = readUserJsonFile(); saveRegisteredUser(users); } void MainWindow::goToLogin() { login *l = new login(); l -> show(); hide(); } void MainWindow::on_loginButton_clicked() { goToLogin(); }