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 <GroupLayoutBuilder.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
32static const uint32 kMsgAddDevices = 'ddDv';
33static const uint32 kMsgRemoveDevice = 'rmDv';
34static const uint32 kMsgPairDevice = 'trDv';
35static const uint32 kMsgDisconnectDevice = 'dsDv';
36static const uint32 kMsgBlockDevice = 'blDv';
37static const uint32 kMsgRefreshDevices = 'rfDv';
38
39using namespace Bluetooth;
40
41RemoteDevicesView::RemoteDevicesView(const char* name, uint32 flags)
42 :	BView(name, flags)
43{
44	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
45
46	addButton = new BButton("add", B_TRANSLATE("Add" B_UTF8_ELLIPSIS),
47		new BMessage(kMsgAddDevices));
48
49	removeButton = new BButton("remove", B_TRANSLATE("Remove"),
50		new BMessage(kMsgRemoveDevice));
51
52	pairButton = new BButton("pair", B_TRANSLATE("Pair" B_UTF8_ELLIPSIS),
53		new BMessage(kMsgPairDevice));
54
55	disconnectButton = new BButton("disconnect", B_TRANSLATE("Disconnect"),
56		new BMessage(kMsgDisconnectDevice));
57
58	blockButton = new BButton("block", B_TRANSLATE("As blocked"),
59		new BMessage(kMsgBlockDevice));
60
61
62	availButton = new BButton("check", B_TRANSLATE("Refresh" B_UTF8_ELLIPSIS),
63		new BMessage(kMsgRefreshDevices));
64
65	// Set up device list
66	fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST);
67
68	fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true);
69	fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
70
71	SetLayout(new BGroupLayout(B_VERTICAL));
72
73	// TODO: use all the additional height
74	AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10)
75		.Add(fScrollView)
76		//.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
77		.Add(BGroupLayoutBuilder(B_VERTICAL)
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			.SetInsets(0, 15, 0, 15)
88		)
89		.SetInsets(5, 5, 5, 100)
90	);
91
92	fDeviceList->SetSelectionMessage(NULL);
93}
94
95
96RemoteDevicesView::~RemoteDevicesView(void)
97{
98
99}
100
101
102void
103RemoteDevicesView::AttachedToWindow(void)
104{
105	fDeviceList->SetTarget(this);
106	addButton->SetTarget(this);
107	removeButton->SetTarget(this);
108	pairButton->SetTarget(this);
109	disconnectButton->SetTarget(this);
110	blockButton->SetTarget(this);
111	availButton->SetTarget(this);
112
113	LoadSettings();
114	fDeviceList->Select(0);
115}
116
117
118void
119RemoteDevicesView::MessageReceived(BMessage* message)
120{
121	switch (message->what) {
122		case kMsgAddDevices:
123		{
124			InquiryPanel* inquiryPanel = new InquiryPanel(BRect(100, 100, 450, 450),
125				ActiveLocalDevice);
126			inquiryPanel->Show();
127			break;
128		}
129
130		case kMsgRemoveDevice:
131			printf("kMsgRemoveDevice: %ld\n", fDeviceList->CurrentSelection(0));
132			fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0));
133			break;
134		case kMsgAddToRemoteList:
135		{
136			BListItem* device;
137			message->FindPointer("device", (void**)&device);
138			fDeviceList->AddItem(device);
139			fDeviceList->Invalidate();
140			break;
141		}
142
143		case kMsgPairDevice:
144		{
145			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
146				->ItemAt(fDeviceList->CurrentSelection(0)));
147			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
148
149			if (remote != NULL)
150				remote->Authenticate();
151
152			break;
153		}
154		case kMsgDisconnectDevice:
155		{
156			DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList
157				->ItemAt(fDeviceList->CurrentSelection(0)));
158			RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device());
159
160			if (remote != NULL)
161				remote->Disconnect();
162
163			break;
164		}
165
166		default:
167			BView::MessageReceived(message);
168			break;
169	}
170}
171
172
173void RemoteDevicesView::LoadSettings(void)
174{
175
176}
177
178
179bool RemoteDevicesView::IsDefaultable(void)
180{
181	return true;
182}
183
184