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 <stdio.h>
11
12#include <Alert.h>
13#include <Catalog.h>
14#include <Messenger.h>
15
16#include <Directory.h>
17#include <Entry.h>
18#include <File.h>
19#include <Path.h>
20
21#include <LayoutBuilder.h>
22#include <SpaceLayoutItem.h>
23
24#include <PincodeWindow.h>
25#include <bluetooth/RemoteDevice.h>
26
27#include "BluetoothWindow.h"
28#include "defs.h"
29#include "DeviceListItem.h"
30#include "InquiryPanel.h"
31#include "RemoteDevicesView.h"
32
33
34#undef B_TRANSLATION_CONTEXT
35#define B_TRANSLATION_CONTEXT "Remote devices"
36
37static const uint32 kMsgAddDevices = 'ddDv';
38static const uint32 kMsgRemoveDevice = 'rmDv';
39static const uint32 kMsgPairDevice = 'trDv';
40static const uint32 kMsgDisconnectDevice = 'dsDv';
41//static const uint32 kMsgBlockDevice = 'blDv';
42//static const uint32 kMsgRefreshDevices = 'rfDv';
43
44using namespace Bluetooth;
45
46RemoteDevicesView::RemoteDevicesView(const char* name, uint32 flags)
47 :	BView(name, flags)
48{
49	addButton = new BButton("add", B_TRANSLATE("Add" B_UTF8_ELLIPSIS),
50		new BMessage(kMsgAddDevices));
51
52	removeButton = new BButton("remove", B_TRANSLATE("Remove"),
53		new BMessage(kMsgRemoveDevice));
54
55	pairButton = new BButton("pair", B_TRANSLATE("Pair" B_UTF8_ELLIPSIS),
56		new BMessage(kMsgPairDevice));
57
58	disconnectButton = new BButton("disconnect", B_TRANSLATE("Disconnect"),
59		new BMessage(kMsgDisconnectDevice));
60/*
61	blockButton = new BButton("block", B_TRANSLATE("As blocked"),
62		new BMessage(kMsgBlockDevice));
63
64	availButton = new BButton("check", B_TRANSLATE("Refresh" B_UTF8_ELLIPSIS),
65		new BMessage(kMsgRefreshDevices));
66*/
67	// Set up device list
68	fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST);
69
70	fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true);
71
72	BLayoutBuilder::Group<>(this, B_HORIZONTAL, 10)
73		.SetInsets(5)
74		.Add(fScrollView)
75		//.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
76		.AddGroup(B_VERTICAL)
77			.SetInsets(0, 15, 0, 15)
78			.Add(addButton)
79			.Add(removeButton)
80			.AddGlue()
81//			.Add(availButton)
82	//		.AddGlue()
83			.Add(pairButton)
84			.Add(disconnectButton)
85//			.Add(blockButton)
86			.AddGlue()
87		.End()
88	.End();
89
90	fDeviceList->SetSelectionMessage(NULL);
91}
92
93
94RemoteDevicesView::~RemoteDevicesView(void)
95{
96
97}
98
99
100void
101RemoteDevicesView::AttachedToWindow(void)
102{
103	fDeviceList->SetTarget(this);
104	addButton->SetTarget(this);
105	removeButton->SetTarget(this);
106	pairButton->SetTarget(this);
107	disconnectButton->SetTarget(this);
108//	blockButton->SetTarget(this);
109//	availButton->SetTarget(this);
110
111	LoadSettings();
112	fDeviceList->Select(0);
113}
114
115
116void
117RemoteDevicesView::MessageReceived(BMessage* message)
118{
119	switch (message->what) {
120		case kMsgAddDevices:
121		{
122			InquiryPanel* inquiryPanel= new InquiryPanel(
123				BRect(100, 100, 450, 450), ActiveLocalDevice);
124			inquiryPanel->Show();
125			break;
126		}
127
128		case kMsgRemoveDevice:
129			fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0));
130			break;
131		case kMsgAddToRemoteList:
132		{
133			BListItem* device;
134			message->FindPointer("device", (void**)&device);
135			fDeviceList->AddItem(device);
136			fDeviceList->Invalidate();
137			break;
138		}
139
140		case kMsgPairDevice:
141		{
142			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
143				->ItemAt(fDeviceList->CurrentSelection(0)));
144			if (device == NULL)
145				break;
146
147			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
148			if (remote == NULL)
149				break;
150
151			remote->Authenticate();
152
153			break;
154		}
155		case kMsgDisconnectDevice:
156		{
157			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
158				->ItemAt(fDeviceList->CurrentSelection(0)));
159			if (device == NULL)
160				break;
161
162			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
163			if (remote == NULL)
164				break;
165
166			remote->Disconnect();
167
168			break;
169		}
170
171		default:
172			BView::MessageReceived(message);
173			break;
174	}
175}
176
177
178void RemoteDevicesView::LoadSettings(void)
179{
180
181}
182
183
184bool RemoteDevicesView::IsDefaultable(void)
185{
186	return true;
187}
188
189