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 fLocalDevicesMenu(NULL) 57 { 58 _BuildConnectionPolicy(); 59 fPolicyMenuField = new BMenuField("antialiasing", 60 TR("Incoming connections policy:"), fPolicyMenu, NULL); 61 62 fInquiryTimeControl = new BSlider("time", 63 TR("Default inquiry time:"), new BMessage(kMsgSetAverageWeight), 0, 255, 64 B_HORIZONTAL); 65 fInquiryTimeControl->SetLimitLabels(TR("15 secs"), TR("61 secs")); 66 fInquiryTimeControl->SetHashMarks(B_HASH_MARKS_BOTTOM); 67 fInquiryTimeControl->SetHashMarkCount(255 / 15); 68 fInquiryTimeControl->SetEnabled(true); 69 70 // hinting menu 71 _BuildClassMenu(); 72 fClassMenuField = new BMenuField("class", TR("Identify host as:"), 73 fClassMenu, 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(fClassMenuField->CreateLabelLayoutItem(), 0, 0) 88 .Add(fClassMenuField->CreateMenuBarLayoutItem(), 1, 0) 89 90 .Add(fPolicyMenuField->CreateLabelLayoutItem(), 0, 1) 91 .Add(fPolicyMenuField->CreateMenuBarLayoutItem(), 1, 1) 92 93 .Add(BSpaceLayoutItem::CreateGlue(), 0, 2, 2) 94 95 .Add(fInquiryTimeControl, 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 fPolicyMenu->SetTargetForItems(this); 125 fClassMenu->SetTargetForItems(this); 126 fLocalDevicesMenu->SetTargetForItems(this); 127 fInquiryTimeControl->SetTarget(this); 128 } 129 130 131 void 132 BluetoothSettingsView::MessageReceived(BMessage* message) 133 { 134 135 DeviceClass devClass; 136 137 switch (message->what) { 138 case kMsgLocalSwitched: 139 { 140 LocalDevice* lDevice; 141 if (message->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(message); 191 } 192 } 193 194 195 void 196 BluetoothSettingsView::_BuildConnectionPolicy() 197 { 198 fPolicyMenu = 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 fPolicyMenu->AddItem(item); 206 207 message = new BMessage(kMsgSetAntialiasing); 208 message->AddBool("antialiasing", true); 209 210 item = new BMenuItem(TR(kTrustedLabel), message); 211 212 fPolicyMenu->AddItem(item); 213 214 BMenuItem* item2 = new BMenuItem(TR(kAlwaysLabel), NULL); 215 216 fPolicyMenu->AddItem(item2); 217 218 } 219 220 221 void 222 BluetoothSettingsView::_BuildClassMenu() 223 { 224 225 fClassMenu = new BPopUpMenu(TR("Identify us as...")); 226 BMessage* message; 227 228 message = new BMessage(kMsgSetDeviceClassDesktop); 229 BMenuItem* item = new BMenuItem(TR(kDesktopLabel), message); 230 fClassMenu->AddItem(item); 231 232 message = new BMessage(kMsgSetDeviceClassServer); 233 item = new BMenuItem(TR(kServerLabel), message); 234 fClassMenu->AddItem(item); 235 236 message = new BMessage(kMsgSetDeviceClassLaptop); 237 item = new BMenuItem(TR(kLaptopLabel), message); 238 fClassMenu->AddItem(item); 239 240 message = new BMessage(kMsgSetDeviceClassHandheld); 241 item = new BMenuItem(TR(kHandheldLabel), message); 242 fClassMenu->AddItem(item); 243 244 message = new BMessage(kMsgSetDeviceClassSmartPhone); 245 item = new BMenuItem(TR(kPhoneLabel), message); 246 fClassMenu->AddItem(item); 247 248 } 249 250 251 void 252 BluetoothSettingsView::_BuildLocalDevicesMenu() 253 { 254 LocalDevice* lDevice; 255 256 if (!fLocalDevicesMenu) 257 fLocalDevicesMenu = new BPopUpMenu(TR("Pick LocalDevice...")); 258 259 for (uint32 index = 0; index < LocalDevice::GetLocalDeviceCount(); index++) { 260 261 lDevice = LocalDevice::GetLocalDevice(); 262 if (lDevice != NULL) { 263 264 // TODO Check if they already exists 265 BMessage* message = new BMessage(kMsgLocalSwitched); 266 message->AddPointer("LocalDevice", lDevice); 267 268 BMenuItem* item = new BMenuItem((lDevice->GetFriendlyName().String()), 269 message); 270 fLocalDevicesMenu->AddItem(item); 271 } 272 } 273 } 274