Forwardport from 2.2.3: Fix autosort happening when it shouldn't
[amule.git] / src / IP2Country.cpp
blobd1cba8bf9fb64c27c69ec76519c37054aab5373d
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2004-2008 Marcelo Roberto Jimenez ( phoenix@amule.org )
5 // Copyright (c) 2006-2008 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.
20 //
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
31 //
32 // These icons are public domain, and as such are free for any use (attribution appreciated but not required).
33 //
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)
39 //
40 // Contact: mjames@gmail.com
43 #ifdef HAVE_CONFIG_H
44 # include "config.h" // Needed for ENABLE_IP2COUNTRY
45 #endif
47 #ifdef ENABLE_IP2COUNTRY
49 #include "Preferences.h" // For thePrefs
50 #include "CFile.h" // For CPath
51 #include "HTTPDownload.h"
52 #include "Logger.h" // For AddLogLineM()
53 #include <common/Format.h> // For CFormat()
54 #include "common/FileFunctions.h" // For UnpackArchive
55 #include <common/StringFunctions.h> // For unicode2char()
56 #include "pixmaps/flags_xpm/CountryFlags.h"
58 #include <wx/intl.h>
59 #include <wx/image.h>
61 #ifdef _MSC_VER
62 // Block unnecessary includes from GeoIP.h
63 #define _WINDOWS_
64 #define _WINSOCK2API_
65 #define _WS2TCPIP_H_
66 #endif
68 #include <GeoIP.h>
69 #include "IP2Country.h"
71 CIP2Country::CIP2Country(const wxString& configDir)
73 m_geoip = NULL;
74 m_DataBaseName = wxT("GeoIP.dat");
75 m_DataBasePath = configDir + m_DataBaseName;
76 if (m_CountryDataMap.empty()) {
77 // this must go to enable - when all usages of GetCountryData() (ClientListCtrl, DownloadListCtrl) are surrounded with an IsEnabled()
78 // right now, flags are loaded even when it's disabled
79 LoadFlags();
83 void CIP2Country::Enable()
85 Disable();
86 if (!CPath::FileExists(m_DataBasePath)) {
87 Update();
88 return;
91 m_geoip = GeoIP_open(unicode2char(m_DataBasePath), GEOIP_STANDARD);
94 void CIP2Country::Update()
96 AddLogLineM(false,CFormat(_("Download new GeoIP.dat from %s")) % thePrefs::GetGeoIPUpdateUrl());
97 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(thePrefs::GetGeoIPUpdateUrl(), m_DataBasePath + wxT(".download"), m_DataBasePath, HTTP_GeoIP);
98 downloader->Create();
99 downloader->Run();
102 void CIP2Country::Disable()
104 if (m_geoip) {
105 GeoIP_delete(m_geoip);
106 m_geoip = NULL;
110 void CIP2Country::DownloadFinished(uint32 result)
112 if (result == HTTP_Success) {
113 Disable();
114 // download succeeded. Switch over to new database.
115 wxString newDat = m_DataBasePath + wxT(".download");
117 // Try to unpack the file, might be an archive
118 const wxChar* geoip_files[] = {
119 m_DataBaseName,
120 NULL
123 if (UnpackArchive(CPath(newDat), geoip_files).second == EFT_Error) {
124 AddLogLineC(_("Download of GeoIP.dat file failed, aborting update."));
125 return;
128 if (wxFileExists(m_DataBasePath)) {
129 if (!wxRemoveFile(m_DataBasePath)) {
130 AddLogLineC(CFormat(_("Failed to remove %s file, aborting update.")) % m_DataBaseName);
131 return;
135 if (!wxRenameFile(newDat, m_DataBasePath)) {
136 AddLogLineC(CFormat(_("Failed to rename %s file, aborting update.")) % m_DataBaseName);
137 return;
140 Enable();
141 if (m_geoip) {
142 AddLogLineN(CFormat(_("Successfully updated %s")) % m_DataBaseName);
143 } else {
144 AddLogLineC(_("Error updating GeoIP.dat"));
146 } else if (result == HTTP_Skipped) {
147 AddLogLineN(CFormat(_("Skipped download of %s, because requested file is not newer.")) % m_DataBaseName);
148 } else {
149 AddLogLineC(CFormat(_("Failed to download %s from %s")) % m_DataBaseName % thePrefs::GetGeoIPUpdateUrl());
150 // if it failed and there is no database, turn it off
151 if (!wxFileExists(m_DataBasePath)) {
152 thePrefs::SetGeoIPEnabled(false);
157 void CIP2Country::LoadFlags()
159 // Load data from xpm files
160 for (int i = 0; i < flags::FLAGS_XPM_SIZE; ++i) {
161 CountryData countrydata;
162 countrydata.Name = char2unicode(flags::flagXPMCodeVector[i].code);
163 countrydata.Flag = wxImage(flags::flagXPMCodeVector[i].xpm);
165 if (countrydata.Flag.IsOk()) {
166 m_CountryDataMap[countrydata.Name] = countrydata;
167 } else {
168 AddLogLineM(true, CFormat(_("Failed to load country data for '%s'.")) % countrydata.Name);
169 continue;
173 AddDebugLogLineN(logGeneral, CFormat(wxT("Loaded %d flag bitmaps.")) % m_CountryDataMap.size()); // there's never just one - no plural needed
177 CIP2Country::~CIP2Country()
179 Disable();
183 const CountryData& CIP2Country::GetCountryData(const wxString &ip)
185 // Should prevent the crash if the GeoIP database does not exists
186 if (m_geoip == NULL) {
187 CountryDataMap::iterator it = m_CountryDataMap.find(wxString(wxT("unknown")));
188 it->second.Name = wxT("?");
189 return it->second;
192 const wxString CCode = wxString(char2unicode(GeoIP_country_code_by_addr(m_geoip, unicode2char(ip)))).MakeLower();
194 CountryDataMap::iterator it = m_CountryDataMap.find(CCode);
195 if (it == m_CountryDataMap.end()) {
196 // Show the code and ?? flag
197 it = m_CountryDataMap.find(wxString(wxT("unknown")));
198 wxASSERT(it != m_CountryDataMap.end());
199 if (CCode.IsEmpty()) {
200 it->second.Name = wxT("?");
201 } else{
202 it->second.Name = CCode;
206 return it->second;
209 #endif // ENABLE_IP2COUNTRY