addressfilter.cpp
1 //------------------------------------------------------------------------------
2 // addressfilter.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2023 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "addressfilter.h"
24 
25 bool AddressFilter::contains(const QPair<QHostAddress, int> &subnet) const
26 {
27  return subnets.contains(subnet);
28 }
29 
30 bool AddressFilter::isEmpty() const
31 {
32  return subnets.isEmpty();
33 }
34 
35 bool AddressFilter::matches(const QHostAddress &address) const
36 {
37  if (subnets.isEmpty())
38  return true;
39 
40  for (const auto &subnet : subnets)
41  {
42  if (address.isInSubnet(subnet))
43  return true;
44  }
45  return false;
46 }
47 
48 AddressFilter &AddressFilter::operator<<(const QPair<QHostAddress, int> &subnet)
49 {
50  subnets.append(subnet);
51  return *this;
52 }
53 
54 bool AddressFilter::operator==(const AddressFilter &other) const
55 {
56  return subnets == other.subnets;
57 }
58 
59 bool AddressFilter::operator!=(const AddressFilter &other) const
60 {
61  return subnets != other.subnets;
62 }
63 
64 AddressFilter AddressFilter::deserialize(const QVariant &variant)
65 {
66  AddressFilter out;
67 
68  QVariantList subnetsVariantList = variant.toList();
69  for (const QVariant &subnetVariant : subnetsVariantList)
70  {
71  QVariantMap subnetVariantMap = subnetVariant.toMap();
72  QPair<QHostAddress, int> subnet;
73  subnet.first = QHostAddress(subnetVariantMap["address"].toString());
74  subnet.second = subnetVariantMap["mask"].toInt();
75  if (!subnet.first.isNull())
76  out << subnet;
77  }
78 
79  return out;
80 }
81 
82 QVariant AddressFilter::serialize() const
83 {
84  QVariantList result;
85  for (const auto &subnet : subnets)
86  {
87  QVariantMap pair;
88  pair["address"] = subnet.first.toString();
89  pair["mask"] = subnet.second;
90  result << pair;
91  }
92  return result;
93 }
94 
95 QString AddressFilter::toString() const
96 {
97  QStringList tokens;
98  for (const auto &subnet : subnets)
99  {
100  tokens << QString("%1/%2").arg(subnet.first.toString()).arg(subnet.second);
101  }
102  return tokens.join(",");
103 }