#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::readJsonFile(const QString title) { QFile file(title); file.open( QIODevice::ReadOnly); QByteArray bytes = file.readAll(); file.close(); QJsonDocument document = QJsonDocument::fromJson( bytes ); return document.object(); } void MainWindow::saveJsonFile(QJsonObject &users, const QString name) const { QFile jsonFile(name); 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::thisBlogIdIsTaken() const { QMessageBox idTaken; idTaken.setText("THIS BLOG ID IS ALREADY TAKEN!"); idTaken.exec(); } void MainWindow::saveRegisteredUser(QJsonObject &users, QJsonObject &blogs) 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; users.insert(id, user); /* 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 */ QJsonObject blog; QString blogTitle = ui -> inputBlogTitle->text(); QString blogId = ui -> inputBlogID->text(); if(blogs.find(blogId) == blogs.end()) { blog["ownerId"] = id; blog["title"] = blogTitle; blog["blogId"] = blogId; blogs.insert(blogId, blog); saveJsonFile(blogs, "blogs.json"); saveJsonFile(users, "user.json"); }else thisBlogIdIsTaken(); }else thisIdIsTaken(); } void MainWindow::on_pushButton_clicked() { QJsonObject blogs = readJsonFile("blogs.json"); QJsonObject users = readJsonFile("user.json"); saveRegisteredUser(users, blogs); } void MainWindow::goToLogin() { login *l = new login(); l -> show(); hide(); } void MainWindow::on_loginButton_clicked() { goToLogin(); }