Messagelib

filtersavedmanager.cpp
1 /*
2  SPDX-FileCopyrightText: 2021 Laurent Montel <[email protected]>
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>
13 using namespace MessageList::Core;
14 FilterSavedManager::FilterSavedManager(QObject *parent)
15  : QObject(parent)
16 {
17 }
18 
19 FilterSavedManager::~FilterSavedManager() = default;
20 
21 FilterSavedManager *FilterSavedManager::self()
22 {
23  static FilterSavedManager s_self;
24  return &s_self;
25 }
26 
27 QStringList FilterSavedManager::existingFilterNames() const
28 {
29  QStringList lst;
30  KConfigGroup grp(KSharedConfig::openConfig(), "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 
40 void FilterSavedManager::saveFilter(MessageList::Core::Filter *filter, const QString &filtername, const QString &iconName)
41 {
42  filter->save(KSharedConfig::openConfig(), filtername, iconName);
43 }
44 
45 void FilterSavedManager::loadMenu(QMenu *menu)
46 {
47  KConfigGroup grp(KSharedConfig::openConfig(), "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 
60 QVector<FilterSavedManager::FilterInfo> FilterSavedManager::filterInfos() const
61 {
62  KConfigGroup grp(KSharedConfig::openConfig(), "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 
78 Filter *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 
91 void FilterSavedManager::removeFilter(const QString &identifier)
92 {
93  KConfigGroup grp(KSharedConfig::openConfig(), "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 
120 void FilterSavedManager::updateFilter(const QString &identifier, const QString &newName, const QString &iconName)
121 {
122  KConfigGroup grp(KSharedConfig::openConfig(), "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 }
void triggered(bool checked)
void reserve(int alloc)
The implementation independent part of the MessageList library.
Definition: aggregation.h:21
QAction * addAction(const QString &text)
void clear()
bool isEmpty() const const
void reserve(int size)
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:32
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Dec 6 2021 23:04:57 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.