7#include "sievescriptblockwidget.h"
8#include "autocreatescriptutil_p.h"
9#include "sieveactionwidgetlister.h"
10#include "sieveconditionwidgetlister.h"
12#include <KLocalizedString>
17#include "libksieveui_debug.h"
18#include <QButtonGroup>
21#include <QRadioButton>
24#include <QXmlStreamReader>
26using namespace KSieveUi;
28SieveScriptBlockWidget::SieveScriptBlockWidget(SieveEditorGraphicalModeWidget *graphicalModeWidget,
QWidget *parent)
29 : SieveWidgetPageAbstract(parent)
30 , mSieveGraphicalModeWidget(graphicalModeWidget)
33 topLayout->setContentsMargins({});
38 mAllMessageRBtn =
new QRadioButton(
i18nc(
"@option:radio",
"Match all messages"),
this);
39 mMatchAll =
new QRadioButton(
i18nc(
"@option:radio",
"Match a&ll of the following"),
this);
40 mMatchAny =
new QRadioButton(
i18nc(
"@option:radio",
"Match an&y of the following"),
this);
42 vbox->addWidget(mMatchAll);
43 vbox->addWidget(mMatchAny);
44 vbox->addWidget(mAllMessageRBtn);
45 mMatchAll->setChecked(
true);
46 mMatchAny->setChecked(
false);
47 mAllMessageRBtn->setChecked(
false);
50 bg->addButton(mMatchAll);
51 bg->addButton(mMatchAny);
52 bg->addButton(mAllMessageRBtn);
55 mConditions->setLayout(vbox);
57 mScriptConditionLister =
new SieveConditionWidgetLister(mSieveGraphicalModeWidget,
this);
58 connect(mScriptConditionLister, &SieveConditionWidgetLister::valueChanged,
this, &SieveScriptBlockWidget::valueChanged);
60 scrollArea->setAutoFillBackground(
false);
61 scrollArea->setWidget(mScriptConditionLister);
62 scrollArea->setWidgetResizable(
true);
64 vbox->addWidget(scrollArea);
66 topLayout->addWidget(mConditions);
70 actions->setLayout(vbox);
71 mScriptActionLister =
new SieveActionWidgetLister(mSieveGraphicalModeWidget,
this);
72 connect(mScriptActionLister, &SieveActionWidgetLister::valueChanged,
this, &SieveScriptBlockWidget::valueChanged);
75 scrollAreaCondition->setAutoFillBackground(
false);
76 scrollAreaCondition->setWidget(mScriptActionLister);
77 scrollAreaCondition->setWidgetResizable(
true);
79 vbox->addWidget(scrollAreaCondition);
80 topLayout->addWidget(actions);
83 auto lab =
new QLabel(
i18nc(
"@label:textbox",
"Add new block:"));
84 newBlockLayout->addWidget(lab);
86 newBlockLayout->addWidget(mNewBlockType);
87 mNewBlockType->addItem(
i18n(
"\"elsif\" block"));
88 mNewBlockType->addItem(
i18n(
"\"else\" block"));
93 newBlockLayout->addWidget(mAddBlockType);
96 topLayout->addLayout(newBlockLayout);
99SieveScriptBlockWidget::~SieveScriptBlockWidget() =
default;
101void SieveScriptBlockWidget::slotAddBlock()
103 KSieveUi::SieveWidgetPageAbstract::PageType
type = BlockElsIf;
114 Q_EMIT addNewBlock(
this, type);
117void SieveScriptBlockWidget::setPageType(PageType type)
119 if (pageType() != type) {
120 SieveWidgetPageAbstract::setPageType(type);
123 mAllMessageRBtn->
show();
129 mAllMessageRBtn->
hide();
135 mAllMessageRBtn->
hide();
146SieveScriptBlockWidget::MatchCondition SieveScriptBlockWidget::matchCondition()
const
148 return mMatchCondition;
153 if (button == mMatchAll) {
154 mMatchCondition = AndCondition;
155 }
else if (button == mMatchAny) {
156 mMatchCondition = OrCondition;
158 mMatchCondition = AllCondition;
164void SieveScriptBlockWidget::updateWidget()
166 mScriptConditionLister->
setEnabled(mMatchCondition != AllCondition);
167 mNewBlockType->
setEnabled(mMatchCondition != AllCondition);
168 mAddBlockType->
setEnabled(mMatchCondition != AllCondition);
171void SieveScriptBlockWidget::generatedScript(
QString &script,
QStringList &required,
bool inForEveryPartLoop)
174 if (inForEveryPartLoop) {
175 indentation = AutoCreateScriptUtil::indentation();
177 bool onlyActions =
false;
178 if (mMatchCondition == AllCondition) {
181 }
else if (pageType() == BlockElse) {
185 int numberOfCondition = 0;
186 mScriptConditionLister->generatedScript(conditionStr, numberOfCondition, required, inForEveryPartLoop);
187 const bool hasUniqCondition = (numberOfCondition == 1);
190 switch (pageType()) {
192 blockStr = indentation + QStringLiteral(
"if ");
195 blockStr = QStringLiteral(
"elsif ");
204 if (hasUniqCondition == 1) {
205 filterStr += blockStr;
206 }
else if (mMatchCondition == AndCondition) {
208 }
else if (mMatchCondition == OrCondition) {
215 script += filterStr + conditionStr;
217 if (hasUniqCondition) {
220 script += indentation + QStringLiteral(
")\n%1{\n").
arg(indentation);
223 mScriptActionLister->generatedScript(script, required, onlyActions, inForEveryPartLoop);
229void SieveScriptBlockWidget::updateCondition()
231 switch (mMatchCondition) {
245void SieveScriptBlockWidget::loadLocalVariable(
const SieveGlobalVariableActionWidget::VariableElement &var)
247 mScriptActionLister->loadLocalVariable(var);
248 mMatchCondition = AllCondition;
255 mScriptActionLister->loadScript(element,
true, error);
256 mMatchCondition = AllCondition;
259 bool uniqueTest =
false;
263 bool notCondition =
false;
267 mMatchCondition = OrCondition;
275 mMatchCondition = OrCondition;
279 mScriptConditionLister->loadScript(element, uniqueTest, notCondition, error);
281 mScriptActionLister->loadScript(element,
false, error);
284 qCDebug(LIBKSIEVEUI_LOG) <<
" e.tag" << tagName;
293#include "moc_sievescriptblockwidget.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
QIcon fromTheme(const QString &name)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString toString() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
QXmlStreamAttributes attributes() const const
QStringView name() const const
bool readNextStartElement()
void skipCurrentElement()