Upstream tarball 20080928
[amule.git] / src / IP2Country.cpp
blob1b0f5890d408e88ff701dbcbc19a8b645a72be86
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 "IP2Country.h"
48 #include "amule.h" // For theApp
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 CIP2Country::CIP2Country()
63 m_geoip = NULL;
64 m_DataBaseName = theApp->ConfigDir + wxT("GeoIP.dat");
65 if (m_CountryDataMap.empty()) {
66 // this must go to enable - when all usages of GetCountryData() (ClientListCtrl, DownloadListCtrl) are surrounded with an IsEnabled()
67 // right now, flags are loaded even when it's disabled
68 LoadFlags();
72 void CIP2Country::Enable()
74 Disable();
75 if (!CPath::FileExists(m_DataBaseName)) {
76 Update();
77 return;
80 const wxChar* geoip_files[] = {
81 wxT("GeoIP.dat"),
82 NULL
85 // Try to unpack the file, might be an archive
86 UnpackArchive(CPath(m_DataBaseName), geoip_files);
88 m_geoip = GeoIP_open(unicode2char(m_DataBaseName), GEOIP_STANDARD);
91 void CIP2Country::Update()
93 AddLogLineM(false,CFormat(_("Download new GeoIP.dat from %s")) % thePrefs::GetGeoIPUpdateUrl());
94 CHTTPDownloadThread *downloader = new CHTTPDownloadThread(thePrefs::GetGeoIPUpdateUrl(), m_DataBaseName + wxT(".download"), HTTP_GeoIP);
95 downloader->Create();
96 downloader->Run();
99 void CIP2Country::Disable()
101 if (m_geoip) {
102 GeoIP_delete(m_geoip);
103 m_geoip = NULL;
107 void CIP2Country::DownloadFinished(uint32 result)
109 if (result == 1) {
110 Disable();
111 // download succeeded. Switch over to new database.
112 wxString newDat = m_DataBaseName + wxT(".download");
114 if (wxFileExists(m_DataBaseName)) {
115 if (!wxRemoveFile(m_DataBaseName)) {
116 AddLogLineM(false, _("Failed to remove GeoIP.dat file, aborting update."));
117 return;
121 if (!wxRenameFile(newDat, m_DataBaseName)) {
122 AddLogLineM(false, _("Failed to rename new GeoIP.dat file, aborting update."));
123 return;
126 Enable();
127 if (m_geoip) {
128 AddLogLineM(false, _("Successfully updated GeoIP.dat"));
129 } else {
130 AddLogLineM(false, _("Error updating GeoIP.dat"));
132 } else {
133 AddLogLineM(false, CFormat(_("Failed to download GeoIP.dat from %s")) % thePrefs::GetGeoIPUpdateUrl());
134 // if it failed, turn it off
135 thePrefs::SetGeoIPEnabled(false);
139 void CIP2Country::LoadFlags()
141 // Load data from xpm files
142 for (int i = 0; i < flags::FLAGS_XPM_SIZE; ++i) {
143 CountryData countrydata;
144 countrydata.Name = char2unicode(flags::flagXPMCodeVector[i].code);
145 countrydata.Flag = wxImage(flags::flagXPMCodeVector[i].xpm);
147 if (countrydata.Flag.IsOk()) {
148 m_CountryDataMap[countrydata.Name] = countrydata;
149 } else {
150 AddLogLineM(true, _("CIP2Country::CIP2Country(): Failed to load country data from ") + countrydata.Name);
151 continue;
155 AddLogLineM(false, CFormat(_("Loaded %d flag bitmaps.")) % m_CountryDataMap.size()); // there's never just one - no plural needed
159 CIP2Country::~CIP2Country()
161 Disable();
165 const CountryData& CIP2Country::GetCountryData(const wxString &ip)
167 // Should prevent the crash if the GeoIP database does not exists
168 if (m_geoip == NULL) {
169 CountryDataMap::iterator it = m_CountryDataMap.find(wxString(wxT("unknown")));
170 it->second.Name = wxT("?");
171 return it->second;
174 const wxString CCode = wxString(char2unicode(GeoIP_country_code_by_addr(m_geoip, unicode2char(ip)))).MakeLower();
176 CountryDataMap::iterator it = m_CountryDataMap.find(CCode);
177 if (it == m_CountryDataMap.end()) {
178 // Show the code and ?? flag
179 it = m_CountryDataMap.find(wxString(wxT("unknown")));
180 wxASSERT(it != m_CountryDataMap.end());
181 if (CCode.IsEmpty()) {
182 it->second.Name = wxT("?");
183 } else{
184 it->second.Name = CCode;
188 return it->second;
191 #endif // ENABLE_IP2COUNTRY