ip2c.h
1 //------------------------------------------------------------------------------
2 // ip2c.h
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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) 2009 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #ifndef __IP2C_H__
25 #define __IP2C_H__
26 
27 #include "ip2c/entities/ip2ccountryinfo.h"
28 #include "global.h"
29 
30 #include <QHash>
31 #include <QHostAddress>
32 #include <QList>
33 #include <QMutex>
34 #include <QPixmap>
35 #include <QProgressBar>
36 #include <QStatusBar>
37 #include <QString>
38 #include <QUrl>
39 
48 class IP2C : public QObject
49 {
50  Q_OBJECT
51 
52  public:
53  class IP2CData
54  {
55  public:
56  unsigned int ipStart;
57  unsigned int ipEnd;
58  QString country;
59  QString countryFullName;
60 
61  IP2CData()
62  {
63  ipStart = ipEnd = 0;
64  }
65 
66  bool isCountryKnown() const;
67 
72  bool isValid() const
73  {
74  return ipStart != ipEnd;
75  }
76  };
77 
78  static IP2C *instance();
79  static void deinstantiate();
80 
81  const QPixmap flagLan;
82  const QPixmap flagLocalhost;
83  const QPixmap flagUnknown;
84 
90  void appendEntryToDatabase(const IP2CData& entry);
91 
92  const QPixmap& flag(const QString& countryShortName);
93 
94  bool isDataAccessLocked() const
95  {
96  return bDataAccessLocked;
97  }
98 
102  const IP2CData& lookupIP(unsigned int ipaddress);
103  const IP2CData& lookupIP(const QHostAddress &ipaddress) { return lookupIP(ipaddress.toIPv4Address()); }
104 
105  int numKnownEntries() const { return database.size(); }
106 
110  IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress);
111  IP2CCountryInfo obtainCountryInfo(const QHostAddress& ipaddress) { return obtainCountryInfo(ipaddress.toIPv4Address()); }
112 
113  void setDataAccessLockEnabled(bool b) { bDataAccessLocked = b; }
114 
121  void setDatabase(const QList<IP2CData>& sortedCountryData)
122  {
123  QMutexLocker dataAccessMutexLocker(&dataAccessMutex);
124  database = sortedCountryData;
125  }
126 
127  protected:
128  inline bool isLANAddress(unsigned ipv4Address)
129  {
130  const static unsigned LAN_1_BEGIN = QHostAddress("10.0.0.0").toIPv4Address();
131  const static unsigned LAN_1_END = QHostAddress("10.255.255.255").toIPv4Address();
132  const static unsigned LAN_2_BEGIN = QHostAddress("172.16.0.0").toIPv4Address();
133  const static unsigned LAN_2_END = QHostAddress("172.31.255.255").toIPv4Address();
134  const static unsigned LAN_3_BEGIN = QHostAddress("192.168.0.0").toIPv4Address();
135  const static unsigned LAN_3_END = QHostAddress("192.168.255.255").toIPv4Address();
136 
137  return (
138  (ipv4Address >= LAN_1_BEGIN && ipv4Address <= LAN_1_END)
139  || (ipv4Address >= LAN_2_BEGIN && ipv4Address <= LAN_2_END)
140  || (ipv4Address >= LAN_3_BEGIN && ipv4Address <= LAN_3_END)
141  );
142  }
143 
144  inline bool isLocalhostAddress(unsigned ipv4Address)
145  {
146  const static unsigned LOCALHOST_BEGIN = QHostAddress("127.0.0.0").toIPv4Address();
147  const static unsigned LOCALHOST_END = QHostAddress("127.255.255.255").toIPv4Address();
148 
149  return (ipv4Address >= LOCALHOST_BEGIN && ipv4Address <= LOCALHOST_END);
150  }
151 
152  private:
153  static QMutex instanceMutex;
154  static IP2C *staticInstance;
155 
163  bool bDataAccessLocked;
164  QMutex dataAccessMutex;
165  QList<IP2CData> database;
166  QHash<QString, QPixmap> flags;
167  const IP2CData invalidData;
168 
169  IP2C();
170  ~IP2C();
171 };
172 
173 #endif /* __IP2C_H__ */
void setDatabase(const QList< IP2CData > &sortedCountryData)
Sets database contents to the list specified.
Definition: ip2c.h:121
IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress)
Definition: ip2c.cpp:152
void appendEntryToDatabase(const IP2CData &entry)
Adds new country entry to the database.
Definition: ip2c.cpp:70
const IP2CData & lookupIP(unsigned int ipaddress)
Definition: ip2c.cpp:117
IP to Country database handler.
Definition: ip2c.h:48
Flag and name of the country.
bool isValid() const
Definition: ip2c.h:72