* Locale Roster : add a function to update the settings from a BMessage

* Locale preference : send such a message broadcast to all applications when the settings are changed
 * Deskbar : receive the message and forward it to the locale roster. This way the time display on the deskbar changes as 
soon as you close the locale preflet.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37663 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Adrien Destugues 2010-07-21 17:04:28 +00:00
parent 4bfbcb535d
commit 9a78a691f8
3 changed files with 69 additions and 41 deletions

View File

@ -513,6 +513,12 @@ TBarApp::MessageReceived(BMessage* message)
break;
}
case B_LOCALE_CHANGED:
{
be_locale_roster->UpdateSettings(message);
break;
}
default:
BApplication::MessageReceived(message);
break;

View File

@ -193,6 +193,7 @@ struct RosterData {
void InitializeCatalogAddOns();
void CleanupCatalogAddOns();
static int CompareInfos(const void *left, const void *right);
void UpdateSettings(BMessage* newSettings);
};
static RosterData gRosterData;
@ -201,8 +202,6 @@ RosterData::RosterData()
:
fLock("LocaleRosterData")
{
BAutolock lock(fLock);
assert(lock.IsLocked());
openlog_team("liblocale.so", LOG_PID, LOG_USER);
#ifndef DEBUG
@ -219,43 +218,7 @@ RosterData::RosterData()
BMessage settingsMessage;
if (file.SetTo(path.Path(), B_READ_ONLY) == B_OK
&& settingsMessage.Unflatten(&file) == B_OK) {
BString langName;
if (settingsMessage.FindString("language", &langName) == B_OK) {
UErrorCode icuError = U_ZERO_ERROR;
Locale icuLocale = Locale::createCanonical(langName.String());
assert(!icuLocale.isBogus());
UnicodeString ustr;
BString bstr;
BStringByteSink bbs(&bstr);
icuLocale.getDisplayName(ustr);
ustr.toUTF8(bbs);
Locale::setDefault(icuLocale, icuError);
assert(icuError == U_ZERO_ERROR);
fPreferredLanguages.RemoveName("language");
for (int i = 0; settingsMessage.FindString("language", i,
&langName) == B_OK; i++) {
fPreferredLanguages.AddString("language", langName);
}
} else
fPreferredLanguages.AddString("language", "en");
BString codeName;
if (settingsMessage.FindString("country", &codeName)
== B_OK)
fDefaultCountry = BCountry(codeName);
else
fDefaultCountry = 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);
UpdateSettings(&settingsMessage);
return;
}
}
@ -574,6 +537,58 @@ BLocaleRoster::SetDefaultCountry(BCountry* newDefault) const
}
void
BLocaleRoster::UpdateSettings(BMessage* newSettings)
{
gRosterData.UpdateSettings(newSettings);
}
void
RosterData::UpdateSettings(BMessage* newSettings)
{
BAutolock lock(fLock);
assert(lock.IsLocked());
BString langName;
if (newSettings->FindString("language", &langName) == B_OK) {
UErrorCode icuError = U_ZERO_ERROR;
Locale icuLocale = Locale::createCanonical(langName.String());
assert(!icuLocale.isBogus());
UnicodeString ustr;
BString bstr;
BStringByteSink bbs(&bstr);
icuLocale.getDisplayName(ustr);
ustr.toUTF8(bbs);
Locale::setDefault(icuLocale, icuError);
assert(icuError == U_ZERO_ERROR);
fPreferredLanguages.RemoveName("language");
for (int i = 0; newSettings->FindString("language", i,
&langName) == B_OK; i++) {
fPreferredLanguages.AddString("language", langName);
}
} else
fPreferredLanguages.AddString("language", "en");
BString codeName;
if (newSettings->FindString("country", &codeName)
== B_OK)
fDefaultCountry = BCountry(codeName);
else
fDefaultCountry = BCountry("en_US");
BString timeFormat;
if (newSettings->FindString("shortTimeFormat", &timeFormat)
== B_OK)
fDefaultCountry.SetTimeFormat(timeFormat, false);
if (newSettings->FindString("longTimeFormat", &timeFormat)
== B_OK)
fDefaultCountry.SetTimeFormat(timeFormat, true);
}
status_t
BLocaleRoster::GetPreferredLanguages(BMessage* languages) const
{
@ -588,7 +603,7 @@ BLocaleRoster::GetPreferredLanguages(BMessage* languages) const
}
status_t
status_t
BLocaleRoster::SetPreferredLanguages(BMessage *languages)
{
BAutolock lock(gRosterData.fLock);
@ -628,7 +643,7 @@ BLocaleRoster::GetInstalledLanguages(BMessage *languages) const
status_t
BLocaleRoster::GetInstalledCatalogs(BMessage * languageList,
const char* sigPattern, const char* langPattern, int32 fingerprint) const
const char* sigPattern, const char* langPattern, int32 fingerprint) const
{
if (languageList == NULL)
return B_BAD_VALUE;

View File

@ -7,7 +7,9 @@
#include "LocaleSettings.h"
#include <FindDirectory.h>
#include <LocaleRoster.h>
#include <Path.h>
#include <Roster.h>
#include <String.h>
#include <SupportDefs.h>
@ -44,6 +46,11 @@ LocaleSettings::Load()
status_t
LocaleSettings::Save()
{
// Send to all running apps to notify them they should update their settings
fMessage.what = B_LOCALE_CHANGED;
be_roster->Broadcast(&fMessage);
// Save on disk for next time we reboot
BFile file;
status_t err;
err = _Open(&file, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);