Mailcommon

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

KDE's Doxygen guidelines are available online.