Commit 6544b68a authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Don't duplicate requires

parent 9e579d72
......@@ -245,7 +245,9 @@ void SieveEditorTextModeWidget::slotEditRule(const QString &selectedText)
if (dlg->exec()) {
QStringList requireModules;
const QString newScript = dlg->script(requireModules);
const QStringList needToAddRequire = insertNecessaryRequires(requireModules);
mTextEdit->insertPlainText(newScript);
insertRequires(needToAddRequire);
}
delete dlg;
} else {
......@@ -253,6 +255,27 @@ void SieveEditorTextModeWidget::slotEditRule(const QString &selectedText)
}
}
void SieveEditorTextModeWidget::insertRequires(const QStringList &needToAddRequire)
{
if (!needToAddRequire.isEmpty()) {
QTextCursor textCursor = mTextEdit->textCursor();
textCursor.movePosition(QTextCursor::MoveOperation::Start);
textCursor.insertText(needToAddRequire.join(QLatin1Char('\n')) + QLatin1Char('\n'));
}
}
QStringList SieveEditorTextModeWidget::insertNecessaryRequires(const QStringList &requireModules)
{
QStringList needToAddRequire;
const QString plainText = mTextEdit->toPlainText();
for (const QString &module : qAsConst(requireModules)) {
if (!plainText.contains(module)) {
needToAddRequire.append(module);
}
}
return needToAddRequire;
}
void SieveEditorTextModeWidget::slotInsertRule()
{
QPointer<AutoCreateScriptDialog> dlg = new AutoCreateScriptDialog(this);
......@@ -262,7 +285,9 @@ void SieveEditorTextModeWidget::slotInsertRule()
if (dlg->exec()) {
QStringList requireModules;
const QString newScript = dlg->script(requireModules);
const QStringList needToAddRequire = insertNecessaryRequires(requireModules);
mTextEdit->insertPlainText(newScript);
insertRequires(needToAddRequire);
}
delete dlg;
}
......@@ -276,8 +301,9 @@ void SieveEditorTextModeWidget::createRulesGraphically()
if (dlg->exec()) {
QStringList requireModules;
const QString script = dlg->script(requireModules);
const QStringList needToAddRequire = insertNecessaryRequires(requireModules);
QString newPlainText = mTextEdit->toPlainText() + script;
if (!requireModules.isEmpty()) {
if (!needToAddRequire.isEmpty()) {
newPlainText.prepend(requireModules.join(QLatin1Char('\n')) + QLatin1Char('\n'));
}
mTextEdit->selectAll();
......
......@@ -126,6 +126,8 @@ private:
void writeConfig();
void slotEditRule(const QString &selectedText);
void slotInsertRule();
void insertRequires(const QStringList &needToAddRequire);
Q_REQUIRED_RESULT QStringList insertNecessaryRequires(const QStringList &requireModules);
QString mOriginalScript;
QStringList mSieveCapabilities;
QStringList mListOfIncludeFile;
......
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