25 #include "configuration/doomseekerconfig.h"
26 #include "serverapi/server.h"
29 #include <QDirIterator>
39 QMutex IP2C::instanceMutex;
40 IP2C *IP2C::staticInstance =
nullptr;
42 static QHash<QString, QPixmap> cacheFlags()
44 QHash<QString, QPixmap> flags;
45 QDirIterator it(
":flags/country/");
48 QString resName = it.next();
49 QFileInfo resPath(resName);
50 flags[resPath.fileName()] = QPixmap(resName);
55 IP2C *IP2C::instance()
57 if (staticInstance ==
nullptr)
59 QMutexLocker locker(&instanceMutex);
60 if (staticInstance ==
nullptr)
61 staticInstance =
new IP2C();
63 return staticInstance;
66 void IP2C::deinstantiate()
68 if (staticInstance !=
nullptr)
70 delete staticInstance;
71 staticInstance =
nullptr;
76 : flagLan(
":flags/lan-small"), flagLocalhost(
":flags/localhost-small"),
77 flagUnknown(
":flags/unknown-small"), countries(
IP2CCountry::all()),
80 for (
auto it = countries.begin(); it != countries.end(); ++it)
82 const QString &code = it.key();
83 auto flagIt = flags.find(code);
84 if (flagIt != flags.end())
85 it->flag = &flagIt.value();
93 const QPixmap &IP2C::flag(
const QString &countryCode)
95 auto it = flags.find(countryCode);
96 if (it != flags.end())
102 logUnknownFlag(countryCode);
107 bool IP2C::hasData()
const
109 return !database.empty();
114 QString code = countryCode.toUpper().trimmed();
115 if (code.isEmpty() || code ==
"XIP")
117 else if (code ==
"XUN")
123 const IP2CRange &IP2C::lookupIP(
unsigned int ipaddress)
125 if (database.empty())
128 unsigned int upper = database.size() - 1;
129 unsigned int lower = 0;
130 unsigned int index = database.size() / 2;
131 unsigned int lastIndex = 0xFFFFFFFF;
132 while (index != lastIndex)
136 if (ipaddress < database[index].ipStart)
139 index -= (index - lower) >> 1;
142 else if (ipaddress > database[index].ipEnd)
145 index += (upper - index) >> 1;
148 return database[index];
156 auto it = countries.find(countryCode);
157 if (it != countries.end())
159 if (it->flag ==
nullptr)
160 logUnknownFlag(countryCode);
165 logUnknownCountry(countryCode);
166 return unknownCountry();
172 if (isLocalhostAddress(ipaddress))
173 return IP2CCountry(&flagLocalhost, tr(
"Localhost"));
175 if (isLANAddress(ipaddress))
181 const IP2CRange &addressRange = lookupIP(ipaddress);
185 if (addressRange.country.isEmpty())
186 return unknownCountry();
193 if (gConfig.doomseeker.bHonorServerCountries)
198 return unknownCountry();
206 if (!isDataAccessLocked())
208 return countryInfoForAddress(server.
address());
214 void IP2C::logUnknownCountry(
const QString &countryCode)
216 if (!unknownCountries.contains(countryCode))
218 unknownCountries.insert(countryCode);
219 gLog << tr(
"Unknown country: %1").arg(countryCode);
223 void IP2C::logUnknownFlag(
const QString &countryCode)
225 if (!unknownFlags.contains(countryCode))
227 unknownFlags.insert(countryCode);
228 gLog << tr(
"No flag for country: %1").arg(countryCode);