xref: /haiku/src/kits/locale/Country.cpp (revision 56187df6ebd9d1fb16bf954fce8df0999e9e3048)
1 /*
2  * Copyright 2003-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2009-2010, Adrien Destugues, pulkomandy@gmail.com.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include <Country.h>
9 
10 #include <AutoDeleter.h>
11 #include <IconUtils.h>
12 #include <List.h>
13 #include <Language.h>
14 #include <LocaleRoster.h>
15 #include <Resources.h>
16 #include <String.h>
17 
18 #include <unicode/locid.h>
19 #include <unicode/ulocdata.h>
20 #include <ICUWrapper.h>
21 
22 #include <iostream>
23 #include <map>
24 #include <monetary.h>
25 #include <new>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 
29 
30 #define ICU_VERSION icu_44
31 
32 
33 BCountry::BCountry(const char* languageCode, const char* countryCode)
34 	:
35 	fICULocale(new ICU_VERSION::Locale(languageCode, countryCode))
36 {
37 }
38 
39 
40 BCountry::BCountry(const char* languageAndCountryCode)
41 	:
42 	fICULocale(new ICU_VERSION::Locale(languageAndCountryCode))
43 {
44 }
45 
46 
47 BCountry::BCountry(const BCountry& other)
48 	:
49 	fICULocale(new ICU_VERSION::Locale(*other.fICULocale))
50 {
51 }
52 
53 
54 BCountry&
55 BCountry::operator=(const BCountry& other)
56 {
57 	if (this == &other)
58 		return *this;
59 
60 	*fICULocale = *other.fICULocale;
61 
62 	return *this;
63 }
64 
65 
66 BCountry::~BCountry()
67 {
68 	delete fICULocale;
69 }
70 
71 
72 status_t
73 BCountry::GetNativeName(BString& name) const
74 {
75 	UnicodeString string;
76 	fICULocale->getDisplayName(*fICULocale, string);
77 	string.toTitle(NULL, *fICULocale);
78 
79 	name.Truncate(0);
80 	BStringByteSink converter(&name);
81 	string.toUTF8(converter);
82 
83 	return B_OK;
84 }
85 
86 
87 status_t
88 BCountry::GetName(BString& name, const BLanguage* displayLanguage) const
89 {
90 	BString appLanguage;
91 	if (displayLanguage == NULL) {
92 		BMessage preferredLanguage;
93 		be_locale_roster->GetPreferredLanguages(&preferredLanguage);
94 		preferredLanguage.FindString("language", 0, &appLanguage);
95 	} else {
96 		appLanguage = displayLanguage->Code();
97 	}
98 
99 	UnicodeString uString;
100 	fICULocale->getDisplayName(Locale(appLanguage), uString);
101 	BStringByteSink stringConverter(&name);
102 	uString.toUTF8(stringConverter);
103 
104 	return B_OK;
105 }
106 
107 
108 const char*
109 BCountry::Code() const
110 {
111 	return fICULocale->getName();
112 }
113 
114 
115 status_t
116 BCountry::GetIcon(BBitmap* result) const
117 {
118 	const char* code = fICULocale->getCountry();
119 	if (code == NULL)
120 		return  B_ERROR;
121 	return be_locale_roster->GetFlagIconForCountry(result, code);
122 }
123 
124 
125 status_t
126 BCountry::GetAvailableTimeZones(BMessage* timeZones) const
127 {
128 	return be_locale_roster->GetAvailableTimeZonesForCountry(timeZones, Code());
129 }
130 
131 
132 // TODO does ICU even support this ? Is it in the keywords ?
133 int8
134 BCountry::Measurement() const
135 {
136 	UErrorCode error = U_ZERO_ERROR;
137 	switch (ulocdata_getMeasurementSystem(Code(), &error)) {
138 		case UMS_US:
139 			return B_US;
140 		case UMS_SI:
141 		default:
142 			return B_METRIC;
143 	}
144 }
145