From 2aeaae9988d7508bdd0876f53cae5655a204bfe4 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 21 Jul 2010 10:12:59 +0000 Subject: [PATCH] * Locale Roster : store an instance of BCountry instead of replicating all of its intrnals and creating instances on demand * Locale preflet : save the time formats to the settings file if they are different from the defaut. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37642 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kits/locale/LocaleRoster.cpp | 64 ++++++++++++------- src/preferences/locale/LocaleSettings.cpp | 33 +++++++--- .../locale/TimeFormatSettingsView.cpp | 7 ++ 3 files changed, 70 insertions(+), 34 deletions(-) diff --git a/src/kits/locale/LocaleRoster.cpp b/src/kits/locale/LocaleRoster.cpp index 4ec66d89aa..1593db3f17 100644 --- a/src/kits/locale/LocaleRoster.cpp +++ b/src/kits/locale/LocaleRoster.cpp @@ -191,8 +191,9 @@ struct RosterData { BLocker fLock; BList fCatalogAddOnInfos; BMessage fPreferredLanguages; - BString fCountryCodeName; - BString fCountryDateFormat; + // BString fCountryCodeName; + // BString fCountryDateFormat; + BCountry* fDefaultCountry; RosterData(); ~RosterData(); @@ -236,7 +237,7 @@ RosterData::RosterData() icuLocale.getDisplayName(ustr); ustr.toUTF8(bbs); - Locale::setDefault(icuLocale,icuError); + Locale::setDefault(icuLocale, icuError); assert(icuError == U_ZERO_ERROR); fPreferredLanguages.RemoveName("language"); for (int i = 0; settingsMessage.FindString("language", i, @@ -246,8 +247,21 @@ RosterData::RosterData() } else fPreferredLanguages.AddString("language", "en"); - if (settingsMessage.FindString("country", &fCountryCodeName) != B_OK) - fCountryCodeName = "en_US"; + BString codeName; + if (settingsMessage.FindString("country", &codeName) + == B_OK) + fDefaultCountry = new BCountry(codeName); + else + fDefaultCountry = new BCountry("en_US"); + + BString timeFormat; + if (settingsMessage.FindString("shortTimeFormat", &timeFormat) + == B_OK) + fDefaultCountry->SetTimeFormat(timeFormat, false); + if (settingsMessage.FindString("longTimeFormat", &timeFormat) + == B_OK) + fDefaultCountry->SetTimeFormat(timeFormat, true); + return; } } @@ -255,8 +269,8 @@ RosterData::RosterData() // Something went wrong (no settings file or invalid BMessage // set everything to default values fPreferredLanguages.AddString("language", "en"); - fCountryCodeName = "en_US"; - log_team(LOG_ERR,"*** No language preference found!\n"); + fDefaultCountry = new BCountry("en_US"); + log_team(LOG_ERR, "*** No language preference found!\n"); } @@ -264,6 +278,7 @@ RosterData::~RosterData() { BAutolock lock(fLock); assert(lock.IsLocked()); + delete fDefaultCountry; CleanupCatalogAddOns(); closelog(); } @@ -278,9 +293,9 @@ RosterData::CompareInfos(const void *left, const void *right) /* - * iterate over add-on-folders and collect information about each - * catalog-add-ons (types of catalogs) into fCatalogAddOnInfos. - */ +iterate over add-on-folders and collect information about each +catalog-add-ons (types of catalogs) into fCatalogAddOnInfos. +*/ void RosterData::InitializeCatalogAddOns() { @@ -397,7 +412,7 @@ RosterData::InitializeCatalogAddOns() } fCatalogAddOnInfos.SortItems(CompareInfos); - for (int32 i=0; i(fCatalogAddOnInfos.ItemAt(i)); } @@ -406,14 +421,14 @@ RosterData::InitializeCatalogAddOns() /* * unloads all catalog-add-ons (which will throw away all loaded catalogs, too) - */ +*/ void RosterData::CleanupCatalogAddOns() { BAutolock lock(fLock); assert(lock.IsLocked()); int32 count = fCatalogAddOnInfos.CountItems(); - for (int32 i=0; i(fCatalogAddOnInfos.ItemAt(i)); delete info; @@ -440,7 +455,7 @@ BLocaleRoster::GetCatalog(BCatalog* catalog, vint32* catalogInitStatus) { // This function is used in the translation macros, so it can't return a // status_t. Maybe it could throw exceptions ? - + if (*catalogInitStatus == true) { // Catalog already loaded - nothing else to do return catalog; @@ -452,15 +467,16 @@ BLocaleRoster::GetCatalog(BCatalog* catalog, vint32* catalogInitStatus) bool found = false; while (get_next_image_info(0, &cookie, &info) == B_OK) { - if ((char*)info.data < (char*)catalog && (char*)info.data+info.data_size - > (char*)catalog) { + if ((char*)info.data < (char*)catalog && (char*)info.data + + info.data_size > (char*)catalog) { found = true; break; } } if (!found) { - log_team(LOG_DEBUG, "Catalog %x doesn't belong to any image !",catalog); + log_team(LOG_DEBUG, "Catalog %x doesn't belong to any image !", + catalog); return catalog; } // figure out mimetype from image @@ -475,7 +491,7 @@ BLocaleRoster::GetCatalog(BCatalog* catalog, vint32* catalogInitStatus) // drop supertype from mimetype (should be "application/"): char* stripSignature = objectSignature; - while(*stripSignature != '/') + while (*stripSignature != '/') stripSignature ++; stripSignature ++; @@ -532,10 +548,7 @@ BLocaleRoster::GetDefaultCountry(BCountry **country) const BAutolock lock(gRosterData.fLock); assert(lock.IsLocked()); - *country = new(std::nothrow) BCountry( - gRosterData.fCountryCodeName.String()); - if (gRosterData.fCountryDateFormat.Length() > 0) - (*country)->SetDateFormat(gRosterData.fCountryDateFormat.String()); + *country = gRosterData.fDefaultCountry; return B_OK; } @@ -559,8 +572,11 @@ BLocaleRoster::GetLanguage(const char* languageCode, void BLocaleRoster::SetDefaultCountry(BCountry* newDefault) const { - gRosterData.fCountryCodeName = newDefault->Code(); - newDefault->DateFormat(gRosterData.fCountryDateFormat, true); + BAutolock lock(gRosterData.fLock); + assert(lock.IsLocked()); + + delete gRosterData.fDefaultCountry; + gRosterData.fDefaultCountry = newDefault; } diff --git a/src/preferences/locale/LocaleSettings.cpp b/src/preferences/locale/LocaleSettings.cpp index 5a5e0e0e55..b928cad66e 100644 --- a/src/preferences/locale/LocaleSettings.cpp +++ b/src/preferences/locale/LocaleSettings.cpp @@ -32,10 +32,10 @@ LocaleSettings::Load() BFile file; status_t err; err = _Open(&file, B_READ_ONLY); - if (err != B_OK) + if (err != B_OK) return err; err = fMessage.Unflatten(&file); - if (err == B_OK) + if (err == B_OK) fSaved = true; return err; } @@ -51,7 +51,7 @@ LocaleSettings::Save() return err; err = fMessage.Flatten(&file); - if (err == B_OK) + if (err == B_OK) fSaved = true; return err; } @@ -73,21 +73,34 @@ LocaleSettings::_Open(BFile* file, int32 mode) void LocaleSettings::UpdateFrom(BMessage* message) { - BString langName; + BString messageContent; - if (message->FindString("language", &langName) == B_OK) { + if (message->FindString("language", &messageContent) == B_OK) { fMessage.RemoveName("language"); for (int i = 0;; i++) { - if (message->FindString("language", i, &langName) != B_OK) + if (message->FindString("language", i, &messageContent) != B_OK) break; - fMessage.AddString("language", langName); + fMessage.AddString("language", messageContent); } + fSaved = false; } - if (message->FindString("country", &langName) == B_OK) - fMessage.ReplaceString("country", langName); + if (message->FindString("country", &messageContent) == B_OK) { + fMessage.ReplaceString("country", messageContent); + fMessage.RemoveName("shortTimeFormat"); + fMessage.RemoveName("longTimeFormat"); + fSaved = false; + } - fSaved = false; + if (message->FindString("shortTimeFormat", &messageContent) == B_OK) { + fMessage.ReplaceString("shortTimeFormat", messageContent); + fSaved = false; + } + + if (message->FindString("longTimeFormat", &messageContent) == B_OK) { + fMessage.ReplaceString("longTimeFormat", messageContent); + fSaved = false; + } } diff --git a/src/preferences/locale/TimeFormatSettingsView.cpp b/src/preferences/locale/TimeFormatSettingsView.cpp index 217c0e428d..df049f0232 100644 --- a/src/preferences/locale/TimeFormatSettingsView.cpp +++ b/src/preferences/locale/TimeFormatSettingsView.cpp @@ -5,8 +5,10 @@ #include "TimeFormatSettingsView.h" +#include "Locale.h" #include +#include #include #include #include @@ -441,6 +443,8 @@ FormatView::MessageReceived(BMessage* message) case kClockFormatChange: { + BMessage newMessage(kMsgSettingsChanged); + BString timeFormat; timeFormat = fOriginalTimeFormat; if (f24HrRadioButton->Value() == 1) { @@ -458,6 +462,7 @@ FormatView::MessageReceived(BMessage* message) } } fCountry->SetTimeFormat(timeFormat.String(), false); + newMessage.AddString("shortTimeFormat", timeFormat); timeFormat = fOriginalLongTimeFormat; if (f24HrRadioButton->Value() == 1) { @@ -475,8 +480,10 @@ FormatView::MessageReceived(BMessage* message) } } fCountry->SetTimeFormat(timeFormat.String(), true); + newMessage.AddString("longTimeFormat", timeFormat); _UpdateExamples(); Window()->PostMessage(kSettingsContentsModified); + be_app_messenger.SendMessage(&newMessage); break; }