xref: /haiku/src/preferences/bluetooth/RemoteDevicesView.cpp (revision c2ee80e5daf4574a3644acf412de0bf31225d329)
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 <Locale.h>
10b973d02fSOliver Ruiz Dorantes #include <Messenger.h>
11b973d02fSOliver Ruiz Dorantes 
1296455f01SOliver Ruiz Dorantes #include <Directory.h>
1396455f01SOliver Ruiz Dorantes #include <Entry.h>
1496455f01SOliver Ruiz Dorantes #include <File.h>
1596455f01SOliver Ruiz Dorantes #include <Path.h>
16b973d02fSOliver Ruiz Dorantes 
17b973d02fSOliver Ruiz Dorantes #include <GroupLayoutBuilder.h>
1896455f01SOliver Ruiz Dorantes #include <SpaceLayoutItem.h>
1996455f01SOliver Ruiz Dorantes 
20b973d02fSOliver Ruiz Dorantes #include <PincodeWindow.h>
21*c2ee80e5SOliver Ruiz Dorantes #include <bluetooth/RemoteDevice.h>
2296455f01SOliver Ruiz Dorantes 
2396455f01SOliver Ruiz Dorantes #include "BluetoothWindow.h"
24*c2ee80e5SOliver Ruiz Dorantes #include "defs.h"
25*c2ee80e5SOliver Ruiz Dorantes #include "DeviceListItem.h"
26*c2ee80e5SOliver Ruiz Dorantes #include "InquiryPanel.h"
27b36b65f9SOliver Ruiz Dorantes #include "RemoteDevicesView.h"
2896455f01SOliver Ruiz Dorantes 
294cbc2061SAdrien Destugues #define TR_CONTEXT "Remote devices"
304cbc2061SAdrien Destugues 
3196455f01SOliver Ruiz Dorantes static const uint32 kMsgAddDevices = 'ddDv';
32623f4e65SOliver Ruiz Dorantes static const uint32 kMsgRemoveDevice = 'rmDv';
33b973d02fSOliver Ruiz Dorantes static const uint32 kMsgPairDevice = 'trDv';
34623f4e65SOliver Ruiz Dorantes static const uint32 kMsgBlockDevice = 'blDv';
35623f4e65SOliver Ruiz Dorantes static const uint32 kMsgRefreshDevices = 'rfDv';
3696455f01SOliver Ruiz Dorantes 
37*c2ee80e5SOliver Ruiz Dorantes using namespace Bluetooth;
38*c2ee80e5SOliver Ruiz Dorantes 
3996455f01SOliver Ruiz Dorantes RemoteDevicesView::RemoteDevicesView(const char* name, uint32 flags)
4096455f01SOliver Ruiz Dorantes  :	BView(name, flags)
4196455f01SOliver Ruiz Dorantes {
4296455f01SOliver Ruiz Dorantes 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
4396455f01SOliver Ruiz Dorantes 
44bef020d3SAdrien Destugues 	addButton = new BButton("add", TR("Add" B_UTF8_ELLIPSIS),
45623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgAddDevices));
4696455f01SOliver Ruiz Dorantes 
47bef020d3SAdrien Destugues 	removeButton = new BButton("remove", TR("Remove"),
48623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgRemoveDevice));
4996455f01SOliver Ruiz Dorantes 
50b973d02fSOliver Ruiz Dorantes 	pairButton = new BButton("pair", TR("Pair" B_UTF8_ELLIPSIS),
51b973d02fSOliver Ruiz Dorantes 		new BMessage(kMsgPairDevice));
5296455f01SOliver Ruiz Dorantes 
5396455f01SOliver Ruiz Dorantes 
549c1a9b92SAdrien Destugues 	blockButton = new BButton("block", TR("As blocked"),
55623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgBlockDevice));
5696455f01SOliver Ruiz Dorantes 
5796455f01SOliver Ruiz Dorantes 
58bef020d3SAdrien Destugues 	availButton = new BButton("check", TR("Refresh" B_UTF8_ELLIPSIS),
59623f4e65SOliver Ruiz Dorantes 		new BMessage(kMsgRefreshDevices));
6096455f01SOliver Ruiz Dorantes 
61623f4e65SOliver Ruiz Dorantes 	// Set up device list
62623f4e65SOliver Ruiz Dorantes 	fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST);
6396455f01SOliver Ruiz Dorantes 
64623f4e65SOliver Ruiz Dorantes 	fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true);
6596455f01SOliver Ruiz Dorantes 	fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
6696455f01SOliver Ruiz Dorantes 
6796455f01SOliver Ruiz Dorantes 	SetLayout(new BGroupLayout(B_VERTICAL));
6896455f01SOliver Ruiz Dorantes 
69cafd739dSOliver Ruiz Dorantes 	// TODO: use all the additional height
7096455f01SOliver Ruiz Dorantes 	AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10)
7196455f01SOliver Ruiz Dorantes 		.Add(fScrollView)
7296455f01SOliver Ruiz Dorantes 		//.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
7396455f01SOliver Ruiz Dorantes 		.Add(BGroupLayoutBuilder(B_VERTICAL)
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)
8096455f01SOliver Ruiz Dorantes 			.Add(blockButton)
8196455f01SOliver Ruiz Dorantes 			.AddGlue()
8296455f01SOliver Ruiz Dorantes 			.SetInsets(0, 15, 0, 15)
8396455f01SOliver Ruiz Dorantes 		)
8496455f01SOliver Ruiz Dorantes 		.SetInsets(5, 5, 5, 100)
8596455f01SOliver Ruiz Dorantes 	);
8696455f01SOliver Ruiz Dorantes 
87623f4e65SOliver Ruiz Dorantes 	fDeviceList->SetSelectionMessage(NULL);
8896455f01SOliver Ruiz Dorantes }
8996455f01SOliver Ruiz Dorantes 
90*c2ee80e5SOliver Ruiz Dorantes 
9196455f01SOliver Ruiz Dorantes RemoteDevicesView::~RemoteDevicesView(void)
9296455f01SOliver Ruiz Dorantes {
9396455f01SOliver Ruiz Dorantes 
9496455f01SOliver Ruiz Dorantes }
9596455f01SOliver Ruiz Dorantes 
96*c2ee80e5SOliver Ruiz Dorantes 
9796455f01SOliver Ruiz Dorantes void
9896455f01SOliver Ruiz Dorantes RemoteDevicesView::AttachedToWindow(void)
9996455f01SOliver Ruiz Dorantes {
100623f4e65SOliver Ruiz Dorantes 	fDeviceList->SetTarget(this);
1013fcbf7dcSOliver Ruiz Dorantes 	addButton->SetTarget(this);
102623f4e65SOliver Ruiz Dorantes 	removeButton->SetTarget(this);
103b973d02fSOliver Ruiz Dorantes 	pairButton->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 
111*c2ee80e5SOliver Ruiz Dorantes 
11296455f01SOliver Ruiz Dorantes void
113*c2ee80e5SOliver Ruiz Dorantes RemoteDevicesView::MessageReceived(BMessage* message)
11496455f01SOliver Ruiz Dorantes {
115*c2ee80e5SOliver Ruiz Dorantes 	switch (message->what) {
1163fcbf7dcSOliver Ruiz Dorantes 		case kMsgAddDevices:
1173fcbf7dcSOliver Ruiz Dorantes 		{
118*c2ee80e5SOliver Ruiz Dorantes 			InquiryPanel* inquiryPanel = new InquiryPanel(BRect(100, 100, 450, 450),
119*c2ee80e5SOliver Ruiz Dorantes 				ActiveLocalDevice);
120*c2ee80e5SOliver Ruiz Dorantes 			inquiryPanel->Show();
1213fcbf7dcSOliver Ruiz Dorantes 			break;
122b973d02fSOliver Ruiz Dorantes 		}
123b973d02fSOliver Ruiz Dorantes 
124623f4e65SOliver Ruiz Dorantes 		case kMsgRemoveDevice:
125623f4e65SOliver Ruiz Dorantes 			printf("kMsgRemoveDevice: %ld\n", fDeviceList->CurrentSelection(0));
126623f4e65SOliver Ruiz Dorantes 			fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0));
127623f4e65SOliver Ruiz Dorantes 			break;
128623f4e65SOliver Ruiz Dorantes 		case kMsgAddToRemoteList:
129623f4e65SOliver Ruiz Dorantes 		{
130623f4e65SOliver Ruiz Dorantes 			BListItem* device;
131*c2ee80e5SOliver Ruiz Dorantes 			message->FindPointer("device", (void**)&device);
132623f4e65SOliver Ruiz Dorantes 			fDeviceList->AddItem(device);
133d254a0ebSOliver Ruiz Dorantes 			fDeviceList->Invalidate();
134623f4e65SOliver Ruiz Dorantes 			break;
135b973d02fSOliver Ruiz Dorantes 		}
136b973d02fSOliver Ruiz Dorantes 
137b973d02fSOliver Ruiz Dorantes 		case kMsgPairDevice:
138b973d02fSOliver Ruiz Dorantes 		{
139*c2ee80e5SOliver Ruiz Dorantes 			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
140*c2ee80e5SOliver Ruiz Dorantes 				->ItemAt(fDeviceList->CurrentSelection(0)));
141*c2ee80e5SOliver Ruiz Dorantes 			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
142b973d02fSOliver Ruiz Dorantes 
143*c2ee80e5SOliver Ruiz Dorantes 			if (remote != NULL)
144*c2ee80e5SOliver Ruiz Dorantes 				remote->Authenticate();
145*c2ee80e5SOliver Ruiz Dorantes 
146b973d02fSOliver Ruiz Dorantes 			break;
147b973d02fSOliver Ruiz Dorantes 		}
148b973d02fSOliver Ruiz Dorantes 
14996455f01SOliver Ruiz Dorantes 		default:
150*c2ee80e5SOliver Ruiz Dorantes 			BView::MessageReceived(message);
15196455f01SOliver Ruiz Dorantes 			break;
15296455f01SOliver Ruiz Dorantes 	}
15396455f01SOliver Ruiz Dorantes }
15496455f01SOliver Ruiz Dorantes 
155*c2ee80e5SOliver Ruiz Dorantes 
15696455f01SOliver Ruiz Dorantes void RemoteDevicesView::LoadSettings(void)
15796455f01SOliver Ruiz Dorantes {
15896455f01SOliver Ruiz Dorantes 
15996455f01SOliver Ruiz Dorantes }
16096455f01SOliver Ruiz Dorantes 
161*c2ee80e5SOliver Ruiz Dorantes 
16296455f01SOliver Ruiz Dorantes bool RemoteDevicesView::IsDefaultable(void)
16396455f01SOliver Ruiz Dorantes {
16496455f01SOliver Ruiz Dorantes 	return true;
16596455f01SOliver Ruiz Dorantes }
16696455f01SOliver Ruiz Dorantes 
167