Libksieve

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

KDE's Doxygen guidelines are available online.