xref: /haiku/src/kits/locale/Country.cpp (revision 922e7ba1f3228e6f28db69b0ded8f86eb32dea17)
1 /*
2  * Copyright 2003-2011, 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* countryCode)
34 	:
35 	fICULocale(new ICU_VERSION::Locale("", countryCode))
36 {
37 }
38 
39 
40 BCountry::BCountry(const BCountry& other)
41 	:
42 	fICULocale(new ICU_VERSION::Locale(*other.fICULocale))
43 {
44 }
45 
46 
47 BCountry&
48 BCountry::operator=(const BCountry& other)
49 {
50 	if (this == &other)
51 		return *this;
52 
53 	*fICULocale = *other.fICULocale;
54 
55 	return *this;
56 }
57 
58 
59 BCountry::~BCountry()
60 {
61 	delete fICULocale;
62 }
63 
64 
65 status_t
66 BCountry::GetNativeName(BString& name) const
67 {
68 	UnicodeString string;
69 	fICULocale->getDisplayName(*fICULocale, string);
70 	string.toTitle(NULL, *fICULocale);
71 
72 	name.Truncate(0);
73 	BStringByteSink converter(&name);
74 	string.toUTF8(converter);
75 
76 	return B_OK;
77 }
78 
79 
80 status_t
81 BCountry::GetName(BString& name, const BLanguage* displayLanguage) const
82 {
83 	status_t status = B_OK;
84 	BString appLanguage;
85 	if (displayLanguage == NULL) {
86 		BMessage preferredLanguages;
87 		status = BLocaleRoster::Default()->GetPreferredLanguages(
88 			&preferredLanguages);
89 		if (status == B_OK)
90 			status = preferredLanguages.FindString("language", 0, &appLanguage);
91 	} else {
92 		appLanguage = displayLanguage->Code();
93 	}
94 
95 	if (status == B_OK) {
96 		UnicodeString uString;
97 		fICULocale->getDisplayName(Locale(appLanguage), uString);
98 		name.Truncate(0);
99 		BStringByteSink stringConverter(&name);
100 		uString.toUTF8(stringConverter);
101 	}
102 
103 	return status;
104 }
105 
106 
107 const char*
108 BCountry::Code() const
109 {
110 	return fICULocale->getCountry();
111 }
112 
113 
114 status_t
115 BCountry::GetIcon(BBitmap* result) const
116 {
117 	return BLocaleRoster::Default()->GetFlagIconForCountry(result, Code());
118 }
119 
120 
121 status_t
122 BCountry::GetAvailableTimeZones(BMessage* timeZones) const
123 {
124 	return BLocaleRoster::Default()->GetAvailableTimeZonesForCountry(timeZones,
125 		Code());
126 }
127 
128