xref: /haiku/src/preferences/bluetooth/RemoteDevicesView.cpp (revision 3c6e2dd68577c34d93e17f19711f6245bf6d0915)
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 <Locale.h>
10 #include <Messenger.h>
11 
12 #include <Directory.h>
13 #include <Entry.h>
14 #include <File.h>
15 #include <Path.h>
16 
17 #include <GroupLayoutBuilder.h>
18 #include <SpaceLayoutItem.h>
19 
20 #include <PincodeWindow.h>
21 
22 #include "defs.h"
23 #include "InquiryPanel.h"
24 #include "BluetoothWindow.h"
25 
26 #include "RemoteDevicesView.h"
27 
28 #define TR_CONTEXT "Remote devices"
29 
30 static const uint32 kMsgAddDevices = 'ddDv';
31 static const uint32 kMsgRemoveDevice = 'rmDv';
32 static const uint32 kMsgPairDevice = 'trDv';
33 static const uint32 kMsgBlockDevice = 'blDv';
34 static const uint32 kMsgRefreshDevices = 'rfDv';
35 
36 RemoteDevicesView::RemoteDevicesView(const char *name, uint32 flags)
37  :	BView(name, flags)
38 {
39 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
40 
41 	addButton = new BButton("add", TR("Add" B_UTF8_ELLIPSIS),
42 										new BMessage(kMsgAddDevices));
43 
44 	removeButton = new BButton("remove", TR("Remove"),
45 										new BMessage(kMsgRemoveDevice));
46 
47 	pairButton = new BButton("pair", TR("Pair" B_UTF8_ELLIPSIS),
48 										new BMessage(kMsgPairDevice));
49 
50 
51 	blockButton = new BButton("block", TR("As blocked"),
52 										new BMessage(kMsgBlockDevice));
53 
54 
55 	availButton = new BButton("check", TR("Refresh" B_UTF8_ELLIPSIS),
56 										new BMessage(kMsgRefreshDevices));
57 
58 	// Set up device list
59 	fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST);
60 
61 	fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true);
62 	fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
63 
64 	SetLayout(new BGroupLayout(B_VERTICAL));
65 
66 	// TODO: use all the additional height
67 	AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10)
68 		.Add(fScrollView)
69 		//.Add(BSpaceLayoutItem::CreateHorizontalStrut(5))
70 		.Add(BGroupLayoutBuilder(B_VERTICAL)
71 						.Add(addButton)
72 						.Add(removeButton)
73 						.AddGlue()
74 						.Add(availButton)
75 						.AddGlue()
76 						.Add(pairButton)
77 						.Add(blockButton)
78 						.AddGlue()
79 						.SetInsets(0, 15, 0, 15)
80 			)
81 		.SetInsets(5, 5, 5, 100)
82 	);
83 
84 	fDeviceList->SetSelectionMessage(NULL);
85 }
86 
87 RemoteDevicesView::~RemoteDevicesView(void)
88 {
89 
90 }
91 
92 void
93 RemoteDevicesView::AttachedToWindow(void)
94 {
95 	fDeviceList->SetTarget(this);
96 	addButton->SetTarget(this);
97 	removeButton->SetTarget(this);
98 	pairButton->SetTarget(this);
99 	blockButton->SetTarget(this);
100 	availButton->SetTarget(this);
101 
102 	LoadSettings();
103 	fDeviceList->Select(0);
104 }
105 
106 void
107 RemoteDevicesView::MessageReceived(BMessage *msg)
108 {
109 	printf("what = %ld\n", msg->what);
110 	switch(msg->what) {
111 		case kMsgAddDevices:
112 		{
113 			InquiryPanel* iPanel = new InquiryPanel(BRect(100,100,450,450), ActiveLocalDevice);
114 			iPanel->Show();
115 			break;
116 		}
117 
118 		case kMsgRemoveDevice:
119 			printf("kMsgRemoveDevice: %ld\n", fDeviceList->CurrentSelection(0));
120 			fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0));
121 			break;
122 		case kMsgAddToRemoteList:
123 		{
124 			BListItem* device;
125 			msg->FindPointer("device", (void**)&device);
126 			fDeviceList->AddItem(device);
127 			fDeviceList->Invalidate();
128 			break;
129 		}
130 
131 		case kMsgPairDevice:
132 		{
133 			bdaddr_t address;
134 			address.b[5] = 0x55;
135 			address.b[4] = 0x44;
136 			address.b[3] = 0x33;
137 			address.b[2] = 0x22;
138 			address.b[1] = 0x11;
139 			address.b[0] = 0x00;
140 
141 			PincodeWindow* iPincode = new PincodeWindow(address, 0);
142 			iPincode->Show();
143 			break;
144 		}
145 
146 		default:
147 			BView::MessageReceived(msg);
148 			break;
149 	}
150 }
151 
152 void RemoteDevicesView::LoadSettings(void)
153 {
154 
155 }
156 
157 bool RemoteDevicesView::IsDefaultable(void)
158 {
159 	return true;
160 }
161 
162