Chatterino
FilterSet.hpp
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace chatterino {
7 
8 class FilterSet
9 {
10 public:
12  {
13  this->listener_ =
15  this->reloadFilters();
16  });
17  }
18 
19  FilterSet(const QList<QUuid> &filterIds)
20  {
21  auto filters = getCSettings().filterRecords.readOnly();
22  for (const auto &f : *filters)
23  {
24  if (filterIds.contains(f->getId()))
25  this->filters_.insert(f->getId(), f);
26  }
27 
28  this->listener_ =
30  this->reloadFilters();
31  });
32  }
33 
35  {
36  this->listener_.disconnect();
37  }
38 
39  bool filter(const MessagePtr &m, ChannelPtr channel) const
40  {
41  if (this->filters_.size() == 0)
42  return true;
43 
44  filterparser::ContextMap context =
45  filterparser::buildContextMap(m, channel.get());
46  for (const auto &f : this->filters_.values())
47  {
48  if (!f->valid() || !f->filter(context))
49  return false;
50  }
51 
52  return true;
53  }
54 
55  const QList<QUuid> filterIds() const
56  {
57  return this->filters_.keys();
58  }
59 
60 private:
61  QMap<QUuid, FilterRecordPtr> filters_;
62  pajlada::Signals::Connection listener_;
63 
64  void reloadFilters()
65  {
66  auto filters = getCSettings().filterRecords.readOnly();
67  for (const auto &key : this->filters_.keys())
68  {
69  bool found = false;
70  for (const auto &f : *filters)
71  {
72  if (f->getId() == key)
73  {
74  found = true;
75  this->filters_.insert(key, f);
76  }
77  }
78  if (!found)
79  {
80  this->filters_.remove(key);
81  }
82  }
83  }
84 };
85 
86 using FilterSetPtr = std::shared_ptr<FilterSet>;
87 
88 } // namespace chatterino
~FilterSet()
Definition: FilterSet.hpp:34
ContextMap buildContextMap(const MessagePtr &m, chatterino::Channel *channel)
Definition: FilterParser.cpp:10
FilterSet(const QList< QUuid > &filterIds)
Definition: FilterSet.hpp:19
Definition: FilterSet.hpp:8
Definition: Application.cpp:48
bool filter(const MessagePtr &m, ChannelPtr channel) const
Definition: FilterSet.hpp:39
pajlada::Signals::NoArgSignal delayedItemsChanged
Definition: SignalVector.hpp:26
SignalVector< FilterRecordPtr > & filterRecords
Definition: Settings.hpp:41
QMap< QString, QVariant > ContextMap
Definition: Types.hpp:10
const QList< QUuid > filterIds() const
Definition: FilterSet.hpp:55
FilterSet()
Definition: FilterSet.hpp:11
std::shared_ptr< const std::vector< T > > readOnly()
A read-only version of the vector which can be used concurrently.
Definition: SignalVector.hpp:50
std::shared_ptr< const Message > MessagePtr
Definition: Channel.hpp:18
std::shared_ptr< Channel > ChannelPtr
Definition: Channel.hpp:125
std::shared_ptr< FilterSet > FilterSetPtr
Definition: FilterSet.hpp:86
ConcurrentSettings & getCSettings()
Definition: Settings.cpp:113