Upstream tarball 20080902
[amule.git] / src / IP2Country.cpp
blobcb631876b68df03521d3654f7dadf97e2959ab73
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 #include "IP2Country.h"
46 #include "Logger.h" // For AddLogLineM()
47 #include <common/Format.h> // For CFormat()
48 #include <common/StringFunctions.h> // For unicode2char()
49 #include "pixmaps/flags_xpm/CountryFlags.h"
51 #include <wx/intl.h>
52 #include <wx/image.h>
54 #ifdef __WXMAC__
55 #include <CoreFoundation/CFBundle.h>
56 #include <wx/mac/corefoundation/cfstring.h>
57 #endif
59 CIP2Country::CIP2Country()
61 m_geoip = NULL;
63 #ifdef __WXMAC__
64 // For the Mac GUI application, look for GeoIP database in the bundle
65 CFURLRef GeoIPDBUrl = CFBundleCopyResourceURL(
66 CFBundleGetMainBundle(),
67 CFSTR("GeoIP"), CFSTR("dat"), CFSTR("GeoIP")
69 if (GeoIPDBUrl) {
70 CFURLRef absoluteURL =
71 CFURLCopyAbsoluteURL(GeoIPDBUrl);
72 CFRelease(GeoIPDBUrl);
73 if (absoluteURL) {
74 CFStringRef pathString =
75 CFURLCopyFileSystemPath(
76 absoluteURL,
77 kCFURLPOSIXPathStyle);
78 CFRelease(absoluteURL);
79 wxString GeoIPDB = wxMacCFStringHolder(pathString).
80 AsString(wxLocale::GetSystemEncoding());
82 m_geoip = GeoIP_open(unicode2char(GeoIPDB),
83 GEOIP_STANDARD);
86 #endif
87 if (m_geoip == NULL)
88 m_geoip = GeoIP_new(GEOIP_STANDARD);
90 // Load data from xpm files
91 for (int i = 0; i < FLAGS_XPM_SIZE; ++i) {
92 CountryData countrydata;
93 countrydata.Name = char2unicode(flagXPMCodeVector[i].code);
94 countrydata.Flag = wxBitmap(flagXPMCodeVector[i].xpm);
96 if (countrydata.Flag.IsOk()) {
97 m_CountryDataMap[countrydata.Name] = countrydata;
98 } else {
99 AddLogLineM(true, _("CIP2Country::CIP2Country(): Failed to load country data from ") + countrydata.Name);
100 continue;
104 AddLogLineM(false, CFormat(wxPLURAL("Loaded %d flag bitmap.", "Loaded %d flag bitmaps.", m_CountryDataMap.size())) % m_CountryDataMap.size());
108 CIP2Country::~CIP2Country()
110 GeoIP_delete(m_geoip);
114 const CountryData& CIP2Country::GetCountryData(const wxString &ip)
116 // Should prevent the crash if the GeoIP database does not exists
117 if (m_geoip == NULL) {
118 CountryDataMap::iterator it = m_CountryDataMap.find(wxString(wxT("unknown")));
119 it->second.Name = wxT("?");
120 return it->second;
123 const wxString CCode = wxString(char2unicode(GeoIP_country_code_by_addr(m_geoip, unicode2char(ip)))).MakeLower();
125 CountryDataMap::iterator it = m_CountryDataMap.find(CCode);
126 if (it == m_CountryDataMap.end()) {
127 // Show the code and ?? flag
128 it = m_CountryDataMap.find(wxString(wxT("unknown")));
129 wxASSERT(it != m_CountryDataMap.end());
130 if (CCode.IsEmpty()) {
131 it->second.Name = wxT("?");
132 } else{
133 it->second.Name = CCode;
137 return it->second;