File indexing completed on 2024-12-29 04:54:45

0001 /*
0002    SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "sievescriptpage.h"
0008 #include "sieveeditorgraphicalmodewidget.h"
0009 #include "sieveforeverypartwidget.h"
0010 #include "sieveglobalvariablewidget.h"
0011 #include "sieveincludewidget.h"
0012 #include "sievescripttabwidget.h"
0013 
0014 #include "sievewidgetpageabstract.h"
0015 
0016 #include <KLocalizedString>
0017 #include <KMessageBox>
0018 
0019 #include <QVBoxLayout>
0020 
0021 using namespace KSieveUi;
0022 
0023 SieveScriptPage::SieveScriptPage(SieveEditorGraphicalModeWidget *sieveEditorGraphicalWidget, QWidget *parent)
0024     : QWidget(parent)
0025     , mSieveGraphicalModeWidget(sieveEditorGraphicalWidget)
0026 {
0027     auto topLayout = new QVBoxLayout(this);
0028     topLayout->setContentsMargins({});
0029     mTabWidget = new SieveScriptTabWidget(this);
0030     connect(mTabWidget, &SieveScriptTabWidget::tabCloseRequested, this, &SieveScriptPage::slotCloseTab);
0031 
0032     if (mSieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1StringView("include"))) {
0033         mIncludeWidget = new SieveIncludeWidget(this);
0034         mIncludeWidget->setListOfIncludeFile(mSieveGraphicalModeWidget->listOfIncludeFile());
0035         connect(mIncludeWidget, &SieveIncludeWidget::valueChanged, this, &SieveScriptPage::valueChanged);
0036         mTabWidget->addTab(mIncludeWidget, i18n("Includes"));
0037 
0038         mGlobalVariableWidget = new SieveGlobalVariableWidget(this);
0039         connect(mGlobalVariableWidget, &SieveGlobalVariableWidget::valueChanged, this, &SieveScriptPage::valueChanged);
0040         mTabWidget->addTab(mGlobalVariableWidget, i18n("Global Variable"));
0041     }
0042 
0043     if (mSieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1StringView("foreverypart"))) {
0044         mForEveryPartWidget = new SieveForEveryPartWidget(this);
0045         connect(mForEveryPartWidget, &SieveForEveryPartWidget::valueChanged, this, &SieveScriptPage::valueChanged);
0046         mTabWidget->addTab(mForEveryPartWidget, i18n("ForEveryPart"));
0047     }
0048 
0049     mBlockIfWidget = createScriptBlock(SieveScriptBlockWidget::BlockIf);
0050     mTabWidget->addTab(mBlockIfWidget, blockName(KSieveUi::SieveScriptBlockWidget::BlockIf));
0051     topLayout->addWidget(mTabWidget);
0052     mTabWidget->setCurrentWidget(mBlockIfWidget);
0053 }
0054 
0055 SieveScriptPage::~SieveScriptPage() = default;
0056 
0057 void SieveScriptPage::setListOfIncludeFile(const QStringList &lst)
0058 {
0059     if (mIncludeWidget) {
0060         mIncludeWidget->setListOfIncludeFile(lst);
0061     }
0062 }
0063 
0064 SieveScriptBlockWidget *SieveScriptPage::addScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
0065 {
0066     SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
0067     mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
0068     mTabWidget->setCurrentWidget(blockWidget);
0069     return blockWidget;
0070 }
0071 
0072 SieveScriptBlockWidget *SieveScriptPage::createScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
0073 {
0074     auto blockWidget = new SieveScriptBlockWidget(mSieveGraphicalModeWidget);
0075     connect(blockWidget, &SieveScriptBlockWidget::addNewBlock, this, &SieveScriptPage::slotAddNewBlock);
0076     connect(blockWidget, &SieveScriptBlockWidget::valueChanged, this, &SieveScriptPage::valueChanged);
0077     blockWidget->setPageType(type);
0078     return blockWidget;
0079 }
0080 
0081 bool SieveScriptPage::hasAnElseBlock() const
0082 {
0083     const int numberOfTab(mTabWidget->count());
0084     for (int i = 0; i < numberOfTab; ++i) {
0085         if (static_cast<SieveWidgetPageAbstract *>(mTabWidget->widget(i))->pageType() == SieveScriptBlockWidget::BlockElse) {
0086             return true;
0087         }
0088     }
0089     return false;
0090 }
0091 
0092 void SieveScriptPage::slotAddNewBlock(QWidget *widget, KSieveUi::SieveWidgetPageAbstract::PageType type)
0093 {
0094     if ((type == KSieveUi::SieveScriptBlockWidget::BlockElse) && hasAnElseBlock()) {
0095         KMessageBox::error(this, i18n("Script should always have just one \"Else\" block. We cannot add another one."));
0096         return;
0097     }
0098     SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
0099     if (type == KSieveUi::SieveScriptBlockWidget::BlockElse) { // Insert at the end of tabwidget
0100         mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
0101     } else {
0102         mTabWidget->insertTab(mTabWidget->indexOf(widget) + 1, blockWidget, blockName(type));
0103     }
0104     mTabWidget->setCurrentWidget(blockWidget);
0105 }
0106 
0107 QString SieveScriptPage::blockName(KSieveUi::SieveWidgetPageAbstract::PageType type) const
0108 {
0109     switch (type) {
0110     case KSieveUi::SieveScriptBlockWidget::BlockIf:
0111         return i18n("Main block");
0112     case KSieveUi::SieveScriptBlockWidget::BlockElsIf:
0113         return i18n("Block \"Elsif\"");
0114     case KSieveUi::SieveScriptBlockWidget::BlockElse:
0115         return i18n("Block \"Else\"");
0116     default:
0117         break;
0118     }
0119     return {};
0120 }
0121 
0122 void SieveScriptPage::generatedScript(QString &script, QStringList &required)
0123 {
0124     QString foreverypartStr;
0125     QStringList foreverypartRequires;
0126     bool inForEveryPartLoop = false;
0127     if (mForEveryPartWidget) {
0128         mForEveryPartWidget->generatedScript(foreverypartStr, foreverypartRequires, false);
0129         inForEveryPartLoop = !foreverypartStr.isEmpty();
0130         if (inForEveryPartLoop) {
0131             required << foreverypartRequires;
0132             script += foreverypartStr + QLatin1Char('\n');
0133         }
0134     }
0135     const int numberOfTab(mTabWidget->count());
0136     for (int i = 0; i < numberOfTab; ++i) {
0137         auto page = static_cast<SieveWidgetPageAbstract *>(mTabWidget->widget(i));
0138         if (page->pageType() != KSieveUi::SieveScriptBlockWidget::ForEveryPart) {
0139             page->generatedScript(script, required, inForEveryPartLoop);
0140         }
0141     }
0142     if (inForEveryPartLoop) {
0143         script += QStringLiteral("\n}\n");
0144     }
0145 }
0146 
0147 void SieveScriptPage::slotCloseTab(int index)
0148 {
0149     mTabWidget->removeTab(index);
0150     Q_EMIT valueChanged();
0151 }
0152 
0153 SieveIncludeWidget *SieveScriptPage::includeWidget() const
0154 {
0155     return mIncludeWidget;
0156 }
0157 
0158 SieveForEveryPartWidget *SieveScriptPage::forEveryPartWidget() const
0159 {
0160     return mForEveryPartWidget;
0161 }
0162 
0163 SieveGlobalVariableWidget *SieveScriptPage::globalVariableWidget() const
0164 {
0165     return mGlobalVariableWidget;
0166 }
0167 
0168 SieveScriptBlockWidget *SieveScriptPage::blockIfWidget() const
0169 {
0170     return mBlockIfWidget;
0171 }
0172 
0173 #include "moc_sievescriptpage.cpp"