xref: /haiku/src/preferences/bluetooth/BluetoothWindow.cpp (revision ec568e04e56fab30a12e8dbf7bf3d39d9e8ed6c2)
1 /*
2  * Copyright 2008-10, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com>
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #include "BluetoothWindow.h"
6 
7 
8 #include <Button.h>
9 #include <Catalog.h>
10 #include <GroupLayoutBuilder.h>
11 #include <Messenger.h>
12 #include <SpaceLayoutItem.h>
13 #include <TabView.h>
14 #include <Roster.h>
15 #include <stdio.h>
16 
17 #include <bluetooth/LocalDevice.h>
18 #include "RemoteDevicesView.h"
19 
20 #include "defs.h"
21 
22 
23 #undef B_TRANSLATE_CONTEXT
24 #define B_TRANSLATE_CONTEXT "Window"
25 
26 static const uint32 kMsgSetDefaults = 'dflt';
27 static const uint32 kMsgRevert = 'rvrt';
28 
29 static const uint32 kMsgStartServices = 'SrSR';
30 static const uint32 kMsgStopServices = 'StST';
31 static const uint32 kMsgShowDebug = 'ShDG';
32 
33 LocalDevice* ActiveLocalDevice = NULL;
34 
35 
36 BluetoothWindow::BluetoothWindow(BRect frame)
37  :	BWindow(frame, B_TRANSLATE("Bluetooth"), B_TITLED_WINDOW,
38  		B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
39 {
40 	SetLayout(new BGroupLayout(B_HORIZONTAL));
41 
42 	fDefaultsButton = new BButton("defaults", B_TRANSLATE("Defaults"),
43 		new BMessage(kMsgSetDefaults), B_WILL_DRAW);
44 
45 	fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
46 		new BMessage(kMsgRevert), B_WILL_DRAW);
47 
48 	// Add the menu bar
49 	fMenubar = new BMenuBar(Bounds(), "menu_bar");
50 
51 	// Add File menu to menu bar
52 	BMenu* menu = new BMenu(B_TRANSLATE("Server"));
53 	menu->AddItem(new BMenuItem(
54 		B_TRANSLATE("Start bluetooth services" B_UTF8_ELLIPSIS),
55 		new BMessage(kMsgStartServices), 0));
56 	menu->AddItem(new BMenuItem(
57 		B_TRANSLATE("Stop bluetooth services" B_UTF8_ELLIPSIS),
58 		new BMessage(kMsgStopServices), 0));
59 	menu->AddSeparatorItem();
60 	menu->AddItem(new BMenuItem(
61 		B_TRANSLATE("Show bluetooth console" B_UTF8_ELLIPSIS),
62 		new BMessage(kMsgStartServices), 0));
63 	menu->AddItem(new BMenuItem(
64 		B_TRANSLATE("Refresh LocalDevices" B_UTF8_ELLIPSIS),
65 		new BMessage(kMsgRefresh), 0));
66 	fMenubar->AddItem(menu);
67 
68 	menu = new BMenu(B_TRANSLATE("View"));
69 	menu->AddItem(new BMenuItem(
70 		B_TRANSLATE("Connections & channels" B_UTF8_ELLIPSIS), NULL, 0));
71 	menu->AddItem(new BMenuItem(
72 		B_TRANSLATE("Remote devices list" B_UTF8_ELLIPSIS), NULL, 0));
73 	fMenubar->AddItem(menu);
74 
75 	menu = new BMenu(B_TRANSLATE("Help"));
76 	menu->AddItem(new BMenuItem(B_TRANSLATE("About Bluetooth" B_UTF8_ELLIPSIS),
77 		new BMessage(B_ABOUT_REQUESTED), 0));
78 	fMenubar->AddItem(menu);
79 
80 	BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL);
81 
82 	fSettingsView = new BluetoothSettingsView(B_TRANSLATE("Settings"));
83 //	fConnChan = new ConnChanView("Connections & Channels", B_WILL_DRAW);
84 	fRemoteDevices = new RemoteDevicesView(
85 		B_TRANSLATE("Remote devices"), B_WILL_DRAW);
86 
87 	tabView->AddTab(fRemoteDevices);
88 //	tabView->AddTab(fConnChan);
89 	tabView->AddTab(fSettingsView);
90 
91 
92 	fRevertButton->SetEnabled(false);
93 
94 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
95 		.Add(fMenubar)
96 		.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
97 		.Add(tabView)
98 		.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
99 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
100 			.Add(fRevertButton)
101 			.AddGlue()
102 			.Add(fDefaultsButton)
103 		)
104 		.SetInsets(5, 5, 5, 5)
105 	);
106 }
107 
108 
109 void
110 BluetoothWindow::MessageReceived(BMessage* message)
111 {
112 	switch (message->what) {
113 		case kMsgSetDefaults:
114 /*			fColorsView -> MessageReceived(new BMessage(DEFAULT_SETTINGS));
115 			fAntialiasingSettings->SetDefaults();
116 			fDefaultsButton->SetEnabled(false);
117 			fRevertButton->SetEnabled(true);
118 */			break;
119 
120 		case kMsgRevert:
121 /*			fColorsView -> MessageReceived(new BMessage(REVERT_SETTINGS));
122 			fAntialiasingSettings->Revert();
123 			fDefaultsButton->SetEnabled(fColorsView->IsDefaultable()
124 								|| fAntialiasingSettings->IsDefaultable());
125 			fRevertButton->SetEnabled(false);
126 */			break;
127 		case kMsgStartServices:
128 			printf("kMsgStartServices\n");
129 			if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
130 				status_t error;
131 
132 				error = be_roster->Launch(BLUETOOTH_SIGNATURE);
133 				printf("kMsgStartServices: %s\n", strerror(error));
134 			}
135 			break;
136 
137 		case kMsgStopServices:
138 			printf("kMsgStopServices\n");
139 			if (be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
140 				status_t error;
141 
142 				error = BMessenger(BLUETOOTH_SIGNATURE).SendMessage(B_QUIT_REQUESTED);
143 				printf("kMsgStopServices: %s\n", strerror(error));
144 			}
145 			break;
146 
147 		case kMsgAddToRemoteList:
148 			PostMessage(message, fRemoteDevices);
149 			break;
150 		case kMsgRefresh:
151 			fSettingsView->MessageReceived(message);
152 			break;
153 		case B_ABOUT_REQUESTED:
154 			be_app->PostMessage(message);
155 			break;
156 		default:
157 			BWindow::MessageReceived(message);
158 			break;
159 	}
160 }
161 
162 
163 bool
164 BluetoothWindow::QuitRequested(void)
165 {
166 	be_app->PostMessage(B_QUIT_REQUESTED);
167 	return true;
168 }
169