xref: /haiku/src/preferences/bluetooth/BluetoothWindow.cpp (revision e381b02559509041a056831116044e5cc1b0b3e4)
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_TRANSLATION_CONTEXT
24 #define B_TRANSLATION_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 	:
38 	BWindow(frame, B_TRANSLATE_SYSTEM_NAME("Bluetooth"), B_TITLED_WINDOW,
39 		B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
40 {
41 	SetLayout(new BGroupLayout(B_HORIZONTAL));
42 
43 	fDefaultsButton = new BButton("defaults", B_TRANSLATE("Defaults"),
44 		new BMessage(kMsgSetDefaults), B_WILL_DRAW);
45 
46 	fRevertButton = new BButton("revert", B_TRANSLATE("Revert"),
47 		new BMessage(kMsgRevert), B_WILL_DRAW);
48 
49 	// Add the menu bar
50 	fMenubar = new BMenuBar(Bounds(), "menu_bar");
51 
52 	// Add File menu to menu bar
53 	BMenu* menu = new BMenu(B_TRANSLATE("Server"));
54 	menu->AddItem(new BMenuItem(
55 		B_TRANSLATE("Start bluetooth services" B_UTF8_ELLIPSIS),
56 		new BMessage(kMsgStartServices), 0));
57 	menu->AddItem(new BMenuItem(
58 		B_TRANSLATE("Stop bluetooth services" B_UTF8_ELLIPSIS),
59 		new BMessage(kMsgStopServices), 0));
60 	menu->AddSeparatorItem();
61 	menu->AddItem(new BMenuItem(
62 		B_TRANSLATE("Show bluetooth console" B_UTF8_ELLIPSIS),
63 		new BMessage(kMsgStartServices), 0));
64 	menu->AddItem(new BMenuItem(
65 		B_TRANSLATE("Refresh LocalDevices" B_UTF8_ELLIPSIS),
66 		new BMessage(kMsgRefresh), 0));
67 	fMenubar->AddItem(menu);
68 
69 	menu = new BMenu(B_TRANSLATE("View"));
70 	menu->AddItem(new BMenuItem(
71 		B_TRANSLATE("Connections & channels" B_UTF8_ELLIPSIS), NULL, 0));
72 	menu->AddItem(new BMenuItem(
73 		B_TRANSLATE("Remote devices list" B_UTF8_ELLIPSIS), NULL, 0));
74 	fMenubar->AddItem(menu);
75 
76 	menu = new BMenu(B_TRANSLATE("Help"));
77 	menu->AddItem(new BMenuItem(B_TRANSLATE("About Bluetooth" B_UTF8_ELLIPSIS),
78 		new BMessage(B_ABOUT_REQUESTED), 0));
79 	fMenubar->AddItem(menu);
80 
81 	BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL);
82 
83 	fSettingsView = new BluetoothSettingsView(B_TRANSLATE("Settings"));
84 //	fConnChan = new ConnChanView("Connections & Channels", B_WILL_DRAW);
85 	fRemoteDevices = new RemoteDevicesView(
86 		B_TRANSLATE("Remote devices"), B_WILL_DRAW);
87 
88 	tabView->AddTab(fRemoteDevices);
89 //	tabView->AddTab(fConnChan);
90 	tabView->AddTab(fSettingsView);
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