serverlistfilterinfo.cpp
1 //------------------------------------------------------------------------------
2 // serverlistfilterinfo.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) 2011 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "serverlistfilterinfo.h"
24 
25 ServerListFilterInfo::ServerListFilterInfo()
26 {
27  bEnabled = true;
28  bShowEmpty = true;
29  bShowFull = true;
30  bShowOnlyValid = false;
31  bShowBannedServers = true;
32  bShowTooSoonServers = true;
33  bShowNotRespondingServers = true;
34  lockedServers = Doomseeker::Indifferent;
35  maxPing = 0;
36  testingServers = Doomseeker::Indifferent;
38 }
39 
40 void ServerListFilterInfo::copy(const ServerListFilterInfo &other)
41 {
42  name = other.name;
43  bEnabled = other.bEnabled;
44  bShowEmpty = other.bShowEmpty;
45  bShowFull = other.bShowFull;
46  bShowOnlyValid = other.bShowOnlyValid;
47  bShowBannedServers = other.bShowBannedServers;
48  bShowTooSoonServers = other.bShowTooSoonServers;
49  bShowNotRespondingServers = other.bShowNotRespondingServers;
50  addresses = other.addresses;
51  gameModes = other.gameModes;
52  gameModesExcluded = other.gameModesExcluded;
53  lockedServers = other.lockedServers;
54  maxPing = other.maxPing;
55  serverName = other.serverName.trimmed();
56  testingServers = other.testingServers;
58 
59  copyTrimmed(this->wads, other.wads);
60  copyTrimmed(this->wadsExcluded, other.wadsExcluded);
61 }
62 
63 void ServerListFilterInfo::copyTrimmed(QStringList &target, const QStringList &source) const
64 {
65  target.clear();
66  for (QString element : source)
67  {
68  element = element.trimmed();
69  if (!element.isEmpty())
70  target << element;
71  }
72 }
73 
75 {
76  if (!serverName.isEmpty())
77  return true;
78 
79  if (!bEnabled)
80  return false;
81 
82  if (!bShowEmpty || !bShowFull)
83  return true;
84 
85  if (bShowOnlyValid
86  || !bShowBannedServers
87  || !bShowTooSoonServers
88  || !bShowNotRespondingServers)
89  {
90  return true;
91  }
92 
93  if (maxPing > 0)
94  return true;
95 
96  if (!gameModes.isEmpty()
97  || !gameModesExcluded.isEmpty()
98  || !wads.isEmpty()
99  || !wadsExcluded.isEmpty()
100  || !addresses.isEmpty())
101  {
102  return true;
103  }
104 
105  if (lockedServers != Doomseeker::Indifferent
106  || testingServers != Doomseeker::Indifferent)
107  {
108  return true;
109  }
110 
111  return false;
112 }
113 
115 {
116  return
117  bEnabled == other.bEnabled &&
118  bShowEmpty == other.bShowEmpty &&
119  bShowFull == other.bShowFull &&
120  bShowOnlyValid == other.bShowOnlyValid &&
121  bShowBannedServers == other.bShowBannedServers &&
122  bShowTooSoonServers == other.bShowTooSoonServers &&
123  bShowNotRespondingServers == other.bShowNotRespondingServers &&
124  addresses == other.addresses &&
125  gameModes == other.gameModes &&
126  gameModesExcluded == other.gameModesExcluded &&
127  lockedServers == other.lockedServers &&
128  maxPing == other.maxPing &&
129  serverName == other.serverName &&
130  wads == other.wads &&
131  wadsExcluded == other.wadsExcluded &&
132  testingServers == other.testingServers &&
134  ;
135 }
136 
137 
138 ServerListFilterInfo ServerListFilterInfo::deserialize(const QVariant &variant)
139 {
141 
142  QVariantMap in = variant.toMap();
143  obj.name = in["name"].toString();
144  obj.bEnabled = in["bEnabled"].toBool();
145  obj.bShowEmpty = in["bShowEmpty"].toBool();
146  obj.bShowFull = in["bShowFull"].toBool();
147  obj.bShowOnlyValid = in["bShowOnlyValid"].toBool();
148  obj.bShowBannedServers = in["bShowBannedServers"].toBool();
149  obj.bShowTooSoonServers = in["bShowTooSoonServers"].toBool();
150  obj.bShowNotRespondingServers = in["bShowNotRespondingServers"].toBool();
151  obj.addresses = AddressFilter::deserialize(in["Addresses"]);
152  obj.gameModes = in["GameModes"].toStringList();
153  obj.gameModesExcluded = in["GameModesExcluded"].toStringList();
154  obj.lockedServers = static_cast<Doomseeker::ShowMode>(in["LockedServers"].toInt());
155  obj.maxPing = in["MaxPing"].toUInt();
156  obj.serverName = in["ServerName"].toString();
157  obj.testingServers = static_cast<Doomseeker::ShowMode>(in["TestingServers"].toInt());
158  obj.wads = in["WADs"].toStringList();
159  obj.wadsExcluded = in["WADsExcluded"].toStringList();
160  obj.bPopulatedServersOnTop = in["bPopulatedServersOnTop"].toBool();
161  return obj;
162 }
163 
164 QVariant ServerListFilterInfo::serialize() const
165 {
166  QVariantMap out;
167  out["name"] = this->name;
168  out["bEnabled"] = this->bEnabled;
169  out["bShowEmpty"] = this->bShowEmpty;
170  out["bShowFull"] = this->bShowFull;
171  out["bShowOnlyValid"] = this->bShowOnlyValid;
172  out["bShowBannedServers"] = this->bShowBannedServers;
173  out["bShowTooSoonServers"] = this->bShowTooSoonServers;
174  out["bShowNotRespondingServers"] = this->bShowNotRespondingServers;
175  out["Addresses"] = this->addresses.serialize();
176  out["GameModes"] = this->gameModes;
177  out["GameModesExcluded"] = this->gameModesExcluded;
178  out["LockedServers"] = this->lockedServers;
179  out["MaxPing"] = this->maxPing;
180  out["ServerName"] = this->serverName;
181  out["TestingServers"] = this->testingServers;
182  out["WADs"].setValue(this->wads);
183  out["WADsExcluded"].setValue(this->wadsExcluded);
184  out["bPopulatedServersOnTop"] = this->bPopulatedServersOnTop;
185  return out;
186 }
187 
188 QString ServerListFilterInfo::toString() const
189 {
190  QString ret = "";
191 
192  ret += QString("name: %1\n").arg(name);
193  ret += QString("bEnabled: ") + (bEnabled ? "Yes" : "No") + "\n";
194  ret += QString("bShowEmpty: ") + (bShowEmpty ? "Yes" : "No") + "\n";
195  ret += QString("bShowFull: ") + (bShowFull ? "Yes" : "No") + "\n";
196  ret += QString("bShowOnlyValid: ") + (bShowOnlyValid ? "Yes" : "No") + "\n";
197  ret += QString("bShowBannedServers: ") + (bShowBannedServers ? "Yes" : "No") + "\n";
198  ret += QString("bShowTooSoonServers: ") + (bShowTooSoonServers ? "Yes" : "No") + "\n";
199  ret += QString("bShowNotRespondingServers: ") + (bShowNotRespondingServers ? "Yes" : "No") + "\n";
200 
201  ret += QString("Addresses: %1\n").arg(addresses.toString());
202  ret += QString("GameModes: ") + gameModes.join(",") + "\n";
203  ret += QString("GameModes Excluded: ") + gameModesExcluded.join(",") + "\n";
204  ret += QString("LockedServers: %1\n").arg(lockedServers);
205  ret += QString("MaxPing: ") + QString::number(maxPing) + "\n";
206  ret += QString("ServerName: ") + serverName + "\n";
207  ret += QString("Testing servers: %1\n").arg(testingServers);
208  ret += QString("WADs: ") + wads.join(",") + "\n";
209  ret += QString("WADs Excluded: ") + wadsExcluded.join(",") + "\n";
210  ret += QString("bPopulatedServersOnTop: ") + (bPopulatedServersOnTop ? "Yes" : "No") + "\n";
211 
212  return ret;
213 }