* Separate the Settings class from the locale preflet a little (it can be reused in other parts of the OS)

* Improve the settings class so it is able to handle a revert
 * Re-enable the revert button in the preflet and make it work.
This fixes #5897.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@37400 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Adrien Destugues 2010-07-05 13:36:16 +00:00
parent 0cf8c36abb
commit c19a67f957
6 changed files with 195 additions and 109 deletions

View File

@ -7,6 +7,7 @@ UsePrivateHeaders shared ;
Preference Locale :
LanguageListView.cpp
Locale.cpp
LocaleSettings.cpp
LocaleWindow.cpp
TimeFormatSettingsView.cpp

View File

@ -1,10 +1,12 @@
/*
* Copyright 2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
* Copyright 2010, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>. All rightts reserved.
* Distributed under the terms of the MIT License.
*/
#include "Locale.h"
#include "LocaleSettings.h"
#include "LocaleWindow.h"
#include <AboutWindow.h>
@ -12,10 +14,7 @@
#include <Alert.h>
#include <Catalog.h>
#include <TextView.h>
#include <FindDirectory.h>
#include <File.h>
#include <Locale.h>
#include <Path.h>
#include <stdio.h>
#include <string.h>
@ -27,111 +26,22 @@
const char* kSignature = "application/x-vnd.Haiku-Locale";
static const uint32 kMsgLocaleSettings = 'LCst';
class Settings {
public:
Settings();
~Settings();
const BMessage& Message() const { return fMessage; }
void UpdateFrom(BMessage* message);
private:
status_t _Open(BFile* file, int32 mode);
BMessage fMessage;
bool fUpdated;
};
class LocalePreflet : public BApplication {
public:
public:
LocalePreflet();
virtual ~LocalePreflet();
virtual ~LocalePreflet();
virtual void MessageReceived(BMessage* message);
virtual void AboutRequested();
virtual void MessageReceived(BMessage* message);
virtual void AboutRequested();
private:
Settings fSettings;
LocaleWindow* fLocaleWindow;
LocaleSettings fSettings;
LocaleSettings fOriginalSettings;
LocaleWindow* fLocaleWindow;
};
//-----------------
Settings::Settings()
:
fMessage(kMsgLocaleSettings),
fUpdated(false)
{
BFile file;
if (_Open(&file, B_READ_ONLY) != B_OK
|| fMessage.Unflatten(&file) != B_OK) {
// set default prefs
fMessage.AddString("language", "en");
fMessage.AddString("country", "en_US");
return;
}
}
Settings::~Settings()
{
if (!fUpdated)
return;
BFile file;
if (_Open(&file, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY) != B_OK)
return;
fMessage.Flatten(&file);
}
status_t
Settings::_Open(BFile* file, int32 mode)
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return B_ERROR;
path.Append("Locale settings");
return file->SetTo(path.Path(), mode);
}
void
Settings::UpdateFrom(BMessage* message)
{
BPoint point;
if (message->FindPoint("window_location", &point) == B_OK)
fMessage.ReplacePoint("window_location", point);
BString langName;
// We make sure there is at least one string before erasing the previous
// settings, then we add the remaining ones, if any
if (message->FindString("language", &langName) == B_OK) {
// Remove any old data as we know we have newer one to replace it
fMessage.RemoveName("language");
for (int i = 0;; i++) {
if (message->FindString("language", i, &langName) != B_OK)
break;
fMessage.AddString("language", langName);
}
}
if (message->FindString("country", &langName) == B_OK)
fMessage.ReplaceString("country", langName);
fUpdated = true;
}
// #pragma mark -
@ -141,10 +51,15 @@ LocalePreflet::LocalePreflet()
{
fLocaleWindow = new LocaleWindow();
fOriginalSettings.Load();
fSettings = fOriginalSettings;
/*
if (fSettings.Message().HasPoint("window_location")) {
BPoint point = fSettings.Message().FindPoint("window_location");
fLocaleWindow->MoveTo(point);
}
*/
fLocaleWindow->Show();
}
@ -152,6 +67,7 @@ LocalePreflet::LocalePreflet()
LocalePreflet::~LocalePreflet()
{
fSettings.Save();
}
@ -163,6 +79,10 @@ LocalePreflet::MessageReceived(BMessage* message)
fSettings.UpdateFrom(message);
break;
case kMsgRevert:
fSettings = fOriginalSettings;
break;
default:
BApplication::MessageReceived(message);
break;

View File

