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