xref: /haiku/src/preferences/bluetooth/RemoteDevicesView.cpp (revision 91cbfa855ee63eae9eff3131a2f8712b0333d395)
196455f01SOliver Ruiz Dorantes /*
2*91cbfa85SFredrik Modéen  * Copyright 2008-2009, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes@gmail.com>
3*91cbfa85SFredrik Modéen  * Copyright 2021, Haiku, Inc.
4*91cbfa85SFredrik Modéen  * Distributed under the terms of the MIT License.
5*91cbfa85SFredrik Modéen  *
6*91cbfa85SFredrik Modéen  * Authors:
7*91cbfa85SFredrik Modéen  * 		Fredrik Modéen <fredrik_at_modeen.se>
896455f01SOliver Ruiz Dorantes  */
9*91cbfa85SFredrik Modéen 
10b36b65f9SOliver Ruiz Dorantes #include <stdio.h>
1196455f01SOliver Ruiz Dorantes 
1296455f01SOliver Ruiz Dorantes #include <Alert.h>
13bef020d3SAdrien Destugues #include <Catalog.h>
14b973d02fSOliver Ruiz Dorantes #include <Messenger.h>
15b973d02fSOliver Ruiz Dorantes 
1696455f01SOliver Ruiz Dorantes #include <Directory.h>
1796455f01SOliver Ruiz Dorantes #include <Entry.h>
1896455f01SOliver Ruiz Dorantes #include <File.h>
1996455f01SOliver Ruiz Dorantes #include <Path.h>
20b973d02fSOliver Ruiz Dorantes 
21deaef0ebSAugustin Cavalier #include <LayoutBuilder.h>
2296455f01SOliver Ruiz Dorantes #include <SpaceLayoutItem.h>
2396455f01SOliver Ruiz Dorantes 
24b973d02fSOliver Ruiz Dorantes #include <PincodeWindow.h>
25c2ee80e5SOliver Ruiz Dorantes #include <bluetooth/RemoteDevice.h>
2696455f01SOliver Ruiz Dorantes 
2796455f01SOliver Ruiz Dorantes #include "BluetoothWindow.h"
28c2ee80e5SOliver Ruiz Dorantes #include "defs.h"
29c2ee80e5SOliver Ruiz Dorantes #include "DeviceListItem.h"
30c2ee80e5SOliver Ruiz Dorantes #include "InquiryPanel.h"
31b36b65f9SOliver Ruiz Dorantes #include "RemoteDevicesView.h"
3296455f01SOliver Ruiz Dorantes 
338eff03f5SOliver Tappe 
34546208a5SOliver Tappe #undef B_TRANSLATION_CONTEXT
35546208a5SOliver Tappe #define B_TRANSLATION_CONTEXT "Remote devices"
364cbc2061SAdrien Destugues 
3796455f01SOliver Ruiz Dorantes static const uint32 kMsgAddDevices = 'ddDv';
38623f4e65SOliver Ruiz Dorantes static const uint32 kMsgRemoveDevice = 'rmDv';
39b973d02fSOliver Ruiz Dorantes static const uint32 kMsgPairDevice = 'trDv';
40ddbc9a0fSOliver Ruiz Dorantes static const uint32 kMsgDisconnectDevice = 'dsDv';
41*91cbfa85SFredrik Modéen //static const uint32 kMsgBlockDevice = 'blDv';
42*91cbfa85SFredrik Modéen //static const uint32 kMsgRefreshDevices = 'rfDv';
4396455f01SOliver Ruiz Dorantes 
44c2ee80e5SOliver Ruiz Dorantes using namespace Bluetooth;
45c2ee80e5SOliver Ruiz Dorantes 
RemoteDevicesView(const char * name,uint32 flags)4696455f01SOliver Ruiz Dorantes RemoteDevicesView::RemoteDevicesView(const char* name, uint32 flags)
4796455f01SOliver Ruiz Dorantes  :	BView(name, flags)
4896455f01SOliver Ruiz Dorantes {
49241ad40bSMatt Madia 	addButton = new BButton("add", B_TRANSLATE("Add" B_UTF8_ELLIPSIS),
50623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgAddDevices));
5196455f01SOliver Ruiz Dorantes 
52241ad40bSMatt Madia 	removeButton = new BButton("remove", B_TRANSLATE("Remove"),
53623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgRemoveDevice));
5496455f01SOliver Ruiz Dorantes 
55241ad40bSMatt Madia 	pairButton = new BButton("pair", B_TRANSLATE("Pair" B_UTF8_ELLIPSIS),
56b973d02fSOliver Ruiz Dorantes 		new BMessage(kMsgPairDevice));
5796455f01SOliver Ruiz Dorantes 
58241ad40bSMatt Madia 	disconnectButton = new BButton("disconnect", B_TRANSLATE("Disconnect"),
59ddbc9a0fSOliver Ruiz Dorantes 		new BMessage(kMsgDisconnectDevice));
60*91cbfa85SFredrik Modéen /*
61241ad40bSMatt Madia 	blockButton = new BButton("block", B_TRANSLATE("As blocked"),
62623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgBlockDevice));
6396455f01SOliver Ruiz Dorantes 
64241ad40bSMatt Madia 	availButton = new BButton("check", B_TRANSLATE("Refresh" B_UTF8_ELLIPSIS),
65623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgRefreshDevices));
66*91cbfa85SFredrik Modéen */
67623f4e65SOliver Ruiz Dorantes 	// Set up device list
68623f4e65SOliver Ruiz Dorantes 	fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST);
6996455f01SOliver Ruiz Dorantes 
70623f4e65SOliver Ruiz Dorantes 	fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true);
7196455f01SOliver Ruiz Dorantes 
72deaef0ebSAugustin Cavalier 	BLayoutBuilder::Group<>(this, B_HORIZONTAL, 10)
73deaef0ebSAugustin Cavalier 		.SetInsets(5)
7496455f01SOliver Ruiz Dorantes 		.Add(fScrollView)
7596455f01SOliver Ruiz Dorantes 		//.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
76deaef0ebSAugustin Cavalier 		.AddGroup(B_VERTICAL)
77deaef0ebSAugustin Cavalier 			.SetInsets(0, 15, 0, 15)
7896455f01SOliver Ruiz Dorantes 			.Add(addButton)
7996455f01SOliver Ruiz Dorantes 			.Add(removeButton)
8096455f01SOliver Ruiz Dorantes 			.AddGlue()
81*91cbfa85SFredrik Modéen //			.Add(availButton)
82*91cbfa85SFredrik Modéen 	//		.AddGlue()
83b973d02fSOliver Ruiz Dorantes 			.Add(pairButton)
84ddbc9a0fSOliver Ruiz Dorantes 			.Add(disconnectButton)
85*91cbfa85SFredrik Modéen //			.Add(blockButton)
8696455f01SOliver Ruiz Dorantes 			.AddGlue()
87deaef0ebSAugustin Cavalier 		.End()
88deaef0ebSAugustin Cavalier 	.End();
8996455f01SOliver Ruiz Dorantes 
90623f4e65SOliver Ruiz Dorantes 	fDeviceList->SetSelectionMessage(NULL);
9196455f01SOliver Ruiz Dorantes }
9296455f01SOliver Ruiz Dorantes 
93c2ee80e5SOliver Ruiz Dorantes 
~RemoteDevicesView(void)9496455f01SOliver Ruiz Dorantes RemoteDevicesView::~RemoteDevicesView(void)
9596455f01SOliver Ruiz Dorantes {
9696455f01SOliver Ruiz Dorantes 
9796455f01SOliver Ruiz Dorantes }
9896455f01SOliver Ruiz Dorantes 
99c2ee80e5SOliver Ruiz Dorantes 
10096455f01SOliver Ruiz Dorantes void
AttachedToWindow(void)10196455f01SOliver Ruiz Dorantes RemoteDevicesView::AttachedToWindow(void)
10296455f01SOliver Ruiz Dorantes {
103623f4e65SOliver Ruiz Dorantes 	fDeviceList->SetTarget(this);
1043fcbf7dcSOliver Ruiz Dorantes 	addButton->SetTarget(this);
105623f4e65SOliver Ruiz Dorantes 	removeButton->SetTarget(this);
106b973d02fSOliver Ruiz Dorantes 	pairButton->SetTarget(this);
107ddbc9a0fSOliver Ruiz Dorantes 	disconnectButton->SetTarget(this);
108*91cbfa85SFredrik Modéen //	blockButton->SetTarget(this);
109*91cbfa85SFredrik Modéen //	availButton->SetTarget(this);
11096455f01SOliver Ruiz Dorantes 
11196455f01SOliver Ruiz Dorantes 	LoadSettings();
112623f4e65SOliver Ruiz Dorantes 	fDeviceList->Select(0);
11396455f01SOliver Ruiz Dorantes }
11496455f01SOliver Ruiz Dorantes 
115c2ee80e5SOliver Ruiz Dorantes 
11696455f01SOliver Ruiz Dorantes void
MessageReceived(BMessage * message)117c2ee80e5SOliver Ruiz Dorantes RemoteDevicesView::MessageReceived(BMessage* message)
11896455f01SOliver Ruiz Dorantes {
119c2ee80e5SOliver Ruiz Dorantes 	switch (message->what) {
1203fcbf7dcSOliver Ruiz Dorantes 		case kMsgAddDevices:
1213fcbf7dcSOliver Ruiz Dorantes 		{
122a4a85baeSAlexander von Gluck IV 			InquiryPanel* inquiryPanel= new InquiryPanel(
123a4a85baeSAlexander von Gluck IV 				BRect(100, 100, 450, 450), ActiveLocalDevice);
124c2ee80e5SOliver Ruiz Dorantes 			inquiryPanel->Show();
1253fcbf7dcSOliver Ruiz Dorantes 			break;
126b973d02fSOliver Ruiz Dorantes 		}
127b973d02fSOliver Ruiz Dorantes 
128623f4e65SOliver Ruiz Dorantes 		case kMsgRemoveDevice:
129623f4e65SOliver Ruiz Dorantes 			fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0));
130623f4e65SOliver Ruiz Dorantes 			break;
131623f4e65SOliver Ruiz Dorantes 		case kMsgAddToRemoteList:
132623f4e65SOliver Ruiz Dorantes 		{
133623f4e65SOliver Ruiz Dorantes 			BListItem* device;
134c2ee80e5SOliver Ruiz Dorantes 			message->FindPointer("device", (void**)&device);
135623f4e65SOliver Ruiz Dorantes 			fDeviceList->AddItem(device);
136d254a0ebSOliver Ruiz Dorantes 			fDeviceList->Invalidate();
137623f4e65SOliver Ruiz Dorantes 			break;
138b973d02fSOliver Ruiz Dorantes 		}
139b973d02fSOliver Ruiz Dorantes 
140b973d02fSOliver Ruiz Dorantes 		case kMsgPairDevice:
141b973d02fSOliver Ruiz Dorantes 		{
142c2ee80e5SOliver Ruiz Dorantes 			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
143c2ee80e5SOliver Ruiz Dorantes 				->ItemAt(fDeviceList->CurrentSelection(0)));
144f52a021aSAugustin Cavalier 			if (device == NULL)
145f52a021aSAugustin Cavalier 				break;
146b973d02fSOliver Ruiz Dorantes 
147f52a021aSAugustin Cavalier 			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
148f52a021aSAugustin Cavalier 			if (remote == NULL)
149f52a021aSAugustin Cavalier 				break;
150f52a021aSAugustin Cavalier 
151c2ee80e5SOliver Ruiz Dorantes 			remote->Authenticate();
152c2ee80e5SOliver Ruiz Dorantes 
153b973d02fSOliver Ruiz Dorantes 			break;
154b973d02fSOliver Ruiz Dorantes 		}
155ddbc9a0fSOliver Ruiz Dorantes 		case kMsgDisconnectDevice:
156ddbc9a0fSOliver Ruiz Dorantes 		{
157ddbc9a0fSOliver Ruiz Dorantes 			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
158ddbc9a0fSOliver Ruiz Dorantes 				->ItemAt(fDeviceList->CurrentSelection(0)));
159f52a021aSAugustin Cavalier 			if (device == NULL)
160f52a021aSAugustin Cavalier 				break;
161ddbc9a0fSOliver Ruiz Dorantes 
162f52a021aSAugustin Cavalier 			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
163f52a021aSAugustin Cavalier 			if (remote == NULL)
164f52a021aSAugustin Cavalier 				break;
165f52a021aSAugustin Cavalier 
166ddbc9a0fSOliver Ruiz Dorantes 			remote->Disconnect();
167ddbc9a0fSOliver Ruiz Dorantes 
168ddbc9a0fSOliver Ruiz Dorantes 			break;
169ddbc9a0fSOliver Ruiz Dorantes 		}
170b973d02fSOliver Ruiz Dorantes 
17196455f01SOliver Ruiz Dorantes 		default:
172c2ee80e5SOliver Ruiz Dorantes 			BView::MessageReceived(message);
17396455f01SOliver Ruiz Dorantes 			break;
17496455f01SOliver Ruiz Dorantes 	}
17596455f01SOliver Ruiz Dorantes }
17696455f01SOliver Ruiz Dorantes 
177c2ee80e5SOliver Ruiz Dorantes 
LoadSettings(void)17896455f01SOliver Ruiz Dorantes void RemoteDevicesView::LoadSettings(void)
17996455f01SOliver Ruiz Dorantes {
18096455f01SOliver Ruiz Dorantes 
18196455f01SOliver Ruiz Dorantes }
18296455f01SOliver Ruiz Dorantes 
183c2ee80e5SOliver Ruiz Dorantes 
IsDefaultable(void)18496455f01SOliver Ruiz Dorantes bool RemoteDevicesView::IsDefaultable(void)
18596455f01SOliver Ruiz Dorantes {
18696455f01SOliver Ruiz Dorantes 	return true;
18796455f01SOliver Ruiz Dorantes }
18896455f01SOliver Ruiz Dorantes 
189