Libksieve

sievescriptpage.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 "autocreatescripts/autocreatescriptdialog.h"
15 #include "sievewidgetpageabstract.h"
16 
17 #include <KLocalizedString>
18 #include <KMessageBox>
19 
20 #include <QVBoxLayout>
21 
22 using namespace KSieveUi;
23 
24 SieveScriptPage::SieveScriptPage(SieveEditorGraphicalModeWidget *sieveEditorGraphicalWidget, QWidget *parent)
25  : QWidget(parent)
26  , mSieveGraphicalModeWidget(sieveEditorGraphicalWidget)
27 {
28  auto topLayout = new QVBoxLayout(this);
29  topLayout->setContentsMargins({});
30  mTabWidget = new SieveScriptTabWidget(this);
31  connect(mTabWidget, &SieveScriptTabWidget::tabCloseRequested, this, &SieveScriptPage::slotCloseTab);
32 
33  if (mSieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1String("include"))) {
34  mIncludeWidget = new SieveIncludeWidget(this);
35  mIncludeWidget->setListOfIncludeFile(mSieveGraphicalModeWidget->listOfIncludeFile());
36  connect(mIncludeWidget, &SieveIncludeWidget::valueChanged, this, &SieveScriptPage::valueChanged);
37  mTabWidget->addTab(mIncludeWidget, i18n("Includes"));
38 
39  mGlobalVariableWidget = new SieveGlobalVariableWidget(this);
40  connect(mGlobalVariableWidget, &SieveGlobalVariableWidget::valueChanged, this, &SieveScriptPage::valueChanged);
41  mTabWidget->addTab(mGlobalVariableWidget, i18n("Global Variable"));
42  }
43 
44  if (mSieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1String("foreverypart"))) {
45  mForEveryPartWidget = new SieveForEveryPartWidget(this);
46  connect(mForEveryPartWidget, &SieveForEveryPartWidget::valueChanged, this, &SieveScriptPage::valueChanged);
47  mTabWidget->addTab(mForEveryPartWidget, i18n("ForEveryPart"));
48  }
49 
50  mBlockIfWidget = createScriptBlock(SieveScriptBlockWidget::BlockIf);
51  mTabWidget->addTab(mBlockIfWidget, blockName(KSieveUi::SieveScriptBlockWidget::BlockIf));
52  topLayout->addWidget(mTabWidget);
53  mTabWidget->setCurrentWidget(mBlockIfWidget);
54 }
55 
56 SieveScriptPage::~SieveScriptPage()
57 {
58 }
59 
60 void SieveScriptPage::setListOfIncludeFile(const QStringList &lst)
61 {
62  if (mIncludeWidget) {
63  mIncludeWidget->setListOfIncludeFile(lst);
64  }
65 }
66 
67 SieveScriptBlockWidget *SieveScriptPage::addScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
68 {
69  SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
70  mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
71  mTabWidget->setCurrentWidget(blockWidget);
72  return blockWidget;
73 }
74 
75 SieveScriptBlockWidget *SieveScriptPage::createScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
76 {
77  auto blockWidget = new SieveScriptBlockWidget(mSieveGraphicalModeWidget);
78  connect(blockWidget, &SieveScriptBlockWidget::addNewBlock, this, &SieveScriptPage::slotAddNewBlock);
79  connect(blockWidget, &SieveScriptBlockWidget::valueChanged, this, &SieveScriptPage::valueChanged);
80  blockWidget->setPageType(type);
81  return blockWidget;
82 }
83 
84 bool SieveScriptPage::hasAnElseBlock() const
85 {
86  const int numberOfTab(mTabWidget->count());
87  for (int i = 0; i < numberOfTab; ++i) {
88  if (static_cast<SieveWidgetPageAbstract *>(mTabWidget->widget(i))->pageType() == SieveScriptBlockWidget::BlockElse) {
89  return true;
90  }
91  }
92  return false;
93 }
94 
95 void SieveScriptPage::slotAddNewBlock(QWidget *widget, KSieveUi::SieveWidgetPageAbstract::PageType type)
96 {
97  if ((type == KSieveUi::SieveScriptBlockWidget::BlockElse) && hasAnElseBlock()) {
98  KMessageBox::error(this, i18n("Script should always have just one \"Else\" block. We cannot add another one."));
99  return;
100  }
101  SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
102  if (type == KSieveUi::SieveScriptBlockWidget::BlockElse) { // Insert at the end of tabwidget
103  mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
104  } else {
105  mTabWidget->insertTab(mTabWidget->indexOf(widget) + 1, blockWidget, blockName(type));
106  }
107  mTabWidget->setCurrentWidget(blockWidget);
108 }
109 
110 QString SieveScriptPage::blockName(KSieveUi::SieveWidgetPageAbstract::PageType type) const
111 {
112  switch (type) {
113  case KSieveUi::SieveScriptBlockWidget::BlockIf:
114  return i18n("Main block");
115  case KSieveUi::SieveScriptBlockWidget::BlockElsIf:
116  return i18n("Block \"Elsif\"");
117  case KSieveUi::SieveScriptBlockWidget::BlockElse:
118  return i18n("Block \"Else\"");
119  default:
120  break;
121  }
122  return QString();
123 }
124 
125 void SieveScriptPage::generatedScript(QString &script, QStringList &required)
126 {
127  QString foreverypartStr;
128  QStringList foreverypartRequires;
129  bool inForEveryPartLoop = false;
130  if (mForEveryPartWidget) {
131  mForEveryPartWidget->generatedScript(foreverypartStr, foreverypartRequires, false);
132  inForEveryPartLoop = !foreverypartStr.isEmpty();
133  if (inForEveryPartLoop) {
134  required << foreverypartRequires;
135  script += foreverypartStr + QLatin1Char('\n');
136  }
137  }
138  const int numberOfTab(mTabWidget->count());
139  for (int i = 0; i < numberOfTab; ++i) {
140  auto page = static_cast<SieveWidgetPageAbstract *>(mTabWidget->widget(i));
141  if (page->pageType() != KSieveUi::SieveScriptBlockWidget::ForEveryPart) {
142  page->generatedScript(script, required, inForEveryPartLoop);
143  }
144  }
145  if (inForEveryPartLoop) {
146  script += QStringLiteral("\n}\n");
147  }
148 }
149 
150 void SieveScriptPage::slotCloseTab(int index)
151 {
152  mTabWidget->removeTab(index);
153  Q_EMIT valueChanged();
154 }
155 
156 SieveIncludeWidget *SieveScriptPage::includeWidget() const
157 {
158  return mIncludeWidget;
159 }
160 
161 SieveForEveryPartWidget *SieveScriptPage::forEveryPartWidget() const
162 {
163  return mForEveryPartWidget;
164 }
165 
166 SieveGlobalVariableWidget *SieveScriptPage::globalVariableWidget() const
167 {
168  return mGlobalVariableWidget;
169 }
170 
171 SieveScriptBlockWidget *SieveScriptPage::blockIfWidget() const
172 {
173  return mBlockIfWidget;
174 }
bool isEmpty() const const
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.