xref: /haiku/src/preferences/bluetooth/ExtendedLocalDeviceView.cpp (revision 56430ad8002b8fd1ac69b590e9cc130de6d9e852)
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 <LayoutBuilder.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(LocalDevice* bDevice,
23 	uint32 flags)
24 	:
25 	BView("ExtendedLocalDeviceView", flags | B_WILL_DRAW),
26 	fDevice(bDevice),
27 	fScanMode(0)
28 {
29 	fDeviceView = new BluetoothDeviceView(bDevice);
30 
31 	fDiscoverable = new BCheckBox("Discoverable",
32 		B_TRANSLATE("Discoverable"), new BMessage(SET_DISCOVERABLE));
33 	fVisible = new BCheckBox("Visible",
34 		B_TRANSLATE("Show name"), new BMessage(SET_VISIBLE));
35 	fAuthentication = new BCheckBox("Authenticate",
36 		B_TRANSLATE("Authenticate"), new BMessage(SET_AUTHENTICATION));
37 
38 	SetEnabled(false);
39 
40 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
41 		.SetInsets(5)
42 		.Add(fDeviceView)
43 		.AddGroup(B_HORIZONTAL, 0)
44 			.SetInsets(5)
45 			.AddGlue()
46 			.Add(fDiscoverable)
47 			.Add(fVisible)
48 		.End()
49 		.Add(fAuthentication)
50 	.End();
51 }
52 
53 
54 ExtendedLocalDeviceView::~ExtendedLocalDeviceView()
55 {
56 }
57 
58 
59 void
60 ExtendedLocalDeviceView::SetLocalDevice(LocalDevice* lDevice)
61 {
62 	if (lDevice != NULL) {
63 		fDevice = lDevice;
64 		SetName(lDevice->GetFriendlyName().String());
65 		fDeviceView->SetBluetoothDevice(lDevice);
66 
67 		ClearDevice();
68 
69 		int value = fDevice->GetDiscoverable();
70 		printf("ExtendedLocalDeviceView::SetLocalDevice value = %d\n", value);
71 		if (value == 1)
72 			fDiscoverable->SetValue(true);
73 		else if (value == 2)
74 			fVisible->SetValue(true);
75 		else if  (value == 3) {
76 			fDiscoverable->SetValue(true);
77 			fVisible->SetValue(true);
78 		}
79 	}
80 }
81 
82 
83 void
84 ExtendedLocalDeviceView::AttachedToWindow()
85 {
86 	fDiscoverable->SetTarget(this);
87 	fVisible->SetTarget(this);
88 	fAuthentication->SetTarget(this);
89 }
90 
91 
92 void
93 ExtendedLocalDeviceView::SetTarget(BHandler* target)
94 {
95 	printf("ExtendedLocalDeviceView::SetTarget\n");
96 }
97 
98 
99 void
100 ExtendedLocalDeviceView::MessageReceived(BMessage* message)
101 {
102 	if (fDevice == NULL) {
103 		printf("ExtendedLocalDeviceView::Device missing\n");
104 		BView::MessageReceived(message);
105 		return;
106 	}
107 
108 	if (message->WasDropped()) {
109 
110 	}
111 
112 	switch (message->what)
113 	{
114 		case SET_DISCOVERABLE:
115 		case SET_VISIBLE:
116 			fScanMode = 0;
117 
118 			if (fDiscoverable->Value()) {
119 				fScanMode = 1;
120 				fVisible->SetEnabled(true);
121 			} else {
122 				fVisible->SetValue(false);
123 				fVisible->SetEnabled(false);
124 			}
125 
126 			if (fVisible->Value())
127 				fScanMode |= 2;
128 
129 			if (fDevice != NULL)
130 				fDevice->SetDiscoverable(fScanMode);
131 
132 			break;
133 		case SET_AUTHENTICATION:
134 			if (fDevice != NULL)
135 				fDevice->SetAuthentication(fAuthentication->Value());
136 			break;
137 
138 		default:
139 			BView::MessageReceived(message);
140 			break;
141 	}
142 }
143 
144 
145 void
146 ExtendedLocalDeviceView::SetEnabled(bool value)
147 {
148 	fVisible->SetEnabled(value);
149 	fAuthentication->SetEnabled(value);
150 	fDiscoverable->SetEnabled(value);
151 }
152 
153 
154 void
155 ExtendedLocalDeviceView::ClearDevice()
156 {
157 	fVisible->SetValue(false);
158 	fAuthentication->SetValue(false);
159 	fDiscoverable->SetValue(false);
160 }
161