xref: /haiku/src/preferences/bluetooth/BluetoothWindow.cpp (revision 546208a53940a26c6379c48a7854ade1a8250fc5)
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 
93 	fRevertButton->SetEnabled(false);
94 
95 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)
96 		.Add(fMenubar)
97 		.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
98 		.Add(tabView)
99 		.Add(BSpaceLayoutItem::CreateVerticalStrut(5))
100 		.Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
101 			.Add(fRevertButton)
102 			.AddGlue()
103 			.Add(fDefaultsButton)
104 		)
105 		.SetInsets(5, 5, 5, 5)
106 	);
107 }
108 
109 
110 void
111 BluetoothWindow::MessageReceived(BMessage* message)
112 {
113 	switch (message->what) {
114 		case kMsgSetDefaults:
115 /*			fColorsView -> MessageReceived(new BMessage(DEFAULT_SETTINGS));
116 			fAntialiasingSettings->SetDefaults();
117 			fDefaultsButton->SetEnabled(false);
118 			fRevertButton->SetEnabled(true);
119 */			break;
120 
121 		case kMsgRevert:
122 /*			fColorsView -> MessageReceived(new BMessage(REVERT_SETTINGS));
123 			fAntialiasingSettings->Revert();
124 			fDefaultsButton->SetEnabled(fColorsView->IsDefaultable()
125 								|| fAntialiasingSettings->IsDefaultable());
126 			fRevertButton->SetEnabled(false);
127 */			break;
128 		case kMsgStartServices:
129 			printf("kMsgStartServices\n");
130 			if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
131 				status_t error;
132 
133 				error = be_roster->Launch(BLUETOOTH_SIGNATURE);
134 				printf("kMsgStartServices: %s\n", strerror(error));
135 			}
136 			break;
137 
138 		case kMsgStopServices:
139 			printf("kMsgStopServices\n");
140 			if (be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
141 				status_t error;
142 
143 				error = BMessenger(BLUETOOTH_SIGNATURE).SendMessage(B_QUIT_REQUESTED);
144 				printf("kMsgStopServices: %s\n", strerror(error));
145 			}
146 			break;
147 
148 		case kMsgAddToRemoteList:
149 			PostMessage(message, fRemoteDevices);
150 			break;
151 		case kMsgRefresh:
152 			fSettingsView->MessageReceived(message);
153 			break;
154 		case B_ABOUT_REQUESTED:
155 			be_app->PostMessage(message);
156 			break;
157 		default:
158 			BWindow::MessageReceived(message);
159 			break;
160 	}
161 }
162 
163 
164 bool
165 BluetoothWindow::QuitRequested(void)
166 {
167 	be_app->PostMessage(B_QUIT_REQUESTED);
168 	return true;
169 }
170