Compare commits
2 Commits
89bd070613
...
f2bff8d0e2
| Author | SHA1 | Date | |
|---|---|---|---|
| f2bff8d0e2 | |||
| 47348a1678 |
@@ -37,6 +37,7 @@ target_link_libraries(
|
||||
${CMAKE_PROJECT_NAME}
|
||||
Qt5::Core
|
||||
Qt5::Gui
|
||||
Qt5::Sql
|
||||
Qt5::Widgets
|
||||
)
|
||||
|
||||
|
||||
3
deps/tables.ddl
vendored
3
deps/tables.ddl
vendored
@@ -10,6 +10,3 @@ CREATE TABLE IF NOT EXISTS clients (
|
||||
yourCompany TEXT NOT NULL,
|
||||
comment TEXT
|
||||
);
|
||||
|
||||
COMMENT ON TABLE clients IS '';
|
||||
COMMENT ON FIELD clients.id IS '';
|
||||
|
||||
@@ -1,16 +1,46 @@
|
||||
#include "LicenseModel.h"
|
||||
|
||||
// Qt
|
||||
#include <QtSql/QSqlDatabase>
|
||||
#include <QtSql/QSqlQuery>
|
||||
#include <QtSql/QSqlError>
|
||||
#include <QDebug>
|
||||
|
||||
// Self
|
||||
#include <qfile.h>
|
||||
|
||||
#include "../def.h"
|
||||
|
||||
const static int COLUMN_COUNT = 9;
|
||||
|
||||
LicenseModel::LicenseModel(QObject* parent)
|
||||
: QAbstractTableModel(parent)
|
||||
{
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE");
|
||||
m_db.setDatabaseName(DB_PATH);
|
||||
|
||||
if (!m_db.open())
|
||||
{
|
||||
m_status = Status::DbExistError;
|
||||
m_errors.append("Database connection failed: " + m_db.lastError().text());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!checkTables())
|
||||
{
|
||||
m_status = Status::DbStructError;
|
||||
m_errors.append("Database tables are not valid");
|
||||
}
|
||||
else
|
||||
{
|
||||
m_status = Status::Ok;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LicenseModel::~LicenseModel()
|
||||
{
|
||||
|
||||
m_db.close();
|
||||
}
|
||||
|
||||
int LicenseModel::rowCount(const QModelIndex &parent) const
|
||||
@@ -72,10 +102,48 @@ QVariant LicenseModel::headerData(int section, Qt::Orientation orientation, int
|
||||
|
||||
LicenseModel::Status LicenseModel::getStatus()
|
||||
{
|
||||
return Status::Ok;
|
||||
return m_status;
|
||||
}
|
||||
|
||||
QString LicenseModel::getStatusText()
|
||||
{
|
||||
return m_errors.join('n');
|
||||
return m_errors.join('\n');
|
||||
}
|
||||
|
||||
bool LicenseModel::checkTables()
|
||||
{
|
||||
bool clienttableExist = false;
|
||||
for (const auto &table : m_db.tables())
|
||||
{
|
||||
if (table == "clients")
|
||||
return true;
|
||||
}
|
||||
if (!clienttableExist)
|
||||
return false;
|
||||
}
|
||||
|
||||
bool LicenseModel::prepareDatabase()
|
||||
{
|
||||
QFile tablesFile(QStringLiteral(":/deps/tables.ddl"));
|
||||
if (!tablesFile.open(QIODevice::ReadOnly | QIODevice::Text))
|
||||
return false;
|
||||
|
||||
m_status = Status::None;
|
||||
m_errors.clear();
|
||||
|
||||
for (auto item : QString(tablesFile.readAll()).split(';'))
|
||||
{
|
||||
if (item.trimmed().isEmpty())
|
||||
continue;
|
||||
QSqlQuery query(item.trimmed() + ";", m_db);
|
||||
if (!query.exec())
|
||||
{
|
||||
m_status = Status::DbStructError;
|
||||
qDebug() << item.trimmed() + ";" << query.lastError().text();
|
||||
m_errors.append(query.lastError().text());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define LICENSEMANAGER_LICENSEMODEL_H
|
||||
|
||||
#include <QAbstractTableModel>
|
||||
#include <QtSql/QSqlDatabase>
|
||||
|
||||
class LicenseModel : public QAbstractTableModel
|
||||
{
|
||||
@@ -41,9 +42,16 @@ public:
|
||||
Status getStatus();
|
||||
QString getStatusText();
|
||||
|
||||
bool prepareDatabase();
|
||||
|
||||
private:
|
||||
bool checkTables();
|
||||
|
||||
private:
|
||||
QList<LicenseItem> m_data;
|
||||
Status m_status = Status::None;
|
||||
QStringList m_errors;
|
||||
QSqlDatabase m_db;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,21 +1,20 @@
|
||||
#include "MainWidget.h"
|
||||
|
||||
// Qt
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include <QHeaderView>
|
||||
#include <QLabel>
|
||||
#include <QMenu>
|
||||
#include <QMessageBox>
|
||||
#include <QPushButton>
|
||||
#include <QTableView>
|
||||
#include <QTimer>
|
||||
#include <QToolBar>
|
||||
#include <QVBoxLayout>
|
||||
#include <QContextMenuEvent>
|
||||
|
||||
// Qt
|
||||
#include <QAction>
|
||||
|
||||
// Self
|
||||
#include <QMessageBox>
|
||||
|
||||
#include "LicenseModel/LicenseModel.h"
|
||||
|
||||
MainWidget::MainWidget(QWidget *parent)
|
||||
@@ -26,6 +25,25 @@ MainWidget::MainWidget(QWidget *parent)
|
||||
setWindowTitle("LicenseManager");
|
||||
resize({800, 600});
|
||||
|
||||
// Model init
|
||||
{
|
||||
if (m_licenseModel->getStatus() == LicenseModel::Status::DbStructError)
|
||||
{
|
||||
if (!m_licenseModel->prepareDatabase())
|
||||
{
|
||||
QTimer::singleShot(0, [&]() {
|
||||
QMessageBox messageBox;
|
||||
messageBox.setIcon(QMessageBox::Critical);
|
||||
messageBox.setWindowTitle(tr("Error"));
|
||||
messageBox.setText(tr("Error with prepare database"));
|
||||
messageBox.setDetailedText(m_licenseModel->getStatusText());
|
||||
messageBox.exec();
|
||||
QApplication::quit();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ui
|
||||
{
|
||||
m_tableView = new QTableView(this);
|
||||
|
||||
Reference in New Issue
Block a user