UI: Conf: Refactor removed app groups handling.

This commit is contained in:
Nodir Temirkhodjaev 2021-05-09 15:03:00 +03:00
parent fbbe90b130
commit ab45fc565a
5 changed files with 37 additions and 44 deletions

View File

@ -144,32 +144,6 @@ QString AppGroup::menuLabel() const
return text; return text;
} }
void AppGroup::clear()
{
m_edited = true;
m_enabled = true;
m_logConn = true;
m_fragmentPacket = false;
m_periodEnabled = false;
m_limitInEnabled = false;
m_limitOutEnabled = false;
m_speedLimitIn = 0;
m_speedLimitOut = 0;
// m_id should pe preserved
m_name = QString();
m_blockText = QString();
m_allowText = QString();
m_periodFrom = QString();
m_periodTo = QString();
}
void AppGroup::copy(const AppGroup &o) void AppGroup::copy(const AppGroup &o)
{ {
m_edited = o.edited(); m_edited = o.edited();

View File

@ -63,7 +63,6 @@ public:
QString menuLabel() const; QString menuLabel() const;
void clear();
void copy(const AppGroup &o); void copy(const AppGroup &o);
QVariant toVariant() const; QVariant toVariant() const;

View File

@ -1025,20 +1025,20 @@ bool ConfManager::removeAppGroupsInDb(const FirewallConf &conf)
Q_ASSERT(!conf.appGroups().isEmpty()); Q_ASSERT(!conf.appGroups().isEmpty());
const auto defaultAppGroupId = conf.appGroups().at(0)->id(); const auto defaultAppGroupId = conf.appGroups().at(0)->id();
for (AppGroup *appGroup : conf.removedAppGroupsList()) { for (const qint64 appGroupId : conf.removedAppGroupIdList()) {
bool ok; bool ok;
sqliteDb()->executeEx(sqlUpdateAppResetGroup, sqliteDb()->executeEx(
QVariantList() << appGroup->id() << defaultAppGroupId, 0, &ok); sqlUpdateAppResetGroup, QVariantList() << appGroupId << defaultAppGroupId, 0, &ok);
if (!ok) if (!ok)
return false; return false;
sqliteDb()->executeEx(sqlDeleteAppGroup, QVariantList() << appGroup->id(), 0, &ok); sqliteDb()->executeEx(sqlDeleteAppGroup, QVariantList() << appGroupId, 0, &ok);
if (!ok) if (!ok)
return false; return false;
} }
conf.clearRemovedAppGroups(); conf.clearRemovedAppGroupIdList();
return true; return true;
} }

View File

