Upstream tarball 10020
[amule.git] / src / IP2Country.cpp
blob1ecab12af7c7c6b4f80d6067809c63a487580889
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 // MSVC projects can't include files configuration dependent, so just double-check the #define
44 #ifdef ENABLE_IP2COUNTRY
46 #include "Preferences.h" // For thePrefs
47 #include "CFile.h" // For CPath
48 #include "HTTPDownload.h"
49 #include "Logger.h" // For AddLogLineM()
50 #include <common/Format.h> // For CFormat()
51 #include "common/FileFunctions.h" // For UnpackArchive
52 #include <common/StringFunctions.h> // For unicode2char()
53 #include "pixmaps/flags_xpm/CountryFlags.h"
55 #include <wx/intl.h>
56 #include <wx/image.h>
58 #ifdef _MSC_VER
59 // Block unnecessary includes from GeoIP.h
60 #define _WINDOWS_
61 #define _WINSOCK2API_
62 #define _WS2TCPIP_H_
63 #endif
65 #include <GeoIP.h>
66 #include "IP2Country.h"
68 CIP2Country::CIP2Country(const wxString& configDir)
70 m_geoip = NULL;
71 m_DataBaseName = configDir + wxT("GeoIP.dat");
72 if (m_CountryDataMap.empty()) {
73 // this must go to enable - when all usages of GetCountryData() (ClientListCtrl, DownloadListCtrl) are surrounded with an IsEnabled()
74 // right now, flags are loaded even when it's disabled
75 LoadFlags();
79 void CIP2Country::Enable()
81 Disable();
82 if (!CPath::FileExists(m_DataBaseName)) {
83 Update();
84 return;
87 m_geoip = GeoIP_open(unicode2char(m_DataBaseName), GEOIP_STANDARD);
90 void CIP2Country::Update()
92 AddLogLineM(false,CFormat(_("Download new GeoIP.dat from %s")) % thePrefs::GetGeoIPUpdateUrl());
93 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(thePrefs::GetGeoIPUpdateUrl(), m_DataBaseName + wxT(".download"), m_DataBaseName, HTTP_GeoIP);
94 downloader->Create();
95 downloader->Run();
98 void CIP2Country::Disable()
100 if (m_geoip) {
101 GeoIP_delete(m_geoip);
102 m_geoip = NULL;
106 void CIP2Country::DownloadFinished(uint32 result)
108 if (result == HTTP_Success) {
109 Disable();
110 // download succeeded. Switch over to new database.
111 wxString newDat = m_DataBaseName + wxT(".download");
113 // Try to unpack the file, might be an archive
114 const wxChar* geoip_files[] = {
115 wxT("GeoIP.dat"),
116 NULL
119 if (UnpackArchive(CPath(newDat), geoip_files).second == EFT_Error) {
120 AddLogLineC(_("Download of GeoIP.dat file failed, aborting update."));
121 return;
124 if (wxFileExists(m_DataBaseName)) {
125 if (!wxRemoveFile(m_DataBaseName)) {
126 AddLogLineC(CFormat(_("Failed to remove %s file, aborting update.")) % wxT("GeoIP.dat"));
127 return;
131 if (!wxRenameFile(newDat, m_DataBaseName)) {
132 AddLogLineC(CFormat(_("Failed to rename %s file, aborting update.")) % wxT("GeoIP.dat"));
133 return;
136 Enable();
137 if (m_geoip) {
138 AddLogLineN(CFormat(_("Successfully updated %s")) % wxT("GeoIP.dat"));
139 } else {
140 AddLogLineC(_("Error updating GeoIP.dat"));
142 } else if (result == HTTP_Skipped) {
143 AddLogLineN(CFormat(_("Skipped download of %s, because requested file is not newer.")) % wxT("GeoIP.dat"));
144 } else {
145 AddLogLineC(CFormat(_("Failed to download %s from %s")) % wxT("GeoIP.dat") % thePrefs::GetGeoIPUpdateUrl());
146 // if it failed and there is no database, turn it off
147 if (!wxFileExists(m_DataBaseName)) {
148 thePrefs::SetGeoIPEnabled(false);
153 void CIP2Country::LoadFlags()
155 // Load data from xpm files
156 for (int i = 0; i < flags::FLAGS_XPM_SIZE; ++i) {
157 CountryData countrydata;
158 countrydata.Name = char2unicode(flags::flagXPMCodeVector[i].code);
159 countrydata.Flag = wxImage(flags::flagXPMCodeVector[i].xpm);
161 if (countrydata.Flag.IsOk()) {
162 m_CountryDataMap[countrydata.Name] = countrydata;
163 } else {
164 AddLogLineM(true, CFormat(_("Failed to load country data for '%s'.")) % countrydata.Name);
165 continue;
169 AddLogLineM(false, CFormat(_("Loaded %d flag bitmaps.")) % m_CountryDataMap.size()); // there's never just one - no plural needed
173 CIP2Country::~CIP2Country()
175 Disable();
179 const CountryData& CIP2Country::GetCountryData(const wxString &ip)
181 // Should prevent the crash if the GeoIP database does not exists
182 if (m_geoip == NULL) {
183 CountryDataMap::iterator it = m_CountryDataMap.find(wxString(wxT("unknown")));
184 it->second.Name = wxT("?");
185 return it->second;
188 const wxString CCode = wxString(char2unicode(GeoIP_country_code_by_addr(m_geoip, unicode2char(ip)))).MakeLower();
190 CountryDataMap::iterator it = m_CountryDataMap.find(CCode);
191 if (it == m_CountryDataMap.end()) {
192 // Show the code and ?? flag
193 it = m_CountryDataMap.find(wxString(wxT("unknown")));
194 wxASSERT(it != m_CountryDataMap.end());
195 if (CCode.IsEmpty()) {
196 it->second.Name = wxT("?");
197 } else{
198 it->second.Name = CCode;
202 return it->second;
205 #endif // ENABLE_IP2COUNTRY