Libksieve

sievescriptpage.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "sievescriptpage.h"
21 #include "sievescripttabwidget.h"
22 #include "sieveincludewidget.h"
23 #include "sieveforeverypartwidget.h"
24 #include "sieveglobalvariablewidget.h"
25 #include "sieveeditorgraphicalmodewidget.h"
26 
27 #include "sievewidgetpageabstract.h"
28 #include "autocreatescripts/autocreatescriptdialog.h"
29 
30 #include <KLocalizedString>
31 #include <KMessageBox>
32 
33 #include <QVBoxLayout>
34 
35 using namespace KSieveUi;
36 
37 SieveScriptPage::SieveScriptPage(SieveEditorGraphicalModeWidget *sieveEditorGraphicalWidget, QWidget *parent)
38  : QWidget(parent)
39  , mSieveGraphicalModeWidget(sieveEditorGraphicalWidget)
40 {
41  QVBoxLayout *topLayout = new QVBoxLayout(this);
42  topLayout->setContentsMargins(0, 0, 0, 0);
43  mTabWidget = new SieveScriptTabWidget(this);
44  connect(mTabWidget, &SieveScriptTabWidget::tabCloseRequested, this, &SieveScriptPage::slotCloseTab);
45 
46  if (mSieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1String("include"))) {
47  mIncludeWidget = new SieveIncludeWidget(this);
48  mIncludeWidget->setListOfIncludeFile(mSieveGraphicalModeWidget->listOfIncludeFile());
49  connect(mIncludeWidget, &SieveIncludeWidget::valueChanged, this, &SieveScriptPage::valueChanged);
50  mTabWidget->addTab(mIncludeWidget, i18n("Includes"));
51 
52  mGlobalVariableWidget = new SieveGlobalVariableWidget(this);
53  connect(mGlobalVariableWidget, &SieveGlobalVariableWidget::valueChanged, this, &SieveScriptPage::valueChanged);
54  mTabWidget->addTab(mGlobalVariableWidget, i18n("Global Variable"));
55  }
56 
57  if (mSieveGraphicalModeWidget->sieveCapabilities().contains(QLatin1String("foreverypart"))) {
58  mForEveryPartWidget = new SieveForEveryPartWidget(this);
59  connect(mForEveryPartWidget, &SieveForEveryPartWidget::valueChanged, this, &SieveScriptPage::valueChanged);
60  mTabWidget->addTab(mForEveryPartWidget, i18n("ForEveryPart"));
61  }
62 
63  mBlockIfWidget = createScriptBlock(SieveScriptBlockWidget::BlockIf);
64  mTabWidget->addTab(mBlockIfWidget, blockName(KSieveUi::SieveScriptBlockWidget::BlockIf));
65  topLayout->addWidget(mTabWidget);
66  mTabWidget->setCurrentWidget(mBlockIfWidget);
67 }
68 
69 SieveScriptPage::~SieveScriptPage()
70 {
71 }
72 
73 void SieveScriptPage::setListOfIncludeFile(const QStringList &lst)
74 {
75  if (mIncludeWidget) {
76  mIncludeWidget->setListOfIncludeFile(lst);
77  }
78 }
79 
80 SieveScriptBlockWidget *SieveScriptPage::addScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
81 {
82  SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
83  mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
84  mTabWidget->setCurrentWidget(blockWidget);
85  return blockWidget;
86 }
87 
88 SieveScriptBlockWidget *SieveScriptPage::createScriptBlock(KSieveUi::SieveWidgetPageAbstract::PageType type)
89 {
90  SieveScriptBlockWidget *blockWidget = new SieveScriptBlockWidget(mSieveGraphicalModeWidget);
91  connect(blockWidget, &SieveScriptBlockWidget::addNewBlock, this, &SieveScriptPage::slotAddNewBlock);
92  connect(blockWidget, &SieveScriptBlockWidget::valueChanged, this, &SieveScriptPage::valueChanged);
93  blockWidget->setPageType(type);
94  return blockWidget;
95 }
96 
97 bool SieveScriptPage::hasAnElseBlock() const
98 {
99  const int numberOfTab(mTabWidget->count());
100  for (int i = 0; i < numberOfTab; ++i) {
101  if (static_cast<SieveWidgetPageAbstract *>(mTabWidget->widget(i))->pageType() == SieveScriptBlockWidget::BlockElse) {
102  return true;
103  }
104  }
105  return false;
106 }
107 
108 void SieveScriptPage::slotAddNewBlock(QWidget *widget, KSieveUi::SieveWidgetPageAbstract::PageType type)
109 {
110  if ((type == KSieveUi::SieveScriptBlockWidget::BlockElse) && hasAnElseBlock()) {
111  KMessageBox::error(this, i18n("Script should always have just one \"Else\" block. We cannot add another one."));
112  return;
113  }
114  SieveScriptBlockWidget *blockWidget = createScriptBlock(type);
115  if (type == KSieveUi::SieveScriptBlockWidget::BlockElse) { //Insert at the end of tabwidget
116  mTabWidget->insertTab(mTabWidget->count(), blockWidget, blockName(type));
117  } else {
118  mTabWidget->insertTab(mTabWidget->indexOf(widget) + 1, blockWidget, blockName(type));
119  }
120  mTabWidget->setCurrentWidget(blockWidget);
121 }
122 
123 QString SieveScriptPage::blockName(KSieveUi::SieveWidgetPageAbstract::PageType type) const
124 {
125  switch (type) {
126  case KSieveUi::SieveScriptBlockWidget::BlockIf:
127  return i18n("Main block");
128  case KSieveUi::SieveScriptBlockWidget::BlockElsIf:
129  return i18n("Block \"Elsif\"");
130  case KSieveUi::SieveScriptBlockWidget::BlockElse:
131  return i18n("Block \"Else\"");
132  default:
133  break;
134  }
135  return QString();
136 }
137 
138 void SieveScriptPage::generatedScript(QString &script, QStringList &required)
139 {
140  QString foreverypartStr;
141  QStringList foreverypartRequires;
142  bool inForEveryPartLoop = false;
143  if (mForEveryPartWidget) {
144  mForEveryPartWidget->generatedScript(foreverypartStr, foreverypartRequires, false);
145  inForEveryPartLoop = !foreverypartStr.isEmpty();
146  if (inForEveryPartLoop) {
147  required << foreverypartRequires;
148  script += foreverypartStr + QLatin1Char('\n');
149  }
150  }
151  const int numberOfTab(mTabWidget->count());
152  for (int i = 0; i < numberOfTab; ++i) {
153  SieveWidgetPageAbstract *page = static_cast<SieveWidgetPageAbstract *>(mTabWidget->widget(i));
154  if (page->pageType() != KSieveUi::SieveScriptBlockWidget::ForEveryPart) {
155  page->generatedScript(script, required, inForEveryPartLoop);
156  }
157  }
158  if (inForEveryPartLoop) {
159  script += QStringLiteral("\n}\n");
160  }
161 }
162 
163 void SieveScriptPage::slotCloseTab(int index)
164 {
165  mTabWidget->removeTab(index);
166  Q_EMIT valueChanged();
167 }
168 
169 SieveIncludeWidget *SieveScriptPage::includeWidget() const
170 {
171  return mIncludeWidget;
172 }
173 
174 SieveForEveryPartWidget *SieveScriptPage::forEveryPartWidget() const
175 {
176  return mForEveryPartWidget;
177 }
178 
179 SieveGlobalVariableWidget *SieveScriptPage::globalVariableWidget() const
180 {
181  return mGlobalVariableWidget;
182 }
183 
184 SieveScriptBlockWidget *SieveScriptPage::blockIfWidget() const
185 {
186  return mBlockIfWidget;
187 }
void setContentsMargins(int left, int top, int right, int bottom)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
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-2020 The KDE developers.
Generated on Tue Jul 14 2020 23:08:50 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.