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