Mailcommon

filterimportergmail.cpp
1/*
2 SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "filterimportergmail.h"
8#include "filter/mailfilter.h"
9#include "mailcommon_debug.h"
10#include <QDir>
11#include <QDomDocument>
12#include <QFile>
13
14using namespace MailCommon;
15
16FilterImporterGmail::FilterImporterGmail(QFile *file)
17 : FilterImporterAbstract()
18{
19 QDomDocument doc;
20 if (!loadDomElement(doc, file)) {
21 return;
22 }
23
24 QDomElement filters = doc.documentElement();
25
26 if (filters.isNull()) {
27 qCDebug(MAILCOMMON_LOG) << "No filters defined";
28 return;
29 }
30 for (QDomElement e = filters.firstChildElement(); !e.isNull(); e = e.nextSiblingElement()) {
31 const QString tag = e.tagName();
32 if (tag == QLatin1StringView("entry")) {
33 qCDebug(MAILCOMMON_LOG) << " filter found !";
34 parseFilters(e);
35 }
36 }
37}
38
39FilterImporterGmail::~FilterImporterGmail() = default;
40
41QString FilterImporterGmail::defaultFiltersSettingsPath()
42{
43 return QDir::homePath();
44}
45
46QString FilterImporterGmail::createUniqFilterName()
47{
48 return i18n("Gmail filter %1", ++mFilterCount);
49}
50
51void FilterImporterGmail::parseFilters(const QDomElement &e)
52{
53 auto filter = new MailCommon::MailFilter();
54 filter->setAutoNaming(true);
55 const QString uniqName = createUniqFilterName();
56 filter->pattern()->setName(uniqName);
57 filter->setToolbarName(uniqName);
58 filter->setEnabled(true);
59 QByteArray fieldName;
60 for (QDomElement ruleFilter = e.firstChildElement(); !ruleFilter.isNull(); ruleFilter = ruleFilter.nextSiblingElement()) {
61 const QString tagName = ruleFilter.tagName();
62 if (tagName == QLatin1StringView("category")) {
63 if (ruleFilter.hasAttribute(QStringLiteral("term"))) {
64 if (ruleFilter.attribute(QStringLiteral("term")) != QLatin1StringView("filter")) {
65 continue;
66 }
67 }
68 } else if (tagName == QLatin1StringView("apps:property")) {
69 if (ruleFilter.hasAttribute(QStringLiteral("name"))) {
70 const QString criteriaProperty = ruleFilter.attribute(QStringLiteral("name"));
71 qCDebug(MAILCOMMON_LOG) << " ruleFilter.attribute" << criteriaProperty;
72 // Criterial
73 if (criteriaProperty == QLatin1StringView("from")) {
74 fieldName = "from";
75 } else if (criteriaProperty == QLatin1StringView("to")) {
76 fieldName = "to";
77 } else if (criteriaProperty == QLatin1StringView("subject")) {
78 fieldName = "subject";
79 } else if (criteriaProperty == QLatin1StringView("hasTheWord")) {
80 } else if (criteriaProperty == QLatin1StringView("doesNotHaveTheWord")) {
81 } else if (criteriaProperty == QLatin1StringView("hasAttachment")) {
82 fieldName = "<size>";
83 }
84 // Action
85 else if (criteriaProperty == QLatin1StringView("shouldArchive")) {
86 } else if (criteriaProperty == QLatin1StringView("shouldMarkAsRead")) {
87 } else if (criteriaProperty == QLatin1StringView("shouldStar")) {
88 } else if (criteriaProperty == QLatin1StringView("label")) {
89 } else if (criteriaProperty == QLatin1StringView("forwardTo")) {
90 } else if (criteriaProperty == QLatin1StringView("shouldTrash")) {
91 } else if (criteriaProperty == QLatin1StringView("neverSpam")) {
92 } else {
93 qCDebug(MAILCOMMON_LOG) << " unknown item " << criteriaProperty;
94 }
95 }
96 }
97 }
98 appendFilter(filter);
99}
The MailFilter class.
Definition mailfilter.h:29
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
QString homePath()
QDomElement documentElement() const const
QDomElement firstChildElement(const QString &tagName, const QString &namespaceURI) const const
bool isNull() 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 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.