Further flesh out GUITeamUISettings to be able to save/restore. This should be
more or less complete now, or at least far enough along to start doing some things with it. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@43112 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
parent
df42e66b1b
commit
9b15fe0a81
|
@ -23,12 +23,14 @@ GUITeamUISettings::GUITeamUISettings(const char* settingsID)
|
|||
|
||||
GUITeamUISettings::GUITeamUISettings(const GUITeamUISettings& other)
|
||||
{
|
||||
fID = other.fID;
|
||||
if (_SetTo(other) != B_OK)
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
|
||||
|
||||
GUITeamUISettings::~GUITeamUISettings()
|
||||
{
|
||||
_Unset();
|
||||
}
|
||||
|
||||
|
||||
|
@ -50,6 +52,10 @@ status_t
|
|||
GUITeamUISettings::SetTo(const BMessage& archive)
|
||||
{
|
||||
status_t error = archive.FindString("ID", &fID);
|
||||
if (error != B_OK)
|
||||
return error;
|
||||
|
||||
error = archive.FindMessage("values", &fValues);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
@ -63,6 +69,10 @@ GUITeamUISettings::WriteTo(BMessage& archive) const
|
|||
return error;
|
||||
|
||||
error = archive.AddInt32("type", Type());
|
||||
if (error != B_OK)
|
||||
return error;
|
||||
|
||||
error = archive.AddMessage("values", &fValues);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
@ -71,15 +81,110 @@ GUITeamUISettings::WriteTo(BMessage& archive) const
|
|||
TeamUISettings*
|
||||
GUITeamUISettings::Clone() const
|
||||
{
|
||||
GUITeamUISettings* settings = new GUITeamUISettings(fID.String());
|
||||
GUITeamUISettings* settings = new(std::nothrow) GUITeamUISettings();
|
||||
|
||||
if (settings == NULL)
|
||||
return NULL;
|
||||
|
||||
if (settings->_SetTo(*this) != B_OK) {
|
||||
delete settings;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
GUITeamUISettings::AddSetting(Setting* setting)
|
||||
{
|
||||
if (!fSettings.AddItem(setting))
|
||||
return B_NO_MEMORY;
|
||||
|
||||
setting->AcquireReference();
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
bool
|
||||
GUITeamUISettings::SetValue(const Setting* setting, const BVariant& value)
|
||||
{
|
||||
const char* fieldName = setting->ID();
|
||||
fValues.RemoveName(fieldName);
|
||||
|
||||
return value.AddToMessage(fValues, fieldName) == B_OK;
|
||||
}
|
||||
|
||||
|
||||
BVariant
|
||||
GUITeamUISettings::Value(const Setting* setting) const
|
||||
{
|
||||
BVariant value;
|
||||
|
||||
return value.SetFromMessage(fValues, setting->ID()) == B_OK ?
|
||||
value : setting->DefaultValue();
|
||||
}
|
||||
|
||||
|
||||
BVariant
|
||||
GUITeamUISettings::Value(const char* settingID) const
|
||||
{
|
||||
BVariant value;
|
||||
|
||||
status_t result = value.SetFromMessage(fValues, settingID);
|
||||
|
||||
if (result != B_OK) {
|
||||
for (int32 i = 0; i < fSettings.CountItems(); i++) {
|
||||
Setting* setting = fSettings.ItemAt(i);
|
||||
if (strcmp(setting->ID(), settingID) == 0) {
|
||||
value = setting->DefaultValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
GUITeamUISettings&
|
||||
GUITeamUISettings::operator=(const GUITeamUISettings& other)
|
||||
{
|
||||
fID = other.fID;
|
||||
if (_SetTo(other) != B_OK)
|
||||
throw std::bad_alloc();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
status_t
|
||||
GUITeamUISettings::_SetTo(const GUITeamUISettings& other)
|
||||
{
|
||||
_Unset();
|
||||
|
||||
for (int32 i = 0; i < other.fSettings.CountItems(); i++) {
|
||||
Setting* setting = other.fSettings.ItemAt(i);
|
||||
if (!fSettings.AddItem(setting))
|
||||
return B_NO_MEMORY;
|
||||
|
||||
setting->AcquireReference();
|
||||
}
|
||||
|
||||
fValues = other.fValues;
|
||||
|
||||
return B_OK;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
GUITeamUISettings::_Unset()
|
||||
{
|
||||
fID.Truncate(0);
|
||||
|
||||
for (int32 i = 0; i < fSettings.CountItems(); i++)
|
||||
fSettings.ItemAt(i)->ReleaseReference();
|
||||
|
||||
fSettings.MakeEmpty();
|
||||
fValues.MakeEmpty();
|
||||
}
|
||||
|
|
|
@ -6,14 +6,14 @@
|
|||
#define GUI_TEAM_UI_SETTINGS_H
|
||||
|
||||
|
||||
#include <Message.h>
|
||||
#include <String.h>
|
||||
|
||||
#include <ObjectList.h>
|
||||
|
||||
#include "Setting.h"
|
||||
#include "TeamUISettings.h"
|
||||
|
||||
class BMessage;
|
||||
|
||||
|
||||
class GUITeamUISettings : public TeamUISettings {
|
||||
public:
|
||||
|
@ -30,11 +30,24 @@ public:
|
|||
virtual status_t WriteTo(BMessage& archive) const;
|
||||
virtual TeamUISettings* Clone() const;
|
||||
|
||||
status_t AddSetting(Setting* setting);
|
||||
bool SetValue(const Setting* setting,
|
||||
const BVariant& value);
|
||||
BVariant Value(const Setting* setting) const;
|
||||
BVariant Value(const char* settingID) const;
|
||||
|
||||
GUITeamUISettings& operator=(const GUITeamUISettings& other);
|
||||
// throws std::bad_alloc
|
||||
|
||||
private:
|
||||
typedef BObjectList<Setting> SettingsList;
|
||||
private:
|
||||
|
||||
status_t _SetTo(const GUITeamUISettings& other);
|
||||
void _Unset();
|
||||
|
||||
SettingsList fSettings;
|
||||
BMessage fValues;
|
||||
BString fID;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue