Libksieve

sievescriptpage.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "sievescriptpage.h"
8#include "sieveeditorgraphicalmodewidget.h"
9#include "sieveforeverypartwidget.h"
10#include "sieveglobalvariablewidget.h"
11#include "sieveincludewidget.h"
12#include "sievescripttabwidget.h"
13
14#include "sievewidgetpageabstract.h"
15
16#include <KLocalizedString>
17#include <KMessageBox>
18
19#include <QVBoxLayout>
20
21using namespace KSieveUi;
22
23SieveScriptPage::SieveScriptPage(SieveEditorGraphicalModeWidget *sieveEditorGraphicalWidget, QWidget *parent)
24 : QWidget(parent)
25 , mSieveGraphicalModeWidget(sieveEditorGraphicalWidget)
26{
27 auto topLayout = new QVBoxLayout(this);
28 topLayout->setContentsMargins({});
29 mTabWidget = new SieveScriptTabWidget(this);
30 connect(mTabWidget, &SieveScriptTabWidget::tabCloseRequested, this, &SieveScriptPage::slotCloseTab);
31
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"));
37
38 mGlobalVariableWidget = new SieveGlobalVariableWidget(this);
39 connect(mGlobalVariableWidget, &SieveGlobalVariableWidget::valueChanged, this, &SieveScriptPage::valueChanged);
40 mTabWidget->addTab(mGlobalVariableWidget, i18n("Global Variable"));
41 }
42
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"));
47 }
48
49 mBlockIfWidget = createScriptBlock(SieveScriptBlockWidget::BlockIf);
50 mTabWidget->addTab(mBlockIfWidget, blockName(KSieveUi::SieveScriptBlockWidget::BlockIf));
51 topLayout->addWidget(mTabWidget);
52 mTabWidget->setCurrentWidget(mBlockIfWidget);
53}
54
55SieveScriptPage::~SieveScriptPage() = default;
56
57void SieveScriptPage::setListOfIncludeFile(const QStringList &lst)
58{
59 if (mIncludeWidget) {
60 mIncludeWidget->setListOfIncludeFile(lst);
61 }
62}
63
64SieveScriptBlockWidget *SieveScriptPage::addScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
65{
66 SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
67 mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
68 mTabWidget->setCurrentWidget(blockWidget);
69 return blockWidget;
70}
71
72SieveScriptBlockWidget *SieveScriptPage::createScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
73{
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);
78 return blockWidget;
79}
80
81bool SieveScriptPage::hasAnElseBlock() const
82{
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) {
86 return true;
87 }
88 }
89 return false;
90}
91
92void SieveScriptPage::slotAddNewBlock(QWidget *widget, KSieveUi::SieveWidgetPageAbstract::PageType type)
93{
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."));
96 return;
97 }
98 SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
99 if (type == KSieveUi::SieveScriptBlockWidget::BlockElse) { // Insert at the end of tabwidget
100 mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
101 } else {
102 mTabWidget->insertTab(mTabWidget->indexOf(widget) + 1, blockWidget, blockName(type));
103 }
104 mTabWidget->setCurrentWidget(blockWidget);
105}
106
107QString SieveScriptPage::blockName(KSieveUi::SieveWidgetPageAbstract::PageType type) const
108{
109 switch (type) {
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\"");
116 default:
117 break;
118 }
119 return {};
120}
121
122void SieveScriptPage::generatedScript(QString &script, QStringList &required)
123{
124 QString foreverypartStr;
125 QStringList foreverypartRequires;
126 bool inForEveryPartLoop = false;
127 if (mForEveryPartWidget) {
128 mForEveryPartWidget->generatedScript(foreverypartStr, foreverypartRequires, false);
129 inForEveryPartLoop = !foreverypartStr.isEmpty();
130 if (inForEveryPartLoop) {
131 required << foreverypartRequires;
132 script += foreverypartStr + QLatin1Char('\n');
133 }
134 }
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);
140 }
141 }
142 if (inForEveryPartLoop) {
143 script += QStringLiteral("\n}\n");
144 }
145}
146
147void SieveScriptPage::slotCloseTab(int index)
148{
149 mTabWidget->removeTab(index);
150 Q_EMIT valueChanged();
151}
152
153SieveIncludeWidget *SieveScriptPage::includeWidget() const
154{
155 return mIncludeWidget;
156}
157
158SieveForEveryPartWidget *SieveScriptPage::forEveryPartWidget() const
159{
160 return mForEveryPartWidget;
161}
162
163SieveGlobalVariableWidget *SieveScriptPage::globalVariableWidget() const
164{
165 return mGlobalVariableWidget;
166}
167
168SieveScriptBlockWidget *SieveScriptPage::blockIfWidget() const
169{
170 return mBlockIfWidget;
171}
172
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)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
int indexOf(const QWidget *w) const const
int insertTab(int index, QWidget *page, const QIcon &icon, const QString &label)
void removeTab(int index)
void setCurrentWidget(QWidget *widget)
QWidget * widget(int index) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.