UI: ConfZoneManager: Simplify addZone()/updateZone()

This commit is contained in:
Nodir Temirkhodjaev 2023-12-26 16:41:01 +03:00
parent 65b52ad0b9
commit bcbc21550d
9 changed files with 84 additions and 132 deletions

View File

@ -21,6 +21,12 @@ const char *const sqlInsertZone = "INSERT INTO zone(zone_id, name, enabled, cust
" source_code, url, form_data, text_inline)"
" VALUES(?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8);";
const char *const sqlUpdateZone = "UPDATE zone"
" SET name = ?2, enabled = ?3, custom_url = ?4,"
" source_code = ?5, url = ?6,"
" form_data = ?7, text_inline = ?8"
" WHERE zone_id = ?1;";
const char *const sqlSelectZoneIds = "SELECT zone_id FROM zone ORDER BY zone_id;";
const char *const sqlDeleteZone = "DELETE FROM zone WHERE zone_id = ?1;";
@ -33,12 +39,6 @@ const char *const sqlDeleteAppZone = "UPDATE app"
" SET accept_zones = accept_zones & ?1,"
" reject_zones = reject_zones & ?1;";
const char *const sqlUpdateZone = "UPDATE zone"
" SET name = ?2, enabled = ?3, custom_url = ?4,"
" source_code = ?5, url = ?6,"
" form_data = ?7, text_inline = ?8"
" WHERE zone_id = ?1;";
const char *const sqlUpdateZoneName = "UPDATE zone SET name = ?2 WHERE zone_id = ?1;";
const char *const sqlUpdateZoneEnabled = "UPDATE zone SET enabled = ?2 WHERE zone_id = ?1;";
@ -49,6 +49,24 @@ const char *const sqlUpdateZoneResult =
" source_modtime = ?5, last_run = ?6, last_success = ?7"
" WHERE zone_id = ?1;";
int getFreeZoneId(SqliteDb *sqliteDb)
{
int zoneId = 1;
SqliteStmt stmt;
if (stmt.prepare(sqliteDb->db(), sqlSelectZoneIds)) {
while (stmt.step() == SqliteStmt::StepRow) {
const int id = stmt.columnInt(0);
if (id > zoneId)
break;
zoneId = id + 1;
}
}
return zoneId;
}
void showErrorMessage(const QString &errorMessage)
{
IoC<WindowManager>()->showErrorBox(
@ -90,43 +108,35 @@ void ConfZoneManager::setUp()
m_confManager = IoC()->setUpDependency<ConfManager>();
}
bool ConfZoneManager::addZone(Zone &zone)
bool ConfZoneManager::addOrUpdateZone(Zone &zone)
{
bool ok = false;
const bool isNew = (zone.zoneId == 0);
zone.zoneId = getFreeZoneId();
if (isNew) {
zone.zoneId = getFreeZoneId(sqliteDb());
} else {
updateDriverZoneFlag(zone.zoneId, zone.enabled);
}
const auto vars = QVariantList()
<< zone.zoneId << zone.zoneName << zone.enabled << zone.customUrl << zone.sourceCode
<< zone.url << zone.formData << zone.textInline;
sqliteDb()->executeEx(sqlInsertZone, vars, 0, &ok);
sqliteDb()->executeEx(isNew ? sqlInsertZone : sqlUpdateZone, vars, 0, &ok);
checkEndTransaction(ok);
if (ok) {
if (!ok)
return false;
if (isNew) {
emit zoneAdded();
} else {
emit zoneUpdated();
}
return ok;
}
int ConfZoneManager::getFreeZoneId()
{
int zoneId = 1;
SqliteStmt stmt;
if (stmt.prepare(sqliteDb()->db(), sqlSelectZoneIds)) {
while (stmt.step() == SqliteStmt::StepRow) {
const int id = stmt.columnInt(0);
if (id > zoneId)
break;
zoneId = id + 1;
}
}
return zoneId;
return true;
}
bool ConfZoneManager::deleteZone(int zoneId)
@ -155,28 +165,6 @@ bool ConfZoneManager::deleteZone(int zoneId)
return ok;
}
bool ConfZoneManager::updateZone(const Zone &zone)
{
if (!updateDriverZoneFlag(zone.zoneId, zone.enabled))
return false;
bool ok = false;
const auto vars = QVariantList()
<< zone.zoneId << zone.zoneName << zone.enabled << zone.customUrl << zone.sourceCode
<< zone.url << zone.formData << zone.textInline;
sqliteDb()->executeEx(sqlUpdateZone, vars, 0, &ok);
checkEndTransaction(ok);
if (ok) {
emit zoneUpdated();
}
return ok;
}
bool ConfZoneManager::updateZoneName(int zoneId, const QString &zoneName)
{
bool ok = false;

View File

@ -25,10 +25,8 @@ public:
void setUp() override;
virtual bool addZone(Zone &zone);
int getFreeZoneId();
virtual bool addOrUpdateZone(Zone &zone);
virtual bool deleteZone(int zoneId);
virtual bool updateZone(const Zone &zone);
virtual bool updateZoneName(int zoneId, const QString &zoneName);
virtual bool updateZoneEnabled(int zoneId, bool enabled);
bool updateZoneResult(const Zone &zone);

View File

@ -26,9 +26,8 @@ const char *const commandString(Command cmd)
CASE_STRING(Rpc_ConfManager_checkPassword)
CASE_STRING(Rpc_ConfManager_confChanged)
CASE_STRING(Rpc_ConfZoneManager_addZone)
CASE_STRING(Rpc_ConfZoneManager_addOrUpdateZone)
CASE_STRING(Rpc_ConfZoneManager_deleteZone)
CASE_STRING(Rpc_ConfZoneManager_updateZone)
CASE_STRING(Rpc_ConfZoneManager_updateZoneName)
CASE_STRING(Rpc_ConfZoneManager_updateZoneEnabled)
CASE_STRING(Rpc_ConfZoneManager_zoneAdded)
@ -116,9 +115,8 @@ RpcManager managerByCommand(Command cmd)
Rpc_ConfManager, // Rpc_ConfManager_checkPassword,
Rpc_ConfManager, // Rpc_ConfManager_confChanged,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_addZone,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_addOrUpdateZone,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_deleteZone,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_updateZone,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_updateZoneName,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_updateZoneEnabled,
Rpc_ConfZoneManager, // Rpc_ConfZoneManager_zoneAdded,
@ -187,9 +185,8 @@ bool commandRequiresValidation(Command cmd)
0, // Rpc_ConfManager_checkPassword,
0, // Rpc_ConfManager_confChanged,
true, // Rpc_ConfZoneManager_addZone,
true, // Rpc_ConfZoneManager_addOrUpdateZone,
true, // Rpc_ConfZoneManager_deleteZone,
true, // Rpc_ConfZoneManager_updateZone,
true, // Rpc_ConfZoneManager_updateZoneName,
true, // Rpc_ConfZoneManager_updateZoneEnabled,
0, // Rpc_ConfZoneManager_zoneAdded,

View File

@ -25,9 +25,8 @@ enum Command : qint8 {
Rpc_ConfManager_checkPassword,
Rpc_ConfManager_confChanged,
Rpc_ConfZoneManager_addZone,
Rpc_ConfZoneManager_addOrUpdateZone,
Rpc_ConfZoneManager_deleteZone,
Rpc_ConfZoneManager_updateZone,
Rpc_ConfZoneManager_updateZoneName,
Rpc_ConfZoneManager_updateZoneEnabled,
Rpc_ConfZoneManager_zoneAdded,

View File

@ -486,7 +486,7 @@ void ZonesWindow::updateZoneEditForm(bool isNew)
const ZoneSourceWrapper zoneSource(zoneListModel()->zoneSourceByCode(zoneRow.sourceCode));
m_formZoneIsNew = isNew;
m_zoneId = zoneRow.zoneId;
m_editZoneName->setText(zoneRow.zoneName);
m_editZoneName->selectAll();
@ -525,6 +525,7 @@ bool ZonesWindow::saveZoneEditForm()
}
Zone zone;
zone.zoneId = m_zoneId;
zone.zoneName = m_editZoneName->text();
zone.sourceCode = zoneSource.code();
zone.enabled = m_cbEnabled->isChecked();
@ -538,11 +539,11 @@ bool ZonesWindow::saveZoneEditForm()
return false;
// Add new zone
if (m_formZoneIsNew) {
if (zone.zoneId == 0) {
return saveZoneEditFormNew(zone);
}
// Edit selected zone
// Update the zone
return saveZoneEditFormEdit(zone);
}
@ -568,7 +569,7 @@ bool ZonesWindow::saveZoneEditFormValidate(const Zone &zone, const ZoneSourceWra
bool ZonesWindow::saveZoneEditFormNew(Zone &zone)
{
if (confZoneManager()->addZone(zone)) {
if (confZoneManager()->addOrUpdateZone(zone)) {
m_zoneListView->selectCell(zone.zoneId - 1);
return true;
}
@ -590,19 +591,7 @@ bool ZonesWindow::saveZoneEditFormEdit(Zone &zone)
zone.zoneId = zoneRow.zoneId;
return confZoneManager()->updateZone(zone);
}
void ZonesWindow::updateZone(int row, bool enabled)
{
const auto zoneRow = zoneListModel()->zoneRowAt(row);
if (zoneRow.isNull())
return;
Zone zone = zoneRow;
zone.enabled = enabled;
confZoneManager()->updateZone(zone);
return confZoneManager()->addOrUpdateZone(zone);
}
void ZonesWindow::deleteZone(int row)
@ -614,11 +603,6 @@ void ZonesWindow::deleteZone(int row)
confZoneManager()->deleteZone(zoneRow.zoneId);
}
void ZonesWindow::updateSelectedZone(bool enabled)
{
updateZone(zoneListCurrentIndex(), enabled);
}
void ZonesWindow::deleteSelectedZone()
{
deleteZone(zoneListCurrentIndex());

View File

@ -79,16 +79,13 @@ private:
bool saveZoneEditFormNew(Zone &zone);
bool saveZoneEditFormEdit(Zone &zone);
void updateZone(int row, bool enabled);
void deleteZone(int row);
void updateSelectedZone(bool enabled);
void deleteSelectedZone();
int zoneListCurrentIndex() const;
private:
bool m_formZoneIsNew = false;
int m_zoneId = 0;
ZonesController *m_ctrl = nullptr;
WidgetWindowStateWatcher *m_stateWatcher = nullptr;

View File

@ -8,14 +8,12 @@
ConfZoneManagerRpc::ConfZoneManagerRpc(QObject *parent) : ConfZoneManager(parent) { }
bool ConfZoneManagerRpc::addZone(Zone &zone)
bool ConfZoneManagerRpc::addOrUpdateZone(Zone &zone)
{
QVariantList resArgs;
if (!IoC<RpcManager>()->doOnServer(Control::Rpc_ConfZoneManager_addZone,
{ zone.enabled, zone.customUrl, zone.zoneName, zone.sourceCode, zone.url,
zone.formData, zone.textInline },
&resArgs))
if (!IoC<RpcManager>()->doOnServer(
Control::Rpc_ConfZoneManager_addOrUpdateZone, zoneToVarList(zone), &resArgs))
return false;
zone.zoneId = resArgs.value(0).toInt();
@ -28,13 +26,6 @@ bool ConfZoneManagerRpc::deleteZone(int zoneId)
return IoC<RpcManager>()->doOnServer(Control::Rpc_ConfZoneManager_deleteZone, { zoneId });
}
bool ConfZoneManagerRpc::updateZone(const Zone &zone)
{
return IoC<RpcManager>()->doOnServer(Control::Rpc_ConfZoneManager_updateZone,
{ zone.enabled, zone.customUrl, zone.zoneId, zone.zoneName, zone.sourceCode, zone.url,
zone.formData, zone.textInline });
}
bool ConfZoneManagerRpc::updateZoneName(int zoneId, const QString &zoneName)
{
return IoC<RpcManager>()->doOnServer(
@ -46,3 +37,23 @@ bool ConfZoneManagerRpc::updateZoneEnabled(int zoneId, bool enabled)
return IoC<RpcManager>()->doOnServer(
Control::Rpc_ConfZoneManager_updateZoneEnabled, { zoneId, enabled });
}
QVariantList ConfZoneManagerRpc::zoneToVarList(const Zone &zone)
{
return { zone.enabled, zone.customUrl, zone.zoneId, zone.zoneName, zone.sourceCode, zone.url,
zone.formData, zone.textInline };
}
Zone ConfZoneManagerRpc::varListToZone(const QVariantList &v)
{
Zone zone;
zone.enabled = v.value(0).toBool();
zone.customUrl = v.value(1).toBool();
zone.zoneId = v.value(2).toInt();
zone.zoneName = v.value(3).toString();
zone.sourceCode = v.value(4).toString();
zone.url = v.value(5).toString();
zone.formData = v.value(6).toString();
zone.textInline = v.value(7).toString();
return zone;
}

View File

@ -12,11 +12,13 @@ class ConfZoneManagerRpc : public ConfZoneManager
public:
explicit ConfZoneManagerRpc(QObject *parent = nullptr);
bool addZone(Zone &zone) override;
bool addOrUpdateZone(Zone &zone) override;
bool deleteZone(int zoneId) override;
bool updateZone(const Zone &zone) override;
bool updateZoneName(int zoneId, const QString &zoneName) override;
bool updateZoneEnabled(int zoneId, bool enabled) override;
static QVariantList zoneToVarList(const Zone &zone);
static Zone varListToZone(const QVariantList &v);
};
#endif // CONFZONEMANAGERRPC_H

View File

@ -183,19 +183,12 @@ inline bool processConfAppManagerRpcResult(
return false;
}
bool processConfZoneManager_addZone(
bool processConfZoneManager_addOrUpdateZone(
ConfZoneManager *confZoneManager, const ProcessCommandArgs &p, QVariantList &resArgs)
{
Zone zone;
zone.enabled = p.args.value(0).toBool();
zone.customUrl = p.args.value(1).toBool();
zone.zoneName = p.args.value(2).toString();
zone.sourceCode = p.args.value(3).toString();
zone.url = p.args.value(4).toString();
zone.formData = p.args.value(5).toString();
zone.textInline = p.args.value(6).toString();
Zone zone = ConfZoneManagerRpc::varListToZone(p.args);
const bool ok = confZoneManager->addZone(zone);
const bool ok = confZoneManager->addOrUpdateZone(zone);
resArgs = { zone.zoneId };
return ok;
}
@ -206,22 +199,6 @@ bool processConfZoneManager_deleteZone(
return confZoneManager->deleteZone(p.args.value(0).toLongLong());
}
bool processConfZoneManager_updateZone(
ConfZoneManager *confZoneManager, const ProcessCommandArgs &p, QVariantList & /*resArgs*/)
{
Zone zone;
zone.enabled = p.args.value(0).toBool();
zone.customUrl = p.args.value(1).toBool();
zone.zoneId = p.args.value(2).toInt();
zone.zoneName = p.args.value(3).toString();
zone.sourceCode = p.args.value(4).toString();
zone.url = p.args.value(5).toString();
zone.formData = p.args.value(6).toString();
zone.textInline = p.args.value(7).toString();
return confZoneManager->updateZone(zone);
}
bool processConfZoneManager_updateZoneName(
ConfZoneManager *confZoneManager, const ProcessCommandArgs &p, QVariantList & /*resArgs*/)
{
@ -240,9 +217,8 @@ using processConfZoneManager_func = bool (*)(
ConfZoneManager *confZoneManager, const ProcessCommandArgs &p, QVariantList &resArgs);
static processConfZoneManager_func processConfZoneManager_funcList[] = {
&processConfZoneManager_addZone, // Rpc_ConfZoneManager_addZone,
&processConfZoneManager_addOrUpdateZone, // Rpc_ConfZoneManager_addOrUpdateZone,
&processConfZoneManager_deleteZone, // Rpc_ConfZoneManager_deleteZone,
&processConfZoneManager_updateZone, // Rpc_ConfZoneManager_updateZone,
&processConfZoneManager_updateZoneName, // Rpc_ConfZoneManager_updateZoneName,
&processConfZoneManager_updateZoneEnabled, // Rpc_ConfZoneManager_updateZoneEnabled,
};
@ -250,10 +226,10 @@ static processConfZoneManager_func processConfZoneManager_funcList[] = {
inline bool processConfZoneManagerRpcResult(
ConfZoneManager *confZoneManager, const ProcessCommandArgs &p, QVariantList &resArgs)
{
if (p.command >= Control::Rpc_ConfZoneManager_addZone
if (p.command >= Control::Rpc_ConfZoneManager_addOrUpdateZone
&& p.command <= Control::Rpc_ConfZoneManager_updateZoneEnabled) {
const int funcIndex = p.command - Control::Rpc_ConfZoneManager_addZone;
const int funcIndex = p.command - Control::Rpc_ConfZoneManager_addOrUpdateZone;
const processConfZoneManager_func func = processConfZoneManager_funcList[funcIndex];
return func(confZoneManager, p, resArgs);