Skip to content

Commit

Permalink
allow close/reload of database file
Browse files Browse the repository at this point in the history
  • Loading branch information
squinky86 committed Mar 18, 2019
1 parent 0c5d16f commit 4df48f3
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 2 deletions.
34 changes: 34 additions & 0 deletions src/dbmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "common.h"

#include <cstdlib>
#include <QCryptographicHash>
#include <QFile>
#include <QMessageBox>
#include <QSqlQuery>
Expand Down Expand Up @@ -652,6 +653,23 @@ bool DbManager::DeleteCCIs()
return ret;
}

/**
* @brief DbManager::DeleteDB
* @return @c True when the database is recreated. Otherwise,
* @c false.
*/
bool DbManager::DeleteDB()
{
QFile dest(_dbPath);
if (dest.open(QFile::WriteOnly))
{
dest.write("", 0);
dest.close();
return UpdateDatabaseFromVersion(0);
}
return false;
}

/**
* @brief DbManager::DeleteEmassImport
* @return \c True when the eMASS import is deleted. Otherwise,
Expand Down Expand Up @@ -1924,6 +1942,22 @@ bool DbManager::SaveDB(const QString &path)
return false;
}

/**
* @brief DbManager::HashDB
* @return The SHA3_256 hash of the database file
*/
QByteArray DbManager::HashDB()
{
QFile source(_dbPath);
QByteArray ret;
if (source.open(QFile::ReadOnly))
{
ret = QCryptographicHash::hash(qCompress(source.readAll(), 9), QCryptographicHash::Sha3_256);
source.close();
}
return ret;
}

/**
* @brief DbManager::UpdateAsset
* @param asset
Expand Down
2 changes: 2 additions & 0 deletions src/dbmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class DbManager
bool DeleteAsset(int id);
bool DeleteAsset(const Asset &asset);
bool DeleteCCIs();
bool DeleteDB();
bool DeleteEmassImport();
bool DeleteSTIG(int id);
bool DeleteSTIG(const STIG &stig);
Expand Down Expand Up @@ -92,6 +93,7 @@ class DbManager

bool LoadDB(const QString &path);
bool SaveDB(const QString &path);
QByteArray HashDB();

bool UpdateAsset(const Asset &asset);
bool UpdateCCI(const CCI &cci);
Expand Down
50 changes: 49 additions & 1 deletion src/stigqter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@
#include "ui_stigqter.h"
#include "workerhtml.h"

#include <QThread>
#include <QCryptographicHash>
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QProcess>
#include <QThread>

/**
* @class STIGQter
Expand Down Expand Up @@ -163,6 +165,52 @@ void STIGQter::OpenCKL()
}
}

/**
* @brief STIGQter::Reset
*
* Check if the .stigqter file has been saved before closing the
* database. If it has not, give the user an opportunity to save it.
*/
void STIGQter::Reset()
{
if (lastSaveLocation.isNull() || lastSaveLocation.isEmpty())
{
QMessageBox::StandardButton reply = QMessageBox::question(this, QStringLiteral("Unsaved Changes"), QStringLiteral("The data have not been saved. Really close?"), QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
db->DeleteDB();
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
}
}
else
{
//check if saved database is up-to-date
QFile dest(lastSaveLocation);
dest.open(QFile::ReadOnly);
QByteArray destHash = QCryptographicHash::hash(dest.readAll(), QCryptographicHash::Sha3_256);
dest.close();
if (destHash == db->HashDB())
{
//database was saved without changes; reset application.
db->DeleteDB();
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
}
else
{
//there are unsaved changes; verify closing the DB
QMessageBox::StandardButton reply = QMessageBox::question(this, QStringLiteral("Unsaved Changes"), QStringLiteral("There are unsaved changes to the file you wrote. Really close?"), QMessageBox::Yes|QMessageBox::No);
if (reply == QMessageBox::Yes)
{
db->DeleteDB();
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments());
}
}
}
}

/**
* @brief STIGQter::Save
*
Expand Down
1 change: 1 addition & 0 deletions src/stigqter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ private slots:
void ImportEMASS();
void Load();
void OpenCKL();
void Reset();
void Save();
void SaveAs();
void SelectAsset();
Expand Down
17 changes: 17 additions & 0 deletions src/stigqter.ui
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,22 @@
</hint>
</hints>
</connection>
<connection>
<sender>actionClear_Database</sender>
<signal>triggered()</signal>
<receiver>STIGQter</receiver>
<slot>Reset()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>217</x>
<y>264</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>UpdateCCIs()</slot>
Expand All @@ -758,5 +774,6 @@
<slot>Load()</slot>
<slot>SaveAs()</slot>
<slot>ExportHTML()</slot>
<slot>Reset()</slot>
</slots>
</ui>
2 changes: 1 addition & 1 deletion src/workercciadd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void WorkerCCIAdd::process()
}
}
db.DelayCommit(false);
emit initialize(todo.size() + 1, 1);
emit initialize(todo.size() + 959, 1); //# of base controls: 958
delete xml;

//Step 3a: Additional Privacy Controls
Expand Down

0 comments on commit 4df48f3

Please sign in to comment.