@ -0,0 +1,93 @@
/*
* Copyright 2010, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>.
* Distributed under the terms of the MIT License.
*/
#include "LocaleSettings.h"
#include <FindDirectory.h>
#include <Path.h>
#include <String.h>
#include <SupportDefs.h>
static const uint32 kMsgLocaleSettings = 'LCst';
LocaleSettings::LocaleSettings()
:
fMessage(kMsgLocaleSettings),
fSaved(false)
{
// Set default preferences
fMessage.AddString("language", "en");
fMessage.AddString("country", "en_US");
}
status_t
LocaleSettings::Load()
{
BFile file;
status_t err;
err = _Open(&file, B_READ_ONLY);
if (err != B_OK)
return err;
err = fMessage.Unflatten(&file);
if (err == B_OK)
fSaved = true;
return err;
}
status_t
LocaleSettings::Save()
{
BFile file;
status_t err;
err = _Open(&file, B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
if (err != B_OK)
return err;
err = fMessage.Flatten(&file);
if (err == B_OK)
fSaved = true;
return err;
}
status_t
LocaleSettings::_Open(BFile* file, int32 mode)
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return B_ERROR;
path.Append("Locale settings");
return file->SetTo(path.Path(), mode);
}
void
LocaleSettings::UpdateFrom(BMessage* message)
{
BString langName;
if (message->FindString("language", &langName) == B_OK) {
fMessage.RemoveName("language");
for (int i = 0;; i++) {
if (message->FindString("language", i, &langName) != B_OK)
break;
fMessage.AddString("language", langName);
}
}
if (message->FindString("country", &langName) == B_OK)
fMessage.ReplaceString("country", langName);
fSaved = false;
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2010, Adrien Destugues <pulkomandy@pulkomandy.ath.cx>.
* Distributed under the terms of the MIT License.
*/
#ifndef __LOCALE_SETTINGS_H__
#define __LOCALE_SETTINGS_H__
#include <File.h>
#include <Message.h>
class LocaleSettings {
public:
LocaleSettings();
status_t Load();
status_t Save();
bool Saved();
bool operator==(const LocaleSettings& other);
void UpdateFrom(BMessage* message);
/*
void SetPreferredLanguages();
void GetPreferredLanguages();
void SetCountry();
void GetCountry();
void SetDateFormat();
void GetDateFormat();
void SetTimeFormat();
void GetTimeFormat();
void SetNumberFormat();
void GetNumberFormat();
void SetMoneyFormat();
void GetMoneyFormat();
*/
private:
status_t _Open(BFile* file, int32 mode);
BMessage fMessage;
bool fSaved;
};
#endif

View File

@ -45,7 +45,6 @@ static const uint32 kMsgPreferredLanguageDragged = 'PLDr';
static const uint32 kMsgPreferredLanguageDeleted = 'PLDl';
static const uint32 kMsgCountrySelection = 'csel';
static const uint32 kMsgDefaults = 'dflt';
static const uint32 kMsgRevert = 'revt';
static const uint32 kMsgPreferredLanguagesChanged = 'lang';
@ -215,23 +214,23 @@ LocaleWindow::LocaleWindow()
BButton* button = new BButton(B_TRANSLATE("Defaults"),
new BMessage(kMsgDefaults));
#if 0
fRevertButton = new BButton(B_TRANSLATE("Revert"),
new BMessage(kMsgRevert));
fRevertButton->SetEnabled(false);
#endif
BLayoutBuilder::Group<>(this, B_VERTICAL, spacing)
.Add(tabView)
.AddGroup(B_HORIZONTAL, spacing)
.Add(button)
// .Add(fRevertButton)
.Add(fRevertButton)
.AddGlue()
.End()
.SetInsets(spacing, spacing, spacing, spacing)
.End();
_UpdatePreferredFromLocaleRoster();
SettingsReverted();
CenterOnScreen();
}
@ -250,7 +249,8 @@ LocaleWindow::MessageReceived(BMessage* message)
break;
case kMsgRevert:
// TODO
be_app_messenger.SendMessage(message);
_UpdatePreferredFromLocaleRoster();
break;
case kMsgLanguageDragged:
@ -345,6 +345,7 @@ LocaleWindow::MessageReceived(BMessage* message)
BMessage newMessage(kMsgSettingsChanged);
newMessage.AddString("country", item->ID());
be_app_messenger.SendMessage(&newMessage);
SettingsChanged();
BCountry* country = new BCountry(item->ID());
fFormatView->SetCountry(country);
@ -361,14 +362,24 @@ LocaleWindow::MessageReceived(BMessage* message)
bool
LocaleWindow::QuitRequested()
{
BMessage update(kMsgSettingsChanged);
update.AddPoint("window_location", Frame().LeftTop());
be_app_messenger.SendMessage(&update);
return true;
}
void
LocaleWindow::SettingsChanged()
{
fRevertButton->SetEnabled(true);
}
void
LocaleWindow::SettingsReverted()
{
fRevertButton->SetEnabled(false);
}
void
LocaleWindow::_PreferredLanguagesChanged()
{
@ -412,6 +423,8 @@ LocaleWindow::_EnableDisableLanguages()
}
}
SettingsChanged();
EnableUpdates();
}
@ -495,5 +508,7 @@ LocaleWindow::_Defaults()
update.AddString("language", "en");
be_app_messenger.SendMessage(&update);
SettingsChanged();
_UpdatePreferredFromLocaleRoster();
}

View File

@ -9,6 +9,9 @@
#include <Window.h>
static const uint32 kMsgRevert = 'revt';
class BButton;
class BListView;
class FormatView;
@ -24,6 +27,9 @@ public:
virtual void MessageReceived(BMessage* message);
virtual bool QuitRequested();
void SettingsChanged();
void SettingsReverted();
private:
void _PreferredLanguagesChanged();
void _EnableDisableLanguages();
@ -32,7 +38,6 @@ private:
int32 atIndex = -1);
void _Defaults();
private:
BButton* fRevertButton;
LanguageListView* fLanguageListView;
LanguageListView* fPreferredListView;