From d2bf94ebb94d9a5c5d3e24da2a8cb05596d1786d Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Tue, 23 Mar 2021 12:46:12 +0300 Subject: [PATCH] UI: Connections: Add "Add Program" button. To add/edit selected connection's program. --- src/ui/3rdparty/sqlite/sqlitedb.cpp | 6 ++++ src/ui/3rdparty/sqlite/sqlitedb.h | 1 + src/ui/form/conn/connectionswindow.cpp | 14 ++++++++ src/ui/form/conn/connectionswindow.h | 1 + src/ui/form/prog/programswindow.cpp | 17 ++++++++++ src/ui/form/prog/programswindow.h | 6 ++++ src/ui/fortmanager.cpp | 6 ++++ src/ui/fortmanager.h | 2 ++ src/ui/model/applistmodel.cpp | 46 +++++++++++++++++--------- src/ui/model/applistmodel.h | 3 ++ src/ui/model/connlistmodel.cpp | 3 +- src/ui/model/zonelistmodel.cpp | 3 +- 12 files changed, 88 insertions(+), 20 deletions(-) diff --git a/src/ui/3rdparty/sqlite/sqlitedb.cpp b/src/ui/3rdparty/sqlite/sqlitedb.cpp index 837684b4..7acba7cb 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.cpp +++ b/src/ui/3rdparty/sqlite/sqlitedb.cpp @@ -135,6 +135,12 @@ bool SqliteDb::prepare(SqliteStmt &stmt, const char *sql, const QVariantList &va return stmt.prepare(db(), sql) && (vars.isEmpty() || stmt.bindVars(vars)); } +bool SqliteDb::prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars) +{ + const auto sqlData = sql.toUtf8(); + return prepare(stmt, sqlData.constData(), vars); +} + bool SqliteDb::done(SqliteStmt *stmt) { const SqliteStmt::StepResult res = stmt->step(); diff --git a/src/ui/3rdparty/sqlite/sqlitedb.h b/src/ui/3rdparty/sqlite/sqlitedb.h index 35cfd89b..7e5c7aae 100644 --- a/src/ui/3rdparty/sqlite/sqlitedb.h +++ b/src/ui/3rdparty/sqlite/sqlitedb.h @@ -37,6 +37,7 @@ public: int resultCount = 1, bool *ok = nullptr); bool prepare(SqliteStmt &stmt, const char *sql, const QVariantList &vars = QVariantList()); + bool prepare(SqliteStmt &stmt, const QString &sql, const QVariantList &vars = QVariantList()); bool done(SqliteStmt *stmt); qint64 lastInsertRowid() const; diff --git a/src/ui/form/conn/connectionswindow.cpp b/src/ui/form/conn/connectionswindow.cpp index cd39cdc0..5efde867 100644 --- a/src/ui/form/conn/connectionswindow.cpp +++ b/src/ui/form/conn/connectionswindow.cpp @@ -72,6 +72,7 @@ void ConnectionsWindow::onRetranslateUi() m_btEdit->setText(tr("Edit")); m_actCopy->setText(tr("Copy")); + m_actAddProgram->setText(tr("Add Program")); m_actRemoveConn->setText(tr("Remove")); m_actClearConns->setText(tr("Clear All")); @@ -131,6 +132,9 @@ QLayout *ConnectionsWindow::setupHeader() m_actCopy = editMenu->addAction(IconCache::icon(":/icons/copy.png"), QString()); m_actCopy->setShortcut(Qt::Key_Copy); + m_actAddProgram = editMenu->addAction(IconCache::icon(":/icons/window.png"), QString()); + m_actAddProgram->setShortcut(Qt::Key_Insert); + m_actRemoveConn = editMenu->addAction(IconCache::icon(":/icons/sign-delete.png"), QString()); m_actRemoveConn->setShortcut(Qt::Key_Delete); @@ -138,6 +142,15 @@ QLayout *ConnectionsWindow::setupHeader() connect(m_actCopy, &QAction::triggered, this, [&] { GuiUtil::setClipboardData(m_connListView->selectedText()); }); + connect(m_actAddProgram, &QAction::triggered, this, [&] { + const auto connIndex = connListCurrentIndex(); + const auto connRow = connListModel()->connRowAt(connIndex); + + if (!fortManager()->showProgramEditForm(connRow.appPath)) { + fortManager()->showErrorBox( + tr("Please close already opened Edit Program window and try again.")); + } + }); connect(m_actRemoveConn, &QAction::triggered, this, [&] { if (fortManager()->showQuestionBox( tr("Are you sure to remove connections till this row?"))) { @@ -291,6 +304,7 @@ void ConnectionsWindow::setupTableConnsChanged() const int connIndex = connListCurrentIndex(); const bool connSelected = (connIndex >= 0); m_actCopy->setEnabled(connSelected); + m_actAddProgram->setEnabled(connSelected); m_actRemoveConn->setEnabled(connSelected); m_appInfoRow->setVisible(connSelected); }; diff --git a/src/ui/form/conn/connectionswindow.h b/src/ui/form/conn/connectionswindow.h index 1b578e3a..0db6af89 100644 --- a/src/ui/form/conn/connectionswindow.h +++ b/src/ui/form/conn/connectionswindow.h @@ -63,6 +63,7 @@ private: QPushButton *m_btEdit = nullptr; QAction *m_actCopy = nullptr; + QAction *m_actAddProgram = nullptr; QAction *m_actRemoveConn = nullptr; QAction *m_actClearConns = nullptr; QPushButton *m_btLogOptions = nullptr; diff --git a/src/ui/form/prog/programswindow.cpp b/src/ui/form/prog/programswindow.cpp index 070dfc2c..3166da7d 100644 --- a/src/ui/form/prog/programswindow.cpp +++ b/src/ui/form/prog/programswindow.cpp @@ -498,6 +498,17 @@ void ProgramsWindow::setupTableAppsChanged() connect(m_appListView, &TableView::currentIndexChanged, this, refreshTableAppsChanged); } +bool ProgramsWindow::openAppEditFormByPath(const QString &appPath) +{ + if (m_formAppEdit->isVisible()) + return false; + + const auto appRow = appListModel()->appRowByPath(appPath); + + openAppEditFormByRow(appRow, false, true); + return true; +} + void ProgramsWindow::updateAppEditForm(bool editCurrentApp) { bool isSingleSelection = true; @@ -514,6 +525,12 @@ void ProgramsWindow::updateAppEditForm(bool editCurrentApp) appRow = appListModel()->appRowAt(appIndex); } + openAppEditFormByRow(appRow, editCurrentApp, isSingleSelection); +} + +void ProgramsWindow::openAppEditFormByRow( + const AppRow &appRow, bool editCurrentApp, bool isSingleSelection) +{ m_formAppIsNew = !editCurrentApp; m_editPath->setText(isSingleSelection ? appRow.appPath : "*"); diff --git a/src/ui/form/prog/programswindow.h b/src/ui/form/prog/programswindow.h index 45e7dbc1..4d34683a 100644 --- a/src/ui/form/prog/programswindow.h +++ b/src/ui/form/prog/programswindow.h @@ -23,6 +23,8 @@ class FortSettings; class ProgramsController; class TableView; +struct AppRow; + class ProgramsWindow : public WidgetWindow { Q_OBJECT @@ -30,6 +32,8 @@ class ProgramsWindow : public WidgetWindow public: explicit ProgramsWindow(FortManager *fortManager, QWidget *parent = nullptr); + bool openAppEditFormByPath(const QString &appPath); + protected slots: void onSaveWindowState(); void onRestoreWindowState(); @@ -57,6 +61,8 @@ private: void setupTableAppsChanged(); void updateAppEditForm(bool editCurrentApp); + void openAppEditFormByRow(const AppRow &appRow, bool editCurrentApp, bool isSingleSelection); + bool saveAppEditForm(); bool saveAppEditFormMulti(const QString &appPath, const QString &appName, const QDateTime &endTime, int groupIndex, bool useGroupPerm, bool blocked); diff --git a/src/ui/fortmanager.cpp b/src/ui/fortmanager.cpp index e4fdcedc..b63e73fc 100644 --- a/src/ui/fortmanager.cpp +++ b/src/ui/fortmanager.cpp @@ -368,6 +368,12 @@ void FortManager::closeProgramsWindow() m_progWindow = nullptr; } +bool FortManager::showProgramEditForm(const QString &appPath) +{ + showProgramsWindow(); + return m_progWindow->openAppEditFormByPath(appPath); +} + void FortManager::showOptionsWindow() { if (!(m_optWindow && m_optWindow->isVisible()) && !checkPassword()) diff --git a/src/ui/fortmanager.h b/src/ui/fortmanager.h index abdb41d2..d35dfb01 100644 --- a/src/ui/fortmanager.h +++ b/src/ui/fortmanager.h @@ -87,6 +87,8 @@ public slots: void showProgramsWindow(); void closeProgramsWindow(); + bool showProgramEditForm(const QString &appPath); + void showOptionsWindow(); void closeOptionsWindow(); diff --git a/src/ui/model/applistmodel.cpp b/src/ui/model/applistmodel.cpp index 9315218f..77dd2693 100644 --- a/src/ui/model/applistmodel.cpp +++ b/src/ui/model/applistmodel.cpp @@ -244,6 +244,25 @@ QString AppListModel::getAppName(const AppRow &appRow) const return appName; } +bool AppListModel::updateAppRow(const QString &sql, const QVariantList &vars, AppRow &appRow) const +{ + SqliteStmt stmt; + if (!(sqliteDb()->prepare(stmt, sql, vars) && stmt.step() == SqliteStmt::StepRow)) + return false; + + appRow.appId = stmt.columnInt64(0); + appRow.groupIndex = stmt.columnInt(1); + appRow.appPath = stmt.columnText(2); + appRow.appName = stmt.columnText(3); + appRow.useGroupPerm = stmt.columnBool(4); + appRow.blocked = stmt.columnBool(5); + appRow.alerted = stmt.columnBool(6); + appRow.endTime = stmt.columnDateTime(7); + appRow.creatTime = stmt.columnDateTime(8); + + return true; +} + const AppRow &AppListModel::appRowAt(int row) const { updateRowCache(row); @@ -251,6 +270,16 @@ const AppRow &AppListModel::appRowAt(int row) const return m_appRow; } +AppRow AppListModel::appRowByPath(const QString &appPath) const +{ + AppRow appRow; + appRow.appPath = appPath; + + updateAppRow(sqlBase() + " WHERE path = ?1;", { appPath }, appRow); + + return appRow; +} + bool AppListModel::addApp(const QString &appPath, const QString &appName, const QDateTime &endTime, int groupIndex, bool useGroupPerm, bool blocked) { @@ -323,22 +352,7 @@ void AppListModel::purgeApps() bool AppListModel::updateTableRow(int row) const { - SqliteStmt stmt; - if (!(sqliteDb()->prepare(stmt, sql().toLatin1(), { row }) - && stmt.step() == SqliteStmt::StepRow)) - return false; - - m_appRow.appId = stmt.columnInt64(0); - m_appRow.groupIndex = stmt.columnInt(1); - m_appRow.appPath = stmt.columnText(2); - m_appRow.appName = stmt.columnText(3); - m_appRow.useGroupPerm = stmt.columnBool(4); - m_appRow.blocked = stmt.columnBool(5); - m_appRow.alerted = stmt.columnBool(6); - m_appRow.endTime = stmt.columnDateTime(7); - m_appRow.creatTime = stmt.columnDateTime(8); - - return true; + return updateAppRow(sql(), { row }, m_appRow); } QString AppListModel::sqlBase() const diff --git a/src/ui/model/applistmodel.h b/src/ui/model/applistmodel.h index daaf15ec..30b0838c 100644 --- a/src/ui/model/applistmodel.h +++ b/src/ui/model/applistmodel.h @@ -54,6 +54,7 @@ public: QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; const AppRow &appRowAt(int row) const; + AppRow appRowByPath(const QString &appPath) const; bool addApp(const QString &appPath, const QString &appName, const QDateTime &endTime, int groupIndex, bool useGroupPerm, bool blocked); @@ -83,6 +84,8 @@ private: QString getAppName(const AppRow &appRow) const; + bool updateAppRow(const QString &sql, const QVariantList &vars, AppRow &appRow) const; + private: ConfManager *m_confManager = nullptr; AppInfoCache *m_appInfoCache = nullptr; diff --git a/src/ui/model/connlistmodel.cpp b/src/ui/model/connlistmodel.cpp index 51f67e13..6edef858 100644 --- a/src/ui/model/connlistmodel.cpp +++ b/src/ui/model/connlistmodel.cpp @@ -221,8 +221,7 @@ bool ConnListModel::updateTableRow(int row) const const qint64 rowId = rowIdMin() + row; SqliteStmt stmt; - if (!(sqliteDb()->prepare(stmt, sql().toLatin1(), { rowId }) - && stmt.step() == SqliteStmt::StepRow)) + if (!(sqliteDb()->prepare(stmt, sql(), { rowId }) && stmt.step() == SqliteStmt::StepRow)) return false; m_connRow.rowId = rowId; diff --git a/src/ui/model/zonelistmodel.cpp b/src/ui/model/zonelistmodel.cpp index e8d3e2e5..c88caa93 100644 --- a/src/ui/model/zonelistmodel.cpp +++ b/src/ui/model/zonelistmodel.cpp @@ -238,8 +238,7 @@ QVariant ZoneListModel::zoneSourceByCode(const QString &sourceCode) const bool ZoneListModel::updateTableRow(int row) const { SqliteStmt stmt; - if (!(sqliteDb()->prepare(stmt, sql().toLatin1(), { row }) - && stmt.step() == SqliteStmt::StepRow)) + if (!(sqliteDb()->prepare(stmt, sql(), { row }) && stmt.step() == SqliteStmt::StepRow)) return false; m_zoneRow.zoneId = stmt.columnInt(0);