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