Mailcommon

filterimporterclawsmail.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 "filterimporterclawsmail.h"
8#include "filter/mailfilter.h"
9#include "mailcommon_debug.h"
10
11#include <QDir>
12#include <QFile>
13
14using namespace MailCommon;
15
16FilterImporterClawsMails::FilterImporterClawsMails(QFile *file)
17 : FilterImporterAbstract()
18{
19 QTextStream stream(file);
20 readStream(stream);
21}
22
23FilterImporterClawsMails::FilterImporterClawsMails(QString string)
24 : FilterImporterAbstract()
25{
26 QTextStream stream(&string);
27 readStream(stream);
28}
29
30FilterImporterClawsMails::FilterImporterClawsMails(bool interactive)
31 : FilterImporterAbstract(interactive)
32{
33}
34
35FilterImporterClawsMails::~FilterImporterClawsMails() = default;
36
37void 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
56QString FilterImporterClawsMails::defaultFiltersSettingsPath()
57{
58 return QStringLiteral("%1/.claws-mail/matcherrc").arg(QDir::homePath());
59}
60
61MailFilter *FilterImporterClawsMails::parseLine(const QString &line)
62{
63 auto filter = new MailFilter();
64 QString tmp = line;
65 // Enabled ?
66 if (tmp.startsWith(QLatin1StringView("enabled"))) {
67 filter->setEnabled(true);
68 tmp.remove(QStringLiteral("enabled "));
69 }
70
71 // Filter name
72 if (tmp.startsWith(QLatin1StringView("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
90QString FilterImporterClawsMails::extractActions(const QString &line, MailFilter *filter)
91{
92 Q_UNUSED(filter)
93 return line;
94}
95
96QString FilterImporterClawsMails::extractConditions(const QString &line, MailFilter *filter)
97{
98 QByteArray fieldName;
99 // Action
100 if (line.startsWith(QLatin1StringView("subject"))) {
101 fieldName = "subject";
102 } else if (line.startsWith(QLatin1StringView("age_lower"))) {
103 }
104 filter->pattern()->setOp(SearchPattern::OpAnd);
105 // TODO
106 return {};
107}
108
109QString FilterImporterClawsMails::extractString(const QString &tmp, int &pos)
110{
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}
The MailFilter class.
Definition mailfilter.h:29
QString name(StandardAction id)
The filter dialog.
bool isSpace(char32_t ucs4)
QString homePath()
QString arg(Args &&... args) const const
const QChar at(qsizetype position) const const
bool endsWith(QChar c, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QFuture< void > filter(QThreadPool *pool, Sequence &sequence, KeepFunctor &&filterFunction)
bool atEnd() const const
QString readLine(qint64 maxlen)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:01 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.