diff --git a/headers/private/shared/SettingsMessage.h b/headers/private/shared/SettingsMessage.h index a69f21148b..356908711e 100644 --- a/headers/private/shared/SettingsMessage.h +++ b/headers/private/shared/SettingsMessage.h @@ -63,6 +63,8 @@ public: const BFlattenable* value); status_t SetValue(const char* name, const BFont& value); + status_t SetValue(const char* name, type_code type, + const void* data, ssize_t numBytes); bool GetValue(const char* name, bool defaultValue) const; @@ -98,6 +100,9 @@ public: const BMessage& defaultValue) const; BFont GetValue(const char* name, const BFont& defaultValue) const; + void* GetValue(const char* name, type_code type, + ssize_t numBytes, + const void** defaultValue)const; private: void _NotifyValueChanged(const char* name) const; diff --git a/src/kits/shared/SettingsMessage.cpp b/src/kits/shared/SettingsMessage.cpp index 4d062457c1..d520f8873b 100644 --- a/src/kits/shared/SettingsMessage.cpp +++ b/src/kits/shared/SettingsMessage.cpp @@ -322,6 +322,19 @@ SettingsMessage::SetValue(const char* name, const BFlattenable* value) } +status_t +SettingsMessage::SetValue(const char* name, type_code type, const void* data, + ssize_t numBytes) +{ + status_t ret = ReplaceData(name, type, data, numBytes); + if (ret != B_OK) + ret = AddData(name, type, data, numBytes); + if (ret == B_OK) + _NotifyValueChanged(name); + return ret; +} + + status_t SettingsMessage::SetValue(const char* name, const BFont& value) { @@ -539,9 +552,19 @@ SettingsMessage::GetValue(const char* name, const BFont& defaultValue) const } -// #pragma mark - private +void* +SettingsMessage::GetValue(const char* name, type_code type, ssize_t numBytes, + const void** defaultValue) const +{ + void* value; + if (FindData(name, type, (const void**)&value, &numBytes) != B_OK) + return defaultValue; + return value; +} +// #pragma mark - private + void SettingsMessage::_NotifyValueChanged(const char* name) const { @@ -564,4 +587,3 @@ SettingsMessage::_NotifyValueChanged(const char* name) const listener->SendMessage(&message); } } - diff --git a/src/preferences/bluetooth/BluetoothSettings.cpp b/src/preferences/bluetooth/BluetoothSettings.cpp index d31d6da4b3..9be30ca138 100644 --- a/src/preferences/bluetooth/BluetoothSettings.cpp +++ b/src/preferences/bluetooth/BluetoothSettings.cpp @@ -7,52 +7,86 @@ * Authors: * Fredrik Modéen */ + + #include "BluetoothSettings.h" +#include + + BluetoothSettings::BluetoothSettings() + : + fSettingsMessage(B_USER_SETTINGS_DIRECTORY, "Bluetooth_settings") { - find_directory(B_USER_SETTINGS_DIRECTORY, &fPath); - fPath.Append("Bluetooth_settings", true); -} - - -BluetoothSettings::~BluetoothSettings() -{ + fCurrentSettings.pickeddevice = bdaddrUtils::NullAddress(); + fCurrentSettings.localdeviceclass = DeviceClass(); + fCurrentSettings.policy = 0; + fCurrentSettings.inquirytime = 15; } void -BluetoothSettings::Defaults() +BluetoothSettings::SetPickedDevice(bdaddr_t pickeddevice) { - Data.PickedDevice = bdaddrUtils::NullAddress(); - Data.LocalDeviceClass = DeviceClass(); - Data.Policy = 0; - Data.InquiryTime = 15; + fCurrentSettings.pickeddevice = pickeddevice; } void -BluetoothSettings::Load() +BluetoothSettings::SetLocalDeviceClass(DeviceClass localdeviceclass) { - fFile = new BFile(fPath.Path(), B_READ_ONLY); - - if (fFile->InitCheck() == B_OK) { - fFile->Read(&Data, sizeof(Data)); - } else - Defaults(); - - delete fFile; + fCurrentSettings.localdeviceclass = localdeviceclass; } void -BluetoothSettings::Save() +BluetoothSettings::SetPolicy(int32 policy) { - fFile = new BFile(fPath.Path(), B_WRITE_ONLY | B_CREATE_FILE); - - if (fFile->InitCheck() == B_OK) { - fFile->Write(&Data, sizeof(Data)); - } - - delete fFile; + fCurrentSettings.policy = policy; +} + + +void +BluetoothSettings::SetInquiryTime(int32 inquirytime) +{ + fCurrentSettings.inquirytime = inquirytime; +} + + +void +BluetoothSettings::LoadSettings() +{ + bdaddr_t* addr; + ssize_t size; + status_t status = fSettingsMessage.FindData("BDAddress", B_RAW_TYPE, + (const void**)&addr, &size); + if (status == B_OK) + SetPickedDevice(*addr); + else + SetPickedDevice(bdaddrUtils::NullAddress()); + + DeviceClass* devclass; + status = fSettingsMessage.FindData("DeviceClass", B_RAW_TYPE, + (const void**)&devclass, &size); + if (status == B_OK) + SetLocalDeviceClass(*devclass); + else + SetLocalDeviceClass(DeviceClass()); + + SetPolicy(fSettingsMessage.GetValue("Policy", (int32)0)); + SetInquiryTime(fSettingsMessage.GetValue("InquiryTime", (int32)15)); +} + + +void +BluetoothSettings::SaveSettings() +{ + fSettingsMessage.SetValue("DeviceClass", B_RAW_TYPE, + &fCurrentSettings.localdeviceclass, sizeof(DeviceClass)); + fSettingsMessage.SetValue("BDAddress", B_RAW_TYPE, &fCurrentSettings.pickeddevice, + sizeof(bdaddr_t)); + fSettingsMessage.SetValue("Policy", fCurrentSettings.policy); + fSettingsMessage.SetValue("InquiryTime", fCurrentSettings.inquirytime); + + fSettingsMessage.Save(); } diff --git a/src/preferences/bluetooth/BluetoothSettings.h b/src/preferences/bluetooth/BluetoothSettings.h index e13d489baa..3dc962fbe8 100644 --- a/src/preferences/bluetooth/BluetoothSettings.h +++ b/src/preferences/bluetooth/BluetoothSettings.h @@ -17,27 +17,42 @@ #include #include #include +#include + class BluetoothSettings { public: - struct { - bdaddr_t PickedDevice; - DeviceClass LocalDeviceClass; - int32 Policy; - int32 InquiryTime; - } Data; - BluetoothSettings(); - ~BluetoothSettings(); - void Defaults(); - void Load(); - void Save(); + bdaddr_t PickedDevice() const + { return fCurrentSettings.pickeddevice; } + DeviceClass LocalDeviceClass() const + { return fCurrentSettings.localdeviceclass; } + int32 Policy() const + { return fCurrentSettings.policy; } + int32 InquiryTime() const + { return fCurrentSettings.inquirytime; } + + void SetPickedDevice(bdaddr_t pickeddevice); + void SetLocalDeviceClass(DeviceClass localdeviceclass); + void SetPolicy(int32 policy); + void SetInquiryTime(int32 inquirytime); + + void LoadSettings(); + void SaveSettings(); private: - BPath fPath; - BFile* fFile; + struct BTSetting { + bdaddr_t pickeddevice; + DeviceClass localdeviceclass; + int32 policy; + int32 inquirytime; + }; + + SettingsMessage fSettingsMessage; + + BTSetting fCurrentSettings; }; #endif // BLUETOOTH_SETTINGS_H diff --git a/src/preferences/bluetooth/BluetoothSettingsView.cpp b/src/preferences/bluetooth/BluetoothSettingsView.cpp index cabde7755e..0b4d2e4dad 100644 --- a/src/preferences/bluetooth/BluetoothSettingsView.cpp +++ b/src/preferences/bluetooth/BluetoothSettingsView.cpp @@ -53,7 +53,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) BView(name, 0), fLocalDevicesMenu(NULL) { - fSettings.Load(); + fSettings.LoadSettings(); fPolicyMenu = new BOptionPopUp("policy", B_TRANSLATE("Incoming connections policy:"), @@ -62,10 +62,10 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) fPolicyMenu->AddOption(B_TRANSLATE_NOCOLLECT(kTrustedLabel), 2); fPolicyMenu->AddOption(B_TRANSLATE_NOCOLLECT(kAlwaysLabel), 3); - fPolicyMenu->SetValue(fSettings.Data.Policy); + fPolicyMenu->SetValue(fSettings.Policy()); BString label(B_TRANSLATE("Default inquiry time:")); - label << " " << fSettings.Data.InquiryTime; + label << " " << fSettings.InquiryTime(); fInquiryTimeControl = new BSlider("time", label.String() , new BMessage(kMsgSetInquiryTime), 15, 61, B_HORIZONTAL); fInquiryTimeControl->SetLimitLabels(B_TRANSLATE("15 secs"), @@ -73,7 +73,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) fInquiryTimeControl->SetHashMarks(B_HASH_MARKS_BOTTOM); fInquiryTimeControl->SetHashMarkCount(20); fInquiryTimeControl->SetEnabled(true); - fInquiryTimeControl->SetValue(fSettings.Data.InquiryTime); + fInquiryTimeControl->SetValue(fSettings.InquiryTime()); fExtDeviceView = new ExtendedLocalDeviceView(NULL); @@ -88,9 +88,8 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) fExtDeviceView->SetEnabled(true); DeviceClass rememberedClass = ActiveLocalDevice->GetDeviceClass(); - if (!rememberedClass.IsUnknownDeviceClass()) - fSettings.Data.LocalDeviceClass = rememberedClass; + fSettings.SetLocalDeviceClass(rememberedClass); } fClassMenu = new BOptionPopUp("DeviceClass", B_TRANSLATE("Identify host as:"), @@ -120,7 +119,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) BluetoothSettingsView::~BluetoothSettingsView() { - fSettings.Save(); + fSettings.SaveSettings(); } @@ -160,14 +159,14 @@ BluetoothSettingsView::MessageReceived(BMessage* message) { int32 policy; if (message->FindInt32("be:value", (int32*)&policy) == B_OK) { - fSettings.Data.Policy = policy; + fSettings.SetPolicy(policy); } break; } case kMsgSetInquiryTime: { - fSettings.Data.InquiryTime = fInquiryTimeControl->Value(); + fSettings.SetInquiryTime(fInquiryTimeControl->Value()); BString label(B_TRANSLATE("Default inquiry time:")); label << " " << fInquiryTimeControl->Value(); fInquiryTimeControl->SetLabel(label.String()); @@ -208,10 +207,10 @@ BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor, { bool haveRun = true; - fSettings.Data.LocalDeviceClass.SetRecord(major, minor, service); + fSettings.SetLocalDeviceClass(DeviceClass(major, minor, service)); if (ActiveLocalDevice != NULL) - ActiveLocalDevice->SetDeviceClass(fSettings.Data.LocalDeviceClass); + ActiveLocalDevice->SetDeviceClass(fSettings.LocalDeviceClass()); else haveRun = false; @@ -249,7 +248,7 @@ BluetoothSettingsView::_BuildLocalDevicesMenu() (lDevice->GetFriendlyName().String()), message); if (bdaddrUtils::Compare(lDevice->GetBluetoothAddress(), - fSettings.Data.PickedDevice)) { + fSettings.PickedDevice())) { item->SetMarked(true); ActiveLocalDevice = lDevice; @@ -268,24 +267,23 @@ BluetoothSettingsView::_MarkLocalDevice(LocalDevice* lDevice) fExtDeviceView->SetLocalDevice(lDevice); fExtDeviceView->SetEnabled(true); ActiveLocalDevice = lDevice; - fSettings.Data.PickedDevice = lDevice->GetBluetoothAddress(); + fSettings.SetPickedDevice(lDevice->GetBluetoothAddress()); } int BluetoothSettingsView::_GetClassForMenu() { - int deviceClass = fSettings.Data.LocalDeviceClass.MajorDeviceClass() + - fSettings.Data.LocalDeviceClass.MinorDeviceClass()-1; + int deviceClass = + fSettings.LocalDeviceClass().MajorDeviceClass()+ + fSettings.LocalDeviceClass().MinorDeviceClass(); // As of now we only support MajorDeviceClass = 1 and MinorDeviceClass 1-4 // and MajorDeviceClass = 2 and MinorDeviceClass 3. - if ((fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 1 - && (fSettings.Data.LocalDeviceClass.MinorDeviceClass() > 0 - && fSettings.Data.LocalDeviceClass.MinorDeviceClass() < 5)) - || (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 2 && - fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 3)) - return deviceClass; //No other wil have the same number. - else - return 0; + if (fSettings.LocalDeviceClass().MajorDeviceClass() == 1 + && (fSettings.LocalDeviceClass().MinorDeviceClass() > 0 + && fSettings.LocalDeviceClass().MinorDeviceClass() < 5)) + deviceClass -= 1; + + return deviceClass; } diff --git a/src/preferences/bluetooth/BluetoothSettingsView.h b/src/preferences/bluetooth/BluetoothSettingsView.h index 2c6f02eb7f..19844e7cc6 100644 --- a/src/preferences/bluetooth/BluetoothSettingsView.h +++ b/src/preferences/bluetooth/BluetoothSettingsView.h @@ -43,7 +43,7 @@ protected: float fDivider; BOptionPopUp* fPolicyMenu; - BOptionPopUp* fClassMenu; + BOptionPopUp* fClassMenu; BMenuField* fLocalDevicesMenuField; BPopUpMenu* fLocalDevicesMenu; diff --git a/src/preferences/bluetooth/Jamfile b/src/preferences/bluetooth/Jamfile index bd86b896e5..d595e28ce2 100644 --- a/src/preferences/bluetooth/Jamfile +++ b/src/preferences/bluetooth/Jamfile @@ -15,7 +15,7 @@ Preference Bluetooth : ExtendedLocalDeviceView.cpp InquiryPanel.cpp RemoteDevicesView.cpp - : be libbluetooth.so [ TargetLibsupc++ ] localestub + : be shared libbluetooth.so [ TargetLibsupc++ ] localestub ; DoCatalogs Bluetooth :