-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
207 lines (184 loc) · 7.15 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "basescorechecker.h"
#include <QFileDialog>
#include "runcommandscorechecker.h"
#include "emptyscorechecker.h"
#include "configpropertieswindow.h"
#include "QMessageBox"
#include "version.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
statusBar()->showMessage("Program loaded.");
config= new ScoreCheckingConfig;
scoringmodel= new ScoreCheckerViewModel(this, config->vecScoreCheckers);
scoringdelegate= new ScoreCheckerViewDelegate(this);
SetupTable();
timer= new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(scoreupdate())); //tell timer to execute scoreupdate()
}
void MainWindow::SetupTable()
{
ui->mainTable->setModel(this->scoringmodel);
ui->mainTable->setItemDelegate(this->scoringdelegate);
ui->mainTable->setDragEnabled(true);
ui->mainTable->setDropIndicatorShown(true);
ui->mainTable->setDragDropMode(QAbstractItemView::DragDrop);
ui->mainTable->setDragDropOverwriteMode(false);
//leftTableView->setDragDropMode(QAbstractItemView::DragOnly);
//ui->mainTable->setDragDropMode(QAbstractItemView:: InternalMove);
QHeaderView *verticalHeader = ui->mainTable->verticalHeader();
QHeaderView *horizontalHeader = ui->mainTable->horizontalHeader();
verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
verticalHeader->setDefaultSectionSize(30);
horizontalHeader->setDefaultSectionSize(60);
}
MainWindow::~MainWindow()
{
delete config;
delete scoringmodel;
delete scoringdelegate;
delete timer;
delete ui;
}
void MainWindow::on_actionExit_triggered()
{
static_cast<QWidget*>(this->parent())->show(); //open up the start menu dialog
this->destroy(true, true);
this->close();
}
void MainWindow::on_actionInsert_Blank_triggered()
{
//Adds an EmptyScoreChecker() via the model
scoringmodel->insertChecker(new EmptyScoreChecker());
}
//***************
// Don't run this code --- please
// Just a test...
//
void MainWindow::TestScoreCheckers() //This is all a test. Not for production...
{
ScoreCheckingConfig configtosave;
PathExistScoreChecker pathcheck;
saveConfigXML(configtosave,"config_empty.xml");
configtosave.vecScoreCheckers->push_back(&pathcheck);
pathcheck.setFilepath("/etc/shadow");
pathcheck.setDesireExist(true);
pathcheck.checkState();
std::cout << "PathCheck returned " << pathcheck.getState() << std::endl;
RunCommandScoreChecker commandcheck;
configtosave.vecScoreCheckers->push_back(&commandcheck);
commandcheck.setCommand("sysctl net.ipv4.ip_forward");
commandcheck.setSearchString("net.ipv4.ip_forward = 0");
commandcheck.setSearchExist(false);
commandcheck.executeCommand();
commandcheck.checkState();
std::cout << "CommandCheck returned " << commandcheck.getState() << std::endl;
ValueScoreChecker valuecheck;
configtosave.vecScoreCheckers->push_back(&valuecheck);
valuecheck.setFilepath("/etc/ssh/sshd_config");
valuecheck.setDesireExist(true);
valuecheck.setSearchString("\nPermitRootLogin no");
valuecheck.checkState();
std::cout << "ValueCheck returned " << valuecheck.getState() << std::endl;
ScriptScoreChecker scriptcheck;
configtosave.vecScoreCheckers->push_back(&scriptcheck);
scriptcheck.setScript("#!/bin/bash\n"
"ls\n"
"lsattr /etc/passwd\n"
"#ps\n");
scriptcheck.setSearchString("--------------e---- /etc/passwd");
scriptcheck.setDesiredState(true);
scriptcheck.checkState();
std::cout << "ScriptCheck returned " << scriptcheck.getState() << std::endl;
saveConfigXML(configtosave,"config.xml");
saveConfigBIN(configtosave,"config.bin");
ScoreCheckingConfig loadedconfig;
this->config=new ScoreCheckingConfig();
loadConfigBIN(*config, "config.bin");
std::cout << "Loaded " << config->Name << std::endl;
std::cout << "SCript" << std::endl << static_cast<ScriptScoreChecker*>(config->vecScoreCheckers->at(3))->getScript() << std::endl;
//this->on_actionOpen_Config_triggered();
}
//Shows the preferences window
void MainWindow::on_actionConfiguration_Prefs_triggered()
{
ConfigPropertiesWindow *confmenu= new ConfigPropertiesWindow(this,this->config);
confmenu->show();
}
//Saves current config
void MainWindow::on_actionSave_Config_triggered()
{
QString filename= QFileDialog::getSaveFileName(this, tr("Save Config"),QString(),tr("Binary Files (*.bin);;XML Files (*.xml)"));
//
QString suffix = QFileInfo(filename).suffix();
if (QFileInfo(filename).suffix()=="bin")
{
saveConfigBIN(*config, filename.toStdString().c_str());
}else if (QFileInfo(filename).suffix()=="xml")
{
saveConfigXML(*config, filename.toStdString().c_str());
}
}
void MainWindow::on_actionOpen_Config_triggered()
{
QString filename= QFileDialog::getOpenFileName(this, tr("Open Config"),QString(),tr("Binary Files (*.bin);;XML Files (*.xml)"));
if (QFileInfo(filename).suffix()=="bin")
{
delete this->config;
this->config=new ScoreCheckingConfig();
loadConfigBIN(*config, filename.toStdString().c_str());
scoringmodel= new ScoreCheckerViewModel(this, config->vecScoreCheckers);
scoringdelegate= new ScoreCheckerViewDelegate(this);
this->SetupTable();
}else if (QFileInfo(filename).suffix()=="xml")
{
delete this->config;
this->config=new ScoreCheckingConfig();
loadConfigXML(*config, filename.toStdString().c_str());
scoringmodel= new ScoreCheckerViewModel(this, config->vecScoreCheckers);
scoringdelegate= new ScoreCheckerViewDelegate(this);
this->SetupTable();
}
}
void MainWindow::scoreupdate()
{
std::cout << "Times up" << std::endl;
config->GenerateScoreReport();
}
void MainWindow::on_actionTimer_Start_toggled(bool arg1)
{
if (arg1){if (this->config->checkSeconds==0)this->config->checkSeconds=5;this->timer->start(this->config->checkSeconds*1000);}else{this->timer->stop();}
}
void MainWindow::on_actionNew_Config_triggered()
{
if (true) //ask first before erasing
{
delete this->config;
this->config=new ScoreCheckingConfig();
scoringmodel= new ScoreCheckerViewModel(this, config->vecScoreCheckers);
scoringdelegate= new ScoreCheckerViewDelegate(this);
this->SetupTable();
}
}
void MainWindow::on_actionDelete_triggered()
{
QItemSelectionModel *select = ui->mainTable->selectionModel();
if (select->hasSelection())
{
//Removes selected scorecheckers via the model
scoringmodel->removeCheckers(select->selectedRows());
}
}
void MainWindow::on_actionAbout_triggered()
{
QMessageBox::about(this, QString("About ScoringEngine"),QString(std::string("ScoringEngine\nCopyright " + std::to_string(VERSION_YEAR) + " by Logan Bateman\nVersion " + std::to_string(VERSION_MAJOR) + "." + std::to_string(VERSION_MINOR) + "." + std::to_string(VERSION_PATCH)).c_str()));
}
void MainWindow::on_actionPlayer_Dashboard_triggered()
{
PlayerDashboard *dashboard= new PlayerDashboard(this,this->config);
dashboard->show();
}