@ -152,7 +152,8 @@ void FirewallConf::addAppGroup(AppGroup *appGroup, int to)
AppGroup *FirewallConf::addAppGroupByName(const QString &name) AppGroup *FirewallConf::addAppGroupByName(const QString &name)
{ {
auto appGroup = !m_removedAppGroups.isEmpty() ? m_removedAppGroups.takeLast() : new AppGroup(); auto appGroup = new AppGroup();
appGroup->setId(m_removedAppGroupIdList.isEmpty() ? 0 : m_removedAppGroupIdList.takeLast());
appGroup->setName(name); appGroup->setName(name);
addAppGroup(appGroup); addAppGroup(appGroup);
return appGroup; return appGroup;
@ -176,12 +177,10 @@ void FirewallConf::removeAppGroup(int from, int to)
const int hi = qMax(from, to); const int hi = qMax(from, to);
for (int i = hi; i >= lo; --i) { for (int i = hi; i >= lo; --i) {
AppGroup *appGroup = m_appGroups.at(i); AppGroup *appGroup = m_appGroups.at(i);
if (appGroup->id() == 0) { if (appGroup->id() > 0) {
appGroup->deleteLater(); m_removedAppGroupIdList.append(appGroup->id());
} else {
appGroup->clear();
m_removedAppGroups.append(appGroup);
} }
appGroup->deleteLater();
m_appGroups.removeAt(i); m_appGroups.removeAt(i);
} }
@ -195,10 +194,9 @@ void FirewallConf::addDefaultAppGroup()
appGroup->setAllowText(FileUtil::appBinLocation() + "/**"); appGroup->setAllowText(FileUtil::appBinLocation() + "/**");
} }
void FirewallConf::clearRemovedAppGroups() const void FirewallConf::clearRemovedAppGroupIdList() const
{ {
qDeleteAll(m_removedAppGroups); m_removedAppGroupIdList.clear();
m_removedAppGroups.clear();
} }
void FirewallConf::loadAppGroupBits() void FirewallConf::loadAppGroupBits()
@ -387,6 +385,23 @@ void FirewallConf::appGroupsFromVariant(const QVariant &v)
} }
} }
QVariant FirewallConf::removedAppGroupIdListToVariant() const
{
QVariantList list;
for (const qint64 id : removedAppGroupIdList()) {
list.append(id);
}
return list;
}
void FirewallConf::removedAppGroupIdListFromVariant(const QVariant &v)
{
const QVariantList list = v.toList();
for (const QVariant &v : list) {
m_removedAppGroupIdList.append(v.toLongLong());
}
}
QVariant FirewallConf::toVariant(bool onlyFlags) const QVariant FirewallConf::toVariant(bool onlyFlags) const
{ {
QVariantMap map; QVariantMap map;
@ -398,6 +413,7 @@ QVariant FirewallConf::toVariant(bool onlyFlags) const
if (!onlyFlags || optEdited()) { if (!onlyFlags || optEdited()) {
map["addressGroups"] = addressesToVariant(); map["addressGroups"] = addressesToVariant();
map["appGroups"] = appGroupsToVariant(); map["appGroups"] = appGroupsToVariant();
map["removedAppGroupIdList"] = removedAppGroupIdListToVariant();
} }
if (!onlyFlags || flagsEdited()) { if (!onlyFlags || flagsEdited()) {
@ -424,6 +440,7 @@ void FirewallConf::fromVariant(const QVariant &v, bool onlyFlags)
if (optEdited()) { if (optEdited()) {
addressesFromVariant(map["addressGroups"]); addressesFromVariant(map["addressGroups"]);
appGroupsFromVariant(map["appGroups"]); appGroupsFromVariant(map["appGroups"]);
removedAppGroupIdListFromVariant(map["removedAppGroupIdList"]);
} }
if (flagsEdited()) { if (flagsEdited()) {

View File

@ -99,8 +99,8 @@ public:
const QList<AppGroup *> &appGroups() const { return m_appGroups; } const QList<AppGroup *> &appGroups() const { return m_appGroups; }
const QList<AppGroup *> &removedAppGroupsList() const { return m_removedAppGroups; } const QVector<qint64> &removedAppGroupIdList() const { return m_removedAppGroupIdList; }
void clearRemovedAppGroups() const; void clearRemovedAppGroupIdList() const;
IniOptions &ini() { return m_iniOptions; } IniOptions &ini() { return m_iniOptions; }
const IniOptions &ini() const { return m_iniOptions; } const IniOptions &ini() const { return m_iniOptions; }
@ -142,6 +142,9 @@ private:
QVariant appGroupsToVariant() const; QVariant appGroupsToVariant() const;
void appGroupsFromVariant(const QVariant &v); void appGroupsFromVariant(const QVariant &v);
QVariant removedAppGroupIdListToVariant() const;
void removedAppGroupIdListFromVariant(const QVariant &v);
private: private:
uint m_editedFlags : 4; uint m_editedFlags : 4;
@ -171,7 +174,7 @@ private:
QList<AddressGroup *> m_addressGroups; QList<AddressGroup *> m_addressGroups;
QList<AppGroup *> m_appGroups; QList<AppGroup *> m_appGroups;
mutable QList<AppGroup *> m_removedAppGroups; mutable QVector<qint64> m_removedAppGroupIdList;
IniOptions m_iniOptions; IniOptions m_iniOptions;
}; };