From e381b02559509041a056831116044e5cc1b0b3e4 Mon Sep 17 00:00:00 2001 From: Tri-Edge AI Date: Sun, 16 Dec 2012 19:11:54 +0200 Subject: [PATCH] Bluetooth: Added settings to remember last used device. Signed-off-by: Matt Madia --- src/preferences/bluetooth/BluetoothMain.cpp | 2 +- .../bluetooth/BluetoothSettings.cpp | 56 ++++++ src/preferences/bluetooth/BluetoothSettings.h | 37 ++++ .../bluetooth/BluetoothSettingsView.cpp | 160 +++++++++++++----- .../bluetooth/BluetoothSettingsView.h | 51 +++--- src/preferences/bluetooth/BluetoothWindow.cpp | 1 - src/preferences/bluetooth/BluetoothWindow.h | 27 ++- .../bluetooth/ExtendedLocalDeviceView.cpp | 10 +- src/preferences/bluetooth/Jamfile | 1 + 9 files changed, 257 insertions(+), 88 deletions(-) create mode 100644 src/preferences/bluetooth/BluetoothSettings.cpp create mode 100644 src/preferences/bluetooth/BluetoothSettings.h diff --git a/src/preferences/bluetooth/BluetoothMain.cpp b/src/preferences/bluetooth/BluetoothMain.cpp index 0b71630b3f..3451ecb656 100644 --- a/src/preferences/bluetooth/BluetoothMain.cpp +++ b/src/preferences/bluetooth/BluetoothMain.cpp @@ -77,7 +77,7 @@ BluetoothApplication::MessageReceived(BMessage* message) BMessageRunner::StartSending(be_app_messenger, new BMessage('Xtmp'), 2 * 1000000, 1); } else { - fWindow = new BluetoothWindow(BRect(100, 100, 550, 420)); + fWindow = new BluetoothWindow(BRect(100, 100, 750, 420)); fWindow->Show(); } break; diff --git a/src/preferences/bluetooth/BluetoothSettings.cpp b/src/preferences/bluetooth/BluetoothSettings.cpp new file mode 100644 index 0000000000..ad6680ca59 --- /dev/null +++ b/src/preferences/bluetooth/BluetoothSettings.cpp @@ -0,0 +1,56 @@ +/* + * Copyright 2008-2009, Oliver Ruiz Dorantes, + * Copyright 2012-2013, Tri-Edge AI + * + * All rights reserved. Distributed under the terms of the MIT license. + */ + +#include "BluetoothSettings.h" + +BluetoothSettings::BluetoothSettings() +{ + find_directory(B_USER_SETTINGS_DIRECTORY, &fPath); + fPath.Append("Bluetooth_settings", true); +} + + +BluetoothSettings::~BluetoothSettings() +{ + +} + + +void +BluetoothSettings::Defaults() +{ + Data.PickedDevice = bdaddrUtils::NullAddress(); +} + + +void +BluetoothSettings::Load() +{ + fFile = new BFile(fPath.Path(), B_READ_ONLY); + + if (fFile->InitCheck() == B_OK) { + fFile->Read(&Data, sizeof(Data)); + // TODO: Add more settings here. + } else + Defaults(); + + delete fFile; +} + + +void +BluetoothSettings::Save() +{ + fFile = new BFile(fPath.Path(), B_WRITE_ONLY | B_CREATE_FILE); + + if (fFile->InitCheck() == B_OK) { + fFile->Write(&Data, sizeof(Data)); + // TODO: Add more settings here. + } + + delete fFile; +} diff --git a/src/preferences/bluetooth/BluetoothSettings.h b/src/preferences/bluetooth/BluetoothSettings.h new file mode 100644 index 0000000000..b04041006f --- /dev/null +++ b/src/preferences/bluetooth/BluetoothSettings.h @@ -0,0 +1,37 @@ +/* + * Copyright 2008-2009, Oliver Ruiz Dorantes, + * Copyright 2012-2013, Tri-Edge AI + * + * All rights reserved. Distributed under the terms of the MIT license. + */ + +#ifndef BLUETOOTH_SETTINGS_H +#define BLUETOOTH_SETTINGS_H + +#include +#include + +#include +#include +#include + +class BluetoothSettings +{ +public: + struct { + bdaddr_t PickedDevice; + } Data; + + BluetoothSettings(); + ~BluetoothSettings(); + + void Defaults(); + void Load(); + void Save(); + +private: + BPath fPath; + BFile* fFile; +}; + +#endif // BLUETOOTH_SETTINGS_H diff --git a/src/preferences/bluetooth/BluetoothSettingsView.cpp b/src/preferences/bluetooth/BluetoothSettingsView.cpp index b9ba836558..42da6dd48e 100644 --- a/src/preferences/bluetooth/BluetoothSettingsView.cpp +++ b/src/preferences/bluetooth/BluetoothSettingsView.cpp @@ -1,11 +1,18 @@ /* - * Copyright 2008-09, Oliver Ruiz Dorantes, + * Copyright 2008-2009, Oliver Ruiz Dorantes + * Copyright 2012-2013, Tri-Edge AI, + * * All rights reserved. Distributed under the terms of the MIT License. */ + #include "BluetoothSettingsView.h" -#include -#include +#include "defs.h" +#include "BluetoothSettings.h" +#include "BluetoothWindow.h" +#include "ExtendedLocalDeviceView.h" + +#include #include #include @@ -19,12 +26,8 @@ #include #include -#include -#include "ExtendedLocalDeviceView.h" - -#include "defs.h" -#include "BluetoothWindow.h" - +#include +#include #undef B_TRANSLATION_CONTEXT #define B_TRANSLATION_CONTEXT "Settings view" @@ -45,13 +48,15 @@ static const char* kLaptopLabel = B_TRANSLATE_MARK("Laptop"); static const char* kHandheldLabel = B_TRANSLATE_MARK("Handheld"); static const char* kPhoneLabel = B_TRANSLATE_MARK("Smart phone"); - // #pragma mark - BluetoothSettingsView::BluetoothSettingsView(const char* name) - : BView(name, 0), + : + BView(name, 0), fLocalDevicesMenu(NULL) { + fSettings.Load(); + _BuildConnectionPolicy(); fPolicyMenuField = new BMenuField("policy", B_TRANSLATE("Incoming connections policy:"), fPolicyMenu); @@ -65,10 +70,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) fInquiryTimeControl->SetHashMarkCount(255 / 15); fInquiryTimeControl->SetEnabled(true); - // hinting menu - _BuildClassMenu(); - fClassMenuField = new BMenuField("class", B_TRANSLATE("Identify host as:"), - fClassMenu); + fExtDeviceView = new ExtendedLocalDeviceView(BRect(0, 0, 5, 5), NULL); // localdevices menu _BuildLocalDevicesMenu(); @@ -76,13 +78,20 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) B_TRANSLATE("Local devices found on system:"), fLocalDevicesMenu); - fExtDeviceView = new ExtendedLocalDeviceView(BRect(0, 0, 5, 5), NULL); - SetLayout(new BGroupLayout(B_VERTICAL)); + if (ActiveLocalDevice != NULL) { + fExtDeviceView->SetLocalDevice(ActiveLocalDevice); + fExtDeviceView->SetEnabled(true); + } + + // hinting menu + _BuildClassMenu(); + fClassMenuField = new BMenuField("class", B_TRANSLATE("Identify host as:"), + fClassMenu); + // controls pane AddChild(BGridLayoutBuilder(10, 10) - .Add(fClassMenuField->CreateLabelLayoutItem(), 0, 0) .Add(fClassMenuField->CreateMenuBarLayoutItem(), 1, 0) @@ -107,7 +116,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name) BluetoothSettingsView::~BluetoothSettingsView() { - + fSettings.Save(); } @@ -133,44 +142,52 @@ BluetoothSettingsView::MessageReceived(BMessage* message) case kMsgLocalSwitched: { LocalDevice* lDevice; - if (message->FindPointer("LocalDevice", (void**) &lDevice) == B_OK) { - // Device integrity should be rechecked - fExtDeviceView->SetLocalDevice(lDevice); - fExtDeviceView->SetEnabled(true); - ActiveLocalDevice = lDevice; + + if (message->FindPointer("LocalDevice", + (void**)&lDevice) == B_OK) { + + _MarkLocalDevice(lDevice); } + + break; } - break; -/* - To be fixed :) + // TODO: To be fixed. :) + + /* case kMsgSetConnectionPolicy: { //uint8 Policy; //if (message->FindInt8("Policy", (int8*)&Policy) == B_OK) break; } - + case kMsgSetInquiryTime: { break; - }*/ - + } + */ case kMsgSetDeviceClass: { uint8 deviceClass; - if (message->FindInt8("DeviceClass", (int8*)&deviceClass) == B_OK) { + + if (message->FindInt8("DeviceClass", + (int8*)&deviceClass) == B_OK) { + if (deviceClass == 5) _SetDeviceClass(2, 3, 0x72); else _SetDeviceClass(1, deviceClass, 0x72); } + break; } - case kMsgRefresh: + { _BuildLocalDevicesMenu(); fLocalDevicesMenu->SetTargetForItems(this); - break; + + break; + } default: BView::MessageReceived(message); } @@ -178,17 +195,18 @@ BluetoothSettingsView::MessageReceived(BMessage* message) bool -BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor, uint16 service) +BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor, + uint16 service) { bool haveRun = true; - - DeviceClass devClass; - devClass.SetRecord(major, minor, service); + + DeviceClass devClass(major, minor, service); + if (ActiveLocalDevice != NULL) ActiveLocalDevice->SetDeviceClass(devClass); else haveRun = false; - + return haveRun; } @@ -217,13 +235,16 @@ BluetoothSettingsView::_BuildConnectionPolicy() fPolicyMenu->AddItem(item); } - void BluetoothSettingsView::_BuildClassMenu() { - BMessage* message = NULL; BMenuItem* item = NULL; + DeviceClass devClass; + + if (ActiveLocalDevice != NULL) { + devClass = ActiveLocalDevice->GetDeviceClass(); + } fClassMenu = new BPopUpMenu(B_TRANSLATE("Identify us as...")); @@ -232,25 +253,45 @@ BluetoothSettingsView::_BuildClassMenu() item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kDesktopLabel), message); fClassMenu->AddItem(item); + if (devClass.MajorDeviceClass() == 1 && + devClass.MinorDeviceClass() == 1) + item->SetMarked(true); + message = new BMessage(kMsgSetDeviceClass); message->AddInt8("DeviceClass", 2); item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kServerLabel), message); fClassMenu->AddItem(item); + if (devClass.MajorDeviceClass() == 1 && + devClass.MinorDeviceClass() == 2) + item->SetMarked(true); + message = new BMessage(kMsgSetDeviceClass); message->AddInt8("DeviceClass", 3); item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kLaptopLabel), message); fClassMenu->AddItem(item); + if (devClass.MajorDeviceClass() == 1 && + devClass.MinorDeviceClass() == 3) + item->SetMarked(true); + message = new BMessage(kMsgSetDeviceClass); message->AddInt8("DeviceClass", 4); item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kHandheldLabel), message); fClassMenu->AddItem(item); + if (devClass.MajorDeviceClass() == 1 && + devClass.MinorDeviceClass() == 4) + item->SetMarked(true); + message = new BMessage(kMsgSetDeviceClass); message->AddInt8("DeviceClass", 5); item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kPhoneLabel), message); fClassMenu->AddItem(item); + + if (devClass.MajorDeviceClass() == 2 && + devClass.MinorDeviceClass() == 3) + item->SetMarked(true); } @@ -260,20 +301,47 @@ BluetoothSettingsView::_BuildLocalDevicesMenu() LocalDevice* lDevice; if (!fLocalDevicesMenu) - fLocalDevicesMenu = new BPopUpMenu(B_TRANSLATE("Pick LocalDevice...")); + fLocalDevicesMenu = new BPopUpMenu(B_TRANSLATE("Pick device...")); - for (uint32 index = 0; index < LocalDevice::GetLocalDeviceCount(); index++) { + while (fLocalDevicesMenu->CountItems() > 0) { + BMenuItem* item = fLocalDevicesMenu->RemoveItem(0L); + if (item != NULL) { + delete item; + } + } + + ActiveLocalDevice = NULL; + + for (uint32 i = 0; i < LocalDevice::GetLocalDeviceCount(); i++) { lDevice = LocalDevice::GetLocalDevice(); - if (lDevice != NULL) { - // TODO Check if they already exists + if (lDevice != NULL) { BMessage* message = new BMessage(kMsgLocalSwitched); message->AddPointer("LocalDevice", lDevice); - BMenuItem* item = new BMenuItem((lDevice->GetFriendlyName().String()), - message); + BMenuItem* item = new BMenuItem( + (lDevice->GetFriendlyName().String()), message); + + if (bdaddrUtils::Compare(lDevice->GetBluetoothAddress(), + fSettings.Data.PickedDevice)) { + + item->SetMarked(true); + ActiveLocalDevice = lDevice; + } + fLocalDevicesMenu->AddItem(item); } } } + +void +BluetoothSettingsView::_MarkLocalDevice(LocalDevice* lDevice) +{ + // TODO: Device integrity should be rechecked. + + fExtDeviceView->SetLocalDevice(lDevice); + fExtDeviceView->SetEnabled(true); + ActiveLocalDevice = lDevice; + fSettings.Data.PickedDevice = lDevice->GetBluetoothAddress(); +} diff --git a/src/preferences/bluetooth/BluetoothSettingsView.h b/src/preferences/bluetooth/BluetoothSettingsView.h index d46bd1666c..25a03e439c 100644 --- a/src/preferences/bluetooth/BluetoothSettingsView.h +++ b/src/preferences/bluetooth/BluetoothSettingsView.h @@ -1,49 +1,58 @@ /* - * Copyright 2008-09, Oliver Ruiz Dorantes, + * Copyright 2008-2009, Oliver Ruiz Dorantes, + * Copyright 2012-2013, Tri-Edge AI, + * * All rights reserved. Distributed under the terms of the MIT License. */ + #ifndef BLUETOOTH_SETTINGS_VIEW_H #define BLUETOOTH_SETTINGS_VIEW_H +#include "BluetoothSettings.h" #include +class BluetoothSettings; +class ExtendedLocalDeviceView; +class LocalDevice; + class BBox; class BMenuField; class BPopUpMenu; class BSlider; -class ExtendedLocalDeviceView; - class BluetoothSettingsView : public BView { public: - BluetoothSettingsView(const char* name); - virtual ~BluetoothSettingsView(); + BluetoothSettingsView(const char* name); + virtual ~BluetoothSettingsView(); - virtual void AttachedToWindow(); - virtual void MessageReceived(BMessage* message); + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage* message); private: - void _BuildConnectionPolicy(); - void _BuildClassMenu(); - void _BuildLocalDevicesMenu(); - bool _SetDeviceClass(uint8 major, uint8 minor - , uint16 service); + void _BuildConnectionPolicy(); + void _BuildClassMenu(); + void _BuildLocalDevicesMenu(); + bool _SetDeviceClass(uint8 major, uint8 minor, + uint16 service); + void _MarkLocalDevice(LocalDevice* lDevice); protected: - float fDivider; + BluetoothSettings fSettings; - BMenuField* fPolicyMenuField; - BPopUpMenu* fPolicyMenu; - BMenuField* fClassMenuField; - BPopUpMenu* fClassMenu; - BMenuField* fLocalDevicesMenuField; - BPopUpMenu* fLocalDevicesMenu; + float fDivider; - ExtendedLocalDeviceView* fExtDeviceView; + BMenuField* fPolicyMenuField; + BPopUpMenu* fPolicyMenu; + BMenuField* fClassMenuField; + BPopUpMenu* fClassMenu; + BMenuField* fLocalDevicesMenuField; + BPopUpMenu* fLocalDevicesMenu; - BSlider* fInquiryTimeControl; + ExtendedLocalDeviceView* fExtDeviceView; + + BSlider* fInquiryTimeControl; }; diff --git a/src/preferences/bluetooth/BluetoothWindow.cpp b/src/preferences/bluetooth/BluetoothWindow.cpp index 87c1938222..28cdf3568f 100644 --- a/src/preferences/bluetooth/BluetoothWindow.cpp +++ b/src/preferences/bluetooth/BluetoothWindow.cpp @@ -89,7 +89,6 @@ BluetoothWindow::BluetoothWindow(BRect frame) // tabView->AddTab(fConnChan); tabView->AddTab(fSettingsView); - fRevertButton->SetEnabled(false); AddChild(BGroupLayoutBuilder(B_VERTICAL, 0) diff --git a/src/preferences/bluetooth/BluetoothWindow.h b/src/preferences/bluetooth/BluetoothWindow.h index 24e299a60d..aef9f05cbe 100644 --- a/src/preferences/bluetooth/BluetoothWindow.h +++ b/src/preferences/bluetooth/BluetoothWindow.h @@ -2,37 +2,36 @@ * Copyright 2008-09, Oliver Ruiz Dorantes, * All rights reserved. Distributed under the terms of the MIT License. */ + #ifndef BLUETOOTH_WINDOW_H #define BLUETOOTH_WINDOW_H +#include "BluetoothSettingsView.h" + #include #include #include #include #include - -#include "BluetoothSettingsView.h" - +class BluetoothSettingsView; class RemoteDevicesView; class ConnChanView; -class BluetoothWindow : public BWindow -{ +class BluetoothWindow : public BWindow { public: - BluetoothWindow(BRect frame); + BluetoothWindow(BRect frame); bool QuitRequested(void); void MessageReceived(BMessage *message); - + private: - RemoteDevicesView* fRemoteDevices; - ConnChanView* fConnChan; - BButton* fDefaultsButton; - BButton* fRevertButton; - BMenuBar* fMenubar; - - BluetoothSettingsView* fSettingsView; + RemoteDevicesView* fRemoteDevices; + ConnChanView* fConnChan; + BButton* fDefaultsButton; + BButton* fRevertButton; + BMenuBar* fMenubar; + BluetoothSettingsView* fSettingsView; }; #endif diff --git a/src/preferences/bluetooth/ExtendedLocalDeviceView.cpp b/src/preferences/bluetooth/ExtendedLocalDeviceView.cpp index 575c044ea2..31ddfaff28 100644 --- a/src/preferences/bluetooth/ExtendedLocalDeviceView.cpp +++ b/src/preferences/bluetooth/ExtendedLocalDeviceView.cpp @@ -72,7 +72,7 @@ ExtendedLocalDeviceView::SetLocalDevice(LocalDevice* lDevice) fDevice = lDevice; SetName(lDevice->GetFriendlyName().String()); fDeviceView->SetBluetoothDevice(lDevice); - + ClearDevice(); int value = fDevice->GetDiscoverable(); @@ -110,12 +110,12 @@ void ExtendedLocalDeviceView::MessageReceived(BMessage* message) { printf("ExtendedLocalDeviceView::MessageReceived\n"); - + if (fDevice == NULL) { printf("ExtendedLocalDeviceView::Device missing\n"); return; } - + if (message->WasDropped()) { } @@ -157,7 +157,7 @@ void ExtendedLocalDeviceView::SetEnabled(bool value) { printf("ExtendedLocalDeviceView::SetEnabled\n"); - + fVisible->SetEnabled(value); fAuthentication->SetEnabled(value); fDiscoverable->SetEnabled(value); @@ -168,7 +168,7 @@ void ExtendedLocalDeviceView::ClearDevice() { printf("ExtendedLocalDeviceView::ClearDevice\n"); - + fVisible->SetValue(false); fAuthentication->SetValue(false); fDiscoverable->SetValue(false); diff --git a/src/preferences/bluetooth/Jamfile b/src/preferences/bluetooth/Jamfile index 4b620f51db..351cab064f 100644 --- a/src/preferences/bluetooth/Jamfile +++ b/src/preferences/bluetooth/Jamfile @@ -8,6 +8,7 @@ AddResources Bluetooth : bluetooth-pref.rdef ; Preference Bluetooth : BluetoothDeviceView.cpp BluetoothMain.cpp + BluetoothSettings.cpp BluetoothSettingsView.cpp BluetoothWindow.cpp DeviceListItem.cpp