diff --git a/docs/user/locale/Country.dox b/docs/user/locale/Country.dox index 8c1a74a49f..2eebdcce5c 100644 --- a/docs/user/locale/Country.dox +++ b/docs/user/locale/Country.dox @@ -1,5 +1,5 @@ /* - * Copyright 2011 Haiku, Inc. All rights reserved. + * Copyright 2011-2019 Haiku, Inc. All rights reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -8,8 +8,8 @@ * John Scipione, jscipione@gmail.com * * Corresponds to: - * headers/os/locale/Country.h rev 42274 - * src/kits/locale/Country.cpp rev 42274 + * headers/os/locale/Country.h rev 53489 + * src/kits/locale/Country.cpp rev 53489 */ @@ -77,6 +77,32 @@ */ +/*! + \fn status_t BCountry::SetTo(const char* countryCode) + \brief Initialize a BCountry from a country code. + + \param countryCode The country code to initialize from. + + \returns Same value as InitCheck. + + \since Haiku R1 +*/ + + +/*! + \fn status_t BCountry::InitCheck() + \brief Check validity of the BCountry object. + + \param countryCode The country code to initialize from. + + \returns B_OK if everything went fine, B_BAD_DATA if the specified country + code is not valid, B_NO_MEMORY if the object could not be + allocated properly. + + \since Haiku R1 +*/ + + /*! \fn status_t BCountry::GetName(BString& name, const BLanguage* displayLanguage = NULL) const diff --git a/headers/os/locale/Country.h b/headers/os/locale/Country.h index 681910e895..ae41f5351f 100644 --- a/headers/os/locale/Country.h +++ b/headers/os/locale/Country.h @@ -1,5 +1,5 @@ /* - * Copyright 2003-2011, Haiku, Inc. + * Copyright 2003-2019, Haiku, Inc. * Distributed under the terms of the MIT Licence. */ #ifndef _COUNTRY_H_ @@ -32,6 +32,9 @@ public: BCountry& operator=(const BCountry& other); ~BCountry(); + status_t SetTo(const char* countryCode); + status_t InitCheck() const; + status_t GetNativeName(BString& name) const; status_t GetName(BString& name, const BLanguage* displayLanguage = NULL diff --git a/src/kits/locale/Country.cpp b/src/kits/locale/Country.cpp index e95711d5f0..7912908d2d 100644 --- a/src/kits/locale/Country.cpp +++ b/src/kits/locale/Country.cpp @@ -1,6 +1,6 @@ /* * Copyright 2003-2011, Axel Dörfler, axeld@pinc-software.de. - * Copyright 2009-2010, Adrien Destugues, pulkomandy@gmail.com. + * Copyright 2009-2019, Adrien Destugues, pulkomandy@gmail.com. * Distributed under the terms of the MIT License. */ @@ -30,8 +30,9 @@ BCountry::BCountry(const char* countryCode) : - fICULocale(new icu::Locale("", countryCode)) + fICULocale(NULL) { + SetTo(countryCode); } @@ -48,7 +49,10 @@ BCountry::operator=(const BCountry& other) if (this == &other) return *this; - *fICULocale = *other.fICULocale; + if (!fICULocale) + fICULocale = new icu::Locale(*other.fICULocale); + else + *fICULocale = *other.fICULocale; return *this; } @@ -60,9 +64,36 @@ BCountry::~BCountry() } +status_t +BCountry::SetTo(const char* countryCode) +{ + delete fICULocale; + fICULocale = new icu::Locale("", countryCode); + + return InitCheck(); +} + + +status_t +BCountry::InitCheck() const +{ + if (fICULocale == NULL) + return B_NO_MEMORY; + + if (fICULocale->isBogus()) + return B_BAD_DATA; + + return B_OK; +} + + status_t BCountry::GetNativeName(BString& name) const { + status_t valid = InitCheck(); + if (valid != B_OK) + return valid; + UnicodeString string; fICULocale->getDisplayName(*fICULocale, string); string.toTitle(NULL, *fICULocale); @@ -78,7 +109,10 @@ BCountry::GetNativeName(BString& name) const status_t BCountry::GetName(BString& name, const BLanguage* displayLanguage) const { - status_t status = B_OK; + status_t status = InitCheck(); + if (status != B_OK) + return status; + BString appLanguage; if (displayLanguage == NULL) { BMessage preferredLanguages; @@ -105,6 +139,10 @@ BCountry::GetName(BString& name, const BLanguage* displayLanguage) const const char* BCountry::Code() const { + status_t status = InitCheck(); + if (status != B_OK) + return NULL; + return fICULocale->getCountry(); } @@ -112,6 +150,10 @@ BCountry::Code() const status_t BCountry::GetIcon(BBitmap* result) const { + status_t status = InitCheck(); + if (status != B_OK) + return status; + return BLocaleRoster::Default()->GetFlagIconForCountry(result, Code()); } @@ -119,6 +161,10 @@ BCountry::GetIcon(BBitmap* result) const status_t BCountry::GetAvailableTimeZones(BMessage* timeZones) const { + status_t status = InitCheck(); + if (status != B_OK) + return status; + return BLocaleRoster::Default()->GetAvailableTimeZonesForCountry(timeZones, Code()); }