Messagelib

filtersavedmanager.cpp
1/*
2 SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "filtersavedmanager.h"
8#include "filter.h"
9#include <KConfigGroup>
10#include <KSharedConfig>
11#include <QMenu>
12#include <QRegularExpression>
13using namespace MessageList::Core;
14FilterSavedManager::FilterSavedManager(QObject *parent)
15 : QObject(parent)
16{
17}
18
19FilterSavedManager::~FilterSavedManager() = default;
20
21FilterSavedManager *FilterSavedManager::self()
22{
23 static FilterSavedManager s_self;
24 return &s_self;
25}
26
27QStringList FilterSavedManager::existingFilterNames() const
28{
29 QStringList lst;
30 KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("General"));
31 const int numberFilter = grp.readEntry("NumberFilter").toInt();
32 lst.reserve(numberFilter);
33 for (int i = 0; i < numberFilter; ++i) {
34 KConfigGroup newGroup(KSharedConfig::openConfig(), QStringLiteral("Filter_%1").arg(i));
35 lst << newGroup.readEntry(QStringLiteral("name"));
36 }
37 return lst;
38}
39
40void FilterSavedManager::saveFilter(MessageList::Core::Filter *filter, const QString &filtername, const QString &iconName)
41{
42 filter->save(KSharedConfig::openConfig(), filtername, iconName);
43}
44
45void FilterSavedManager::loadMenu(QMenu *menu)
46{
47 KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("General"));
48 const int numberFilter = grp.readEntry("NumberFilter").toInt();
49 for (int i = 0; i < numberFilter; ++i) {
50 KConfigGroup newGroup(KSharedConfig::openConfig(), QStringLiteral("Filter_%1").arg(i));
51 const QString iconName = newGroup.readEntry(QStringLiteral("iconName"));
52 auto act = menu->addAction(QIcon::fromTheme(iconName), newGroup.readEntry(QStringLiteral("name")));
53 const QString identifier = newGroup.readEntry(QStringLiteral("identifier"));
54 connect(act, &QAction::triggered, this, [this, identifier]() {
55 Q_EMIT activateFilter(identifier);
56 });
57 }
58}
59
60QList<FilterSavedManager::FilterInfo> FilterSavedManager::filterInfos() const
61{
62 KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("General"));
63 const int numberFilter = grp.readEntry("NumberFilter").toInt();
65 lst.reserve(numberFilter);
66 for (int i = 0; i < numberFilter; ++i) {
67 KConfigGroup newGroup(KSharedConfig::openConfig(), QStringLiteral("Filter_%1").arg(i));
68
69 FilterSavedManager::FilterInfo info;
70 info.filterName = newGroup.readEntry(QStringLiteral("name"));
71 info.identifier = newGroup.readEntry(QStringLiteral("identifier"));
72 info.iconName = newGroup.readEntry(QStringLiteral("iconName"));
73 lst << info;
74 }
75 return lst;
76}
77
78Filter *FilterSavedManager::loadFilter(const QString &identifier)
79{
80 const QStringList list = KSharedConfig::openConfig()->groupList().filter(QRegularExpression(QStringLiteral("Filter_\\d+")));
81 for (const QString &group : list) {
82 KConfigGroup newGroup(KSharedConfig::openConfig(), group);
83 if (newGroup.readEntry("identifier") == identifier) {
84 Filter *f = Filter::loadFromConfigGroup(newGroup);
85 return f;
86 }
87 }
88 return {};
89}
90
91void FilterSavedManager::removeFilter(const QString &identifier)
92{
93 KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("General"));
94 const int numberFilter = grp.readEntry("NumberFilter").toInt();
96 lst.reserve(numberFilter);
97 for (int i = 0; i < numberFilter; ++i) {
98 Filter *f = Filter::load(KSharedConfig::openConfig(), i);
99 lst << f;
100 }
101
102 const QStringList list = KSharedConfig::openConfig()->groupList().filter(QRegularExpression(QStringLiteral("Filter_\\d+")));
103 for (const QString &group : list) {
104 KSharedConfig::openConfig()->deleteGroup(group);
105 }
106
107 int numberOfFilter = 0;
108 for (Filter *f : std::as_const(lst)) {
109 if ((f->identifier() != identifier) && !f->identifier().isEmpty()) {
110 f->save(KSharedConfig::openConfig(), f->filterName(), f->iconName(), numberOfFilter);
111 numberOfFilter++;
112 }
113 }
114 grp.writeEntry("NumberFilter", numberOfFilter);
115 grp.sync();
116 qDeleteAll(lst);
117 lst.clear();
118}
119
120void FilterSavedManager::updateFilter(const QString &identifier, const QString &newName, const QString &iconName)
121{
122 KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("General"));
123 const int numberFilter = grp.readEntry("NumberFilter").toInt();
124 for (int i = 0; i < numberFilter; ++i) {
125 KConfigGroup newGroup(KSharedConfig::openConfig(), QStringLiteral("Filter_%1").arg(i));
126 if (newGroup.readEntry("identifier") == identifier) {
127 newGroup.writeEntry("name", newName);
128 if (!iconName.isEmpty()) {
129 newGroup.writeEntry("iconName", iconName);
130 }
131 newGroup.sync();
132 break;
133 }
134 }
135}
136
137#include "moc_filtersavedmanager.cpp"
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
This class is responsible of matching messages that should be displayed in the View.
Definition filter.h:34
KIOCORE_EXPORT QStringList list(const QString &fileClass)
The implementation independent part of the MessageList library.
Definition aggregation.h:22
void triggered(bool checked)
QIcon fromTheme(const QString &name)
void clear()
void reserve(qsizetype size)
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QStringList filter(QStringView str, Qt::CaseSensitivity cs) 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 4 2024 16:33:26 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.