* BCountry : add copy constructor and assignment operator

* The locale roster uses them instead of messing with pointers, to avoid ownership problems
 * The Locale preflet works on a copy of the default country instead of altering it directly


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37649 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Adrien Destugues
2010-07-21 14:09:48 +00:00
parent aea00dc3bc
commit 82a115e8bc
5 changed files with 78 additions and 34 deletions
+2
View File
@@ -36,6 +36,8 @@ class BCountry {
public: public:
BCountry(const char* languageCode, const char* countryCode); BCountry(const char* languageCode, const char* countryCode);
BCountry(const char* languageAndCountryCode = "en_US"); BCountry(const char* languageAndCountryCode = "en_US");
BCountry(const BCountry& other);
BCountry& operator=(const BCountry& other);
virtual ~BCountry(); virtual ~BCountry();
virtual bool Name(BString&) const; virtual bool Name(BString&) const;
+40
View File
@@ -63,6 +63,46 @@ BCountry::BCountry(const char* languageAndCountryCode)
} }
BCountry::BCountry(const BCountry& other)
{
fICULocale = new ICU_VERSION::Locale(*other.fICULocale);
fICULongDateFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICULongDateFormatter));
fICUShortDateFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICUShortDateFormatter));
fICULongTimeFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICULongTimeFormatter));
fICUShortTimeFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICUShortTimeFormatter));
}
BCountry&
BCountry::operator=(const BCountry& other)
{
if (this == &other)
return *this;
delete fICULongTimeFormatter;
delete fICUShortTimeFormatter;
delete fICULongDateFormatter;
delete fICUShortDateFormatter;
delete fICULocale;
fICULocale = new ICU_VERSION::Locale(*other.fICULocale);
fICULongDateFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICULongDateFormatter));
fICUShortDateFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICUShortDateFormatter));
fICULongTimeFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICULongTimeFormatter));
fICUShortTimeFormatter = new ICU_VERSION::SimpleDateFormat(
*static_cast<SimpleDateFormat*>(other.fICUShortTimeFormatter));
return *this;
}
BCountry::~BCountry() BCountry::~BCountry()
{ {
delete fICULongTimeFormatter; delete fICULongTimeFormatter;
+12 -13
View File
@@ -186,7 +186,7 @@ struct RosterData {
BMessage fPreferredLanguages; BMessage fPreferredLanguages;
// BString fCountryCodeName; // BString fCountryCodeName;
// BString fCountryDateFormat; // BString fCountryDateFormat;
BCountry* fDefaultCountry; BCountry fDefaultCountry;
RosterData(); RosterData();
~RosterData(); ~RosterData();
@@ -241,19 +241,20 @@ RosterData::RosterData()
fPreferredLanguages.AddString("language", "en"); fPreferredLanguages.AddString("language", "en");
BString codeName; BString codeName;
if (settingsMessage.FindString("country", &codeName) if (settingsMessage.FindString("country", &codeName)
== B_OK) == B_OK)
fDefaultCountry = new BCountry(codeName); fDefaultCountry = BCountry(codeName);
else else
fDefaultCountry = new BCountry("en_US"); fDefaultCountry = BCountry("en_US");
BString timeFormat; BString timeFormat;
if (settingsMessage.FindString("shortTimeFormat", &timeFormat) if (settingsMessage.FindString("shortTimeFormat", &timeFormat)
== B_OK) == B_OK)
fDefaultCountry->SetTimeFormat(timeFormat, false); fDefaultCountry.SetTimeFormat(timeFormat, false);
if (settingsMessage.FindString("longTimeFormat", &timeFormat) if (settingsMessage.FindString("longTimeFormat", &timeFormat)
== B_OK) == B_OK)
fDefaultCountry->SetTimeFormat(timeFormat, true); fDefaultCountry.SetTimeFormat(timeFormat, true);
return; return;
} }
@@ -262,7 +263,7 @@ RosterData::RosterData()
// Something went wrong (no settings file or invalid BMessage // Something went wrong (no settings file or invalid BMessage
// set everything to default values // set everything to default values
fPreferredLanguages.AddString("language", "en"); fPreferredLanguages.AddString("language", "en");
fDefaultCountry = new BCountry("en_US"); fDefaultCountry = BCountry("en_US");
log_team(LOG_ERR, "*** No language preference found!\n"); log_team(LOG_ERR, "*** No language preference found!\n");
} }
@@ -271,7 +272,6 @@ RosterData::~RosterData()
{ {
BAutolock lock(fLock); BAutolock lock(fLock);
assert(lock.IsLocked()); assert(lock.IsLocked());
delete fDefaultCountry;
CleanupCatalogAddOns(); CleanupCatalogAddOns();
closelog(); closelog();
} }
@@ -540,10 +540,7 @@ BLocaleRoster::GetDefaultCountry(BCountry **country) const
if (!country) if (!country)
return B_BAD_VALUE; return B_BAD_VALUE;
BAutolock lock(gRosterData.fLock); *country = &gRosterData.fDefaultCountry;
assert(lock.IsLocked());
*country = gRosterData.fDefaultCountry;
return B_OK; return B_OK;
} }
@@ -567,11 +564,13 @@ BLocaleRoster::GetLanguage(const char* languageCode,
void void
BLocaleRoster::SetDefaultCountry(BCountry* newDefault) const BLocaleRoster::SetDefaultCountry(BCountry* newDefault) const
{ {
if (newDefault == NULL)
return;
BAutolock lock(gRosterData.fLock); BAutolock lock(gRosterData.fLock);
assert(lock.IsLocked()); assert(lock.IsLocked());
delete gRosterData.fDefaultCountry; gRosterData.fDefaultCountry = *newDefault;
gRosterData.fDefaultCountry = newDefault;
} }
@@ -147,7 +147,7 @@ IsSpecialDateChar(char charToTest)
FormatView::FormatView(BCountry* country) FormatView::FormatView(BCountry* country)
: :
BView("WindowsSettingsView", B_FRAME_EVENTS), BView("WindowsSettingsView", B_FRAME_EVENTS),
fCountry(country) fCountry(*country)
{ {
SetLayout(new BGroupLayout(B_HORIZONTAL)); SetLayout(new BGroupLayout(B_HORIZONTAL));
@@ -185,8 +185,8 @@ FormatView::FormatView(BCountry* country)
f12HrRadioButton = new BRadioButton("", B_TRANSLATE("12 hour"), f12HrRadioButton = new BRadioButton("", B_TRANSLATE("12 hour"),
new BMessage(kClockFormatChange)); new BMessage(kClockFormatChange));
fCountry->TimeFormat(fOriginalTimeFormat, false); fCountry.TimeFormat(fOriginalTimeFormat, false);
fCountry->TimeFormat(fOriginalLongTimeFormat, true); fCountry.TimeFormat(fOriginalLongTimeFormat, true);
if (fOriginalTimeFormat.FindFirst("a") != B_ERROR) { if (fOriginalTimeFormat.FindFirst("a") != B_ERROR) {
f12HrRadioButton->SetValue(B_CONTROL_ON); f12HrRadioButton->SetValue(B_CONTROL_ON);
fCountryIs24Hr = false; fCountryIs24Hr = false;
@@ -461,7 +461,7 @@ FormatView::MessageReceived(BMessage* message)
timeFormat.Append(" a"); timeFormat.Append(" a");
} }
} }
fCountry->SetTimeFormat(timeFormat.String(), false); fCountry.SetTimeFormat(timeFormat.String(), false);
newMessage.AddString("shortTimeFormat", timeFormat); newMessage.AddString("shortTimeFormat", timeFormat);
timeFormat = fOriginalLongTimeFormat; timeFormat = fOriginalLongTimeFormat;
@@ -479,7 +479,7 @@ FormatView::MessageReceived(BMessage* message)
timeFormat.Append(" a"); timeFormat.Append(" a");
} }
} }
fCountry->SetTimeFormat(timeFormat.String(), true); fCountry.SetTimeFormat(timeFormat.String(), true);
newMessage.AddString("longTimeFormat", timeFormat); newMessage.AddString("longTimeFormat", timeFormat);
_UpdateExamples(); _UpdateExamples();
Window()->PostMessage(kSettingsContentsModified); Window()->PostMessage(kSettingsContentsModified);
@@ -504,8 +504,11 @@ FormatView::SetDefaults()
settings.SetClockTo24Hr(false); settings.SetClockTo24Hr(false);
*/ */
delete fCountry; BCountry* defaultCountry;
be_locale_roster->GetDefaultCountry(&fCountry); be_locale_roster->GetDefaultCountry(&defaultCountry);
fCountry = *defaultCountry;
// We work on a copy of the default country and set the changes when
// closing the preflet
_UpdateExamples(); _UpdateExamples();
_SendNotices(); _SendNotices();
} }
@@ -544,13 +547,12 @@ FormatView::Revert()
void void
FormatView::SetCountry(BCountry* country) FormatView::SetCountry(BCountry* country)
{ {
delete fCountry; fCountry = *country;
fCountry = country;
fOriginalTimeFormat.Truncate(0); fOriginalTimeFormat.Truncate(0);
fCountry->TimeFormat(fOriginalTimeFormat, false); fCountry.TimeFormat(fOriginalTimeFormat, false);
fOriginalLongTimeFormat.Truncate(0); fOriginalLongTimeFormat.Truncate(0);
fCountry->TimeFormat(fOriginalLongTimeFormat, true); fCountry.TimeFormat(fOriginalLongTimeFormat, true);
if (fOriginalTimeFormat.FindFirst("a") != B_ERROR) { if (fOriginalTimeFormat.FindFirst("a") != B_ERROR) {
f12HrRadioButton->SetValue(B_CONTROL_ON); f12HrRadioButton->SetValue(B_CONTROL_ON);
@@ -616,19 +618,19 @@ FormatView::_UpdateExamples()
time_t timeValue = (time_t)time(NULL); time_t timeValue = (time_t)time(NULL);
BString timeFormat; BString timeFormat;
fCountry->FormatDate(&timeFormat, timeValue, true); fCountry.FormatDate(&timeFormat, timeValue, true);
fLongDateExampleView->SetText(timeFormat); fLongDateExampleView->SetText(timeFormat);
fCountry->FormatDate(&timeFormat, timeValue, false); fCountry.FormatDate(&timeFormat, timeValue, false);
fShortDateExampleView->SetText(timeFormat); fShortDateExampleView->SetText(timeFormat);
fCountry->FormatTime(&timeFormat, timeValue, true); fCountry.FormatTime(&timeFormat, timeValue, true);
fLongTimeExampleView->SetText(timeFormat); fLongTimeExampleView->SetText(timeFormat);
fCountry->FormatTime(&timeFormat, timeValue, false); fCountry.FormatTime(&timeFormat, timeValue, false);
fShortTimeExampleView->SetText(timeFormat); fShortTimeExampleView->SetText(timeFormat);
status_t Error = fCountry->FormatNumber(&timeFormat, 1234.5678); status_t Error = fCountry.FormatNumber(&timeFormat, 1234.5678);
if (Error == B_OK) if (Error == B_OK)
fNumberFormatExampleView->SetText(timeFormat); fNumberFormatExampleView->SetText(timeFormat);
else else
@@ -658,7 +660,7 @@ FormatView::_ParseDateFormat()
{ {
// TODO parse the short date too // TODO parse the short date too
BString dateFormatString; BString dateFormatString;
fCountry->DateFormat(dateFormatString, true); fCountry.DateFormat(dateFormatString, true);
const char* dateFormat = dateFormatString.String(); const char* dateFormat = dateFormatString.String();
// Travel trough the string and parse it // Travel trough the string and parse it
@@ -710,7 +712,7 @@ FormatView::_ParseDateFormat()
// Short date is a bit more tricky, we want to extract the separator // Short date is a bit more tricky, we want to extract the separator
dateFormatString.Truncate(0); dateFormatString.Truncate(0);
fCountry->DateFormat(dateFormatString, false); fCountry.DateFormat(dateFormatString, false);
dateFormat = dateFormatString.String(); dateFormat = dateFormatString.String();
// Travel trough the string and parse it // Travel trough the string and parse it
@@ -776,7 +778,7 @@ FormatView::_UpdateLongDateFormatString()
} }
// TODO save this in the settings preflet and make the roster load it back // TODO save this in the settings preflet and make the roster load it back
fCountry->SetDateFormat(newDateFormat.String()); fCountry.SetDateFormat(newDateFormat.String());
newDateFormat.Truncate(0); newDateFormat.Truncate(0);
@@ -787,5 +789,5 @@ FormatView::_UpdateLongDateFormatString()
newDateFormat.Append(fDateString[2]); newDateFormat.Append(fDateString[2]);
// TODO save this in the settings preflet and make the roster load it back // TODO save this in the settings preflet and make the roster load it back
fCountry->SetDateFormat(newDateFormat.String(), false); fCountry.SetDateFormat(newDateFormat.String(), false);
} }
@@ -7,6 +7,7 @@
#include <Box.h> #include <Box.h>
#include <Country.h>
#include <String.h> #include <String.h>
#include <View.h> #include <View.h>
@@ -81,7 +82,7 @@ private:
BString fOriginalLongTimeFormat; BString fOriginalLongTimeFormat;
bool fCountryIs24Hr; bool fCountryIs24Hr;
BCountry* fCountry; BCountry fCountry;
BBox* fDateBox; BBox* fDateBox;
BBox* fTimeBox; BBox* fTimeBox;