1 /*
2   SPDX-FileCopyrightText: 2015-2021 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 
14 using namespace MailCommon;
15 
FilterImporterGmail(QFile * file)16 FilterImporterGmail::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 == QLatin1String("entry")) {
33             qCDebug(MAILCOMMON_LOG) << " filter found !";
34             parseFilters(e);
35         }
36     }
37 }
38 
~FilterImporterGmail()39 FilterImporterGmail::~FilterImporterGmail()
40 {
41 }
42 
defaultFiltersSettingsPath()43 QString FilterImporterGmail::defaultFiltersSettingsPath()
44 {
45     return QDir::homePath();
46 }
47 
createUniqFilterName()48 QString FilterImporterGmail::createUniqFilterName()
49 {
50     return i18n("Gmail filter %1", ++mFilterCount);
51 }
52 
parseFilters(const QDomElement & e)53 void FilterImporterGmail::parseFilters(const QDomElement &e)
54 {
55     auto filter = new MailCommon::MailFilter();
56     filter->setAutoNaming(true);
57     const QString uniqName = createUniqFilterName();
58     filter->pattern()->setName(uniqName);
59     filter->setToolbarName(uniqName);
60     filter->setEnabled(true);
61     QByteArray fieldName;
62     for (QDomElement ruleFilter = e.firstChildElement(); !ruleFilter.isNull(); ruleFilter = ruleFilter.nextSiblingElement()) {
63         const QString tagName = ruleFilter.tagName();
64         if (tagName == QLatin1String("category")) {
65             if (ruleFilter.hasAttribute(QStringLiteral("term"))) {
66                 if (ruleFilter.attribute(QStringLiteral("term")) != QLatin1String("filter")) {
67                     continue;
68                 }
69             }
70         } else if (tagName == QLatin1String("apps:property")) {
71             if (ruleFilter.hasAttribute(QStringLiteral("name"))) {
72                 const QString criteriaProperty = ruleFilter.attribute(QStringLiteral("name"));
73                 qCDebug(MAILCOMMON_LOG) << " ruleFilter.attribute" << criteriaProperty;
74                 // Criterial
75                 if (criteriaProperty == QLatin1String("from")) {
76                     fieldName = "from";
77                 } else if (criteriaProperty == QLatin1String("to")) {
78                     fieldName = "to";
79                 } else if (criteriaProperty == QLatin1String("subject")) {
80                     fieldName = "subject";
81                 } else if (criteriaProperty == QLatin1String("hasTheWord")) {
82                 } else if (criteriaProperty == QLatin1String("doesNotHaveTheWord")) {
83                 } else if (criteriaProperty == QLatin1String("hasAttachment")) {
84                     fieldName = "<size>";
85                 }
86                 // Action
87                 else if (criteriaProperty == QLatin1String("shouldArchive")) {
88                 } else if (criteriaProperty == QLatin1String("shouldMarkAsRead")) {
89                 } else if (criteriaProperty == QLatin1String("shouldStar")) {
90                 } else if (criteriaProperty == QLatin1String("label")) {
91                 } else if (criteriaProperty == QLatin1String("forwardTo")) {
92                 } else if (criteriaProperty == QLatin1String("shouldTrash")) {
93                 } else if (criteriaProperty == QLatin1String("neverSpam")) {
94                 } else {
95                     qCDebug(MAILCOMMON_LOG) << " unknown item " << criteriaProperty;
96                 }
97             }
98         }
99     }
100     appendFilter(filter);
101 }
102