#include "LiveSetting.h"
void
LiveSetting::Init(uint32 id, const char *name, const LiveSettingType dataType)
{
fId = id;
fName = name;
fDataType = dataType;
}
LiveSetting::LiveSetting(uint32 id, const char *name, bool val)
{
Init(id, name, LIVE_SETTING_BOOL);
fBoolVal = val;
}
LiveSetting::LiveSetting(uint32 id, const char *name, int32 val)
{
Init(id, name, LIVE_SETTING_INT32);
fInt32Val = val;
}
LiveSetting::LiveSetting(uint32 id, const char *name, const char *val)
{
Init(id, name, LIVE_SETTING_STRING);
fCharpVal = val;
}
bool
LiveSetting::AddReplaceValue(BMessage *msgTo, BMessage *msgFrom ) const
{
bool bResult = false;
switch (fDataType) {
case LIVE_SETTING_BOOL:
{
bool bVal;
if (GetValue(msgFrom, bVal) == true)
return AddReplaceValue(msgTo, bVal);
}
case LIVE_SETTING_INT32:
{
int32 iVal;
if (GetValue(msgFrom, iVal) == true)
return AddReplaceValue(msgTo, iVal);
}
case LIVE_SETTING_STRING:
{
BString str;
if (GetValue(msgFrom, str) == true)
return AddReplaceValue(msgTo, str);
}
default:
break;
}
return bResult;
}
bool
LiveSetting::AddReplaceValue(BMessage *msgTo, bool val) const
{
if (msgTo == NULL)
return false;
bool bResult = false;
bool dummy;
if (msgTo->FindBool(fName, &dummy) == B_OK) {
if (msgTo->ReplaceBool(fName, val) == B_OK)
bResult = true;
} else {
if (msgTo->AddBool(fName, val) == B_OK)
bResult = true;
}
return bResult;
}
bool
LiveSetting::AddReplaceValue(BMessage *msgTo, int32 val) const
{
if (msgTo == NULL)
return false;
bool bResult = false;
int32 dummy;
if (msgTo->FindInt32(fName, &dummy) == B_OK) {
if (msgTo->ReplaceInt32(fName, val) == B_OK)
bResult = true;
} else {
if (msgTo->AddInt32(fName, val) == B_OK)
bResult = true;
}
return bResult;
}
bool
LiveSetting::AddReplaceValue(BMessage *msgTo, const BString &val) const
{
if (msgTo == NULL)
return false;
bool bResult = false;
BString dummy;
if (msgTo->FindString(fName, &dummy) == B_OK) {
if (msgTo->ReplaceString(fName, val) == B_OK)
bResult = true;
} else {
if (msgTo->AddString(fName, val) == B_OK)
bResult = true;
}
return bResult;
}
bool
LiveSetting::GetValue(BMessage *msgFrom, bool &val) const
{
if (fDataType != LIVE_SETTING_BOOL)
return false;
bool bResult = false;
if (msgFrom == NULL) {
val = fBoolVal;
bResult = true;
} else if (msgFrom->FindBool(fName, &val) == B_OK)
bResult = true;
return bResult;
}
bool
LiveSetting::GetValue(BMessage *msgFrom, int32 &val) const
{
if (fDataType != LIVE_SETTING_INT32)
return false;
bool bResult = false;
if (msgFrom == NULL) {
val = fInt32Val;
bResult = true;
} else if (msgFrom->FindInt32(fName, &val) == B_OK)
bResult = true;
return bResult;
}
bool
LiveSetting::GetValue(BMessage *msgFrom, BString &val) const
{
if (fDataType != LIVE_SETTING_STRING)
return false;
bool bResult = false;
if (msgFrom == NULL) {
val = fCharpVal;
bResult = true;
} else if (msgFrom->FindString(fName, &val) == B_OK)
bResult = true;
return bResult;
}