Mailcommon

filterimporterbalsa.cpp
1/*
2 SPDX-FileCopyrightText: 2012-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "filterimporterbalsa.h"
8#include "filter/mailfilter.h"
9#include "mailcommon_debug.h"
10
11#include <KConfig>
12#include <KConfigGroup>
13
14#include <QDir>
15#include <QFile>
16#include <QRegularExpression>
17
18using namespace MailCommon;
19
20FilterImporterBalsa::FilterImporterBalsa(QFile *file)
21 : FilterImporterAbstract()
22{
23 KConfig config(file->fileName());
24 readConfig(&config);
25}
26
27FilterImporterBalsa::FilterImporterBalsa()
28 : FilterImporterAbstract()
29{
30}
31
32FilterImporterBalsa::~FilterImporterBalsa() = default;
33
34QString FilterImporterBalsa::defaultFiltersSettingsPath()
35{
36 return QStringLiteral("%1/.balsa/config").arg(QDir::homePath());
37}
38
39void FilterImporterBalsa::readConfig(KConfig *config)
40{
41 const QStringList filterList = config->groupList().filter(QRegularExpression(QStringLiteral("filter-\\d+")));
42 for (const QString &filter : filterList) {
43 KConfigGroup grp = config->group(filter);
44 parseFilter(grp);
45 }
46}
47
48void FilterImporterBalsa::parseFilter(const KConfigGroup &grp)
49{
50 auto filter = new MailCommon::MailFilter();
51 const QString name = grp.readEntry(QStringLiteral("Name"));
52 filter->pattern()->setName(name);
53 filter->setToolbarName(name);
54
55 // TODO not implemented in kmail.
56 // const QString popupText = grp.readEntry(QStringLiteral("Popup-text"));
57
58 const QString sound = grp.readEntry(QStringLiteral("Sound"));
59 if (!sound.isEmpty()) {
60 const QString actionName = QStringLiteral("play sound");
61 createFilterAction(filter, actionName, sound);
62 }
63
64 const int actionType = grp.readEntry(QStringLiteral("Action-type"), -1);
65 const QString actionStr = grp.readEntry(QStringLiteral("Action-string"));
66 parseAction(actionType, actionStr, filter);
67
68 const QString condition = grp.readEntry(QStringLiteral("Condition"));
69 parseCondition(condition, filter);
70
71 appendFilter(filter);
72}
73
74void FilterImporterBalsa::parseCondition(const QString &condition, MailCommon::MailFilter *filter)
75{
76 QStringList conditionList;
77 if (condition.startsWith(QLatin1StringView("OR "))) {
78 conditionList = condition.split(QStringLiteral("OR"));
79 filter->pattern()->setOp(SearchPattern::OpOr);
80 } else if (condition.startsWith(QLatin1StringView("AND "))) {
81 conditionList = condition.split(QStringLiteral("AND"));
82 filter->pattern()->setOp(SearchPattern::OpAnd);
83 } else {
84 // no multi condition
85 conditionList << condition;
86 }
87 for (QString cond : std::as_const(conditionList)) {
88 cond = cond.trimmed();
89 if (cond.startsWith(QLatin1StringView("NOT"))) {
90 cond = cond.right(cond.length() - 3);
91 cond = cond.trimmed();
92 }
93 qCDebug(MAILCOMMON_LOG) << " cond" << cond;
94
95 // Date between
96 QByteArray fieldName;
97 if (cond.startsWith(QLatin1StringView("DATE"))) {
98 fieldName = "<date>";
99 cond = cond.right(cond.length() - 4);
100 cond = cond.trimmed();
101 QStringList splitDate = cond.split(QLatin1Char(' '));
102 qCDebug(MAILCOMMON_LOG) << " splitDate " << splitDate;
103 } else if (cond.startsWith(QLatin1StringView("FLAG"))) {
104 qCDebug(MAILCOMMON_LOG) << " FLAG :";
105 } else if (cond.startsWith(QLatin1StringView("STRING"))) {
106 qCDebug(MAILCOMMON_LOG) << " STRING";
107 } else {
108 qCDebug(MAILCOMMON_LOG) << " condition not implemented :" << cond;
109 }
110
111 // SearchRule::Ptr rule = SearchRule::createInstance( fieldName, functionName, line );
112 // filter->pattern()->append( rule );
113 }
114}
115
116void FilterImporterBalsa::parseAction(int actionType, const QString &action, MailCommon::MailFilter *filter)
117{
118 QString actionName;
119 QString actionStr(action);
120 switch (actionType) {
121 case 0:
122 break;
123 case 1:
124 // Copy
125 actionName = QStringLiteral("copy");
126 break;
127 case 2:
128 // Move
129 actionName = QStringLiteral("transfer");
130 break;
131 case 3:
132 // Print
133 // Not implemented in kmail
134 break;
135 case 4:
136 // Execute
137 actionName = QStringLiteral("execute");
138 break;
139 case 5:
140 // Move to trash
141 actionName = QStringLiteral("transfer");
142 // Special !
143 break;
144 case 6:
145 // Put color
146 break;
147 default:
148 qCDebug(MAILCOMMON_LOG) << " unknown parse action type " << actionType;
149 break;
150 }
151 if (!actionName.isEmpty()) {
152 // TODO adapt actionStr
153 createFilterAction(filter, actionName, actionStr);
154 }
155}
KConfigGroup group(const QString &group)
QString readEntry(const char *key, const char *aDefault=nullptr) const
QStringList groupList() const override
The MailFilter class.
Definition mailfilter.h:29
QString name(StandardAction id)
The filter dialog.
QByteArray right(qsizetype len) const const
QByteArray trimmed() const const
QString homePath()
virtual QString fileName() const const override
QString arg(Args &&... args) const const
bool isEmpty() const const
QStringList split(QChar sep, Qt::SplitBehavior behavior, Qt::CaseSensitivity cs) const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QStringList filter(QStringView str, Qt::CaseSensitivity cs) const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 6 2024 12:02:04 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.