7#include "sievescriptpage.h"
8#include "sieveeditorgraphicalmodewidget.h"
9#include "sieveforeverypartwidget.h"
10#include "sieveglobalvariablewidget.h"
11#include "sieveincludewidget.h"
12#include "sievescripttabwidget.h"
14#include "sievewidgetpageabstract.h"
16#include <KLocalizedString>
21using namespace KSieveUi;
23SieveScriptPage::SieveScriptPage(SieveEditorGraphicalModeWidget *sieveEditorGraphicalWidget,
QWidget *parent)
25 , mSieveGraphicalModeWidget(sieveEditorGraphicalWidget)
28 topLayout->setContentsMargins({});
29 mTabWidget =
new SieveScriptTabWidget(
this);
30 connect(mTabWidget, &SieveScriptTabWidget::tabCloseRequested,
this, &SieveScriptPage::slotCloseTab);
32 if (mSieveGraphicalModeWidget->sieveCapabilities().contains(
QLatin1StringView(
"include"))) {
33 mIncludeWidget =
new SieveIncludeWidget(
this);
34 mIncludeWidget->setListOfIncludeFile(mSieveGraphicalModeWidget->listOfIncludeFile());
35 connect(mIncludeWidget, &SieveIncludeWidget::valueChanged,
this, &SieveScriptPage::valueChanged);
36 mTabWidget->addTab(mIncludeWidget,
i18n(
"Includes"));
38 mGlobalVariableWidget =
new SieveGlobalVariableWidget(
this);
39 connect(mGlobalVariableWidget, &SieveGlobalVariableWidget::valueChanged,
this, &SieveScriptPage::valueChanged);
40 mTabWidget->addTab(mGlobalVariableWidget,
i18n(
"Global Variable"));
43 if (mSieveGraphicalModeWidget->sieveCapabilities().contains(
QLatin1StringView(
"foreverypart"))) {
44 mForEveryPartWidget =
new SieveForEveryPartWidget(
this);
45 connect(mForEveryPartWidget, &SieveForEveryPartWidget::valueChanged,
this, &SieveScriptPage::valueChanged);
46 mTabWidget->addTab(mForEveryPartWidget,
i18n(
"ForEveryPart"));
49 mBlockIfWidget = createScriptBlock(SieveScriptBlockWidget::BlockIf);
50 mTabWidget->addTab(mBlockIfWidget, blockName(KSieveUi::SieveScriptBlockWidget::BlockIf));
51 topLayout->addWidget(mTabWidget);
52 mTabWidget->setCurrentWidget(mBlockIfWidget);
55SieveScriptPage::~SieveScriptPage() =
default;
57void SieveScriptPage::setListOfIncludeFile(
const QStringList &lst)
60 mIncludeWidget->setListOfIncludeFile(lst);
64SieveScriptBlockWidget *SieveScriptPage::addScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
66 SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
67 mTabWidget->
insertTab(mTabWidget->
count(), blockWidget, blockName(type));
72SieveScriptBlockWidget *SieveScriptPage::createScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
74 auto blockWidget =
new SieveScriptBlockWidget(mSieveGraphicalModeWidget);
75 connect(blockWidget, &SieveScriptBlockWidget::addNewBlock,
this, &SieveScriptPage::slotAddNewBlock);
76 connect(blockWidget, &SieveScriptBlockWidget::valueChanged,
this, &SieveScriptPage::valueChanged);
77 blockWidget->setPageType(type);
81bool SieveScriptPage::hasAnElseBlock()
const
83 const int numberOfTab(mTabWidget->
count());
84 for (
int i = 0; i < numberOfTab; ++i) {
85 if (
static_cast<SieveWidgetPageAbstract *
>(mTabWidget->
widget(i))->pageType() == SieveScriptBlockWidget::BlockElse) {
92void SieveScriptPage::slotAddNewBlock(
QWidget *widget, KSieveUi::SieveWidgetPageAbstract::PageType type)
94 if ((type == KSieveUi::SieveScriptBlockWidget::BlockElse) && hasAnElseBlock()) {
95 KMessageBox::error(
this,
i18n(
"Script should always have just one \"Else\" block. We cannot add another one."));
98 SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
99 if (type == KSieveUi::SieveScriptBlockWidget::BlockElse) {
100 mTabWidget->
insertTab(mTabWidget->
count(), blockWidget, blockName(type));
102 mTabWidget->
insertTab(mTabWidget->
indexOf(widget) + 1, blockWidget, blockName(type));
107QString SieveScriptPage::blockName(KSieveUi::SieveWidgetPageAbstract::PageType type)
const
110 case KSieveUi::SieveScriptBlockWidget::BlockIf:
111 return i18n(
"Main block");
112 case KSieveUi::SieveScriptBlockWidget::BlockElsIf:
113 return i18n(
"Block \"Elsif\"");
114 case KSieveUi::SieveScriptBlockWidget::BlockElse:
115 return i18n(
"Block \"Else\"");
126 bool inForEveryPartLoop =
false;
127 if (mForEveryPartWidget) {
128 mForEveryPartWidget->generatedScript(foreverypartStr, foreverypartRequires,
false);
129 inForEveryPartLoop = !foreverypartStr.
isEmpty();
130 if (inForEveryPartLoop) {
131 required << foreverypartRequires;
135 const int numberOfTab(mTabWidget->
count());
136 for (
int i = 0; i < numberOfTab; ++i) {
137 auto page =
static_cast<SieveWidgetPageAbstract *
>(mTabWidget->
widget(i));
138 if (page->pageType() != KSieveUi::SieveScriptBlockWidget::ForEveryPart) {
139 page->generatedScript(script, required, inForEveryPartLoop);
142 if (inForEveryPartLoop) {
143 script += QStringLiteral(
"\n}\n");
147void SieveScriptPage::slotCloseTab(
int index)
153SieveIncludeWidget *SieveScriptPage::includeWidget()
const
155 return mIncludeWidget;
158SieveForEveryPartWidget *SieveScriptPage::forEveryPartWidget()
const
160 return mForEveryPartWidget;
163SieveGlobalVariableWidget *SieveScriptPage::globalVariableWidget()
const
165 return mGlobalVariableWidget;
168SieveScriptBlockWidget *SieveScriptPage::blockIfWidget()
const
170 return mBlockIfWidget;
173#include "moc_sievescriptpage.cpp"
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)