Fixed compilation with wx 3.0 STL build
[amule.git] / src / IP2Country.cpp
blob24563daabd7a3c2c71d43a5d5d253f9cd5ae2d58
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2011 Marcelo Roberto Jimenez ( phoenix@amule.org )
5 // Copyright (c) 2006-2011 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 // Country flags are from FAMFAMFAM (http://www.famfamfam.com)
30 // Flag icons - http://www.famfamfam.com
32 // These icons are public domain, and as such are free for any use (attribution appreciated but not required).
34 // Note that these flags are named using the ISO3166-1 alpha-2 country codes where appropriate.
35 // A list of codes can be found at http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2
37 // If you find these icons useful, please donate via paypal to mjames@gmail.com
38 // (or click the donate button available at http://www.famfamfam.com/lab/icons/silk)
40 // Contact: mjames@gmail.com
43 #ifdef HAVE_CONFIG_H
44 # include "config.h" // Needed for ENABLE_IP2COUNTRY
45 #endif
47 #ifdef _MSC_VER
48 // For MSVC we need to check here if geoip is available at all. MinGW needs the include below however.
50 // Block unnecessary includes from GeoIP.h
51 #define _WINDOWS_
52 #define _WINSOCK2API_
53 #define _WS2TCPIP_H_
54 #define _WSPIAPI_H_
55 #include <GeoIP.h>
56 #endif
58 #ifdef ENABLE_IP2COUNTRY
60 #include "Preferences.h" // For thePrefs
61 #include "CFile.h" // For CPath
62 #include "HTTPDownload.h"
63 #include "Logger.h" // For AddLogLineM()
64 #include <common/Format.h> // For CFormat()
65 #include "common/FileFunctions.h" // For UnpackArchive
66 #include <common/StringFunctions.h> // For unicode2char()
67 #include "pixmaps/flags_xpm/CountryFlags.h"
69 #include <wx/intl.h>
70 #include <wx/image.h>
72 #include <GeoIP.h>
73 #include "IP2Country.h"
75 CIP2Country::CIP2Country(const wxString& configDir)
77 m_geoip = NULL;
78 m_DataBaseName = wxT("GeoIP.dat");
79 m_DataBasePath = configDir + m_DataBaseName;
82 void CIP2Country::Enable()
84 Disable();
86 if (m_CountryDataMap.empty()) {
87 LoadFlags();
90 if (!CPath::FileExists(m_DataBasePath)) {
91 Update();
92 return;
95 m_geoip = GeoIP_open(unicode2char(m_DataBasePath), GEOIP_STANDARD);
98 void CIP2Country::Update()
100 AddLogLineN(CFormat(_("Download new GeoIP.dat from %s")) % thePrefs::GetGeoIPUpdateUrl());
101 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(thePrefs::GetGeoIPUpdateUrl(), m_DataBasePath + wxT(".download"), m_DataBasePath, HTTP_GeoIP, true, true);
102 downloader->Create();
103 downloader->Run();
106 void CIP2Country::Disable()
108 if (m_geoip) {
109 GeoIP_delete(m_geoip);
110 m_geoip = NULL;
114 void CIP2Country::DownloadFinished(uint32 result)
116 if (result == HTTP_Success) {
117 Disable();
118 // download succeeded. Switch over to new database.
119 wxString newDat = m_DataBasePath + wxT(".download");
121 // Try to unpack the file, might be an archive
122 wxWCharBuffer dataBaseName = m_DataBaseName.wc_str();
123 const wxChar* geoip_files[] = {
124 dataBaseName,
125 NULL
128 if (UnpackArchive(CPath(newDat), geoip_files).second == EFT_Error) {
129 AddLogLineC(_("Download of GeoIP.dat file failed, aborting update."));
130 return;
133 if (wxFileExists(m_DataBasePath)) {
134 if (!wxRemoveFile(m_DataBasePath)) {
135 AddLogLineC(CFormat(_("Failed to remove %s file, aborting update.")) % m_DataBaseName);
136 return;
140 if (!wxRenameFile(newDat, m_DataBasePath)) {
141 AddLogLineC(CFormat(_("Failed to rename %s file, aborting update.")) % m_DataBaseName);
142 return;
145 Enable();
146 if (m_geoip) {
147 AddLogLineN(CFormat(_("Successfully updated %s")) % m_DataBaseName);
148 } else {
149 AddLogLineC(_("Error updating GeoIP.dat"));
151 } else if (result == HTTP_Skipped) {
152 AddLogLineN(CFormat(_("Skipped download of %s, because requested file is not newer.")) % m_DataBaseName);
153 } else {
154 AddLogLineC(CFormat(_("Failed to download %s from %s")) % m_DataBaseName % thePrefs::GetGeoIPUpdateUrl());
155 // if it failed and there is no database, turn it off
156 if (!wxFileExists(m_DataBasePath)) {
157 thePrefs::SetGeoIPEnabled(false);
162 void CIP2Country::LoadFlags()
164 // Load data from xpm files
165 for (int i = 0; i < flags::FLAGS_XPM_SIZE; ++i) {
166 CountryData countrydata;
167 countrydata.Name = wxString(flags::flagXPMCodeVector[i].code, wxConvISO8859_1);
168 countrydata.Flag = wxImage(flags::flagXPMCodeVector[i].xpm);
170 if (countrydata.Flag.IsOk()) {
171 m_CountryDataMap[countrydata.Name] = countrydata;
172 } else {
173 AddLogLineC(CFormat(_("Failed to load country data for '%s'.")) % countrydata.Name);
174 continue;
178 AddDebugLogLineN(logGeneral, CFormat(wxT("Loaded %d flag bitmaps.")) % m_CountryDataMap.size()); // there's never just one - no plural needed
182 CIP2Country::~CIP2Country()
184 Disable();
188 const CountryData& CIP2Country::GetCountryData(const wxString &ip)
190 // Should prevent the crash if the GeoIP database does not exists
191 if (m_geoip == NULL) {
192 CountryDataMap::iterator it = m_CountryDataMap.find(wxString(wxT("unknown")));
193 it->second.Name = wxT("?");
194 return it->second;
197 // wxString::MakeLower() fails miserably in Turkish localization with their dotted/non-dotted 'i's
198 // So fall back to some good ole C string processing.
199 std::string strCode;
200 const char * c = GeoIP_country_code_by_addr(m_geoip, unicode2char(ip));
201 if (!c) {
202 c = "unknown";
204 for ( ; *c; c++) {
205 strCode += ((*c >= 'A' && *c <= 'Z') ? *c + 'a' - 'A' : *c);
208 const wxString CCode(strCode.c_str(), wxConvISO8859_1);
210 CountryDataMap::iterator it = m_CountryDataMap.find(CCode);
211 if (it == m_CountryDataMap.end()) {
212 // Show the code and ?? flag
213 it = m_CountryDataMap.find(wxString(wxT("unknown")));
214 wxASSERT(it != m_CountryDataMap.end());
215 if (CCode.IsEmpty()) {
216 it->second.Name = wxT("?");
217 } else{
218 it->second.Name = CCode;
222 return it->second;
225 #else
227 #include "IP2Country.h"
229 CIP2Country::CIP2Country(const wxString&)
231 m_geoip = NULL;
234 CIP2Country::~CIP2Country() {}
235 void CIP2Country::Enable() {}
236 void CIP2Country::DownloadFinished(uint32) {}
238 const CountryData& CIP2Country::GetCountryData(const wxString &)
240 static CountryData dummy;
241 return dummy;
244 #endif // ENABLE_IP2COUNTRY