xref: /haiku/src/preferences/bluetooth/BluetoothWindow.cpp (revision f1d1e8297bfe73811358625c8b7601c69c93e02b)
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 
6 
7 #include "BluetoothWindow.h"
8 #include "RemoteDevicesView.h"
9 
10 #include <Button.h>
11 #include <Catalog.h>
12 #include <LayoutBuilder.h>
13 #include <Messenger.h>
14 #include <Roster.h>
15 #include <TabView.h>
16 
17 #include <stdio.h>
18 
19 #include <bluetooth/LocalDevice.h>
20 
21 #include "defs.h"
22 
23 
24 #undef B_TRANSLATION_CONTEXT
25 #define B_TRANSLATION_CONTEXT "Window"
26 
27 static const uint32 kMsgSetDefaults = 'dflt';
28 static const uint32 kMsgRevert = 'rvrt';
29 
30 static const uint32 kMsgStartServices = 'SrSR';
31 static const uint32 kMsgStopServices = 'StST';
32 static const uint32 kMsgShowDebug = 'ShDG';
33 
34 LocalDevice* ActiveLocalDevice = NULL;
35 
36 
37 BluetoothWindow::BluetoothWindow(BRect frame)
38 	:
39 	BWindow(frame, B_TRANSLATE_SYSTEM_NAME("Bluetooth"), B_TITLED_WINDOW,
40 		B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
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("Help"));
69 	menu->AddItem(new BMenuItem(B_TRANSLATE("About Bluetooth" B_UTF8_ELLIPSIS),
70 		new BMessage(B_ABOUT_REQUESTED), 0));
71 	fMenubar->AddItem(menu);
72 
73 	BTabView* tabView = new BTabView("tabview", B_WIDTH_FROM_LABEL);
74 
75 	fSettingsView = new BluetoothSettingsView(B_TRANSLATE("Settings"));
76 //	fConnChan = new ConnChanView("Connections & Channels", B_WILL_DRAW);
77 	fRemoteDevices = new RemoteDevicesView(
78 		B_TRANSLATE("Remote devices"), B_WILL_DRAW);
79 
80 	tabView->AddTab(fRemoteDevices);
81 //	tabView->AddTab(fConnChan);
82 	tabView->AddTab(fSettingsView);
83 
84 	fRevertButton->SetEnabled(false);
85 
86 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
87 		.SetInsets(0)
88 		.Add(fMenubar)
89 		.AddStrut(B_USE_HALF_ITEM_SPACING)
90 		.Add(tabView)
91 		.AddGroup(B_HORIZONTAL, B_USE_HALF_ITEM_SPACING)
92 			.AddGlue()
93 			.Add(fDefaultsButton)
94 			.Add(fRevertButton)
95 		.End()
96 	.End();
97 }
98 
99 
100 void
101 BluetoothWindow::MessageReceived(BMessage* message)
102 {
103 	switch (message->what) {
104 		case kMsgSetDefaults:
105 /*			fColorsView -> MessageReceived(new BMessage(DEFAULT_SETTINGS));
106 			fAntialiasingSettings->SetDefaults();
107 			fDefaultsButton->SetEnabled(false);
108 			fRevertButton->SetEnabled(true);
109 */			break;
110 
111 		case kMsgRevert:
112 /*			fColorsView -> MessageReceived(new BMessage(REVERT_SETTINGS));
113 			fAntialiasingSettings->Revert();
114 			fDefaultsButton->SetEnabled(fColorsView->IsDefaultable()
115 								|| fAntialiasingSettings->IsDefaultable());
116 			fRevertButton->SetEnabled(false);
117 */			break;
118 		case kMsgStartServices:
119 			printf("kMsgStartServices\n");
120 			if (!be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
121 				status_t error = be_roster->Launch(BLUETOOTH_SIGNATURE);
122 				printf("kMsgStartServices: %s\n", strerror(error));
123 			}
124 			break;
125 
126 		case kMsgStopServices:
127 			printf("kMsgStopServices\n");
128 			if (be_roster->IsRunning(BLUETOOTH_SIGNATURE)) {
129 				status_t error = BMessenger(BLUETOOTH_SIGNATURE).SendMessage(B_QUIT_REQUESTED);
130 				printf("kMsgStopServices: %s\n", strerror(error));
131 			}
132 			break;
133 
134 		case kMsgAddToRemoteList:
135 			PostMessage(message, fRemoteDevices);
136 			break;
137 		case kMsgRefresh:
138 			fSettingsView->MessageReceived(message);
139 			break;
140 		case B_ABOUT_REQUESTED:
141 			be_app->PostMessage(message);
142 			break;
143 		default:
144 			BWindow::MessageReceived(message);
145 			break;
146 	}
147 }
148 
149 
150 bool
151 BluetoothWindow::QuitRequested()
152 {
153 	be_app->PostMessage(B_QUIT_REQUESTED);
154 	return true;
155 }
156