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