xref: /haiku/src/preferences/bluetooth/ExtendedLocalDeviceView.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
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 "ExtendedLocalDeviceView.h"
6 
7 #include <bluetooth/bdaddrUtils.h>
8 
9 #include "defs.h"
10 
11 #include <Bitmap.h>
12 #include <Catalog.h>
13 #include <CheckBox.h>
14 #include <GroupLayoutBuilder.h>
15 #include <SpaceLayoutItem.h>
16 #include <StringView.h>
17 
18 
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "Extended local device view"
21 
22 ExtendedLocalDeviceView::ExtendedLocalDeviceView(BRect frame,
23 	LocalDevice* bDevice, uint32 resizingMode, uint32 flags)
24 	:
25 	BView(frame,"ExtendedLocalDeviceView", resizingMode, flags | B_WILL_DRAW),
26 	fDevice(bDevice),
27 	fScanMode(0)
28 {
29 	SetViewColor(B_TRANSPARENT_COLOR);
30 	SetLowColor(0, 0, 0);
31 	BRect iDontCare(0, 0, 0, 0);
32 
33 	SetLayout(new BGroupLayout(B_HORIZONTAL));
34 
35 	fDeviceView = new BluetoothDeviceView(BRect(0, 0, 5, 5), bDevice);
36 
37 	fDiscoverable = new BCheckBox(iDontCare, "Discoverable",
38 		B_TRANSLATE("Discoverable"), new BMessage(SET_DISCOVERABLE));
39 	fVisible = new BCheckBox(iDontCare, "Visible",
40 		B_TRANSLATE("Show name"), new BMessage(SET_VISIBLE));
41 	fAuthentication = new BCheckBox(iDontCare, "Authenticate",
42 		B_TRANSLATE("Authenticate"), new BMessage(SET_AUTHENTICATION));
43 
44 	SetEnabled(false);
45 
46 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
47 				.Add(fDeviceView)
48 				.Add(BGroupLayoutBuilder(B_HORIZONTAL)
49 						.AddGlue()
50 						.Add(fDiscoverable)
51 						.Add(fVisible)
52 						.SetInsets(5, 5, 5, 5)
53 					)
54 				.Add(fAuthentication)
55 				.Add(BSpaceLayoutItem::CreateVerticalStrut(0))
56 			.SetInsets(5, 5, 5, 5)
57 	);
58 }
59 
60 
61 ExtendedLocalDeviceView::~ExtendedLocalDeviceView(void)
62 {
63 
64 }
65 
66 
67 void
68 ExtendedLocalDeviceView::SetLocalDevice(LocalDevice* lDevice)
69 {
70 	printf("ExtendedLocalDeviceView::SetLocalDevice\n");
71 	if (lDevice != NULL) {
72 		fDevice = lDevice;
73 		SetName(lDevice->GetFriendlyName().String());
74 		fDeviceView->SetBluetoothDevice(lDevice);
75 
76 		ClearDevice();
77 
78 		int value = fDevice->GetDiscoverable();
79 		printf("ExtendedLocalDeviceView::SetLocalDevice value = %d\n", value);
80 		if (value == 1)
81 			fDiscoverable->SetValue(true);
82 		else if (value == 2)
83 			fVisible->SetValue(true);
84 		else if  (value == 3) {
85 			fDiscoverable->SetValue(true);
86 			fVisible->SetValue(true);
87 		}
88 	}
89 }
90 
91 
92 void
93 ExtendedLocalDeviceView::AttachedToWindow()
94 {
95 	printf("ExtendedLocalDeviceView::AttachedToWindow\n");
96 	fDiscoverable->SetTarget(this);
97 	fVisible->SetTarget(this);
98 	fAuthentication->SetTarget(this);
99 }
100 
101 
102 void
103 ExtendedLocalDeviceView::SetTarget(BHandler* target)
104 {
105 	printf("ExtendedLocalDeviceView::SetTarget\n");
106 }
107 
108 
109 void
110 ExtendedLocalDeviceView::MessageReceived(BMessage* message)
111 {
112 	printf("ExtendedLocalDeviceView::MessageReceived\n");
113 
114 	if (fDevice == NULL) {
115 		printf("ExtendedLocalDeviceView::Device missing\n");
116 		return;
117 	}
118 
119 	if (message->WasDropped()) {
120 
121 	}
122 
123 	switch (message->what)
124 	{
125 		case SET_DISCOVERABLE:
126 		case SET_VISIBLE:
127 			fScanMode = 0;
128 
129 			if (fDiscoverable->Value()) {
130 				fScanMode = 1;
131 				fVisible->SetEnabled(true);
132 			} else {
133 				fVisible->SetValue(false);
134 				fVisible->SetEnabled(false);
135 			}
136 
137 			if (fVisible->Value())
138 				fScanMode |= 2;
139 
140 			if (fDevice != NULL)
141 				fDevice->SetDiscoverable(fScanMode);
142 
143 			break;
144 		case SET_AUTHENTICATION:
145 			if (fDevice != NULL)
146 				fDevice->SetAuthentication(fAuthentication->Value());
147 			break;
148 
149 		default:
150 			BView::MessageReceived(message);
151 		break;
152 	}
153 }
154 
155 
156 void
157 ExtendedLocalDeviceView::SetEnabled(bool value)
158 {
159 	printf("ExtendedLocalDeviceView::SetEnabled\n");
160 
161 	fVisible->SetEnabled(value);
162 	fAuthentication->SetEnabled(value);
163 	fDiscoverable->SetEnabled(value);
164 }
165 
166 
167 void
168 ExtendedLocalDeviceView::ClearDevice()
169 {
170 	printf("ExtendedLocalDeviceView::ClearDevice\n");
171 
172 	fVisible->SetValue(false);
173 	fAuthentication->SetValue(false);
174 	fDiscoverable->SetValue(false);
175 }
176