35 QMutex IP2C::instanceMutex;
36 IP2C *IP2C::staticInstance =
nullptr;
38 IP2C *IP2C::instance()
40 if (staticInstance ==
nullptr)
42 QMutexLocker locker(&instanceMutex);
43 if (staticInstance ==
nullptr)
44 staticInstance =
new IP2C();
46 return staticInstance;
49 void IP2C::deinstantiate()
51 if (staticInstance !=
nullptr)
53 delete staticInstance;
54 staticInstance =
nullptr;
59 : flagLan(
":flags/lan-small"), flagLocalhost(
":flags/localhost-small"),
60 flagUnknown(
":flags/unknown-small")
70 QMutexLocker dataAccessMutexLocker(&dataAccessMutex);
72 if (database.isEmpty() || entry.ipStart > database.back().ipStart)
74 else if (entry.ipStart < database.first().ipStart)
75 database.insert(0, entry);
78 QList<IP2CData>::iterator it;
79 for (it = database.begin(); it != database.end(); ++it)
81 if (entry.ipStart < it->ipStart)
83 database.insert(it, entry);
90 const QPixmap &IP2C::flag(
const QString &countryShortName)
92 if (flags.contains(countryShortName))
93 return flags[countryShortName];
95 QString resName =
":flags/" + countryShortName;
96 QResource res(resName);
100 gLog << tr(
"No flag for country: %1").arg(countryShortName);
101 flags[countryShortName] = flagUnknown;
105 flags[countryShortName] = QPixmap(resName);
106 return flags[countryShortName];
109 bool IP2C::hasData()
const 111 return !database.empty();
116 QMutexLocker dataAccessMutexLocker(&dataAccessMutex);
118 if (database.empty())
121 unsigned int upper = database.size() - 1;
122 unsigned int lower = 0;
123 unsigned int index = database.size() / 2;
124 unsigned int lastIndex = 0xFFFFFFFF;
125 while (index != lastIndex)
129 if (ipaddress < database[index].ipStart)
132 index -= (index - lower) >> 1;
135 else if (ipaddress > database[index].ipEnd)
138 index += (upper - index) >> 1;
141 return database[index];
149 if (isLocalhostAddress(ipaddress))
152 if (isLANAddress(ipaddress))
158 const IP2CData &data = lookupIP(ipaddress);
160 if (!data.isCountryKnown())
166 const QPixmap *pFlag = &flag(data.country);
170 bool IP2C::IP2CData::isCountryKnown()
const 172 return !country.isEmpty() && country !=
"ZZZ";
IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress)
void appendEntryToDatabase(const IP2CData &entry)
Adds new country entry to the database.
const IP2CData & lookupIP(unsigned int ipaddress)
IP to Country database handler.
Flag and name of the country.