Libksieve

autocreatescriptutil.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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 <KPluginMetaData>
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"
24using namespace KSieveUi;
25
26QString AutoCreateScriptUtil::createMultiLine(const QString &str)
27{
28 const QString result = QStringLiteral("\n%1\n.\n;\n").arg(str);
29 return result;
30}
31
32QString 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 {};
39 case 1:
40 return QLatin1StringView("\"") + list.first() + QLatin1StringView("\"");
41 default: {
42 const QString result = createList(list, addEndSemiColon);
43 return result;
44 }
45 }
46}
47
48QString 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(QLatin1StringView("\""), QStringLiteral("\\\""));
55}
56
57QString AutoCreateScriptUtil::protectSlash(QString str)
58{
59 return str.replace(QLatin1Char('\\'), QStringLiteral("\\\\"));
60}
61
62QString 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 += QLatin1StringView(" ]");
75 if (addSemiColon) {
76 result += QLatin1Char(';');
77 }
78
79 return result;
80}
81
82QStringList AutoCreateScriptUtil::createListFromString(QString str)
83{
84 QStringList lst;
85 if (str.startsWith(QLatin1Char('[')) && str.endsWith(QLatin1StringView("];"))) {
86 str.remove(0, 1);
87 str.remove(str.length() - 2, 2);
88 } else if (str.startsWith(QLatin1Char('[')) && str.endsWith(QLatin1StringView("]"))) {
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 : std::as_const(lst)) {
98 s.remove(QLatin1Char('"'));
99 resultLst << s.trimmed();
100 }
101 lst = resultLst;
102 return lst;
103}
104
105QString 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
113QString AutoCreateScriptUtil::negativeString(bool isNegative)
114{
115 return isNegative ? QStringLiteral("not ") : QString();
116}
117
118QString AutoCreateScriptUtil::tagValueWithCondition(const QString &tag, bool notCondition)
119{
120 return (notCondition ? QStringLiteral("[NOT]") : QString()) + QLatin1Char(':') + tag;
121}
122
123QString AutoCreateScriptUtil::tagValue(const QString &tag)
124{
125 return QLatin1Char(':') + tag;
126}
127
128QString AutoCreateScriptUtil::strValue(QXmlStreamReader &element)
129{
130 if (element.readNextStartElement()) {
131 const QStringView textElementTagName = element.name();
132 if (textElementTagName == QLatin1StringView("str")) {
133 return element.readElementText();
134 } else {
135 element.skipCurrentElement();
136 }
137 }
138 return {};
139}
140
141QString AutoCreateScriptUtil::listValueToStr(QXmlStreamReader &element)
142{
143 const QStringList lst = AutoCreateScriptUtil::listValue(element);
144 // Don't add semicolon
145 return createList(lst, false);
146}
147
148QStringList AutoCreateScriptUtil::listValue(QXmlStreamReader &element)
149{
150 QStringList lst;
151 while (element.readNextStartElement()) {
152 const QStringView tagName = element.name();
153 if (tagName == QLatin1StringView("str")) {
154 lst << element.readElementText();
155 } else {
156 element.skipCurrentElement();
157 }
158 }
159 return lst;
160}
161
162QString AutoCreateScriptUtil::fixListValue(QString valueStr)
163{
164 static QRegularExpression reg(QStringLiteral("^\\[\\s*\".*\"\\s*]$"));
165 if (!(valueStr.startsWith(QLatin1Char('[')) && valueStr.endsWith(QLatin1Char(']')))) {
166 valueStr = QStringLiteral("\"%1\"").arg(valueStr);
167 } else if (valueStr.contains(reg)) {
168 } else {
169 valueStr = QStringLiteral("\"%1\"").arg(valueStr);
170 }
171
172 return valueStr;
173}
174
175void AutoCreateScriptUtil::comboboxItemNotFound(const QString &searchItem, const QString &name, QString &error)
176{
177 error += i18n("Cannot find item \"%1\" in widget \"%2\"", searchItem, name) + QLatin1Char('\n');
178}
179
180QString AutoCreateScriptUtil::createFullWhatsThis(const QString &help, const QString &href)
181{
182 if (href.isEmpty()) {
183 return help;
184 }
185 const QString fullWhatsThis = QLatin1StringView("<qt>") + help + QStringLiteral("<br><a href=\'%1\'>%2</a></qt>").arg(href, i18n("More information"));
186 return fullWhatsThis;
187}
188
189QString AutoCreateScriptUtil::indentation()
190{
191 return QStringLiteral(" ");
192}
193
194KSieveUi::AbstractMoveImapFolderWidget *AutoCreateScriptUtil::createImapFolderWidget()
195{
197 const KPluginMetaData editWidgetPlugin(QStringLiteral("pim6/libksieve/imapfoldercompletionplugin"));
198
199 const auto result = KPluginFactory::instantiatePlugin<KSieveUi::AbstractMoveImapFolderWidget>(editWidgetPlugin);
200 if (result) {
201 edit = result.plugin;
202 } else {
203 edit = new KSieveUi::MoveImapFolderWidget;
204 }
205 return edit;
206}
207
208KSieveUi::AbstractSelectEmailLineEdit *AutoCreateScriptUtil::createSelectEmailsWidget()
209{
211 const KPluginMetaData editWidgetPlugin(QStringLiteral("pim6/libksieve/emaillineeditplugin"));
212
213 const auto result = KPluginFactory::instantiatePlugin<KSieveUi::AbstractSelectEmailLineEdit>(editWidgetPlugin);
214 if (result) {
215 edit = result.plugin;
216 } else {
217 edit = new AddressLineEdit;
218 }
219 return edit;
220}
221
222AbstractRegexpEditorLineEdit *AutoCreateScriptUtil::createRegexpEditorLineEdit(QWidget *parent)
223{
224 auto *edit = new KSieveUi::RegexpEditorLineEdit(parent);
225 return edit;
226}
227
228QString AutoCreateScriptUtil::generateConditionComment(const QString &comment)
229{
230 QString strComment;
231 if (!comment.trimmed().isEmpty()) {
232 const QList<QStringView> commentList = QStringView(comment).split(QLatin1Char('\n'));
233 for (const QStringView str : commentList) {
234 if (str.isEmpty()) {
235 strComment += QLatin1Char('\n');
236 } else {
237 if (!strComment.isEmpty()) {
238 strComment += QLatin1Char('\n');
239 }
240 strComment += QLatin1StringView(" #") + str;
241 }
242 }
243 }
244 return strComment;
245}
246
247QString AutoCreateScriptUtil::loadConditionComment(QString originalComment, const QString &comment)
248{
249 if (originalComment.isEmpty()) {
250 originalComment = comment;
251 } else {
252 originalComment += QLatin1Char('\n') + comment;
253 }
254 return originalComment;
255}
The AbstractMoveImapFolderWidget class.
The AbstractRegexpEditorLineEdit class.
The AbstractSelectEmailLineEdit class.
QString i18n(const char *text, const TYPE &arg...)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
const QList< QKeySequence > & help()
qsizetype count() const const
T & first()
void reserve(qsizetype size)
QString arg(Args &&... args) const const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString & replace(QChar before, QChar after, Qt::CaseSensitivity cs)
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QString trimmed() const const
QList< QStringView > split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:17:19 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.