Fixed referencing and dereferencing errors in apparatus
This commit is contained in:
parent
d69b18f083
commit
f99974d8ae
12 changed files with 148 additions and 112 deletions
|
|
@ -1,10 +1,10 @@
|
|||
#include "adminpanel.h"
|
||||
#include "ui_adminpanel.h"
|
||||
|
||||
|
||||
AdminPanel::AdminPanel(QWidget *parent) : QMainWindow(parent), ui(new Ui::AdminPanel) {
|
||||
ui->setupUi(this);
|
||||
|
||||
uvm = new UsersViewModel(this);
|
||||
|
||||
// connect(ui->pb_logout, &QPushButton::clicked, this, &AdminPanel::on_logout_requested);
|
||||
|
||||
|
|
@ -20,9 +20,14 @@ AdminPanel::AdminPanel(QWidget *parent) : QMainWindow(parent), ui(new Ui::AdminP
|
|||
// connect(ui->pb_dp_add, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_add);
|
||||
// connect(ui->pb_dp_remove, &QPushButton::clicked, this, &AdminPanel::on_delivery_point_remove);
|
||||
|
||||
|
||||
// ui->tv_vessels->setModel();
|
||||
|
||||
uvm = new UsersViewModel(this);
|
||||
ui->tv_users->setModel(uvm);
|
||||
|
||||
// ui->tv_dp->setModel();
|
||||
|
||||
// ui->tv_storages->setModel();
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -23,8 +23,9 @@ void AuthWindow::on_auth_requested() {
|
|||
|
||||
bool success = false;
|
||||
auto a = apparatus::instance()->get_auth_subsystem();
|
||||
|
||||
if (apparatus::isFirstRun()) {
|
||||
success = a.register_user(login, passw, UserRole::ADMINISTRATOR);
|
||||
success = a->register_user(login, passw, UserRole::ADMINISTRATOR);
|
||||
if (!success) {
|
||||
QMessageBox::critical(this, "Error", "Cannot register you. Check filesystem permission");
|
||||
return;
|
||||
|
|
@ -35,7 +36,7 @@ void AuthWindow::on_auth_requested() {
|
|||
}
|
||||
}
|
||||
|
||||
auto user = a.get_user(login, success);
|
||||
auto user = a->get_user(login, success);
|
||||
if (!success) {
|
||||
QMessageBox::critical(this, "Error", "User not found");
|
||||
return;
|
||||
|
|
@ -48,18 +49,25 @@ void AuthWindow::on_auth_requested() {
|
|||
}
|
||||
|
||||
|
||||
QWidget *w;
|
||||
if (user->role() == UserRole::ADMINISTRATOR) {
|
||||
AdminPanel(nullptr).set_user(*user).showNormal();
|
||||
w = new AdminPanel(nullptr);
|
||||
((AdminPanel*) w)->set_user(*user);
|
||||
}
|
||||
else if (user->role() == UserRole::DISPATCHER) {
|
||||
// DispatcherPanel(nullptr, user).set_user(user).show();
|
||||
return;
|
||||
}
|
||||
else if (user->role() == UserRole::SKIPPER) {
|
||||
// SkipperPanel(nullptr, user).set_user(user).show();
|
||||
return;
|
||||
}
|
||||
else {
|
||||
QMessageBox::critical(this, "Error", "Deserialized user have wrong type. "
|
||||
"It may mean corruption of data.");
|
||||
return;
|
||||
}
|
||||
|
||||
w->show();
|
||||
close();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ int main(int argc, char *argv[]) {
|
|||
w.show();
|
||||
|
||||
QObject::connect(&a, &QApplication::aboutToQuit, []() {
|
||||
apparatus::shutdown();
|
||||
if (apparatus::isFirstRun()) {
|
||||
apparatus::instance()->save();
|
||||
if (apparatus::isFirstRun() && apparatus::instance()->get_auth_subsystem()->users().length() > 0) {
|
||||
apparatus::generate_lock_file();
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -4,27 +4,15 @@
|
|||
apparatus *apparatus::_instance = nullptr;
|
||||
const QString apparatus::filename = "data.bin";
|
||||
|
||||
void apparatus::open_stream() {
|
||||
this->_bin_file = new QFile(apparatus::filename);
|
||||
this->_bin_file->open(QIODevice::ReadWrite);
|
||||
|
||||
stream.setDevice(_bin_file);
|
||||
}
|
||||
|
||||
void apparatus::close_stream() {
|
||||
stream.setDevice(nullptr);
|
||||
|
||||
this->_bin_file->close();
|
||||
delete this->_bin_file;
|
||||
this->_bin_file = nullptr;
|
||||
}
|
||||
|
||||
apparatus::apparatus() {
|
||||
|
||||
this->_auth_system = new auth_system();
|
||||
this->_object_system = new object_system();
|
||||
}
|
||||
|
||||
apparatus::~apparatus() {
|
||||
|
||||
this->save();
|
||||
delete this->_auth_system;
|
||||
delete this->_object_system;
|
||||
}
|
||||
|
||||
void apparatus::generate_lock_file() {
|
||||
|
|
@ -42,52 +30,79 @@ apparatus* apparatus::instance() {
|
|||
return apparatus::_instance;
|
||||
}
|
||||
|
||||
void apparatus::save() {
|
||||
if (_instance == nullptr) {
|
||||
return;
|
||||
}
|
||||
QFile f(apparatus::filename);
|
||||
f.open(QIODevice::WriteOnly);
|
||||
QDataStream stream(&f);
|
||||
|
||||
// saving GIDs
|
||||
entity_id vgid = vessel_entity::GID();
|
||||
entity_id sgid = storage_entity::GID();
|
||||
stream << vgid << sgid;
|
||||
|
||||
// serializing data
|
||||
this->_auth_system->serialize_data(&stream);
|
||||
this->_object_system->serialize_data(&stream);
|
||||
|
||||
f.close();
|
||||
}
|
||||
|
||||
void apparatus::load() {
|
||||
if (_instance == nullptr) {
|
||||
throw std::runtime_error("HOW DU FUCK INSTANCE IS NULL????");
|
||||
}
|
||||
QFile f(apparatus::filename);
|
||||
f.open(QIODevice::ReadOnly);
|
||||
QDataStream stream(&f);
|
||||
|
||||
// loading GIDs
|
||||
entity_id vgid, sgid;
|
||||
stream >> vgid >> sgid;
|
||||
vessel_entity::preloadGlobalId(vgid);
|
||||
storage_entity::preloadGlobalId(sgid);
|
||||
|
||||
// deserializing data
|
||||
this->_auth_system->deserialize_data(&stream);
|
||||
this->_object_system->deserialize_data(&stream);
|
||||
|
||||
f.close();
|
||||
}
|
||||
|
||||
bool apparatus::isFirstRun() {
|
||||
return !QFile().exists("lock");
|
||||
}
|
||||
|
||||
auth_system& apparatus::get_auth_subsystem() {
|
||||
auth_system* apparatus::get_auth_subsystem() {
|
||||
return this->_auth_system;
|
||||
}
|
||||
|
||||
object_system& apparatus::get_object_subsystem() {
|
||||
object_system* apparatus::get_object_subsystem() {
|
||||
return this->_object_system;
|
||||
}
|
||||
|
||||
void apparatus::init() {
|
||||
if (apparatus::_instance != nullptr) {
|
||||
throw std::runtime_error("System already initialized!");
|
||||
}
|
||||
|
||||
bool fr = apparatus::isFirstRun();
|
||||
apparatus::_instance = new apparatus();
|
||||
|
||||
apparatus::_instance->open_stream();
|
||||
apparatus::_instance->loadGIDS();
|
||||
apparatus::_instance->deserialize_data();
|
||||
if (fr) {
|
||||
if (!QFile().exists(apparatus::filename)) {
|
||||
QFile init(apparatus::filename);
|
||||
init.open(QIODevice::ReadWrite);
|
||||
init.close();
|
||||
}
|
||||
apparatus::_instance->save();
|
||||
}
|
||||
|
||||
apparatus::_instance->load();
|
||||
}
|
||||
|
||||
void apparatus::shutdown() {
|
||||
apparatus::_instance->writeGIDS();
|
||||
apparatus::_instance->serialize_data();
|
||||
apparatus::_instance->close_stream();
|
||||
delete apparatus::_instance;
|
||||
}
|
||||
|
||||
void apparatus::writeGIDS() {
|
||||
entity_id vgid = vessel_entity::GID();
|
||||
entity_id sgid = storage_entity::GID();
|
||||
this->stream << vgid << sgid;
|
||||
}
|
||||
|
||||
void apparatus::loadGIDS() {
|
||||
entity_id vgid, sgid;
|
||||
this->stream >> vgid >> sgid;
|
||||
vessel_entity::preloadGlobalId(vgid);
|
||||
storage_entity::preloadGlobalId(sgid);
|
||||
}
|
||||
|
||||
void apparatus::serialize_data() {
|
||||
this->_auth_system.serialize_data(this->stream);
|
||||
this->_object_system.serialize_data(this->stream);
|
||||
}
|
||||
|
||||
void apparatus::deserialize_data() {
|
||||
this->_auth_system.deserialize_data(this->stream);
|
||||
this->_object_system.deserialize_data(this->stream);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,29 +18,18 @@ private:
|
|||
static apparatus *_instance;
|
||||
static const QString filename;
|
||||
|
||||
QFile *_bin_file;
|
||||
QDataStream stream;
|
||||
|
||||
auth_system _auth_system;
|
||||
object_system _object_system;
|
||||
|
||||
void open_stream();
|
||||
void close_stream();
|
||||
|
||||
void writeGIDS();
|
||||
void loadGIDS();
|
||||
auth_system* _auth_system;
|
||||
object_system* _object_system;
|
||||
|
||||
public:
|
||||
apparatus();
|
||||
explicit apparatus();
|
||||
~apparatus();
|
||||
|
||||
void generate_empty_data();
|
||||
auth_system& get_auth_subsystem();
|
||||
object_system& get_object_subsystem();
|
||||
auth_system* get_auth_subsystem();
|
||||
object_system* get_object_subsystem();
|
||||
|
||||
|
||||
void serialize_data();
|
||||
void deserialize_data();
|
||||
void save();
|
||||
void load();
|
||||
|
||||
static bool isFirstRun();
|
||||
static void generate_lock_file();
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
#include "apparatus.h"
|
||||
#include "auth_system.h"
|
||||
#include <iostream>
|
||||
|
||||
|
||||
const user_entity* auth_system::get_user(const QString &login, bool &success) {
|
||||
|
|
@ -32,29 +34,35 @@ bool auth_system::register_user(const QString &login, const QString &password, U
|
|||
bool exists = false;
|
||||
this->get_user(login, exists);
|
||||
if (!exists) {
|
||||
std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl;
|
||||
this->_users.push_back(user_entity(login, password, role));
|
||||
std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl;
|
||||
apparatus::instance()->save();
|
||||
std::cout << apparatus::instance()->get_auth_subsystem()->users().length() << std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
const QVector<user_entity> auth_system::users() const {
|
||||
const QVector<user_entity>& auth_system::users() const {
|
||||
return this->_users;
|
||||
}
|
||||
|
||||
void auth_system::deserialize_data(QDataStream &stream) {
|
||||
void auth_system::deserialize_data(QDataStream *stream) {
|
||||
int icnt;
|
||||
stream >> icnt;
|
||||
this->_users.resize(icnt);
|
||||
for (int i = 0; i < icnt; i++) {
|
||||
this->_users[i].deserialize(stream);
|
||||
*stream >> icnt;
|
||||
if (icnt > 0) {
|
||||
this->_users.resize(icnt);
|
||||
for (int i = 0; i < icnt; i++) {
|
||||
this->_users[i].deserialize(*stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void auth_system::serialize_data(QDataStream &stream) {
|
||||
stream << this->_users.size();
|
||||
void auth_system::serialize_data(QDataStream *stream) {
|
||||
*stream << this->_users.size();
|
||||
for (auto &item : this->_users) {
|
||||
item.serialize(stream);
|
||||
item.serialize(*stream);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,10 +17,10 @@ public:
|
|||
bool remove_user(const QString &login);
|
||||
bool register_user(const QString &login, const QString &password, UserRole role);
|
||||
|
||||
const QVector<user_entity> users() const;
|
||||
const QVector<user_entity>& users() const;
|
||||
|
||||
void deserialize_data(QDataStream &stream);
|
||||
void serialize_data(QDataStream &stream);
|
||||
void deserialize_data(QDataStream *stream);
|
||||
void serialize_data(QDataStream *stream);
|
||||
};
|
||||
|
||||
#endif // AUTH_SYSTEM_H
|
||||
|
|
|
|||
|
|
@ -85,30 +85,34 @@ const QVector<vessel_entity> object_system::vessels() const {
|
|||
return this->_vessels;
|
||||
}
|
||||
|
||||
void object_system::deserialize_data(QDataStream &stream) {
|
||||
void object_system::deserialize_data(QDataStream *stream) {
|
||||
int dicnt;
|
||||
stream >> dicnt;
|
||||
this->_dpoints.resize(dicnt);
|
||||
for (int i = 0; i < dicnt; i++) {
|
||||
this->_dpoints[i].deserialize(stream);
|
||||
*stream >> dicnt;
|
||||
if (dicnt > 0) {
|
||||
this->_dpoints.resize(dicnt);
|
||||
for (int i = 0; i < dicnt; i++) {
|
||||
this->_dpoints[i].deserialize(*stream);
|
||||
}
|
||||
}
|
||||
|
||||
int vicnt;
|
||||
stream >> vicnt;
|
||||
this->_vessels.resize(vicnt);
|
||||
for (int i = 0; i < vicnt; i++) {
|
||||
this->_vessels[i].deserialize(stream);
|
||||
*stream >> vicnt;
|
||||
if (vicnt > 0) {
|
||||
this->_vessels.resize(vicnt);
|
||||
for (int i = 0; i < vicnt; i++) {
|
||||
this->_vessels[i].deserialize(*stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void object_system::serialize_data(QDataStream &stream) {
|
||||
stream << this->_dpoints.size();
|
||||
void object_system::serialize_data(QDataStream *stream) {
|
||||
*stream << this->_dpoints.size();
|
||||
for (auto &item : this->_dpoints) {
|
||||
item.serialize(stream);
|
||||
item.serialize(*stream);
|
||||
}
|
||||
|
||||
stream << this->_vessels.size();
|
||||
*stream << this->_vessels.size();
|
||||
for (auto &item : this->_vessels) {
|
||||
item.serialize(stream);
|
||||
item.serialize(*stream);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -28,8 +28,8 @@ public:
|
|||
const QVector<dpoint_entity> dpoints() const;
|
||||
const QVector<vessel_entity> vessels() const;
|
||||
|
||||
void deserialize_data(QDataStream &stream);
|
||||
void serialize_data(QDataStream &stream);
|
||||
void deserialize_data(QDataStream *stream);
|
||||
void serialize_data(QDataStream *stream);
|
||||
};
|
||||
|
||||
#endif // OBJECT_SYSTEM_H
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ UsersViewModel::UsersViewModel(QObject *parent) : QAbstractTableModel(parent) {
|
|||
}
|
||||
|
||||
int UsersViewModel::rowCount(const QModelIndex & /*parent*/) const {
|
||||
return apparatus::instance()->get_object_subsystem().vessels().size();
|
||||
return apparatus::instance()->get_object_subsystem()->vessels().size();
|
||||
}
|
||||
|
||||
int UsersViewModel::columnCount(const QModelIndex & /*parent*/) const {
|
||||
|
|
@ -28,7 +28,7 @@ QVariant UsersViewModel::headerData(int section, Qt::Orientation orientation, in
|
|||
|
||||
QVariant UsersViewModel::data(const QModelIndex &index, int role) const {
|
||||
if (role == Qt::DisplayRole) {
|
||||
auto item = apparatus::instance()->get_auth_subsystem().users()[index.row()];
|
||||
auto item = apparatus::instance()->get_auth_subsystem()->users()[index.row()];
|
||||
int col = index.column();
|
||||
|
||||
switch (col) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE QtCreatorProject>
|
||||
<!-- Written by QtCreator 4.13.3, 2020-12-18T19:53:31. -->
|
||||
<!-- Written by QtCreator 4.13.3, 2020-12-22T14:58:07. -->
|
||||
<qtcreator>
|
||||
<data>
|
||||
<variable>EnvironmentId</variable>
|
||||
|
|
@ -65,10 +65,17 @@
|
|||
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
|
||||
</valuemap>
|
||||
<valuemap type="QVariantMap" key="AutoTest.CheckStates">
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:apparatus_check_auth_subsystem">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:apparatus_check_first_run">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:apparatus_check_null_throws_error">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:apparatus_check_object_subsystem">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:cargo_entity_serialization_test">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:cleanupTestCase">Unchecked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:dpoint_entity_serialization_test">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:initTestCase">Unchecked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:st_test">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:storage_entity_serialization_test">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:user_entity_password_verification_test">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:user_entity_serialization_test">Checked</value>
|
||||
<value type="Qt::CheckState" key="C:/Users/Admin/Documents/repos/sea_transport_project/st_test/tst_st_test.cpp:vessel_entity_serialization_test">Checked</value>
|
||||
</valuemap>
|
||||
|
|
@ -99,7 +106,7 @@
|
|||
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5150.win64_mingw81_kit</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">1</value>
|
||||
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
|
||||
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
|
||||
<value type="bool">true</value>
|
||||
<value type="int" key="EnableQmlDebugging">0</value>
|
||||
|
|
|
|||
|
|
@ -245,18 +245,18 @@ void st_test::apparatus_check_first_run() {
|
|||
|
||||
void st_test::apparatus_check_auth_subsystem() {
|
||||
apparatus::init();
|
||||
auth_system as = apparatus::instance()->get_auth_subsystem();
|
||||
auto as = apparatus::instance()->get_auth_subsystem();
|
||||
{
|
||||
bool test = as.register_user("testor", "passwd", UserRole::ADMINISTRATOR);
|
||||
bool test = as->register_user("testor", "passwd", UserRole::ADMINISTRATOR);
|
||||
QVERIFY(test);
|
||||
}
|
||||
{
|
||||
bool test;
|
||||
as.get_user("testor", test);
|
||||
as->get_user("testor", test);
|
||||
QVERIFY(test);
|
||||
}
|
||||
{
|
||||
bool test = as.remove_user("testor");
|
||||
bool test = as->remove_user("testor");
|
||||
QVERIFY(test);
|
||||
}
|
||||
apparatus::shutdown();
|
||||
|
|
@ -264,19 +264,19 @@ void st_test::apparatus_check_auth_subsystem() {
|
|||
|
||||
void st_test::apparatus_check_object_subsystem() {
|
||||
apparatus::init();
|
||||
object_system os = apparatus::instance()->get_object_subsystem();
|
||||
auto os = apparatus::instance()->get_object_subsystem();
|
||||
dpoint_entity p("test");
|
||||
{
|
||||
bool test = os.add_dpoint(p);
|
||||
bool test = os->add_dpoint(p);
|
||||
QVERIFY(test);
|
||||
}
|
||||
{
|
||||
bool test;
|
||||
os.get_dpoint(p.id(), test);
|
||||
os->get_dpoint(p.id(), test);
|
||||
QVERIFY(test);
|
||||
}
|
||||
{
|
||||
bool test = os.remove_dpoint(p.id());
|
||||
bool test = os->remove_dpoint(p.id());
|
||||
QVERIFY(test);
|
||||
}
|
||||
apparatus::shutdown();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue