Make Bluetooth Pref use BMessage to save data.

* This will break previus ways to store settings (as it only stored a struct)
* Now we use BMessage to save data.
* Added some stuff to SettingsMessage.
* Fix a bug in BluetoothSettingsView::_GetClassForMenu() and SettingsMessage::SetValue

Change-Id: I6a0fa1564e78460258f480947592eb4007985007
Reviewed-on: https://review.haiku-os.org/c/haiku/+/3887
Reviewed-by: Adrien Destugues <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Fredrik Modéen
2021-06-02 13:44:13 +00:00
parent 7fca70f67e
commit 329ae20bac
7 changed files with 143 additions and 69 deletions
+5
View File
@@ -63,6 +63,8 @@ public:
const BFlattenable* value); const BFlattenable* value);
status_t SetValue(const char* name, status_t SetValue(const char* name,
const BFont& value); const BFont& value);
status_t SetValue(const char* name, type_code type,
const void* data, ssize_t numBytes);
bool GetValue(const char* name, bool GetValue(const char* name,
bool defaultValue) const; bool defaultValue) const;
@@ -98,6 +100,9 @@ public:
const BMessage& defaultValue) const; const BMessage& defaultValue) const;
BFont GetValue(const char* name, BFont GetValue(const char* name,
const BFont& defaultValue) const; const BFont& defaultValue) const;
void* GetValue(const char* name, type_code type,
ssize_t numBytes,
const void** defaultValue)const;
private: private:
void _NotifyValueChanged(const char* name) const; void _NotifyValueChanged(const char* name) const;
+24 -2
View File
@@ -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 status_t
SettingsMessage::SetValue(const char* name, const BFont& value) 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 void
SettingsMessage::_NotifyValueChanged(const char* name) const SettingsMessage::_NotifyValueChanged(const char* name) const
{ {
@@ -564,4 +587,3 @@ SettingsMessage::_NotifyValueChanged(const char* name) const
listener->SendMessage(&message); listener->SendMessage(&message);
} }
} }
+61 -27
View File
@@ -7,52 +7,86 @@
* Authors: * Authors:
* Fredrik Modéen <fredrik_at_modeen.se> * Fredrik Modéen <fredrik_at_modeen.se>
*/ */
#include "BluetoothSettings.h" #include "BluetoothSettings.h"
#include <SettingsMessage.h>
BluetoothSettings::BluetoothSettings() BluetoothSettings::BluetoothSettings()
:
fSettingsMessage(B_USER_SETTINGS_DIRECTORY, "Bluetooth_settings")
{ {
find_directory(B_USER_SETTINGS_DIRECTORY, &fPath); fCurrentSettings.pickeddevice = bdaddrUtils::NullAddress();
fPath.Append("Bluetooth_settings", true); fCurrentSettings.localdeviceclass = DeviceClass();
} fCurrentSettings.policy = 0;
fCurrentSettings.inquirytime = 15;
BluetoothSettings::~BluetoothSettings()
{
} }
void void
BluetoothSettings::Defaults() BluetoothSettings::SetPickedDevice(bdaddr_t pickeddevice)
{ {
Data.PickedDevice = bdaddrUtils::NullAddress(); fCurrentSettings.pickeddevice = pickeddevice;
Data.LocalDeviceClass = DeviceClass();
Data.Policy = 0;
Data.InquiryTime = 15;
} }
void void
BluetoothSettings::Load() BluetoothSettings::SetLocalDeviceClass(DeviceClass localdeviceclass)
{ {
fFile = new BFile(fPath.Path(), B_READ_ONLY); fCurrentSettings.localdeviceclass = localdeviceclass;
if (fFile->InitCheck() == B_OK) {
fFile->Read(&Data, sizeof(Data));
} else
Defaults();
delete fFile;
} }
void void
BluetoothSettings::Save() BluetoothSettings::SetPolicy(int32 policy)
{ {
fFile = new BFile(fPath.Path(), B_WRITE_ONLY | B_CREATE_FILE); fCurrentSettings.policy = policy;
if (fFile->InitCheck() == B_OK) {
fFile->Write(&Data, sizeof(Data));
} }
delete fFile;
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();
} }
+28 -13
View File
@@ -17,27 +17,42 @@
#include <File.h> #include <File.h>
#include <FindDirectory.h> #include <FindDirectory.h>
#include <Path.h> #include <Path.h>
#include <SettingsMessage.h>
class BluetoothSettings class BluetoothSettings
{ {
public: public:
struct {
bdaddr_t PickedDevice;
DeviceClass LocalDeviceClass;
int32 Policy;
int32 InquiryTime;
} Data;
BluetoothSettings(); BluetoothSettings();
~BluetoothSettings();
void Defaults(); bdaddr_t PickedDevice() const
void Load(); { return fCurrentSettings.pickeddevice; }
void Save(); 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: private:
BPath fPath; struct BTSetting {
BFile* fFile; bdaddr_t pickeddevice;
DeviceClass localdeviceclass;
int32 policy;
int32 inquirytime;
};
SettingsMessage fSettingsMessage;
BTSetting fCurrentSettings;
}; };
#endif // BLUETOOTH_SETTINGS_H #endif // BLUETOOTH_SETTINGS_H
@@ -53,7 +53,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
BView(name, 0), BView(name, 0),
fLocalDevicesMenu(NULL) fLocalDevicesMenu(NULL)
{ {
fSettings.Load(); fSettings.LoadSettings();
fPolicyMenu = new BOptionPopUp("policy", fPolicyMenu = new BOptionPopUp("policy",
B_TRANSLATE("Incoming connections 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(kTrustedLabel), 2);
fPolicyMenu->AddOption(B_TRANSLATE_NOCOLLECT(kAlwaysLabel), 3); fPolicyMenu->AddOption(B_TRANSLATE_NOCOLLECT(kAlwaysLabel), 3);
fPolicyMenu->SetValue(fSettings.Data.Policy); fPolicyMenu->SetValue(fSettings.Policy());
BString label(B_TRANSLATE("Default inquiry time:")); BString label(B_TRANSLATE("Default inquiry time:"));
label << " " << fSettings.Data.InquiryTime; label << " " << fSettings.InquiryTime();
fInquiryTimeControl = new BSlider("time", label.String() fInquiryTimeControl = new BSlider("time", label.String()
, new BMessage(kMsgSetInquiryTime), 15, 61, B_HORIZONTAL); , new BMessage(kMsgSetInquiryTime), 15, 61, B_HORIZONTAL);
fInquiryTimeControl->SetLimitLabels(B_TRANSLATE("15 secs"), fInquiryTimeControl->SetLimitLabels(B_TRANSLATE("15 secs"),
@@ -73,7 +73,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
fInquiryTimeControl->SetHashMarks(B_HASH_MARKS_BOTTOM); fInquiryTimeControl->SetHashMarks(B_HASH_MARKS_BOTTOM);
fInquiryTimeControl->SetHashMarkCount(20); fInquiryTimeControl->SetHashMarkCount(20);
fInquiryTimeControl->SetEnabled(true); fInquiryTimeControl->SetEnabled(true);
fInquiryTimeControl->SetValue(fSettings.Data.InquiryTime); fInquiryTimeControl->SetValue(fSettings.InquiryTime());
fExtDeviceView = new ExtendedLocalDeviceView(NULL); fExtDeviceView = new ExtendedLocalDeviceView(NULL);
@@ -88,9 +88,8 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
fExtDeviceView->SetEnabled(true); fExtDeviceView->SetEnabled(true);
DeviceClass rememberedClass = ActiveLocalDevice->GetDeviceClass(); DeviceClass rememberedClass = ActiveLocalDevice->GetDeviceClass();
if (!rememberedClass.IsUnknownDeviceClass()) if (!rememberedClass.IsUnknownDeviceClass())
fSettings.Data.LocalDeviceClass = rememberedClass; fSettings.SetLocalDeviceClass(rememberedClass);
} }
fClassMenu = new BOptionPopUp("DeviceClass", B_TRANSLATE("Identify host as:"), fClassMenu = new BOptionPopUp("DeviceClass", B_TRANSLATE("Identify host as:"),
@@ -120,7 +119,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
BluetoothSettingsView::~BluetoothSettingsView() BluetoothSettingsView::~BluetoothSettingsView()
{ {
fSettings.Save(); fSettings.SaveSettings();
} }
@@ -160,14 +159,14 @@ BluetoothSettingsView::MessageReceived(BMessage* message)
{ {
int32 policy; int32 policy;
if (message->FindInt32("be:value", (int32*)&policy) == B_OK) { if (message->FindInt32("be:value", (int32*)&policy) == B_OK) {
fSettings.Data.Policy = policy; fSettings.SetPolicy(policy);
} }
break; break;
} }
case kMsgSetInquiryTime: case kMsgSetInquiryTime:
{ {
fSettings.Data.InquiryTime = fInquiryTimeControl->Value(); fSettings.SetInquiryTime(fInquiryTimeControl->Value());
BString label(B_TRANSLATE("Default inquiry time:")); BString label(B_TRANSLATE("Default inquiry time:"));
label << " " << fInquiryTimeControl->Value(); label << " " << fInquiryTimeControl->Value();
fInquiryTimeControl->SetLabel(label.String()); fInquiryTimeControl->SetLabel(label.String());
@@ -208,10 +207,10 @@ BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor,
{ {
bool haveRun = true; bool haveRun = true;
fSettings.Data.LocalDeviceClass.SetRecord(major, minor, service); fSettings.SetLocalDeviceClass(DeviceClass(major, minor, service));
if (ActiveLocalDevice != NULL) if (ActiveLocalDevice != NULL)
ActiveLocalDevice->SetDeviceClass(fSettings.Data.LocalDeviceClass); ActiveLocalDevice->SetDeviceClass(fSettings.LocalDeviceClass());
else else
haveRun = false; haveRun = false;
@@ -249,7 +248,7 @@ BluetoothSettingsView::_BuildLocalDevicesMenu()
(lDevice->GetFriendlyName().String()), message); (lDevice->GetFriendlyName().String()), message);
if (bdaddrUtils::Compare(lDevice->GetBluetoothAddress(), if (bdaddrUtils::Compare(lDevice->GetBluetoothAddress(),
fSettings.Data.PickedDevice)) { fSettings.PickedDevice())) {
item->SetMarked(true); item->SetMarked(true);
ActiveLocalDevice = lDevice; ActiveLocalDevice = lDevice;
@@ -268,24 +267,23 @@ BluetoothSettingsView::_MarkLocalDevice(LocalDevice* lDevice)
fExtDeviceView->SetLocalDevice(lDevice); fExtDeviceView->SetLocalDevice(lDevice);
fExtDeviceView->SetEnabled(true); fExtDeviceView->SetEnabled(true);
ActiveLocalDevice = lDevice; ActiveLocalDevice = lDevice;
fSettings.Data.PickedDevice = lDevice->GetBluetoothAddress(); fSettings.SetPickedDevice(lDevice->GetBluetoothAddress());
} }
int int
BluetoothSettingsView::_GetClassForMenu() BluetoothSettingsView::_GetClassForMenu()
{ {
int deviceClass = fSettings.Data.LocalDeviceClass.MajorDeviceClass() + int deviceClass =
fSettings.Data.LocalDeviceClass.MinorDeviceClass()-1; fSettings.LocalDeviceClass().MajorDeviceClass()+
fSettings.LocalDeviceClass().MinorDeviceClass();
// As of now we only support MajorDeviceClass = 1 and MinorDeviceClass 1-4 // As of now we only support MajorDeviceClass = 1 and MinorDeviceClass 1-4
// and MajorDeviceClass = 2 and MinorDeviceClass 3. // and MajorDeviceClass = 2 and MinorDeviceClass 3.
if ((fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 1 if (fSettings.LocalDeviceClass().MajorDeviceClass() == 1
&& (fSettings.Data.LocalDeviceClass.MinorDeviceClass() > 0 && (fSettings.LocalDeviceClass().MinorDeviceClass() > 0
&& fSettings.Data.LocalDeviceClass.MinorDeviceClass() < 5)) && fSettings.LocalDeviceClass().MinorDeviceClass() < 5))
|| (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 2 && deviceClass -= 1;
fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 3))
return deviceClass; //No other wil have the same number. return deviceClass;
else
return 0;
} }
+1 -1
View File
@@ -15,7 +15,7 @@ Preference Bluetooth :
ExtendedLocalDeviceView.cpp ExtendedLocalDeviceView.cpp
InquiryPanel.cpp InquiryPanel.cpp
RemoteDevicesView.cpp RemoteDevicesView.cpp
: be libbluetooth.so [ TargetLibsupc++ ] localestub : be shared libbluetooth.so [ TargetLibsupc++ ] localestub
; ;
DoCatalogs Bluetooth : DoCatalogs Bluetooth :