2022-04-01 21:28:09 +02:00
|
|
|
#include "mainwindow.h" // Notepad class header file that was generated by the wizard
|
2022-04-03 21:23:02 +02:00
|
|
|
#include "ui_mainwindow.h"// UI header file that was generated by the uic tool
|
|
|
|
|
#include "login.h"
|
2022-04-06 14:30:57 +02:00
|
|
|
#include "universalFunctions.h"
|
2022-04-01 21:28:09 +02:00
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <QJsonObject>
|
|
|
|
|
#include <QJsonDocument>
|
|
|
|
|
#include <QTextStream>
|
2022-04-06 01:06:01 +02:00
|
|
|
#include <QJsonArray>
|
2022-03-26 12:43:58 +01:00
|
|
|
|
2022-04-01 21:28:09 +02:00
|
|
|
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:
|
2022-03-26 12:43:58 +01:00
|
|
|
{
|
2022-04-01 21:28:09 +02:00
|
|
|
ui->setupUi(this); // setup user interface
|
2022-04-06 14:30:57 +02:00
|
|
|
defineConnections();
|
2022-03-26 12:43:58 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-06 14:30:57 +02:00
|
|
|
void MainWindow::defineConnections() const
|
2022-04-03 21:23:02 +02:00
|
|
|
{
|
2022-04-06 14:30:57 +02:00
|
|
|
connect(ui -> loginButton, &QPushButton::clicked, this, &MainWindow::goToLogin);
|
|
|
|
|
connect(ui -> pushButton, &QPushButton::clicked, this, &MainWindow::saveRegisteredUser);
|
|
|
|
|
connect(ui -> actionExit, &QAction::triggered, this, &MainWindow::exit);
|
|
|
|
|
connect(ui -> actionLogin, &QAction::triggered, this, &MainWindow::goToLogin);
|
2022-04-03 21:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-06 16:31:31 +02:00
|
|
|
void MainWindow::exit() const
|
2022-04-01 21:28:09 +02:00
|
|
|
{
|
2022-04-06 14:30:57 +02:00
|
|
|
QApplication::quit();
|
2022-04-03 21:23:02 +02:00
|
|
|
}
|
2022-04-01 21:28:09 +02:00
|
|
|
|
2022-04-06 14:30:57 +02:00
|
|
|
MainWindow::~MainWindow()
|
2022-04-05 00:26:36 +02:00
|
|
|
{
|
2022-04-06 14:30:57 +02:00
|
|
|
delete ui; // in the destructor, we delete the ui
|
2022-04-05 00:26:36 +02:00
|
|
|
}
|
|
|
|
|
|
2022-04-06 16:31:31 +02:00
|
|
|
QJsonObject MainWindow::createUserObject(const QString &mail, const QString &password, const QString &userId) const
|
|
|
|
|
{
|
|
|
|
|
QJsonObject user;
|
|
|
|
|
user["userId"] = userId;
|
|
|
|
|
// userId - unique user id - text obtained from the user during user registration
|
|
|
|
|
user["email"] = mail;
|
|
|
|
|
// email - e-mail address of the user
|
|
|
|
|
user["password"] = password;
|
|
|
|
|
// password - password provided by the user
|
|
|
|
|
return user;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonObject MainWindow::insertUserObject(QJsonObject &users, const QString &id)
|
|
|
|
|
{
|
|
|
|
|
QString mail = ui->inputMail->text();
|
|
|
|
|
QString password = ui->inputPassword->text();
|
|
|
|
|
QJsonObject user = createUserObject(mail, password, id);
|
|
|
|
|
users.insert(id, user);
|
|
|
|
|
return users;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QJsonObject MainWindow::createBlogObject(const QString &id, const QString &ownerId) const
|
|
|
|
|
{
|
|
|
|
|
QJsonObject blog;
|
|
|
|
|
blog["blogId"] = id;
|
|
|
|
|
QString title = ui -> inputBlogTitle -> text();
|
|
|
|
|
blog["title"] = title;
|
|
|
|
|
blog["ownerId"] = ownerId;
|
|
|
|
|
QJsonArray items;
|
|
|
|
|
blog["items"] = items;
|
|
|
|
|
return blog;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QJsonObject MainWindow::insertBlogObject(QJsonObject &blogs, const QString &blogId, const QString &ownerId)
|
|
|
|
|
{
|
|
|
|
|
QJsonObject blog = createBlogObject(blogId, ownerId);
|
|
|
|
|
blogs.insert(blogId, blog);
|
|
|
|
|
return blogs;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MainWindow::saveFiles(QJsonObject &users, QJsonObject &blogs)
|
|
|
|
|
{
|
|
|
|
|
saveJsonFile(blogs, "blogs.json");
|
|
|
|
|
saveJsonFile(users, "user.json");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::registerUser(QJsonObject &users, QJsonObject &blogs, const QString &id, const QString &blogId)
|
|
|
|
|
{
|
|
|
|
|
users = insertUserObject(users, id);
|
|
|
|
|
blogs = insertBlogObject(blogs, blogId, id);
|
|
|
|
|
saveFiles(users, blogs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::saveRegisteredUser()
|
2022-04-03 21:23:02 +02:00
|
|
|
{
|
2022-04-06 14:30:57 +02:00
|
|
|
QJsonObject blogs = readJsonFile("blogs.json");
|
|
|
|
|
QJsonObject users = readJsonFile("user.json");
|
2022-04-06 16:31:31 +02:00
|
|
|
|
2022-04-03 21:23:02 +02:00
|
|
|
QString id = ui->inputId->text();
|
2022-04-06 16:31:31 +02:00
|
|
|
if (stringEmpty(id, "THIS USER ID IS EMPTY!")) return;
|
|
|
|
|
if (idExists(id, users, "THIS USER ID IS ALREADY TAKEN!")) return;
|
|
|
|
|
|
|
|
|
|
QString blogId = ui -> inputBlogID->text();
|
|
|
|
|
if (stringEmpty(blogId, "THIS BLOG ID IS EMPTY!")) return;
|
|
|
|
|
if (idExists(blogId, blogs, "THIS BLOG ID IS ALREADY TAKEN!")) return;
|
|
|
|
|
|
|
|
|
|
registerUser(users, blogs, id, blogId);
|
2022-04-03 21:23:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MainWindow::goToLogin()
|
|
|
|
|
{
|
|
|
|
|
login *l = new login();
|
|
|
|
|
l -> show();
|
|
|
|
|
hide();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 16:31:31 +02:00
|
|
|
|
2022-03-26 12:43:58 +01:00
|
|
|
|
2022-04-06 14:30:57 +02:00
|
|
|
|