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> 21c2ee80e5SOliver Ruiz Dorantes #include <bluetooth/RemoteDevice.h> 2296455f01SOliver Ruiz Dorantes 2396455f01SOliver Ruiz Dorantes #include "BluetoothWindow.h" 24c2ee80e5SOliver Ruiz Dorantes #include "defs.h" 25c2ee80e5SOliver Ruiz Dorantes #include "DeviceListItem.h" 26c2ee80e5SOliver 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'; 34ddbc9a0fSOliver Ruiz Dorantes static const uint32 kMsgDisconnectDevice = 'dsDv'; 35623f4e65SOliver Ruiz Dorantes static const uint32 kMsgBlockDevice = 'blDv'; 36623f4e65SOliver Ruiz Dorantes static const uint32 kMsgRefreshDevices = 'rfDv'; 3796455f01SOliver Ruiz Dorantes 38c2ee80e5SOliver Ruiz Dorantes using namespace Bluetooth; 39c2ee80e5SOliver Ruiz Dorantes 4096455f01SOliver Ruiz Dorantes RemoteDevicesView::RemoteDevicesView(const char* name, uint32 flags) 4196455f01SOliver Ruiz Dorantes : BView(name, flags) 4296455f01SOliver Ruiz Dorantes { 4396455f01SOliver Ruiz Dorantes SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 4496455f01SOliver Ruiz Dorantes 45*241ad40bSMatt Madia addButton = new BButton("add", B_TRANSLATE("Add" B_UTF8_ELLIPSIS), 46623f4e65SOliver Ruiz Dorantes new BMessage(kMsgAddDevices)); 4796455f01SOliver Ruiz Dorantes 48*241ad40bSMatt Madia removeButton = new BButton("remove", B_TRANSLATE("Remove"), 49623f4e65SOliver Ruiz Dorantes new BMessage(kMsgRemoveDevice)); 5096455f01SOliver Ruiz Dorantes 51*241ad40bSMatt Madia pairButton = new BButton("pair", B_TRANSLATE("Pair" B_UTF8_ELLIPSIS), 52b973d02fSOliver Ruiz Dorantes new BMessage(kMsgPairDevice)); 5396455f01SOliver Ruiz Dorantes 54*241ad40bSMatt Madia disconnectButton = new BButton("disconnect", B_TRANSLATE("Disconnect"), 55ddbc9a0fSOliver Ruiz Dorantes new BMessage(kMsgDisconnectDevice)); 5696455f01SOliver Ruiz Dorantes 57*241ad40bSMatt Madia blockButton = new BButton("block", B_TRANSLATE("As blocked"), 58623f4e65SOliver Ruiz Dorantes new BMessage(kMsgBlockDevice)); 5996455f01SOliver Ruiz Dorantes 6096455f01SOliver Ruiz Dorantes 61*241ad40bSMatt Madia availButton = new BButton("check", B_TRANSLATE("Refresh" B_UTF8_ELLIPSIS), 62623f4e65SOliver Ruiz Dorantes new BMessage(kMsgRefreshDevices)); 6396455f01SOliver Ruiz Dorantes 64623f4e65SOliver Ruiz Dorantes // Set up device list 65623f4e65SOliver Ruiz Dorantes fDeviceList = new BListView("DeviceList", B_SINGLE_SELECTION_LIST); 6696455f01SOliver Ruiz Dorantes 67623f4e65SOliver Ruiz Dorantes fScrollView = new BScrollView("ScrollView", fDeviceList, 0, false, true); 6896455f01SOliver Ruiz Dorantes fScrollView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR)); 6996455f01SOliver Ruiz Dorantes 7096455f01SOliver Ruiz Dorantes SetLayout(new BGroupLayout(B_VERTICAL)); 7196455f01SOliver Ruiz Dorantes 72cafd739dSOliver Ruiz Dorantes // TODO: use all the additional height 7396455f01SOliver Ruiz Dorantes AddChild(BGroupLayoutBuilder(B_HORIZONTAL, 10) 7496455f01SOliver Ruiz Dorantes .Add(fScrollView) 7596455f01SOliver Ruiz Dorantes //.Add(BSpaceLayoutItem::CreateHorizontalStrut(5)) 7696455f01SOliver Ruiz Dorantes .Add(BGroupLayoutBuilder(B_VERTICAL) 7796455f01SOliver Ruiz Dorantes .Add(addButton) 7896455f01SOliver Ruiz Dorantes .Add(removeButton) 7996455f01SOliver Ruiz Dorantes .AddGlue() 8096455f01SOliver Ruiz Dorantes .Add(availButton) 8196455f01SOliver Ruiz Dorantes .AddGlue() 82b973d02fSOliver Ruiz Dorantes .Add(pairButton) 83ddbc9a0fSOliver Ruiz Dorantes .Add(disconnectButton) 8496455f01SOliver Ruiz Dorantes .Add(blockButton) 8596455f01SOliver Ruiz Dorantes .AddGlue() 8696455f01SOliver Ruiz Dorantes .SetInsets(0, 15, 0, 15) 8796455f01SOliver Ruiz Dorantes ) 8896455f01SOliver Ruiz Dorantes .SetInsets(5, 5, 5, 100) 8996455f01SOliver Ruiz Dorantes ); 9096455f01SOliver Ruiz Dorantes 91623f4e65SOliver Ruiz Dorantes fDeviceList->SetSelectionMessage(NULL); 9296455f01SOliver Ruiz Dorantes } 9396455f01SOliver Ruiz Dorantes 94c2ee80e5SOliver Ruiz Dorantes 9596455f01SOliver Ruiz Dorantes RemoteDevicesView::~RemoteDevicesView(void) 9696455f01SOliver Ruiz Dorantes { 9796455f01SOliver Ruiz Dorantes 9896455f01SOliver Ruiz Dorantes } 9996455f01SOliver Ruiz Dorantes 100c2ee80e5SOliver Ruiz Dorantes 10196455f01SOliver Ruiz Dorantes void 10296455f01SOliver Ruiz Dorantes RemoteDevicesView::AttachedToWindow(void) 10396455f01SOliver Ruiz Dorantes { 104623f4e65SOliver Ruiz Dorantes fDeviceList->SetTarget(this); 1053fcbf7dcSOliver Ruiz Dorantes addButton->SetTarget(this); 106623f4e65SOliver Ruiz Dorantes removeButton->SetTarget(this); 107b973d02fSOliver Ruiz Dorantes pairButton->SetTarget(this); 108ddbc9a0fSOliver Ruiz Dorantes disconnectButton->SetTarget(this); 109623f4e65SOliver Ruiz Dorantes blockButton->SetTarget(this); 110623f4e65SOliver Ruiz Dorantes availButton->SetTarget(this); 11196455f01SOliver Ruiz Dorantes 11296455f01SOliver Ruiz Dorantes LoadSettings(); 113623f4e65SOliver Ruiz Dorantes fDeviceList->Select(0); 11496455f01SOliver Ruiz Dorantes } 11596455f01SOliver Ruiz Dorantes 116c2ee80e5SOliver Ruiz Dorantes 11796455f01SOliver Ruiz Dorantes void 118c2ee80e5SOliver Ruiz Dorantes RemoteDevicesView::MessageReceived(BMessage* message) 11996455f01SOliver Ruiz Dorantes { 120c2ee80e5SOliver Ruiz Dorantes switch (message->what) { 1213fcbf7dcSOliver Ruiz Dorantes case kMsgAddDevices: 1223fcbf7dcSOliver Ruiz Dorantes { 123c2ee80e5SOliver Ruiz Dorantes InquiryPanel* inquiryPanel = new InquiryPanel(BRect(100, 100, 450, 450), 124c2ee80e5SOliver Ruiz Dorantes ActiveLocalDevice); 125c2ee80e5SOliver Ruiz Dorantes inquiryPanel->Show(); 1263fcbf7dcSOliver Ruiz Dorantes break; 127b973d02fSOliver Ruiz Dorantes } 128b973d02fSOliver Ruiz Dorantes 129623f4e65SOliver Ruiz Dorantes case kMsgRemoveDevice: 130623f4e65SOliver Ruiz Dorantes printf("kMsgRemoveDevice: %ld\n", fDeviceList->CurrentSelection(0)); 131623f4e65SOliver Ruiz Dorantes fDeviceList->RemoveItem(fDeviceList->CurrentSelection(0)); 132623f4e65SOliver Ruiz Dorantes break; 133623f4e65SOliver Ruiz Dorantes case kMsgAddToRemoteList: 134623f4e65SOliver Ruiz Dorantes { 135623f4e65SOliver Ruiz Dorantes BListItem* device; 136c2ee80e5SOliver Ruiz Dorantes message->FindPointer("device", (void**)&device); 137623f4e65SOliver Ruiz Dorantes fDeviceList->AddItem(device); 138d254a0ebSOliver Ruiz Dorantes fDeviceList->Invalidate(); 139623f4e65SOliver Ruiz Dorantes break; 140b973d02fSOliver Ruiz Dorantes } 141b973d02fSOliver Ruiz Dorantes 142b973d02fSOliver Ruiz Dorantes case kMsgPairDevice: 143b973d02fSOliver Ruiz Dorantes { 144c2ee80e5SOliver Ruiz Dorantes DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList 145c2ee80e5SOliver Ruiz Dorantes ->ItemAt(fDeviceList->CurrentSelection(0))); 146c2ee80e5SOliver Ruiz Dorantes RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device()); 147b973d02fSOliver Ruiz Dorantes 148c2ee80e5SOliver Ruiz Dorantes if (remote != NULL) 149c2ee80e5SOliver Ruiz Dorantes remote->Authenticate(); 150c2ee80e5SOliver Ruiz Dorantes 151b973d02fSOliver Ruiz Dorantes break; 152b973d02fSOliver Ruiz Dorantes } 153ddbc9a0fSOliver Ruiz Dorantes case kMsgDisconnectDevice: 154ddbc9a0fSOliver Ruiz Dorantes { 155ddbc9a0fSOliver Ruiz Dorantes DeviceListItem* device = static_cast<DeviceListItem*>(fDeviceList 156ddbc9a0fSOliver Ruiz Dorantes ->ItemAt(fDeviceList->CurrentSelection(0))); 157ddbc9a0fSOliver Ruiz Dorantes RemoteDevice* remote = dynamic_cast<RemoteDevice*>(device->Device()); 158ddbc9a0fSOliver Ruiz Dorantes 159ddbc9a0fSOliver Ruiz Dorantes if (remote != NULL) 160ddbc9a0fSOliver Ruiz Dorantes remote->Disconnect(); 161ddbc9a0fSOliver Ruiz Dorantes 162ddbc9a0fSOliver Ruiz Dorantes break; 163ddbc9a0fSOliver Ruiz Dorantes } 164b973d02fSOliver Ruiz Dorantes 16596455f01SOliver Ruiz Dorantes default: 166c2ee80e5SOliver Ruiz Dorantes BView::MessageReceived(message); 16796455f01SOliver Ruiz Dorantes break; 16896455f01SOliver Ruiz Dorantes } 16996455f01SOliver Ruiz Dorantes } 17096455f01SOliver Ruiz Dorantes 171c2ee80e5SOliver Ruiz Dorantes 17296455f01SOliver Ruiz Dorantes void RemoteDevicesView::LoadSettings(void) 17396455f01SOliver Ruiz Dorantes { 17496455f01SOliver Ruiz Dorantes 17596455f01SOliver Ruiz Dorantes } 17696455f01SOliver Ruiz Dorantes 177c2ee80e5SOliver Ruiz Dorantes 17896455f01SOliver Ruiz Dorantes bool RemoteDevicesView::IsDefaultable(void) 17996455f01SOliver Ruiz Dorantes { 18096455f01SOliver Ruiz Dorantes return true; 18196455f01SOliver Ruiz Dorantes } 18296455f01SOliver Ruiz Dorantes 183