1 /* 2 * Copyright 2008-09, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com> 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #include "BluetoothSettingsView.h" 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 10 #include <Box.h> 11 #include <GridLayoutBuilder.h> 12 #include <GroupLayoutBuilder.h> 13 #include <MenuField.h> 14 #include <MenuItem.h> 15 #include <PopUpMenu.h> 16 #include <Slider.h> 17 #include <SpaceLayoutItem.h> 18 #include <String.h> 19 #include <TextView.h> 20 21 #include <bluetooth/LocalDevice.h> 22 #include "ExtendedLocalDeviceView.h" 23 24 #include "defs.h" 25 #include "BluetoothWindow.h" 26 27 static const int32 kMsgSetAntialiasing = 'anti'; 28 static const int32 kMsgSetDeviceClassDesktop = 'sDCd'; 29 static const int32 kMsgSetDeviceClassServer = 'sDCs'; 30 static const int32 kMsgSetDeviceClassLaptop = 'sDCl'; 31 static const int32 kMsgSetDeviceClassHandheld = 'sDCn'; 32 static const int32 kMsgSetDeviceClassSmartPhone = 'sDCp'; 33 34 static const int32 kMsgSetAverageWeight = 'afEa'; 35 static const int32 kMsgLocalSwitched = 'lDsW'; 36 37 static const char* kAllLabel = "From all devices"; 38 static const char* kTrustedLabel = "Only from Trusted devices"; 39 static const char* kAlwaysLabel = "Always ask"; 40 41 static const char* kDesktopLabel = "Desktop"; 42 static const char* kServerLabel = "Server"; 43 static const char* kLaptopLabel = "Laptop"; 44 static const char* kHandheldLabel = "Handheld"; 45 static const char* kPhoneLabel = "Smart Phone"; 46 47 48 // #pragma mark - 49 50 BluetoothSettingsView::BluetoothSettingsView(const char* name) 51 : BView(name, 0) 52 { 53 // antialiasing menu 54 _BuildConnectionPolicy(); 55 fAntialiasingMenuField = new BMenuField("antialiasing", 56 "Incoming connections policy:", fAntialiasingMenu, NULL); 57 58 fAverageWeightControl = new BSlider("averageWeightControl", 59 "Default Inquiry time:", new BMessage(kMsgSetAverageWeight), 0, 255, B_HORIZONTAL); 60 fAverageWeightControl->SetLimitLabels("15 secs", "61 secs"); 61 fAverageWeightControl->SetHashMarks(B_HASH_MARKS_BOTTOM); 62 fAverageWeightControl->SetHashMarkCount(255 / 15); 63 fAverageWeightControl->SetEnabled(true); 64 65 // hinting menu 66 _BuildHintingMenu(); 67 fHintingMenuField = new BMenuField("hinting", "Identify host as:", 68 fHintingMenu, NULL); 69 70 // localdevices menu 71 _BuildLocalDevicesMenu(); 72 fLocalDevicesMenuField = new BMenuField("devices", "Local Devices found on system:", 73 fLocalDevicesMenu, NULL); 74 75 fExtDeviceView = new ExtendedLocalDeviceView(BRect(0,0,5,5), NULL); 76 77 SetLayout(new BGroupLayout(B_VERTICAL)); 78 79 // controls pane 80 AddChild(BGridLayoutBuilder(10, 10) 81 82 .Add(fHintingMenuField->CreateLabelLayoutItem(), 0, 0) 83 .Add(fHintingMenuField->CreateMenuBarLayoutItem(), 1, 0) 84 85 .Add(fAntialiasingMenuField->CreateLabelLayoutItem(), 0, 1) 86 .Add(fAntialiasingMenuField->CreateMenuBarLayoutItem(), 1, 1) 87 88 .Add(BSpaceLayoutItem::CreateGlue(), 0, 2, 2) 89 90 .Add(fAverageWeightControl, 0, 3, 2) 91 .Add(BSpaceLayoutItem::CreateGlue(), 0, 4, 2) 92 93 .Add(fLocalDevicesMenuField->CreateLabelLayoutItem(), 0, 5) 94 .Add(fLocalDevicesMenuField->CreateMenuBarLayoutItem(), 1, 5) 95 96 .Add(fExtDeviceView, 0, 6, 2) 97 .Add(BSpaceLayoutItem::CreateGlue(), 0, 7, 2) 98 99 .SetInsets(10, 10, 10, 10) 100 ); 101 102 } 103 104 105 BluetoothSettingsView::~BluetoothSettingsView() 106 { 107 108 } 109 110 111 void 112 BluetoothSettingsView::AttachedToWindow() 113 { 114 if (Parent() != NULL) 115 SetViewColor(Parent()->ViewColor()); 116 else 117 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 118 119 fAntialiasingMenu->SetTargetForItems(this); 120 fHintingMenu->SetTargetForItems(this); 121 fLocalDevicesMenu->SetTargetForItems(this); 122 fAverageWeightControl->SetTarget(this); 123 } 124 125 126 void 127 BluetoothSettingsView::MessageReceived(BMessage *msg) 128 { 129 130 DeviceClass devClass; 131 132 switch (msg->what) { 133 case kMsgLocalSwitched: 134 { 135 LocalDevice* lDevice; 136 if (msg->FindPointer("LocalDevice", (void**) &lDevice) == B_OK) { 137 // Device integrity should be rechecked 138 fExtDeviceView->SetLocalDevice(lDevice); 139 fExtDeviceView->SetEnabled(true); 140 ActiveLocalDevice = lDevice; 141 } 142 } 143 break; 144 145 case kMsgSetDeviceClassDesktop: 146 { 147 devClass.SetRecord(1, 1, 0x72); 148 ActiveLocalDevice->SetDeviceClass(devClass); 149 break; 150 } 151 152 case kMsgSetDeviceClassServer: 153 { 154 devClass.SetRecord(1, 2, 0x72); 155 ActiveLocalDevice->SetDeviceClass(devClass); 156 break; 157 } 158 159 case kMsgSetDeviceClassLaptop: 160 { 161 devClass.SetRecord(1, 3, 0x72); 162 ActiveLocalDevice->SetDeviceClass(devClass); 163 break; 164 } 165 166 case kMsgSetDeviceClassHandheld: 167 { 168 devClass.SetRecord(1, 4, 0x72); 169 ActiveLocalDevice->SetDeviceClass(devClass); 170 break; 171 } 172 173 case kMsgSetDeviceClassSmartPhone: 174 { 175 devClass.SetRecord(2, 3, 0x72); 176 ActiveLocalDevice->SetDeviceClass(devClass); 177 break; 178 } 179 180 case kMsgRefresh: 181 _BuildLocalDevicesMenu(); 182 fLocalDevicesMenu->SetTargetForItems(this); 183 break; 184 default: 185 BView::MessageReceived(msg); 186 } 187 } 188 189 190 void 191 BluetoothSettingsView::_BuildConnectionPolicy() 192 { 193 fAntialiasingMenu = new BPopUpMenu("Policy..."); 194 195 BMessage* message = new BMessage(kMsgSetAntialiasing); 196 message->AddBool("antialiasing", false); 197 198 BMenuItem* item = new BMenuItem(kAllLabel, message); 199 200 fAntialiasingMenu->AddItem(item); 201 202 message = new BMessage(kMsgSetAntialiasing); 203 message->AddBool("antialiasing", true); 204 205 item = new BMenuItem(kTrustedLabel, message); 206 207 fAntialiasingMenu->AddItem(item); 208 209 BMenuItem* item2 = new BMenuItem(kAlwaysLabel, NULL); 210 211 fAntialiasingMenu->AddItem(item2); 212 213 } 214 215 216 void 217 BluetoothSettingsView::_BuildHintingMenu() 218 { 219 220 fHintingMenu = new BPopUpMenu("Identify us as..."); 221 BMessage* message; 222 223 message = new BMessage(kMsgSetDeviceClassDesktop); 224 BMenuItem* item = new BMenuItem(kDesktopLabel, message); 225 fHintingMenu->AddItem(item); 226 227 message = new BMessage(kMsgSetDeviceClassServer); 228 item = new BMenuItem(kServerLabel, message); 229 fHintingMenu->AddItem(item); 230 231 message = new BMessage(kMsgSetDeviceClassLaptop); 232 item = new BMenuItem(kLaptopLabel, message); 233 fHintingMenu->AddItem(item); 234 235 message = new BMessage(kMsgSetDeviceClassHandheld); 236 item = new BMenuItem(kHandheldLabel, message); 237 fHintingMenu->AddItem(item); 238 239 message = new BMessage(kMsgSetDeviceClassSmartPhone); 240 item = new BMenuItem(kPhoneLabel, message); 241 fHintingMenu->AddItem(item); 242 243 244 } 245 246 247 void 248 BluetoothSettingsView::_BuildLocalDevicesMenu() 249 { 250 LocalDevice* lDevice; 251 252 if (!fLocalDevicesMenu) 253 fLocalDevicesMenu = new BPopUpMenu("Pick LocalDevice..."); 254 255 for (uint32 index = 0 ; index < LocalDevice::GetLocalDeviceCount() ; index++) { 256 257 lDevice = LocalDevice::GetLocalDevice(); 258 if (lDevice != NULL) { 259 // TODO Check if they already exists 260 261 BMessage* message = new BMessage(kMsgLocalSwitched); 262 message->AddPointer("LocalDevice", lDevice); 263 264 BMenuItem* item = new BMenuItem((lDevice->GetFriendlyName().String()), message); 265 fLocalDevicesMenu->AddItem(item); 266 } 267 } 268 } 269 270 271 272