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 kMsgSetHinting = 'hint'; 29 static const int32 kMsgSetAverageWeight = 'afEa'; 30 static const int32 kMsgLocalSwitched = 'lDsW'; 31 32 static const char* kAllLabel = "From all devices"; 33 static const char* kTrustedLabel = "Only from Trusted devices"; 34 static const char* kAlwaysLabel = "Always ask"; 35 36 static const char* kDesktopLabel = "Desktop"; 37 static const char* kLaptopLabel = "Laptop"; 38 static const char* kPhoneLabel = "Haiku Phone"; 39 40 41 // #pragma mark - 42 43 BluetoothSettingsView::BluetoothSettingsView(const char* name) 44 : BView(name, 0) 45 { 46 // antialiasing menu 47 _BuildConnectionPolicy(); 48 fAntialiasingMenuField = new BMenuField("antialiasing", 49 "Incoming connections policy:", fAntialiasingMenu, NULL); 50 51 fAverageWeightControl = new BSlider("averageWeightControl", 52 "Default Inquiry time:", new BMessage(kMsgSetAverageWeight), 0, 255, B_HORIZONTAL); 53 fAverageWeightControl->SetLimitLabels("15 secs", "61 secs"); 54 fAverageWeightControl->SetHashMarks(B_HASH_MARKS_BOTTOM); 55 fAverageWeightControl->SetHashMarkCount(255 / 15); 56 fAverageWeightControl->SetEnabled(true); 57 58 // hinting menu 59 _BuildHintingMenu(); 60 fHintingMenuField = new BMenuField("hinting", "Identify host as:", 61 fHintingMenu, NULL); 62 63 // localdevices menu 64 _BuildLocalDevicesMenu(); 65 fLocalDevicesMenuField = new BMenuField("devices", "Local Devices found on system:", 66 fLocalDevicesMenu, NULL); 67 68 fExtDeviceView = new ExtendedLocalDeviceView(BRect(0,0,5,5), NULL); 69 70 SetLayout(new BGroupLayout(B_VERTICAL)); 71 72 // controls pane 73 AddChild(BGridLayoutBuilder(10, 10) 74 75 .Add(fHintingMenuField->CreateLabelLayoutItem(), 0, 0) 76 .Add(fHintingMenuField->CreateMenuBarLayoutItem(), 1, 0) 77 78 .Add(fAntialiasingMenuField->CreateLabelLayoutItem(), 0, 1) 79 .Add(fAntialiasingMenuField->CreateMenuBarLayoutItem(), 1, 1) 80 81 .Add(BSpaceLayoutItem::CreateGlue(), 0, 2, 2) 82 83 .Add(fAverageWeightControl, 0, 3, 2) 84 .Add(BSpaceLayoutItem::CreateGlue(), 0, 4, 2) 85 86 .Add(fLocalDevicesMenuField->CreateLabelLayoutItem(), 0, 5) 87 .Add(fLocalDevicesMenuField->CreateMenuBarLayoutItem(), 1, 5) 88 89 .Add(fExtDeviceView, 0, 6, 2) 90 .Add(BSpaceLayoutItem::CreateGlue(), 0, 7, 2) 91 92 .SetInsets(10, 10, 10, 10) 93 ); 94 95 } 96 97 98 BluetoothSettingsView::~BluetoothSettingsView() 99 { 100 101 } 102 103 104 void 105 BluetoothSettingsView::AttachedToWindow() 106 { 107 if (Parent() != NULL) 108 SetViewColor(Parent()->ViewColor()); 109 else 110 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 111 112 fAntialiasingMenu->SetTargetForItems(this); 113 fHintingMenu->SetTargetForItems(this); 114 fLocalDevicesMenu->SetTargetForItems(this); 115 fAverageWeightControl->SetTarget(this); 116 } 117 118 119 void 120 BluetoothSettingsView::MessageReceived(BMessage *msg) 121 { 122 switch (msg->what) { 123 case kMsgLocalSwitched: 124 { 125 LocalDevice* lDevice; 126 if (msg->FindPointer("LocalDevice", (void**) &lDevice) == B_OK) { 127 // Device integrity should be rechecked 128 fExtDeviceView->SetLocalDevice(lDevice); 129 fExtDeviceView->SetEnabled(true); 130 ActiveLocalDevice = lDevice; 131 } 132 } 133 break; 134 case kMsgRefresh: 135 _BuildLocalDevicesMenu(); 136 fLocalDevicesMenu->SetTargetForItems(this); 137 break; 138 default: 139 BView::MessageReceived(msg); 140 } 141 } 142 143 144 void 145 BluetoothSettingsView::_BuildConnectionPolicy() 146 { 147 fAntialiasingMenu = new BPopUpMenu("Policy..."); 148 149 BMessage* message = new BMessage(kMsgSetAntialiasing); 150 message->AddBool("antialiasing", false); 151 152 BMenuItem* item = new BMenuItem(kAllLabel, message); 153 154 fAntialiasingMenu->AddItem(item); 155 156 message = new BMessage(kMsgSetAntialiasing); 157 message->AddBool("antialiasing", true); 158 159 item = new BMenuItem(kTrustedLabel, message); 160 161 fAntialiasingMenu->AddItem(item); 162 163 BMenuItem* item2 = new BMenuItem(kAlwaysLabel, NULL); 164 165 fAntialiasingMenu->AddItem(item2); 166 167 } 168 169 170 void 171 BluetoothSettingsView::_BuildHintingMenu() 172 { 173 fHintingMenu = new BPopUpMenu("Identify us as..."); 174 175 BMessage* message = new BMessage(kMsgSetHinting); 176 message->AddBool("hinting", false); 177 178 BMenuItem* item = new BMenuItem(kDesktopLabel, message); 179 180 fHintingMenu->AddItem(item); 181 182 message = new BMessage(kMsgSetAverageWeight); 183 message->AddBool("hinting", true); 184 185 item = new BMenuItem(kLaptopLabel, message); 186 187 fHintingMenu->AddItem(item); 188 189 BMenuItem* item2 = new BMenuItem(kPhoneLabel, NULL); 190 fHintingMenu->AddItem(item2); 191 192 } 193 194 195 void 196 BluetoothSettingsView::_BuildLocalDevicesMenu() 197 { 198 LocalDevice* lDevice; 199 200 if (!fLocalDevicesMenu) 201 fLocalDevicesMenu = new BPopUpMenu("Pick LocalDevice..."); 202 203 for (uint32 index = 0 ; index < LocalDevice::GetLocalDeviceCount() ; index++) { 204 205 lDevice = LocalDevice::GetLocalDevice(); 206 if (lDevice != NULL) { 207 // TODO Check if they already exists 208 209 BMessage* message = new BMessage(kMsgLocalSwitched); 210 message->AddPointer("LocalDevice", lDevice); 211 212 BMenuItem* item = new BMenuItem((lDevice->GetFriendlyName().String()), message); 213 fLocalDevicesMenu->AddItem(item); 214 } 215 } 216 } 217 218 219 220