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