Libksieve

sieveconditionhasflag.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionhasflag.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
10 #include "editor/sieveeditorutil.h"
11 
12 #include <KLocalizedString>
13 #include <Libkdepim/LineEditCatchReturnKey>
14 #include <QLineEdit>
15 
16 #include "libksieve_debug.h"
17 #include <QHBoxLayout>
18 #include <QLabel>
19 #include <QWidget>
20 #include <QXmlStreamReader>
21 
22 using namespace KSieveUi;
23 SieveConditionHasFlag::SieveConditionHasFlag(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("hasflag"), i18n("Has Flag"), parent)
25 {
26  hasVariableSupport = sieveCapabilities().contains(QLatin1String("variables"));
27 }
28 
29 QWidget *SieveConditionHasFlag::createParamWidget(QWidget *parent) const
30 {
31  auto w = new QWidget(parent);
32  auto lay = new QHBoxLayout;
33  lay->setContentsMargins({});
34  w->setLayout(lay);
35  auto selecttype = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
36  selecttype->setObjectName(QStringLiteral("matchtype"));
37  connect(selecttype, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHasFlag::valueChanged);
38  lay->addWidget(selecttype);
39 
40  auto grid = new QGridLayout;
41  grid->setContentsMargins({});
42  lay->addLayout(grid);
43 
44  int row = 0;
45  if (hasVariableSupport) {
46  auto lab = new QLabel(i18n("Variable name\n (if empty it uses internal variable):"));
47  grid->addWidget(lab, row, 0);
48 
49  auto variableName = new QLineEdit;
50  new KPIM::LineEditCatchReturnKey(variableName, w);
51  variableName->setObjectName(QStringLiteral("variablename"));
52  connect(variableName, &QLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged);
53  grid->addWidget(variableName, row, 1);
54  ++row;
55  }
56  auto lab = new QLabel(i18n("Value:"));
57  grid->addWidget(lab, row, 0);
58 
59  AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
60  connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHasFlag::valueChanged);
61  connect(selecttype, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
62  value->setObjectName(QStringLiteral("value"));
63  grid->addWidget(value, row, 1);
64 
65  return w;
66 }
67 
68 QString SieveConditionHasFlag::code(QWidget *w) const
69 {
70  const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
71  bool isNegative = false;
72  const QString matchString = matchTypeCombo->code(isNegative);
73 
74  QString result = AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("hasflag %1").arg(matchString);
75 
76  if (hasVariableSupport) {
77  const QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
78  const QString variableNameStr = variableName->text();
79  if (!variableNameStr.isEmpty()) {
80  result += QLatin1String(" \"") + variableNameStr + QLatin1Char('"');
81  }
82 
83  const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
84  const QString valueStr = value->code();
85  result += QLatin1String(" \"") + valueStr + QLatin1Char('"');
86  }
87  return result + AutoCreateScriptUtil::generateConditionComment(comment());
88 }
89 
90 QStringList SieveConditionHasFlag::needRequires(QWidget *w) const
91 {
92  QStringList lst;
93  if (sieveCapabilities().contains(QLatin1String("imap4flags"))) {
94  lst << QStringLiteral("imap4flags");
95  } else {
96  lst << QStringLiteral("imapflags");
97  }
98  if (hasVariableSupport) {
99  lst << QStringLiteral("variables");
100  }
101  const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
102  lst << matchTypeCombo->needRequires();
103  return lst;
104 }
105 
106 bool SieveConditionHasFlag::needCheckIfServerHasCapability() const
107 {
108  return true;
109 }
110 
111 QString SieveConditionHasFlag::serverNeedsCapability() const
112 {
113  return QStringLiteral("variables");
114 #if 0
115  if (sieveCapabilities().contains(QLatin1String("variables"))) {
116  }
117  if (sieveCapabilities().contains(QLatin1String("imap4flags"))) {
118  return QStringLiteral("imap4flags");
119  } else {
120  return QStringLiteral("imapflags");
121  }
122 #endif
123 }
124 
125 QString SieveConditionHasFlag::help() const
126 {
127  return i18n("The hasflag test evaluates to true if any of the variables matches any flag name.");
128 }
129 
130 void SieveConditionHasFlag::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
131 {
132  QStringList strList;
133  QString commentStr;
134  while (element.readNextStartElement()) {
135  const QStringRef tagName = element.name();
136  if (tagName == QLatin1String("tag")) {
137  auto matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
138  matchTypeCombo->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
139  } else if (tagName == QLatin1String("str")) {
140  strList << element.readElementText();
141  } else if (tagName == QLatin1String("crlf")) {
142  element.skipCurrentElement();
143  // nothing
144  } else if (tagName == QLatin1String("comment")) {
145  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
146  } else {
147  unknownTag(tagName, error);
148  qCDebug(LIBKSIEVE_LOG) << " SieveConditionExists::setParamWidgetValue unknown tagName " << tagName;
149  }
150  }
151  if (!commentStr.isEmpty()) {
152  setComment(commentStr);
153  }
154 
155  switch (strList.count()) {
156  case 1: {
157  auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
158  value->setCode(strList.at(0));
159  break;
160  }
161  case 2:
162  if (hasVariableSupport) {
163  auto variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
164  variableName->setText(strList.at(0));
165  auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
166  value->setCode(strList.at(1));
167  } else {
168  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHasFlag has not variable support";
169  }
170  break;
171  default:
172  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHasFlag::setParamWidgetValue str list count not correct :" << strList.count();
173  break;
174  }
175 }
176 
177 QUrl SieveConditionHasFlag::href() const
178 {
179  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
180 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
const T & at(int i) const const
void textChanged(const QString &text)
void skipCurrentElement()
int count(const T &value) const const
The AbstractRegexpEditorLineEdit class.
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
void setObjectName(const QString &name)
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QStringRef name() const const
T findChild(const QString &name, Qt::FindChildOptions options) const const
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.