Mailcommon

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

KDE's Doxygen guidelines are available online.