xref: /haiku/src/preferences/bluetooth/RemoteDevicesView.cpp (revision deaef0eb2edd684a4417dda8ce93ea2c9e5ef6b7)
196455f01SOliver Ruiz Dorantes /*
296455f01SOliver Ruiz Dorantes  * Copyright 2008-09, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com>
396455f01SOliver Ruiz Dorantes  * All rights reserved. Distributed under the terms of the MIT License.
496455f01SOliver Ruiz Dorantes  */
5b36b65f9SOliver Ruiz Dorantes #include <stdio.h>
696455f01SOliver Ruiz Dorantes 
796455f01SOliver Ruiz Dorantes #include <Alert.h>
8bef020d3SAdrien Destugues #include <Catalog.h>
9b973d02fSOliver Ruiz Dorantes #include <Messenger.h>
10b973d02fSOliver Ruiz Dorantes 
1196455f01SOliver Ruiz Dorantes #include <Directory.h>
1296455f01SOliver Ruiz Dorantes #include <Entry.h>
1396455f01SOliver Ruiz Dorantes #include <File.h>
1496455f01SOliver Ruiz Dorantes #include <Path.h>
15b973d02fSOliver Ruiz Dorantes 
16*deaef0ebSAugustin Cavalier #include <LayoutBuilder.h>
1796455f01SOliver Ruiz Dorantes #include <SpaceLayoutItem.h>
1896455f01SOliver Ruiz Dorantes 
19b973d02fSOliver Ruiz Dorantes #include <PincodeWindow.h>
20c2ee80e5SOliver Ruiz Dorantes #include <bluetooth/RemoteDevice.h>
2196455f01SOliver Ruiz Dorantes 
2296455f01SOliver Ruiz Dorantes #include "BluetoothWindow.h"
23c2ee80e5SOliver Ruiz Dorantes #include "defs.h"
24c2ee80e5SOliver Ruiz Dorantes #include "DeviceListItem.h"
25c2ee80e5SOliver Ruiz Dorantes #include "InquiryPanel.h"
26b36b65f9SOliver Ruiz Dorantes #include "RemoteDevicesView.h"
2796455f01SOliver Ruiz Dorantes 
288eff03f5SOliver Tappe 
29546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
30546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "Remote devices"
314cbc2061SAdrien Destugues 
3296455f01SOliver Ruiz Dorantes static const uint32 kMsgAddDevices = 'ddDv';
33623f4e65SOliver Ruiz Dorantes static const uint32 kMsgRemoveDevice = 'rmDv';
34b973d02fSOliver Ruiz Dorantes static const uint32 kMsgPairDevice = 'trDv';
35ddbc9a0fSOliver Ruiz Dorantes static const uint32 kMsgDisconnectDevice = 'dsDv';
36623f4e65SOliver Ruiz Dorantes static const uint32 kMsgBlockDevice = 'blDv';
37623f4e65SOliver Ruiz Dorantes static const uint32 kMsgRefreshDevices = 'rfDv';
3896455f01SOliver Ruiz Dorantes 
39c2ee80e5SOliver Ruiz Dorantes using namespace Bluetooth;
40c2ee80e5SOliver Ruiz Dorantes 
4196455f01SOliver Ruiz Dorantes RemoteDevicesView::RemoteDevicesView(const char* name, uint32 flags)
4296455f01SOliver Ruiz Dorantes  :	BView(name, flags)
4396455f01SOliver Ruiz Dorantes {
44241ad40bSMatt Madia 	addButton = new BButton("add", B_TRANSLATE("Add" B_UTF8_ELLIPSIS),
45623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgAddDevices));
4696455f01SOliver Ruiz Dorantes 
47241ad40bSMatt Madia 	removeButton = new BButton("remove", B_TRANSLATE("Remove"),
48623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgRemoveDevice));
4996455f01SOliver Ruiz Dorantes 
50241ad40bSMatt Madia 	pairButton = new BButton("pair", B_TRANSLATE("Pair" B_UTF8_ELLIPSIS),
51b973d02fSOliver Ruiz Dorantes 		new BMessage(kMsgPairDevice));
5296455f01SOliver Ruiz Dorantes 
53241ad40bSMatt Madia 	disconnectButton = new BButton("disconnect", B_TRANSLATE("Disconnect"),
54ddbc9a0fSOliver Ruiz Dorantes 		new BMessage(kMsgDisconnectDevice));
5596455f01SOliver Ruiz Dorantes 
56241ad40bSMatt Madia 	blockButton = new BButton("block", B_TRANSLATE("As blocked"),
57623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgBlockDevice));
5896455f01SOliver Ruiz Dorantes 
5996455f01SOliver Ruiz Dorantes 
60241ad40bSMatt Madia 	availButton = new BButton("check", B_TRANSLATE("Refresh" B_UTF8_ELLIPSIS),
61623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgRefreshDevices));
6296455f01SOliver Ruiz Dorantes 
63623f4e65SOliver Ruiz Dorantes 	// Set up device list
64623f4e65SOliver Ruiz Dorantes 	fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST);
6596455f01SOliver Ruiz Dorantes 
66623f4e65SOliver Ruiz Dorantes 	fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true);
6796455f01SOliver Ruiz Dorantes 
68*deaef0ebSAugustin Cavalier 	BLayoutBuilder::Group<>(this, B_HORIZONTAL, 10)
69*deaef0ebSAugustin Cavalier 		.SetInsets(5)
7096455f01SOliver Ruiz Dorantes 		.Add(fScrollView)
7196455f01SOliver Ruiz Dorantes 		//.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
72*deaef0ebSAugustin Cavalier 		.AddGroup(B_VERTICAL)
73*deaef0ebSAugustin Cavalier 			.SetInsets(0, 15, 0, 15)
7496455f01SOliver Ruiz Dorantes 			.Add(addButton)
7596455f01SOliver Ruiz Dorantes 			.Add(removeButton)
7696455f01SOliver Ruiz Dorantes 			.AddGlue()
7796455f01SOliver Ruiz Dorantes 			.Add(availButton)
7896455f01SOliver Ruiz Dorantes 			.AddGlue()
79b973d02fSOliver Ruiz Dorantes 			.Add(pairButton)
80ddbc9a0fSOliver Ruiz Dorantes 			.Add(disconnectButton)
8196455f01SOliver Ruiz Dorantes 			.Add(blockButton)
8296455f01SOliver Ruiz Dorantes 			.AddGlue()
83*deaef0ebSAugustin Cavalier 		.End()
84*deaef0ebSAugustin Cavalier 	.End();
8596455f01SOliver Ruiz Dorantes 
86623f4e65SOliver Ruiz Dorantes 	fDeviceList->SetSelectionMessage(NULL);
8796455f01SOliver Ruiz Dorantes }
8896455f01SOliver Ruiz Dorantes 
89c2ee80e5SOliver Ruiz Dorantes 
9096455f01SOliver Ruiz Dorantes RemoteDevicesView::~RemoteDevicesView(void)
9196455f01SOliver Ruiz Dorantes {
9296455f01SOliver Ruiz Dorantes 
9396455f01SOliver Ruiz Dorantes }
9496455f01SOliver Ruiz Dorantes 
95c2ee80e5SOliver Ruiz Dorantes 
9696455f01SOliver Ruiz Dorantes void
9796455f01SOliver Ruiz Dorantes RemoteDevicesView::AttachedToWindow(void)
9896455f01SOliver Ruiz Dorantes {
99623f4e65SOliver Ruiz Dorantes 	fDeviceList->SetTarget(this);
1003fcbf7dcSOliver Ruiz Dorantes 	addButton->SetTarget(this);
101623f4e65SOliver Ruiz Dorantes 	removeButton->SetTarget(this);
102b973d02fSOliver Ruiz Dorantes 	pairButton->SetTarget(this);
103ddbc9a0fSOliver Ruiz Dorantes 	disconnectButton->SetTarget(this);
104623f4e65SOliver Ruiz Dorantes 	blockButton->SetTarget(this);
105623f4e65SOliver Ruiz Dorantes 	availButton->SetTarget(this);
10696455f01SOliver Ruiz Dorantes 
10796455f01SOliver Ruiz Dorantes 	LoadSettings();
108623f4e65SOliver Ruiz Dorantes 	fDeviceList->Select(0);
10996455f01SOliver Ruiz Dorantes }
11096455f01SOliver Ruiz Dorantes 
111c2ee80e5SOliver Ruiz Dorantes 
11296455f01SOliver Ruiz Dorantes void
113c2ee80e5SOliver Ruiz Dorantes RemoteDevicesView::MessageReceived(BMessage* message)
11496455f01SOliver Ruiz Dorantes {
115c2ee80e5SOliver Ruiz Dorantes 	switch (message->what) {
1163fcbf7dcSOliver Ruiz Dorantes 		case kMsgAddDevices:
1173fcbf7dcSOliver Ruiz Dorantes 		{
118a4a85baeSAlexander von Gluck IV 			InquiryPanel* inquiryPanel= new InquiryPanel(
119a4a85baeSAlexander von Gluck IV 				BRect(100, 100, 450, 450), ActiveLocalDevice);
120c2ee80e5SOliver Ruiz Dorantes 			inquiryPanel->Show();
1213fcbf7dcSOliver Ruiz Dorantes 			break;
122b973d02fSOliver Ruiz Dorantes 		}
123b973d02fSOliver Ruiz Dorantes 
124623f4e65SOliver Ruiz Dorantes 		case kMsgRemoveDevice:
125a4a85baeSAlexander von Gluck IV 			printf("kMsgRemoveDevice: %" B_PRId32 "\n",
126a4a85baeSAlexander von Gluck IV 				fDeviceList->CurrentSelection(0));
127623f4e65SOliver Ruiz Dorantes 			fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0));
128623f4e65SOliver Ruiz Dorantes 			break;
129623f4e65SOliver Ruiz Dorantes 		case kMsgAddToRemoteList:
130623f4e65SOliver Ruiz Dorantes 		{
131623f4e65SOliver Ruiz Dorantes 			BListItem* device;
132c2ee80e5SOliver Ruiz Dorantes 			message->FindPointer("device", (void**)&device);
133623f4e65SOliver Ruiz Dorantes 			fDeviceList->AddItem(device);
134d254a0ebSOliver Ruiz Dorantes 			fDeviceList->Invalidate();
135623f4e65SOliver Ruiz Dorantes 			break;
136b973d02fSOliver Ruiz Dorantes 		}
137b973d02fSOliver Ruiz Dorantes 
138b973d02fSOliver Ruiz Dorantes 		case kMsgPairDevice:
139b973d02fSOliver Ruiz Dorantes 		{
140c2ee80e5SOliver Ruiz Dorantes 			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
141c2ee80e5SOliver Ruiz Dorantes 				->ItemAt(fDeviceList->CurrentSelection(0)));
142c2ee80e5SOliver Ruiz Dorantes 			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
143b973d02fSOliver Ruiz Dorantes 
144c2ee80e5SOliver Ruiz Dorantes 			if (remote != NULL)
145c2ee80e5SOliver Ruiz Dorantes 				remote->Authenticate();
146c2ee80e5SOliver Ruiz Dorantes 
147b973d02fSOliver Ruiz Dorantes 			break;
148b973d02fSOliver Ruiz Dorantes 		}
149ddbc9a0fSOliver Ruiz Dorantes 		case kMsgDisconnectDevice:
150ddbc9a0fSOliver Ruiz Dorantes 		{
151ddbc9a0fSOliver Ruiz Dorantes 			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
152ddbc9a0fSOliver Ruiz Dorantes 				->ItemAt(fDeviceList->CurrentSelection(0)));
153ddbc9a0fSOliver Ruiz Dorantes 			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
154ddbc9a0fSOliver Ruiz Dorantes 
155ddbc9a0fSOliver Ruiz Dorantes 			if (remote != NULL)
156ddbc9a0fSOliver Ruiz Dorantes 				remote->Disconnect();
157ddbc9a0fSOliver Ruiz Dorantes 
158ddbc9a0fSOliver Ruiz Dorantes 			break;
159ddbc9a0fSOliver Ruiz Dorantes 		}
160b973d02fSOliver Ruiz Dorantes 
16196455f01SOliver Ruiz Dorantes 		default:
162c2ee80e5SOliver Ruiz Dorantes 			BView::MessageReceived(message);
16396455f01SOliver Ruiz Dorantes 			break;
16496455f01SOliver Ruiz Dorantes 	}
16596455f01SOliver Ruiz Dorantes }
16696455f01SOliver Ruiz Dorantes 
167c2ee80e5SOliver Ruiz Dorantes 
16896455f01SOliver Ruiz Dorantes void RemoteDevicesView::LoadSettings(void)
16996455f01SOliver Ruiz Dorantes {
17096455f01SOliver Ruiz Dorantes 
17196455f01SOliver Ruiz Dorantes }
17296455f01SOliver Ruiz Dorantes 
173c2ee80e5SOliver Ruiz Dorantes 
17496455f01SOliver Ruiz Dorantes bool RemoteDevicesView::IsDefaultable(void)
17596455f01SOliver Ruiz Dorantes {
17696455f01SOliver Ruiz Dorantes 	return true;
17796455f01SOliver Ruiz Dorantes }
17896455f01SOliver Ruiz Dorantes 
179