File indexing completed on 2024-12-29 04:54:43
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "autocreatescriptutil_p.h" 0008 0009 #include <KLocalizedString> 0010 0011 #include <KPluginFactory> 0012 #include <KPluginMetaData> 0013 #include <QRegularExpression> 0014 #include <QStringList> 0015 0016 #include "autocreatescripts/sieveconditions/widgets/regexpeditorlineedit.h" 0017 0018 #include <KSieveUi/AbstractMoveImapFolderWidget> 0019 #include <KSieveUi/AbstractSelectEmailLineEdit> 0020 0021 #include "widgets/moveimapfolderwidget.h" 0022 0023 #include "autocreatescripts/sieveactions/widgets/addresslineedit.h" 0024 using namespace KSieveUi; 0025 0026 QString AutoCreateScriptUtil::createMultiLine(const QString &str) 0027 { 0028 const QString result = QStringLiteral("\n%1\n.\n;\n").arg(str); 0029 return result; 0030 } 0031 0032 QString AutoCreateScriptUtil::createList(const QString &str, QChar separator, bool addEndSemiColon) 0033 { 0034 const QStringList list = str.trimmed().split(separator); 0035 const int count = list.count(); 0036 switch (count) { 0037 case 0: 0038 return {}; 0039 case 1: 0040 return QLatin1StringView("\"") + list.first() + QLatin1StringView("\""); 0041 default: { 0042 const QString result = createList(list, addEndSemiColon); 0043 return result; 0044 } 0045 } 0046 } 0047 0048 QString AutoCreateScriptUtil::quoteStr(const QString &str, bool protectSlash) 0049 { 0050 QString st = str; 0051 if (protectSlash) { 0052 st = AutoCreateScriptUtil::protectSlash(str); 0053 } 0054 return st.replace(QLatin1StringView("\""), QStringLiteral("\\\"")); 0055 } 0056 0057 QString AutoCreateScriptUtil::protectSlash(QString str) 0058 { 0059 return str.replace(QLatin1Char('\\'), QStringLiteral("\\\\")); 0060 } 0061 0062 QString AutoCreateScriptUtil::createList(const QStringList &lst, bool addSemiColon, bool protectSlash) 0063 { 0064 QString result; 0065 result = QLatin1Char('['); 0066 bool wasFirst = true; 0067 for (QString str : lst) { 0068 if (protectSlash) { 0069 str = AutoCreateScriptUtil::protectSlash(str); 0070 } 0071 result += (wasFirst ? QString() : QStringLiteral(",")) + QStringLiteral(" \"%1\"").arg(quoteStr(str, false)); 0072 wasFirst = false; 0073 } 0074 result += QLatin1StringView(" ]"); 0075 if (addSemiColon) { 0076 result += QLatin1Char(';'); 0077 } 0078 0079 return result; 0080 } 0081 0082 QStringList AutoCreateScriptUtil::createListFromString(QString str) 0083 { 0084 QStringList lst; 0085 if (str.startsWith(QLatin1Char('[')) && str.endsWith(QLatin1StringView("];"))) { 0086 str.remove(0, 1); 0087 str.remove(str.length() - 2, 2); 0088 } else if (str.startsWith(QLatin1Char('[')) && str.endsWith(QLatin1StringView("]"))) { 0089 str.remove(0, 1); 0090 str.remove(str.length() - 1, 1); 0091 } else { 0092 return lst; 0093 } 0094 lst = str.split(QStringLiteral(", ")); 0095 QStringList resultLst; 0096 resultLst.reserve(lst.count()); 0097 for (QString s : std::as_const(lst)) { 0098 s.remove(QLatin1Char('"')); 0099 resultLst << s.trimmed(); 0100 } 0101 lst = resultLst; 0102 return lst; 0103 } 0104 0105 QString AutoCreateScriptUtil::createAddressList(const QString &str, bool addSemiColon) 0106 { 0107 if (str.trimmed().startsWith(QLatin1Char('[')) && str.trimmed().endsWith(QLatin1Char(']'))) { 0108 return str; 0109 } 0110 return createList(str, QLatin1Char(';'), addSemiColon); 0111 } 0112 0113 QString AutoCreateScriptUtil::negativeString(bool isNegative) 0114 { 0115 return isNegative ? QStringLiteral("not ") : QString(); 0116 } 0117 0118 QString AutoCreateScriptUtil::tagValueWithCondition(const QString &tag, bool notCondition) 0119 { 0120 return (notCondition ? QStringLiteral("[NOT]") : QString()) + QLatin1Char(':') + tag; 0121 } 0122 0123 QString AutoCreateScriptUtil::tagValue(const QString &tag) 0124 { 0125 return QLatin1Char(':') + tag; 0126 } 0127 0128 QString AutoCreateScriptUtil::strValue(QXmlStreamReader &element) 0129 { 0130 if (element.readNextStartElement()) { 0131 const QStringView textElementTagName = element.name(); 0132 if (textElementTagName == QLatin1StringView("str")) { 0133 return element.readElementText(); 0134 } else { 0135 element.skipCurrentElement(); 0136 } 0137 } 0138 return {}; 0139 } 0140 0141 QString AutoCreateScriptUtil::listValueToStr(QXmlStreamReader &element) 0142 { 0143 const QStringList lst = AutoCreateScriptUtil::listValue(element); 0144 // Don't add semicolon 0145 return createList(lst, false); 0146 } 0147 0148 QStringList AutoCreateScriptUtil::listValue(QXmlStreamReader &element) 0149 { 0150 QStringList lst; 0151 while (element.readNextStartElement()) { 0152 const QStringView tagName = element.name(); 0153 if (tagName == QLatin1StringView("str")) { 0154 lst << element.readElementText(); 0155 } else { 0156 element.skipCurrentElement(); 0157 } 0158 } 0159 return lst; 0160 } 0161 0162 QString AutoCreateScriptUtil::fixListValue(QString valueStr) 0163 { 0164 static QRegularExpression reg(QStringLiteral("^\\[\\s*\".*\"\\s*]$")); 0165 if (!(valueStr.startsWith(QLatin1Char('[')) && valueStr.endsWith(QLatin1Char(']')))) { 0166 valueStr = QStringLiteral("\"%1\"").arg(valueStr); 0167 } else if (valueStr.contains(reg)) { 0168 } else { 0169 valueStr = QStringLiteral("\"%1\"").arg(valueStr); 0170 } 0171 0172 return valueStr; 0173 } 0174 0175 void AutoCreateScriptUtil::comboboxItemNotFound(const QString &searchItem, const QString &name, QString &error) 0176 { 0177 error += i18n("Cannot find item \"%1\" in widget \"%2\"", searchItem, name) + QLatin1Char('\n'); 0178 } 0179 0180 QString AutoCreateScriptUtil::createFullWhatsThis(const QString &help, const QString &href) 0181 { 0182 if (href.isEmpty()) { 0183 return help; 0184 } 0185 const QString fullWhatsThis = QLatin1StringView("<qt>") + help + QStringLiteral("<br><a href=\'%1\'>%2</a></qt>").arg(href, i18n("More information")); 0186 return fullWhatsThis; 0187 } 0188 0189 QString AutoCreateScriptUtil::indentation() 0190 { 0191 return QStringLiteral(" "); 0192 } 0193 0194 KSieveUi::AbstractMoveImapFolderWidget *AutoCreateScriptUtil::createImapFolderWidget() 0195 { 0196 KSieveUi::AbstractMoveImapFolderWidget *edit = nullptr; 0197 const KPluginMetaData editWidgetPlugin(QStringLiteral("pim6/libksieve/imapfoldercompletionplugin")); 0198 0199 const auto result = KPluginFactory::instantiatePlugin<KSieveUi::AbstractMoveImapFolderWidget>(editWidgetPlugin); 0200 if (result) { 0201 edit = result.plugin; 0202 } else { 0203 edit = new KSieveUi::MoveImapFolderWidget; 0204 } 0205 return edit; 0206 } 0207 0208 KSieveUi::AbstractSelectEmailLineEdit *AutoCreateScriptUtil::createSelectEmailsWidget() 0209 { 0210 KSieveUi::AbstractSelectEmailLineEdit *edit = nullptr; 0211 const KPluginMetaData editWidgetPlugin(QStringLiteral("pim6/libksieve/emaillineeditplugin")); 0212 0213 const auto result = KPluginFactory::instantiatePlugin<KSieveUi::AbstractSelectEmailLineEdit>(editWidgetPlugin); 0214 if (result) { 0215 edit = result.plugin; 0216 } else { 0217 edit = new AddressLineEdit; 0218 } 0219 return edit; 0220 } 0221 0222 AbstractRegexpEditorLineEdit *AutoCreateScriptUtil::createRegexpEditorLineEdit(QWidget *parent) 0223 { 0224 auto *edit = new KSieveUi::RegexpEditorLineEdit(parent); 0225 return edit; 0226 } 0227 0228 QString AutoCreateScriptUtil::generateConditionComment(const QString &comment) 0229 { 0230 QString strComment; 0231 if (!comment.trimmed().isEmpty()) { 0232 const QList<QStringView> commentList = QStringView(comment).split(QLatin1Char('\n')); 0233 for (const QStringView str : commentList) { 0234 if (str.isEmpty()) { 0235 strComment += QLatin1Char('\n'); 0236 } else { 0237 if (!strComment.isEmpty()) { 0238 strComment += QLatin1Char('\n'); 0239 } 0240 strComment += QLatin1StringView(" #") + str; 0241 } 0242 } 0243 } 0244 return strComment; 0245 } 0246 0247 QString AutoCreateScriptUtil::loadConditionComment(QString originalComment, const QString &comment) 0248 { 0249 if (originalComment.isEmpty()) { 0250 originalComment = comment; 0251 } else { 0252 originalComment += QLatin1Char('\n') + comment; 0253 } 0254 return originalComment; 0255 }