filtersavedmanager.cpp 2.66 KB
Newer Older
1
2
3
4
5
6
7
/*
  SPDX-FileCopyrightText: 2021 Laurent Montel <montel@kde.org>

  SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "filtersavedmanager.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "filter.h"
9
#include <KConfigGroup>
Laurent Montel's avatar
Laurent Montel committed
10
#include <KSharedConfig>
11
#include <QMenu>
12
13
14
15
16
17
18
19
20
using namespace MessageList::Core;
FilterSavedManager::FilterSavedManager(QObject *parent)
    : QObject(parent)
{
}

FilterSavedManager::~FilterSavedManager()
{
}
Laurent Montel's avatar
Laurent Montel committed
21

Laurent Montel's avatar
Laurent Montel committed
22
23
24
25
26
27
FilterSavedManager *FilterSavedManager::self()
{
    static FilterSavedManager s_self;
    return &s_self;
}

Laurent Montel's avatar
Laurent Montel committed
28
29
30
31
void FilterSavedManager::saveFilter(MessageList::Core::Filter *filter, const QString &filtername)
{
    filter->save(KSharedConfig::openConfig(), filtername);
}
32
33
34

void FilterSavedManager::loadMenu(QMenu *menu)
{
35
36
37
38
39
40
41
42
43
44
    KConfigGroup grp(KSharedConfig::openConfig(), "General");
    const int numberFilter = grp.readEntry("NumberFilter").toInt();
    for (int i = 0; i < numberFilter; ++i) {
        KConfigGroup newGroup(KSharedConfig::openConfig(), QStringLiteral("Filter_%1").arg(i));
        auto act = menu->addAction(newGroup.readEntry(QStringLiteral("name")));
        const QString identifier = newGroup.readEntry(QStringLiteral("identifier"));
        connect(act, &QAction::triggered, this, [this, identifier]() {
            Q_EMIT activateFilter(identifier);
        });
    }
45
}
46
47
48

QVector<FilterSavedManager::FilterInfo> FilterSavedManager::filterInfos() const
{
Laurent Montel's avatar
Laurent Montel committed
49
50
51
52
53
54
55
56
57
58
59
60
61
    KConfigGroup grp(KSharedConfig::openConfig(), "General");
    const int numberFilter = grp.readEntry("NumberFilter").toInt();
    QVector<FilterSavedManager::FilterInfo> lst;
    lst.reserve(numberFilter);
    for (int i = 0; i < numberFilter; ++i) {
        KConfigGroup newGroup(KSharedConfig::openConfig(), QStringLiteral("Filter_%1").arg(i));

        FilterSavedManager::FilterInfo info;
        info.filterName = newGroup.readEntry(QStringLiteral("name"));
        info.identifier = newGroup.readEntry(QStringLiteral("identifier"));
        lst << info;
    }
    return lst;
62
}
Laurent Montel's avatar
Laurent Montel committed
63
64
65

void FilterSavedManager::removeFilter(const QString &identifier)
{
Laurent Montel's avatar
Laurent Montel committed
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    KConfigGroup grp(KSharedConfig::openConfig(), "General");
    const int numberFilter = grp.readEntry("NumberFilter").toInt();
    QVector<Filter *> lst;
    lst.reserve(numberFilter);
    for (int i = 0; i < numberFilter; ++i) {
        Filter *f = Filter::load(KSharedConfig::openConfig(), i);
        lst << f;
    }

    int numberOfFilter = 0;
    for (Filter *f : qAsConst(lst)) {
#if 0
        if (f->identifier != identifier) {
            f->save(KSharedConfig::openConfig(), f->name());
            numberOfFilter++;
        }
#endif
    }
    // TODO save number of filter
    qDeleteAll(lst);
    lst.clear();
Laurent Montel's avatar
Laurent Montel committed
87
    // TODO load filter + remove specific filter + delete it
Laurent Montel's avatar
Laurent Montel committed
88
}