Libksieve

autocreatescriptutil.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 "autocreatescriptutil_p.h"
8 
9 #include <KLocalizedString>
10 
11 #include <KPluginFactory>
12 #include <KPluginLoader>
13 #include <QRegularExpression>
14 #include <QStringList>
15 
16 #include "autocreatescripts/sieveconditions/widgets/regexpeditorlineedit.h"
17 
18 #include <KSieveUi/AbstractMoveImapFolderWidget>
19 #include <KSieveUi/AbstractSelectEmailLineEdit>
20 
21 #include <widgets/moveimapfolderwidget.h>
22 
23 #include <autocreatescripts/sieveactions/widgets/addresslineedit.h>
24 using namespace KSieveUi;
25 
26 QString AutoCreateScriptUtil::createMultiLine(const QString &str)
27 {
28  const QString result = QStringLiteral("\n%1\n.\n;\n").arg(str);
29  return result;
30 }
31 
32 QString AutoCreateScriptUtil::createList(const QString &str, QChar separator, bool addEndSemiColon)
33 {
34  const QStringList list = str.trimmed().split(separator);
35  const int count = list.count();
36  switch (count) {
37  case 0:
38  return QString();
39  case 1:
40  return QLatin1String("\"") + list.first() + QLatin1String("\"");
41  default: {
42  const QString result = createList(list, addEndSemiColon);
43  return result;
44  }
45  }
46 }
47 
48 QString AutoCreateScriptUtil::quoteStr(const QString &str, bool protectSlash)
49 {
50  QString st = str;
51  if (protectSlash) {
52  st = AutoCreateScriptUtil::protectSlash(str);
53  }
54  return st.replace(QLatin1String("\""), QStringLiteral("\\\""));
55 }
56 
57 QString AutoCreateScriptUtil::protectSlash(QString str)
58 {
59  return str.replace(QLatin1Char('\\'), QStringLiteral("\\\\"));
60 }
61 
62 QString AutoCreateScriptUtil::createList(const QStringList &lst, bool addSemiColon, bool protectSlash)
63 {
64  QString result;
65  result = QLatin1Char('[');
66  bool wasFirst = true;
67  for (QString str : lst) {
68  if (protectSlash) {
69  str = AutoCreateScriptUtil::protectSlash(str);
70  }
71  result += (wasFirst ? QString() : QStringLiteral(",")) + QStringLiteral(" \"%1\"").arg(quoteStr(str, false));
72  wasFirst = false;
73  }
74  result += QLatin1String(" ]");
75  if (addSemiColon) {
76  result += QLatin1Char(';');
77  }
78 
79  return result;
80 }
81 
82 QStringList AutoCreateScriptUtil::createListFromString(QString str)
83 {
84  QStringList lst;
85  if (str.startsWith(QLatin1Char('[')) && str.endsWith(QLatin1String("];"))) {
86  str.remove(0, 1);
87  str.remove(str.length() - 2, 2);
88  } else if (str.startsWith(QLatin1Char('[')) && str.endsWith(QLatin1String("]"))) {
89  str.remove(0, 1);
90  str.remove(str.length() - 1, 1);
91  } else {
92  return lst;
93  }
94  lst = str.split(QStringLiteral(", "));
95  QStringList resultLst;
96  resultLst.reserve(lst.count());
97  for (QString s : qAsConst(lst)) {
98  s.remove(QLatin1Char('"'));
99  resultLst << s.trimmed();
100  }
101  lst = resultLst;
102  return lst;
103 }
104 
105 QString AutoCreateScriptUtil::createAddressList(const QString &str, bool addSemiColon)
106 {
107  if (str.trimmed().startsWith(QLatin1Char('[')) && str.trimmed().endsWith(QLatin1Char(']'))) {
108  return str;
109  }
110  return createList(str, QLatin1Char(';'), addSemiColon);
111 }
112 
113 QString AutoCreateScriptUtil::negativeString(bool isNegative)
114 {
115  return isNegative ? QStringLiteral("not ") : QString();
116 }
117 
118 QString AutoCreateScriptUtil::tagValueWithCondition(const QString &tag, bool notCondition)
119 {
120  return (notCondition ? QStringLiteral("[NOT]") : QString()) + QLatin1Char(':') + tag;
121 }
122 
123 QString AutoCreateScriptUtil::tagValue(const QString &tag)
124 {
125  return QLatin1Char(':') + tag;
126 }
127 
128 QString AutoCreateScriptUtil::strValue(QXmlStreamReader &element)
129 {
130  if (element.readNextStartElement()) {
131  const QStringRef textElementTagName = element.name();
132  if (textElementTagName == QLatin1String("str")) {
133  return element.readElementText();
134  } else {
135  element.skipCurrentElement();
136  }
137  }
138  return QString();
139 }
140 
141 QString AutoCreateScriptUtil::listValueToStr(QXmlStreamReader &element)
142 {
143  const QStringList lst = AutoCreateScriptUtil::listValue(element);
144  // Don't add semicolon
145  return createList(lst, false);
146 }
147 
148 QStringList AutoCreateScriptUtil::listValue(QXmlStreamReader &element)
149 {
150  QStringList lst;
151  while (element.readNextStartElement()) {
152  const QStringRef tagName = element.name();
153  if (tagName == QLatin1String("str")) {
154  lst << element.readElementText();
155  } else {
156  element.skipCurrentElement();
157  }
158  }
159  return lst;
160 }
161 
162 QString AutoCreateScriptUtil::fixListValue(QString valueStr)
163 {
164  if (!(valueStr.startsWith(QLatin1Char('[')) && valueStr.endsWith(QLatin1Char(']')))) {
165  valueStr = QStringLiteral("\"%1\"").arg(valueStr);
166  } else if (valueStr.contains(QRegularExpression(QStringLiteral("^\\[\\s*\".*\"\\s*]$")))) {
167  } else {
168  valueStr = QStringLiteral("\"%1\"").arg(valueStr);
169  }
170 
171  return valueStr;
172 }
173 
174 void AutoCreateScriptUtil::comboboxItemNotFound(const QString &searchItem, const QString &name, QString &error)
175 {
176  error += i18n("Cannot find item \"%1\" in widget \"%2\"", searchItem, name) + QLatin1Char('\n');
177 }
178 
179 QString AutoCreateScriptUtil::createFullWhatsThis(const QString &help, const QString &href)
180 {
181  if (href.isEmpty()) {
182  return help;
183  }
184  const QString fullWhatsThis = QLatin1String("<qt>") + help + QStringLiteral("<br><a href=\'%1\'>%2</a></qt>").arg(href, i18n("More information"));
185  return fullWhatsThis;
186 }
187 
188 QString AutoCreateScriptUtil::indentation()
189 {
190  return QStringLiteral(" ");
191 }
192 
193 KSieveUi::AbstractMoveImapFolderWidget *AutoCreateScriptUtil::createImapFolderWidget()
194 {
196  KPluginLoader loader(QStringLiteral("libksieve/imapfoldercompletionplugin"));
197  KPluginFactory *factory = loader.factory();
198  if (factory) {
200  } else {
201  edit = new KSieveUi::MoveImapFolderWidget;
202  }
203  return edit;
204 }
205 
206 KSieveUi::AbstractSelectEmailLineEdit *AutoCreateScriptUtil::createSelectEmailsWidget()
207 {
209  KPluginLoader loader(QStringLiteral("libksieve/emaillineeditplugin"));
210  KPluginFactory *factory = loader.factory();
211  if (factory) {
213  } else {
214  edit = new AddressLineEdit;
215  }
216  return edit;
217 }
218 
219 AbstractRegexpEditorLineEdit *AutoCreateScriptUtil::createRegexpEditorLineEdit(QWidget *parent)
220 {
222  KPluginLoader loader(QStringLiteral("libksieve/regexpeditorlineeditplugin"));
223  KPluginFactory *factory = loader.factory();
224  if (factory) {
225  edit = factory->create<KSieveUi::AbstractRegexpEditorLineEdit>(parent);
226  } else {
227  edit = new KSieveUi::RegexpEditorLineEdit(parent);
228  }
229  return edit;
230 }
231 
232 QString AutoCreateScriptUtil::generateConditionComment(const QString &comment)
233 {
234  QString strComment;
235  if (!comment.trimmed().isEmpty()) {
236  const QVector<QStringRef> commentList = comment.splitRef(QLatin1Char('\n'));
237 
238  for (const QStringRef &str : commentList) {
239  if (str.isEmpty()) {
240  strComment += QLatin1Char('\n');
241  } else {
242  if (!strComment.isEmpty()) {
243  strComment += QLatin1Char('\n');
244  }
245  strComment += QLatin1String(" #") + str;
246  }
247  }
248  }
249  return strComment;
250 }
251 
252 QString AutoCreateScriptUtil::loadConditionComment(QString originalComment, const QString &comment)
253 {
254  if (originalComment.isEmpty()) {
255  originalComment = comment;
256  } else {
257  originalComment += QLatin1Char('\n') + comment;
258  }
259  return originalComment;
260 }
bool readNextStartElement()
T * create(QObject *parent=nullptr, const QVariantList &args=QVariantList())
void reserve(int alloc)
QString & remove(int position, int n)
The AbstractSelectEmailLineEdit class.
void skipCurrentElement()
int count(const T &value) const const
The AbstractRegexpEditorLineEdit class.
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool isEmpty() const const
QString trimmed() const const
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QStringList split(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
T & first()
The AbstractMoveImapFolderWidget class.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString i18n(const char *text, const TYPE &arg...)
QString & replace(int position, int n, QChar after)
QVector< QStringRef > splitRef(const QString &sep, QString::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
int length() const const
QStringRef name() const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.