BCountry: add SetTo and InitCheck.

Change-Id: I5fbc2a1c0e735d6edeb23672017bb64d1b3f4390
Reviewed-on: https://review.haiku-os.org/c/haiku/+/1872
Reviewed-by: Adrien Destugues <pulkomandy@gmail.com>
This commit is contained in:
Adrien Destugues 2019-09-20 12:34:29 +02:00 committed by Adrien Destugues
parent b4c187b004
commit 70cdd7d4f5
3 changed files with 83 additions and 8 deletions

View File

@ -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

View File

@ -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

View File

@ -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());
}