Libksieve

sieveglobalvariablewidget.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 "sieveglobalvariablewidget.h"
21 #include "sievescriptblockwidget.h"
22 #include "autocreatescriptutil_p.h"
23 #include "commonwidgets/sievehelpbutton.h"
24 #include "editor/sieveeditorutil.h"
25 #include "widgets/lineeditvalidator.h"
26 
27 #include <QPushButton>
28 #include <KLocalizedString>
29 #include <QLineEdit>
30 #include <QIcon>
31 
32 #include <QGridLayout>
33 #include <QCheckBox>
34 #include <QLabel>
35 #include <QWhatsThis>
36 #include "libksieve_debug.h"
37 #include <QXmlStreamReader>
38 
39 using namespace KSieveUi;
40 static const int MINIMUMGLOBALVARIABLEACTION = 1;
41 static const int MAXIMUMGLOBALVARIABLEACTION = 15;
42 
43 SieveGlobalVariableActionWidget::SieveGlobalVariableActionWidget(QWidget *parent)
44  : QWidget(parent)
45 {
46  initWidget();
47 }
48 
49 SieveGlobalVariableActionWidget::~SieveGlobalVariableActionWidget()
50 {
51 }
52 
53 void SieveGlobalVariableActionWidget::generatedScript(QString &script)
54 {
55  const QString variableName = mVariableName->text();
56  if (variableName.trimmed().isEmpty()) {
57  return;
58  }
59  script += QLatin1String("global ");
60  script += QStringLiteral("\"%1\";\n").arg(variableName);
61  if (mSetValueTo->isChecked() && !mVariableValue->text().isEmpty()) {
62  script += QStringLiteral("set \"%1\" \"%2\";\n").arg(variableName, mVariableValue->text());
63  }
64 }
65 
66 void SieveGlobalVariableActionWidget::initWidget()
67 {
68  mLayout = new QGridLayout(this);
69  mLayout->setContentsMargins(0, 0, 0, 0);
70 
71  QLabel *lab = new QLabel(i18n("Variable name:"));
72  mLayout->addWidget(lab, 1, 0);
73 
74  mVariableName = new LineEditValidator;
75  connect(mVariableName, &QLineEdit::textChanged, this, &SieveGlobalVariableActionWidget::valueChanged);
76  mLayout->addWidget(mVariableName, 1, 1);
77 
78  mSetValueTo = new QCheckBox(i18n("Set value to:"));
79  connect(mSetValueTo, &QCheckBox::toggled, this, &SieveGlobalVariableActionWidget::valueChanged);
80  mLayout->addWidget(mSetValueTo, 1, 2);
81  mSetValueTo->setChecked(false);
82 
83  mVariableValue = new QLineEdit;
84  connect(mVariableValue, &QLineEdit::textChanged, this, &SieveGlobalVariableActionWidget::valueChanged);
85  mVariableValue->setEnabled(false);
86  mLayout->addWidget(mVariableValue, 1, 3);
87 
88  connect(mSetValueTo, &QCheckBox::clicked, mVariableValue, &QLineEdit::setEnabled);
89 
90  mAdd = new QPushButton(this);
91  mAdd->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
92  mAdd->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
93 
94  mRemove = new QPushButton(this);
95  mRemove->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
96  mRemove->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed));
97  mLayout->addWidget(mAdd, 1, 4);
98  mLayout->addWidget(mRemove, 1, 5);
99 
100  connect(mAdd, &QPushButton::clicked, this, &SieveGlobalVariableActionWidget::slotAddWidget);
101  connect(mRemove, &QPushButton::clicked, this, &SieveGlobalVariableActionWidget::slotRemoveWidget);
102 }
103 
104 void SieveGlobalVariableActionWidget::clear()
105 {
106  mVariableName->clear();
107  mSetValueTo->setChecked(false);
108  mVariableValue->setEnabled(false);
109  mVariableValue->clear();
110 }
111 
112 bool SieveGlobalVariableActionWidget::isInitialized() const
113 {
114  return !mVariableName->text().isEmpty();
115 }
116 
117 QString SieveGlobalVariableActionWidget::variableName() const
118 {
119  const QString varName = mVariableName->text();
120  if (varName.trimmed().isEmpty()) {
121  return {};
122  }
123  return varName;
124 }
125 
126 void SieveGlobalVariableActionWidget::setVariableValue(const QString &name)
127 {
128  mSetValueTo->setChecked(true);
129  mVariableValue->setText(name);
130  mVariableValue->setEnabled(true);
131 }
132 
133 void SieveGlobalVariableActionWidget::loadScript(QXmlStreamReader &element, QString &error)
134 {
135  while (element.readNextStartElement()) {
136  const QStringRef tagName = element.name();
137  if (tagName == QLatin1String("str")) {
138  mVariableName->setText(element.readElementText());
139  } else {
140  error += i18n("Unknown tag \"%1\" during loading of variables.", *tagName.string()) + QLatin1Char('\n');
141  qCDebug(LIBKSIEVE_LOG) << " SieveGlobalVariableActionWidget::loadScript unknown tagName " << tagName;
142  }
143  }
144 }
145 
146 void SieveGlobalVariableActionWidget::slotAddWidget()
147 {
148  Q_EMIT addWidget(this);
149  Q_EMIT valueChanged();
150 }
151 
152 void SieveGlobalVariableActionWidget::slotRemoveWidget()
153 {
154  Q_EMIT removeWidget(this);
155  Q_EMIT valueChanged();
156 }
157 
158 void SieveGlobalVariableActionWidget::updateAddRemoveButton(bool addButtonEnabled, bool removeButtonEnabled)
159 {
160  mAdd->setEnabled(addButtonEnabled);
161  mRemove->setEnabled(removeButtonEnabled);
162 }
163 
164 SieveGlobalVariableWidget::SieveGlobalVariableWidget(QWidget *parent)
165  : SieveWidgetPageAbstract(parent)
166 {
167  QVBoxLayout *lay = new QVBoxLayout(this);
168  mHelpButton = new SieveHelpButton(this);
169  lay->addWidget(mHelpButton);
170  connect(mHelpButton, &SieveHelpButton::clicked, this, &SieveGlobalVariableWidget::slotHelp);
171 
172  mIncludeLister = new SieveGlobalVariableLister(this);
173  connect(mIncludeLister, &SieveGlobalVariableLister::valueChanged, this, &SieveGlobalVariableWidget::valueChanged);
174  lay->addWidget(mIncludeLister, 0, Qt::AlignTop);
175  setPageType(KSieveUi::SieveScriptBlockWidget::GlobalVariable);
176 }
177 
178 SieveGlobalVariableWidget::~SieveGlobalVariableWidget()
179 {
180 }
181 
182 void SieveGlobalVariableWidget::slotHelp()
183 {
184  const QString help = i18n(
185  "A variable has global scope in all scripts that have declared it with the \"global\" command. If a script uses that variable name without declaring it global, the name specifies a separate, non-global variable within that script.");
186  const QUrl href = KSieveUi::SieveEditorUtil::helpUrl(KSieveUi::SieveEditorUtil::GlobalVariable);
187  const QString fullWhatsThis = AutoCreateScriptUtil::createFullWhatsThis(help, href.toString());
188  QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton);
189 }
190 
191 void SieveGlobalVariableWidget::generatedScript(QString &script, QStringList &requireModules, bool inForEveryPartLoop)
192 {
193  Q_UNUSED(inForEveryPartLoop)
194  QString result;
195  QStringList lst;
196  mIncludeLister->generatedScript(result, lst);
197  if (!result.isEmpty()) {
198  script += result;
199  requireModules << lst;
200  }
201 }
202 
203 void SieveGlobalVariableWidget::loadScript(QXmlStreamReader &element, QString &error)
204 {
205  mIncludeLister->loadScript(element, error);
206 }
207 
208 SieveGlobalVariableActionWidget::VariableElement SieveGlobalVariableWidget::loadSetVariable(QXmlStreamReader &element, QString &error)
209 {
210  return mIncludeLister->loadSetVariable(element, error);
211 }
212 
213 SieveGlobalVariableLister::SieveGlobalVariableLister(QWidget *parent)
214  : KPIM::KWidgetLister(false, MINIMUMGLOBALVARIABLEACTION, MAXIMUMGLOBALVARIABLEACTION, parent)
215 {
216  slotClear();
217  updateAddRemoveButton();
218 }
219 
220 SieveGlobalVariableLister::~SieveGlobalVariableLister()
221 {
222 }
223 
224 void SieveGlobalVariableLister::slotAddWidget(QWidget *w)
225 {
226  addWidgetAfterThisWidget(w);
227  updateAddRemoveButton();
228 }
229 
230 void SieveGlobalVariableLister::slotRemoveWidget(QWidget *w)
231 {
232  removeWidget(w);
233  updateAddRemoveButton();
234 }
235 
236 void SieveGlobalVariableLister::updateAddRemoveButton()
237 {
238  QList<QWidget *> widgetList = widgets();
239  const int numberOfWidget(widgetList.count());
240  bool addButtonEnabled = false;
241  bool removeButtonEnabled = false;
242  if (numberOfWidget <= widgetsMinimum()) {
243  addButtonEnabled = true;
244  removeButtonEnabled = false;
245  } else if (numberOfWidget >= widgetsMaximum()) {
246  addButtonEnabled = false;
247  removeButtonEnabled = true;
248  } else {
249  addButtonEnabled = true;
250  removeButtonEnabled = true;
251  }
252  QList<QWidget *>::ConstIterator wIt = widgetList.constBegin();
253  QList<QWidget *>::ConstIterator wEnd = widgetList.constEnd();
254  for (; wIt != wEnd; ++wIt) {
255  SieveGlobalVariableActionWidget *w = qobject_cast<SieveGlobalVariableActionWidget *>(*wIt);
256  w->updateAddRemoveButton(addButtonEnabled, removeButtonEnabled);
257  }
258 }
259 
260 void SieveGlobalVariableLister::generatedScript(QString &script, QStringList &requireModules)
261 {
262  requireModules << QStringLiteral("include");
263  const QList<QWidget *> widgetList = widgets();
264  QList<QWidget *>::ConstIterator wIt = widgetList.constBegin();
265  QList<QWidget *>::ConstIterator wEnd = widgetList.constEnd();
266  for (; wIt != wEnd; ++wIt) {
267  SieveGlobalVariableActionWidget *w = qobject_cast<SieveGlobalVariableActionWidget *>(*wIt);
268  w->generatedScript(script);
269  }
270 }
271 
272 void SieveGlobalVariableLister::reconnectWidget(SieveGlobalVariableActionWidget *w)
273 {
274  connect(w, &SieveGlobalVariableActionWidget::addWidget, this, &SieveGlobalVariableLister::slotAddWidget, Qt::UniqueConnection);
275  connect(w, &SieveGlobalVariableActionWidget::removeWidget, this, &SieveGlobalVariableLister::slotRemoveWidget, Qt::UniqueConnection);
276  connect(w, &SieveGlobalVariableActionWidget::valueChanged, this, &SieveGlobalVariableLister::valueChanged, Qt::UniqueConnection);
277 }
278 
279 void SieveGlobalVariableLister::clearWidget(QWidget *aWidget)
280 {
281  if (aWidget) {
282  SieveGlobalVariableActionWidget *widget = static_cast<SieveGlobalVariableActionWidget *>(aWidget);
283  widget->clear();
284  }
285  Q_EMIT valueChanged();
286 }
287 
288 QWidget *SieveGlobalVariableLister::createWidget(QWidget *parent)
289 {
290  SieveGlobalVariableActionWidget *w = new SieveGlobalVariableActionWidget(parent);
291  reconnectWidget(w);
292  return w;
293 }
294 
295 void SieveGlobalVariableLister::loadScript(QXmlStreamReader &element, QString &error)
296 {
297  SieveGlobalVariableActionWidget *w = static_cast<SieveGlobalVariableActionWidget *>(widgets().constLast());
298  if (w->isInitialized()) {
299  addWidgetAfterThisWidget(widgets().constLast());
300  w = static_cast<SieveGlobalVariableActionWidget *>(widgets().constLast());
301  }
302  w->loadScript(element, error);
303 }
304 
305 SieveGlobalVariableActionWidget::VariableElement SieveGlobalVariableLister::loadSetVariable(QXmlStreamReader &element, QString & /*error*/)
306 {
307  SieveGlobalVariableActionWidget::VariableElement var;
308  QString variableName;
309  QString variableValue;
310  int index = 0;
311  while (element.readNextStartElement()) {
312  const QStringRef tagName = element.name();
313  if (tagName == QLatin1String("str")) {
314  if (index == 0) {
315  variableName = element.readElementText();
316  } else if (index == 1) {
317  variableValue = element.readElementText();
318  } else {
319  qCDebug(LIBKSIEVE_LOG) << " SieveGlobalVariableLister::loadSetVariable too many argument:" << index;
320  }
321  ++index;
322  } else {
323  qCDebug(LIBKSIEVE_LOG) << " SieveGlobalVariableLister::loadSetVariable unknown tagName " << tagName;
324  }
325  }
326 
327  const QList<QWidget *> lstWidget = widgets();
328  bool globalVariableFound = false;
329  for (QWidget *widget : lstWidget) {
330  SieveGlobalVariableActionWidget *w = static_cast<SieveGlobalVariableActionWidget *>(widget);
331  if (w->variableName() == variableName) {
332  w->setVariableValue(variableValue);
333  globalVariableFound = true;
334  }
335  }
336  if (!globalVariableFound) {
337  var.variableName = variableName;
338  var.variableValue = variableValue;
339  }
340  return var;
341 }
void showText(const QPoint &pos, const QString &text, QWidget *w)
bool readNextStartElement()
void textChanged(const QString &text)
QString toString(QUrl::FormattingOptions options) const const
AlignTop
void setEnabled(bool)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
int count(const T &value) const const
const QList< QKeySequence > & help()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool isEmpty() const const
QString trimmed() const const
void clicked(bool checked)
QString i18n(const char *text, const TYPE &arg...)
QPoint pos()
const QString * string() const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void toggled(bool checked)
QIcon fromTheme(const QString &name)
QList::const_iterator constEnd() const const
QList::const_iterator constBegin() const const
UniqueConnection
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
QObject * parent() const const
QStringRef name() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jul 6 2020 23:07:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.