fat: Greatly simplify and clean up dosfs_get_file_map().
[haiku.git] / src / preferences / bluetooth / BluetoothSettingsView.cpp
blob54a186409467feaab615a2eec2fbf7b1ca5041c7
1 /*
2 * Copyright 2008-2009, Oliver Ruiz Dorantes <oliver.ruiz.dorantes@gmail.com>
3 * Copyright 2012-2013, Tri-Edge AI, <triedgeai@gmail.com>
5 * All rights reserved. Distributed under the terms of the MIT License.
6 */
8 #include "BluetoothSettingsView.h"
10 #include "defs.h"
11 #include "BluetoothSettings.h"
12 #include "BluetoothWindow.h"
13 #include "ExtendedLocalDeviceView.h"
15 #include <bluetooth/LocalDevice.h>
17 #include <Box.h>
18 #include <Catalog.h>
19 #include <GridLayoutBuilder.h>
20 #include <GroupLayoutBuilder.h>
21 #include <MenuField.h>
22 #include <MenuItem.h>
23 #include <PopUpMenu.h>
24 #include <Slider.h>
25 #include <SpaceLayoutItem.h>
26 #include <String.h>
27 #include <TextView.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "Settings view"
35 static const int32 kMsgSetConnectionPolicy = 'sCpo';
36 static const int32 kMsgSetDeviceClass = 'sDC0';
37 static const int32 kMsgSetInquiryTime = 'afEa';
38 static const int32 kMsgLocalSwitched = 'lDsW';
40 static const char* kAllLabel = B_TRANSLATE_MARK("From all devices");
41 static const char* kTrustedLabel =
42 B_TRANSLATE_MARK("Only from trusted devices");
43 static const char* kAlwaysLabel = B_TRANSLATE_MARK("Always ask");
45 static const char* kDesktopLabel = B_TRANSLATE_MARK("Desktop");
46 static const char* kServerLabel = B_TRANSLATE_MARK("Server");
47 static const char* kLaptopLabel = B_TRANSLATE_MARK("Laptop");
48 static const char* kHandheldLabel = B_TRANSLATE_MARK("Handheld");
49 static const char* kPhoneLabel = B_TRANSLATE_MARK("Smart phone");
51 // #pragma mark -
53 BluetoothSettingsView::BluetoothSettingsView(const char* name)
55 BView(name, 0),
56 fLocalDevicesMenu(NULL)
58 fSettings.Load();
60 _BuildConnectionPolicy();
61 fPolicyMenuField = new BMenuField("policy",
62 B_TRANSLATE("Incoming connections policy:"), fPolicyMenu);
64 fInquiryTimeControl = new BSlider("time",
65 B_TRANSLATE("Default inquiry time:"), new BMessage(kMsgSetInquiryTime),
66 0, 255, B_HORIZONTAL);
67 fInquiryTimeControl->SetLimitLabels(B_TRANSLATE("15 secs"),
68 B_TRANSLATE("61 secs"));
69 fInquiryTimeControl->SetHashMarks(B_HASH_MARKS_BOTTOM);
70 fInquiryTimeControl->SetHashMarkCount(255 / 15);
71 fInquiryTimeControl->SetEnabled(true);
73 fExtDeviceView = new ExtendedLocalDeviceView(BRect(0, 0, 5, 5), NULL);
75 // localdevices menu
76 _BuildLocalDevicesMenu();
77 fLocalDevicesMenuField = new BMenuField("devices",
78 B_TRANSLATE("Local devices found on system:"),
79 fLocalDevicesMenu);
81 SetLayout(new BGroupLayout(B_VERTICAL));
83 if (ActiveLocalDevice != NULL) {
84 fExtDeviceView->SetLocalDevice(ActiveLocalDevice);
85 fExtDeviceView->SetEnabled(true);
87 DeviceClass rememberedClass = ActiveLocalDevice->GetDeviceClass();
89 if (!rememberedClass.IsUnknownDeviceClass())
90 fSettings.Data.LocalDeviceClass = rememberedClass;
93 // hinting menu
94 _BuildClassMenu();
95 fClassMenuField = new BMenuField("class", B_TRANSLATE("Identify host as:"),
96 fClassMenu);
98 // controls pane
99 AddChild(BGridLayoutBuilder(10, 10)
100 .Add(fClassMenuField->CreateLabelLayoutItem(), 0, 0)
101 .Add(fClassMenuField->CreateMenuBarLayoutItem(), 1, 0)
103 .Add(fPolicyMenuField->CreateLabelLayoutItem(), 0, 1)
104 .Add(fPolicyMenuField->CreateMenuBarLayoutItem(), 1, 1)
106 .Add(BSpaceLayoutItem::CreateGlue(), 0, 2, 2)
108 .Add(fInquiryTimeControl, 0, 3, 2)
109 .Add(BSpaceLayoutItem::CreateGlue(), 0, 4, 2)
111 .Add(fLocalDevicesMenuField->CreateLabelLayoutItem(), 0, 5)
112 .Add(fLocalDevicesMenuField->CreateMenuBarLayoutItem(), 1, 5)
114 .Add(fExtDeviceView, 0, 6, 2)
115 .Add(BSpaceLayoutItem::CreateGlue(), 0, 7, 2)
117 .SetInsets(10, 10, 10, 10)
122 BluetoothSettingsView::~BluetoothSettingsView()
124 fSettings.Save();
128 void
129 BluetoothSettingsView::AttachedToWindow()
131 if (Parent() != NULL)
132 SetViewColor(Parent()->ViewColor());
133 else
134 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
136 fPolicyMenu->SetTargetForItems(this);
137 fClassMenu->SetTargetForItems(this);
138 fLocalDevicesMenu->SetTargetForItems(this);
139 fInquiryTimeControl->SetTarget(this);
143 void
144 BluetoothSettingsView::MessageReceived(BMessage* message)
146 switch (message->what) {
147 case kMsgLocalSwitched:
149 LocalDevice* lDevice;
151 if (message->FindPointer("LocalDevice",
152 (void**)&lDevice) == B_OK) {
154 _MarkLocalDevice(lDevice);
157 break;
159 // TODO: To be fixed. :)
162 case kMsgSetConnectionPolicy:
164 //uint8 Policy;
165 //if (message->FindInt8("Policy", (int8*)&Policy) == B_OK)
166 break;
169 case kMsgSetInquiryTime:
171 break;
174 case kMsgSetDeviceClass:
176 uint8 deviceClass;
178 if (message->FindInt8("DeviceClass",
179 (int8*)&deviceClass) == B_OK) {
181 if (deviceClass == 5)
182 _SetDeviceClass(2, 3, 0x72);
183 else
184 _SetDeviceClass(1, deviceClass, 0x72);
187 break;
189 case kMsgRefresh:
191 _BuildLocalDevicesMenu();
192 fLocalDevicesMenu->SetTargetForItems(this);
194 break;
196 default:
197 BView::MessageReceived(message);
202 bool
203 BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor,
204 uint16 service)
206 bool haveRun = true;
208 fSettings.Data.LocalDeviceClass.SetRecord(major, minor, service);
210 if (ActiveLocalDevice != NULL)
211 ActiveLocalDevice->SetDeviceClass(fSettings.Data.LocalDeviceClass);
212 else
213 haveRun = false;
215 return haveRun;
219 void
220 BluetoothSettingsView::_BuildConnectionPolicy()
222 BMessage* message = NULL;
223 BMenuItem* item = NULL;
225 fPolicyMenu = new BPopUpMenu(B_TRANSLATE("Policy..."));
227 message = new BMessage(kMsgSetConnectionPolicy);
228 message->AddInt8("Policy", 1);
229 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kAllLabel), message);
230 fPolicyMenu->AddItem(item);
232 message = new BMessage(kMsgSetConnectionPolicy);
233 message->AddInt8("Policy", 2);
234 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kTrustedLabel), message);
235 fPolicyMenu->AddItem(item);
237 message = new BMessage(kMsgSetConnectionPolicy);
238 message->AddInt8("Policy", 3);
239 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kAlwaysLabel), NULL);
240 fPolicyMenu->AddItem(item);
243 void
244 BluetoothSettingsView::_BuildClassMenu()
246 BMessage* message = NULL;
247 BMenuItem* item = NULL;
249 fClassMenu = new BPopUpMenu(B_TRANSLATE("Identify us as..."));
251 message = new BMessage(kMsgSetDeviceClass);
252 message->AddInt8("DeviceClass", 1);
253 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kDesktopLabel), message);
254 fClassMenu->AddItem(item);
256 if (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 1 &&
257 fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 1)
258 item->SetMarked(true);
260 message = new BMessage(kMsgSetDeviceClass);
261 message->AddInt8("DeviceClass", 2);
262 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kServerLabel), message);
263 fClassMenu->AddItem(item);
265 if (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 1 &&
266 fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 2)
267 item->SetMarked(true);
269 message = new BMessage(kMsgSetDeviceClass);
270 message->AddInt8("DeviceClass", 3);
271 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kLaptopLabel), message);
272 fClassMenu->AddItem(item);
274 if (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 1 &&
275 fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 3)
276 item->SetMarked(true);
278 message = new BMessage(kMsgSetDeviceClass);
279 message->AddInt8("DeviceClass", 4);
280 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kHandheldLabel), message);
281 fClassMenu->AddItem(item);
283 if (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 1 &&
284 fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 4)
285 item->SetMarked(true);
287 message = new BMessage(kMsgSetDeviceClass);
288 message->AddInt8("DeviceClass", 5);
289 item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kPhoneLabel), message);
290 fClassMenu->AddItem(item);
292 if (fSettings.Data.LocalDeviceClass.MajorDeviceClass() == 2 &&
293 fSettings.Data.LocalDeviceClass.MinorDeviceClass() == 3)
294 item->SetMarked(true);
298 void
299 BluetoothSettingsView::_BuildLocalDevicesMenu()
301 LocalDevice* lDevice;
303 if (!fLocalDevicesMenu)
304 fLocalDevicesMenu = new BPopUpMenu(B_TRANSLATE("Pick device..."));
306 while (fLocalDevicesMenu->CountItems() > 0) {
307 BMenuItem* item = fLocalDevicesMenu->RemoveItem(0L);
309 if (item != NULL) {
310 delete item;
314 ActiveLocalDevice = NULL;
316 for (uint32 i = 0; i < LocalDevice::GetLocalDeviceCount(); i++) {
317 lDevice = LocalDevice::GetLocalDevice();
319 if (lDevice != NULL) {
320 BMessage* message = new BMessage(kMsgLocalSwitched);
321 message->AddPointer("LocalDevice", lDevice);
323 BMenuItem* item = new BMenuItem(
324 (lDevice->GetFriendlyName().String()), message);
326 if (bdaddrUtils::Compare(lDevice->GetBluetoothAddress(),
327 fSettings.Data.PickedDevice)) {
329 item->SetMarked(true);
330 ActiveLocalDevice = lDevice;
333 fLocalDevicesMenu->AddItem(item);
338 void
339 BluetoothSettingsView::_MarkLocalDevice(LocalDevice* lDevice)
341 // TODO: Device integrity should be rechecked.
343 fExtDeviceView->SetLocalDevice(lDevice);
344 fExtDeviceView->SetEnabled(true);
345 ActiveLocalDevice = lDevice;
346 fSettings.Data.PickedDevice = lDevice->GetBluetoothAddress();