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