1/*
2 * Copyright 2008-2009, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes@gmail.com>
3 * Copyright 2021, Haiku, Inc.
4 * Distributed under the terms of the MIT License.
5 *
6 * Authors:
7 * 		Fredrik Mod��en <fredrik_at_modeen.se>
8 */
9
10#include "ExtendedLocalDeviceView.h"
11
12#include <bluetooth/bdaddrUtils.h>
13
14#include "defs.h"
15
16#include <Bitmap.h>
17#include <Catalog.h>
18#include <CheckBox.h>
19#include <LayoutBuilder.h>
20#include <SpaceLayoutItem.h>
21#include <StringView.h>
22
23
24#undef B_TRANSLATION_CONTEXT
25#define B_TRANSLATION_CONTEXT "Extended local device view"
26
27ExtendedLocalDeviceView::ExtendedLocalDeviceView(LocalDevice* bDevice,
28	uint32 flags)
29	:
30	BView("ExtendedLocalDeviceView", flags | B_WILL_DRAW),
31	fDevice(bDevice),
32	fScanMode(0)
33{
34	fDeviceView = new BluetoothDeviceView(bDevice);
35
36	fDiscoverable = new BCheckBox("Discoverable",
37		B_TRANSLATE("Discoverable"), new BMessage(SET_DISCOVERABLE));
38	fVisible = new BCheckBox("Visible",
39		B_TRANSLATE("Show name"), new BMessage(SET_VISIBLE));
40	fAuthentication = new BCheckBox("Authenticate",
41		B_TRANSLATE("Authenticate"), new BMessage(SET_AUTHENTICATION));
42	fAuthentication->SetEnabled(false);
43
44	SetEnabled(false);
45
46	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
47		.SetInsets(5)
48		.Add(fDeviceView)
49		.AddGroup(B_HORIZONTAL, 0)
50			.SetInsets(5)
51			.Add(fDiscoverable)
52			.Add(fVisible)
53			.Add(fAuthentication)
54		.End()
55	.End();
56}
57
58
59ExtendedLocalDeviceView::~ExtendedLocalDeviceView()
60{
61}
62
63
64void
65ExtendedLocalDeviceView::SetLocalDevice(LocalDevice* lDevice)
66{
67	if (lDevice != NULL) {
68		fDevice = lDevice;
69		SetName(lDevice->GetFriendlyName().String());
70		fDeviceView->SetBluetoothDevice(lDevice);
71
72		ClearDevice();
73
74		int value = fDevice->GetDiscoverable();
75		if (value == 1)
76			fDiscoverable->SetValue(true);
77		else if (value == 2)
78			fVisible->SetValue(true);
79		else if  (value == 3) {
80			fDiscoverable->SetValue(true);
81			fVisible->SetValue(true);
82		}
83#if 0
84//		TODO implement GetAuthentication in LocalDevice
85		if (fDevice->GetAuthentication())
86			fAuthentication->SetValue(true);
87#endif
88	}
89}
90
91
92void
93ExtendedLocalDeviceView::AttachedToWindow()
94{
95	fDiscoverable->SetTarget(this);
96	fVisible->SetTarget(this);
97	fAuthentication->SetTarget(this);
98}
99
100
101void
102ExtendedLocalDeviceView::SetTarget(BHandler* target)
103{
104	printf("ExtendedLocalDeviceView::SetTarget\n");
105}
106
107
108void
109ExtendedLocalDeviceView::MessageReceived(BMessage* message)
110{
111	if (fDevice == NULL) {
112		printf("ExtendedLocalDeviceView::Device missing\n");
113		BView::MessageReceived(message);
114		return;
115	}
116
117	if (message->WasDropped()) {
118
119	}
120
121	switch (message->what)
122	{
123		case SET_DISCOVERABLE:
124		case SET_VISIBLE:
125			fScanMode = 0;
126
127			if (fDiscoverable->Value())
128				fScanMode = 1;
129
130			if (fVisible->Value())
131				fScanMode = 2;
132
133			if (fVisible->Value() && fDiscoverable->Value())
134				fScanMode = 3;
135
136			if (fDevice != NULL)
137				fDevice->SetDiscoverable(fScanMode);
138
139			break;
140		case SET_AUTHENTICATION:
141			if (fDevice != NULL)
142				fDevice->SetAuthentication(fAuthentication->Value());
143			break;
144
145		default:
146			BView::MessageReceived(message);
147			break;
148	}
149}
150
151
152void
153ExtendedLocalDeviceView::SetEnabled(bool value)
154{
155	fVisible->SetEnabled(value);
156	fAuthentication->SetEnabled(value);
157	fDiscoverable->SetEnabled(value);
158}
159
160
161void
162ExtendedLocalDeviceView::ClearDevice()
163{
164	fVisible->SetValue(false);
165	fAuthentication->SetValue(false);
166	fDiscoverable->SetValue(false);
167}
168