Commit c0bd8890 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

When we import rules, we mustn't clear existing rules + add messagebox

when we imported 0 rules
parent 518bd9ec
...@@ -27,7 +27,7 @@ using namespace MessageViewer; ...@@ -27,7 +27,7 @@ using namespace MessageViewer;
DKIMManagerRules::DKIMManagerRules(QObject *parent) DKIMManagerRules::DKIMManagerRules(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
loadRules(); (void)loadRules();
} }
DKIMManagerRules::~DKIMManagerRules() DKIMManagerRules::~DKIMManagerRules()
...@@ -64,12 +64,15 @@ QStringList DKIMManagerRules::ruleGroups(const KSharedConfig::Ptr &config) const ...@@ -64,12 +64,15 @@ QStringList DKIMManagerRules::ruleGroups(const KSharedConfig::Ptr &config) const
return config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Rule #\\d+"))); return config->groupList().filter(QRegularExpression(QStringLiteral("DKIM Rule #\\d+")));
} }
void DKIMManagerRules::loadRules(const QString &fileName) int DKIMManagerRules::loadRules(const QString &fileName)
{ {
const KSharedConfig::Ptr &config = KSharedConfig::openConfig(fileName.isEmpty() ? MessageViewer::DKIMUtil::defaultConfigFileName() : fileName, KConfig::NoGlobals); const KSharedConfig::Ptr &config = KSharedConfig::openConfig(fileName.isEmpty() ? MessageViewer::DKIMUtil::defaultConfigFileName() : fileName, KConfig::NoGlobals);
const QStringList rulesGroups = ruleGroups(config); const QStringList rulesGroups = ruleGroups(config);
mRules.clear(); if (fileName.isEmpty()) {
mRules.clear();
}
int numberOfRulesAdded = 0;
for (const QString &groupName : rulesGroups) { for (const QString &groupName : rulesGroups) {
KConfigGroup group = config->group(groupName); KConfigGroup group = config->group(groupName);
const QStringList signedDomainIdentifier = group.readEntry(QLatin1String("SignedDomainIdentifier"), QStringList()); const QStringList signedDomainIdentifier = group.readEntry(QLatin1String("SignedDomainIdentifier"), QStringList());
...@@ -85,8 +88,12 @@ void DKIMManagerRules::loadRules(const QString &fileName) ...@@ -85,8 +88,12 @@ void DKIMManagerRules::loadRules(const QString &fileName)
rule.setListId(listId); rule.setListId(listId);
rule.setSignedDomainIdentifier(signedDomainIdentifier); rule.setSignedDomainIdentifier(signedDomainIdentifier);
rule.setRuleType(static_cast<DKIMRule::RuleType>(ruleType)); rule.setRuleType(static_cast<DKIMRule::RuleType>(ruleType));
mRules.append(rule); if (rule.isValid()) {
numberOfRulesAdded++;
mRules.append(rule);
}
} }
return numberOfRulesAdded;
} }
void DKIMManagerRules::saveRules(const QVector<DKIMRule> &lst) void DKIMManagerRules::saveRules(const QVector<DKIMRule> &lst)
...@@ -101,9 +108,9 @@ void DKIMManagerRules::clear() ...@@ -101,9 +108,9 @@ void DKIMManagerRules::clear()
save(); save();
} }
void DKIMManagerRules::importRules(const QString &fileName) int DKIMManagerRules::importRules(const QString &fileName)
{ {
loadRules(fileName); return loadRules(fileName);
} }
void DKIMManagerRules::exportRules(const QString &fileName) void DKIMManagerRules::exportRules(const QString &fileName)
......
...@@ -46,12 +46,12 @@ public: ...@@ -46,12 +46,12 @@ public:
void clear(); void clear();
void importRules(const QString &fileName); Q_REQUIRED_RESULT int importRules(const QString &fileName);
void exportRules(const QString &fileName); void exportRules(const QString &fileName);
Q_REQUIRED_RESULT bool isEmpty() const; Q_REQUIRED_RESULT bool isEmpty() const;
private: private:
void loadRules(const QString &fileName = QString()); Q_REQUIRED_RESULT int loadRules(const QString &fileName = QString());
void save(const QString &fileName = QString()); void save(const QString &fileName = QString());
Q_REQUIRED_RESULT QStringList ruleGroups(const KSharedConfig::Ptr &config) const; Q_REQUIRED_RESULT QStringList ruleGroups(const KSharedConfig::Ptr &config) const;
QVector<DKIMRule> mRules; QVector<DKIMRule> mRules;
......
...@@ -96,7 +96,9 @@ void DKIMManageRulesDialog::slotImport() ...@@ -96,7 +96,9 @@ void DKIMManageRulesDialog::slotImport()
{ {
const QString fileName = QFileDialog::getOpenFileName(this, i18n("Import Rules")); const QString fileName = QFileDialog::getOpenFileName(this, i18n("Import Rules"));
if (!fileName.isEmpty()) { if (!fileName.isEmpty()) {
MessageViewer::DKIMManagerRules::self()->importRules(fileName); if (MessageViewer::DKIMManagerRules::self()->importRules(fileName) == 0) {
KMessageBox::error(this, i18n("No rules imported."), i18n("Import Rules"));
}
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment