xref: /haiku/src/preferences/bluetooth/BluetoothSettingsView.cpp (revision 675ffabd70492a962f8c0288a32208c22ce5de18)
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 
30 #undef B_TRANSLATE_CONTEXT
31 #define B_TRANSLATE_CONTEXT "Settings view"
32 
33 static const int32 kMsgSetAntialiasing = 'anti';
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 kMsgSetAverageWeight = '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("antialiasing",
63 		B_TRANSLATE("Incoming connections policy:"), fPolicyMenu, NULL);
64 
65 	fInquiryTimeControl = new BSlider("time",
66 		B_TRANSLATE("Default inquiry time:"), new BMessage(kMsgSetAverageWeight),
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 			ActiveLocalDevice->SetDeviceClass(devClass);
159 			break;
160 		}
161 
162 		case kMsgSetDeviceClassServer:
163 		{
164 			devClass.SetRecord(1, 2, 0x72);
165 			ActiveLocalDevice->SetDeviceClass(devClass);
166 			break;
167 		}
168 
169 		case kMsgSetDeviceClassLaptop:
170 		{
171 			devClass.SetRecord(1, 3, 0x72);
172 			ActiveLocalDevice->SetDeviceClass(devClass);
173 			break;
174 		}
175 
176 		case kMsgSetDeviceClassHandheld:
177 		{
178 			devClass.SetRecord(1, 4, 0x72);
179 			ActiveLocalDevice->SetDeviceClass(devClass);
180 			break;
181 		}
182 
183 		case kMsgSetDeviceClassSmartPhone:
184 		{
185 			devClass.SetRecord(2, 3, 0x72);
186 			ActiveLocalDevice->SetDeviceClass(devClass);
187 			break;
188 		}
189 
190 		case kMsgRefresh:
191 			_BuildLocalDevicesMenu();
192 			fLocalDevicesMenu->SetTargetForItems(this);
193 		break;
194 		default:
195 			BView::MessageReceived(message);
196 	}
197 }
198 
199 
200 void
201 BluetoothSettingsView::_BuildConnectionPolicy()
202 {
203 	fPolicyMenu = new BPopUpMenu(B_TRANSLATE("Policy..."));
204 
205 	BMessage* message = new BMessage(kMsgSetAntialiasing);
206 	message->AddBool("antialiasing", false);
207 
208 	BMenuItem* item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kAllLabel), message);
209 
210 	fPolicyMenu->AddItem(item);
211 
212 	message = new BMessage(kMsgSetAntialiasing);
213 	message->AddBool("antialiasing", true);
214 
215 	item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kTrustedLabel), message);
216 
217 	fPolicyMenu->AddItem(item);
218 
219 	BMenuItem* item2 = new BMenuItem(B_TRANSLATE_NOCOLLECT(kAlwaysLabel), NULL);
220 
221 	fPolicyMenu->AddItem(item2);
222 
223 }
224 
225 
226 void
227 BluetoothSettingsView::_BuildClassMenu()
228 {
229 
230 	fClassMenu = new BPopUpMenu(B_TRANSLATE("Identify us as..."));
231 	BMessage* message;
232 
233 	message = new BMessage(kMsgSetDeviceClassDesktop);
234 	BMenuItem* item
235 		= new BMenuItem(B_TRANSLATE_NOCOLLECT(kDesktopLabel), message);
236 	fClassMenu->AddItem(item);
237 
238 	message = new BMessage(kMsgSetDeviceClassServer);
239 	item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kServerLabel), message);
240 	fClassMenu->AddItem(item);
241 
242 	message = new BMessage(kMsgSetDeviceClassLaptop);
243 	item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kLaptopLabel), message);
244 	fClassMenu->AddItem(item);
245 
246 	message = new BMessage(kMsgSetDeviceClassHandheld);
247 	item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kHandheldLabel), message);
248 	fClassMenu->AddItem(item);
249 
250 	message = new BMessage(kMsgSetDeviceClassSmartPhone);
251 	item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kPhoneLabel), message);
252 	fClassMenu->AddItem(item);
253 
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