Mailcommon

filterimporterclawsmail.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 "filterimporterclawsmail.h"
8 #include "filter/filtermanager.h"
9 #include "filter/mailfilter.h"
10 #include "mailcommon_debug.h"
11 
12 #include <QDir>
13 #include <QFile>
14 
15 using namespace MailCommon;
16 
17 FilterImporterClawsMails::FilterImporterClawsMails(QFile *file)
18  : FilterImporterAbstract()
19 {
20  QTextStream stream(file);
21  readStream(stream);
22 }
23 
24 FilterImporterClawsMails::FilterImporterClawsMails(QString string)
25  : FilterImporterAbstract()
26 {
27  QTextStream stream(&string);
28  readStream(stream);
29 }
30 
31 FilterImporterClawsMails::FilterImporterClawsMails(bool interactive)
32  : FilterImporterAbstract(interactive)
33 {
34 }
35 
36 FilterImporterClawsMails::~FilterImporterClawsMails() = default;
37 
38 void FilterImporterClawsMails::readStream(QTextStream &stream)
39 {
40  MailFilter *filter = nullptr;
41  while (!stream.atEnd()) {
42  QString line = stream.readLine();
43  qCDebug(MAILCOMMON_LOG) << " line :" << line << " filter " << filter;
44 
45  if (line.isEmpty()) {
46  // Nothing
47  } else if (line.startsWith(QLatin1Char('[')) && line.endsWith(QLatin1Char(']'))) {
48  // TODO
49  } else {
50  appendFilter(filter);
51  filter = parseLine(line);
52  }
53  }
54  appendFilter(filter);
55 }
56 
57 QString FilterImporterClawsMails::defaultFiltersSettingsPath()
58 {
59  return QStringLiteral("%1/.claws-mail/matcherrc").arg(QDir::homePath());
60 }
61 
62 MailFilter *FilterImporterClawsMails::parseLine(const QString &line)
63 {
64  auto filter = new MailFilter();
65  QString tmp = line;
66  // Enabled ?
67  if (tmp.startsWith(QLatin1String("enabled"))) {
68  filter->setEnabled(true);
69  tmp.remove(QStringLiteral("enabled "));
70  }
71 
72  // Filter name
73  if (tmp.startsWith(QLatin1String("rulename"))) {
74  tmp.remove(QStringLiteral("rulename "));
75  int pos;
76  const QString name = extractString(tmp, pos);
77  filter->pattern()->setName(name);
78  filter->setToolbarName(name);
79 
80  tmp = tmp.mid(pos + 2); // remove "\" "
81  qCDebug(MAILCOMMON_LOG) << " new tmp" << tmp;
82  }
83 
84  tmp = extractConditions(tmp, filter);
85 
86  tmp = extractActions(tmp, filter);
87  // TODO
88  return filter;
89 }
90 
91 QString FilterImporterClawsMails::extractActions(const QString &line, MailFilter *filter)
92 {
93  Q_UNUSED(filter)
94  return line;
95 }
96 
97 QString FilterImporterClawsMails::extractConditions(const QString &line, MailFilter *filter)
98 {
99  QByteArray fieldName;
100  // Action
101  if (line.startsWith(QLatin1String("subject"))) {
102  fieldName = "subject";
103  } else if (line.startsWith(QLatin1String("age_lower"))) {
104  }
105  filter->pattern()->setOp(SearchPattern::OpAnd);
106  // TODO
107  return {};
108 }
109 
110 QString FilterImporterClawsMails::extractString(const QString &tmp, int &pos)
111 {
112  QString name;
113  QChar previousChar;
114  int i = 0;
115  for (; i < tmp.length(); ++i) {
116  const QChar currentChar = tmp.at(i);
117  if (i == 0 && (currentChar.isSpace() || currentChar == QLatin1Char('"'))) {
118  } else {
119  if (currentChar != QLatin1Char('"')) {
120  if (currentChar != QLatin1Char('\\')) {
121  name += currentChar;
122  }
123  } else {
124  if (previousChar == QLatin1Char('\\')) {
125  name += currentChar;
126  } else {
127  break;
128  }
129  }
130  previousChar = currentChar;
131  }
132  }
133  pos = i;
134  qCDebug(MAILCOMMON_LOG) << " name " << name;
135  return name;
136 }
bool endsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString homePath()
The MailFilter class.
Definition: mailfilter.h:28
bool isSpace() const const
bool atEnd() const const
bool isEmpty() const const
int length() const const
QString readLine(qint64 maxlen)
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString & remove(int position, int n)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString name(StandardShortcut id)
const QChar at(int position) const const
QString mid(int position, int n) const const
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.