Ticket #9020: WirelessConfigDialog.cpp

File WirelessConfigDialog.cpp, 6.3 KB (added by dsjonny, 11 years ago)
Line 
1/*
2 * WPA Supplicant - Wireless Config Dialog
3 * Copyright (c) 2011, Michael Lotz <mmlr@mlotz.ch>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include <Button.h>
16#include <GridLayout.h>
17#include <GridView.h>
18#include <GroupLayout.h>
19#include <GroupView.h>
20#include <MenuField.h>
21#include <MenuItem.h>
22#include <NetworkDevice.h>
23#include <PopUpMenu.h>
24#include <SpaceLayoutItem.h>
25#include <TextControl.h>
26#include <Window.h>
27#include <View.h>
28
29#include <new>
30// Locale Kit
31#include <Catalog.h>
32
33#undef B_TRANSLATION_CONTEXT
34#define B_TRANSLATION_CONTEXT "wpa_supplicant"
35
36static const uint32 kMessageCancel = 'btcl';
37static const uint32 kMessageOk = 'btok';
38
39
40class WirelessConfigView : public BView {
41public:
42 WirelessConfigView()
43 :
44 BView("WirelessConfigView", B_WILL_DRAW),
45 fPassword(NULL)
46 {
47 SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
48
49 BGroupLayout* rootLayout = new(std::nothrow) BGroupLayout(B_VERTICAL);
50 if (rootLayout == NULL)
51 return;
52
53 SetLayout(rootLayout);
54
55 BGridView* controls = new(std::nothrow) BGridView();
56 if (controls == NULL)
57 return;
58
59 BGridLayout* layout = controls->GridLayout();
60
61 float inset = ceilf(be_plain_font->Size() * 0.7);
62 rootLayout->SetInsets(inset, inset, inset, inset);
63 rootLayout->SetSpacing(inset);
64 layout->SetSpacing(inset, inset);
65
66 fNetworkName = new(std::nothrow) BTextControl(B_TRANSLATE("Network Name:"), "",
67 NULL);
68 if (fNetworkName == NULL)
69 return;
70
71 layout->AddItem(fNetworkName->CreateLabelLayoutItem(), 0, 0);
72 layout->AddItem(fNetworkName->CreateTextViewLayoutItem(), 1, 0);
73
74 BPopUpMenu* authMenu = new(std::nothrow) BPopUpMenu("authMode");
75 if (authMenu == NULL)
76 return;
77
78 fAuthOpen = new(std::nothrow) BMenuItem(B_TRANSLATE("Open"), NULL);
79 authMenu->AddItem(fAuthOpen);
80 fAuthWEP = new(std::nothrow) BMenuItem(B_TRANSLATE("WEP"), NULL);
81 authMenu->AddItem(fAuthWEP);
82 fAuthWPA = new(std::nothrow) BMenuItem(B_TRANSLATE("WPA/WPA2"), NULL);
83 authMenu->AddItem(fAuthWPA);
84
85 BMenuField* authMenuField = new(std::nothrow) BMenuField(
86 "Authentication:", authMenu);
87 if (authMenuField == NULL)
88 return;
89
90 layout->AddItem(authMenuField->CreateLabelLayoutItem(), 0, 1);
91 layout->AddItem(authMenuField->CreateMenuBarLayoutItem(), 1, 1);
92
93 fPassword = new(std::nothrow) BTextControl(B_TRANSLATE("Password:"), "", NULL);
94 if (fPassword == NULL)
95 return;
96
97 BLayoutItem* layoutItem = fPassword->CreateTextViewLayoutItem();
98 layoutItem->SetExplicitMinSize(BSize(fPassword->StringWidth(
99 "0123456789012345678901234567890123456789") + inset,
100 B_SIZE_UNSET));
101
102 layout->AddItem(fPassword->CreateLabelLayoutItem(), 0, 2);
103 layout->AddItem(layoutItem, 1, 2);
104
105 BGroupView* buttons = new(std::nothrow) BGroupView(B_HORIZONTAL);
106 if (buttons == NULL)
107 return;
108
109 fCancelButton = new(std::nothrow) BButton(B_TRANSLATE("Cancel"),
110 new BMessage(kMessageCancel));
111 buttons->GroupLayout()->AddView(fCancelButton);
112
113 buttons->GroupLayout()->AddItem(BSpaceLayoutItem::CreateGlue());
114
115 fOkButton = new(std::nothrow) BButton(B_TRANSLATE("OK"), new BMessage(kMessageOk));
116 buttons->GroupLayout()->AddView(fOkButton);
117
118 rootLayout->AddView(controls);
119 rootLayout->AddView(buttons);
120 }
121
122 virtual void
123 AttachedToWindow()
124 {
125 fCancelButton->SetTarget(Window());
126 fOkButton->SetTarget(Window());
127 }
128
129 void
130 SetUp(BString& networkName, uint32 authMode, BString& password)
131 {
132 fNetworkName->SetText(networkName);
133
134 switch (authMode) {
135 default:
136 case B_NETWORK_AUTHENTICATION_NONE:
137 fAuthOpen->SetMarked(true);
138 break;
139 case B_NETWORK_AUTHENTICATION_WEP:
140 fAuthWEP->SetMarked(true);
141 break;
142 case B_NETWORK_AUTHENTICATION_WPA:
143 case B_NETWORK_AUTHENTICATION_WPA2:
144 fAuthWPA->SetMarked(true);
145 break;
146 }
147
148 fPassword->SetText(password);
149 }
150
151 void
152 Complete(BString& networkName, uint32& authMode, BString& password)
153 {
154 networkName = fNetworkName->Text();
155
156 authMode = B_NETWORK_AUTHENTICATION_NONE;
157 if (fAuthWEP->IsMarked())
158 authMode = B_NETWORK_AUTHENTICATION_WEP;
159 else if (fAuthWPA->IsMarked())
160 authMode = B_NETWORK_AUTHENTICATION_WPA;
161
162 password = fPassword->Text();
163 }
164
165private:
166 BTextControl* fNetworkName;
167 BMenuItem* fAuthOpen;
168 BMenuItem* fAuthWEP;
169 BMenuItem* fAuthWPA;
170 BTextControl* fPassword;
171 BButton* fCancelButton;
172 BButton* fOkButton;
173};
174
175
176class WirelessConfigWindow : public BWindow {
177public:
178 WirelessConfigWindow(BRect frame)
179 :
180 BWindow(BRect(50, 50, 269, 302), B_TRANSLATE("Connect Wireless Network"),
181 B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
182 | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
183 fConfigView(NULL),
184 fDoneSem(-1),
185 fResult(B_ERROR)
186 {
187 fDoneSem = create_sem(0, "wireless config done");
188 if (fDoneSem < 0)
189 return;
190
191 BLayout* layout = new(std::nothrow) BGroupLayout(B_HORIZONTAL);
192 if (layout == NULL)
193 return;
194
195 SetLayout(layout);
196
197 fConfigView = new(std::nothrow) WirelessConfigView();
198 if (fConfigView == NULL)
199 return;
200
201 layout->AddView(fConfigView);
202 }
203
204 virtual
205 ~WirelessConfigWindow()
206 {
207 if (fDoneSem >= 0)
208 delete_sem(fDoneSem);
209 }
210
211 virtual void
212 MessageReceived(BMessage* message)
213 {
214 switch (message->what) {
215 case kMessageCancel:
216 case kMessageOk:
217 fResult = message->what == kMessageCancel ? B_CANCELED : B_OK;
218 release_sem(fDoneSem);
219 return;
220 }
221
222 BWindow::MessageReceived(message);
223 }
224
225 status_t
226 WaitForDialog(BString& networkName, uint32& authMode, BString& password)
227 {
228 fConfigView->SetUp(networkName, authMode, password);
229
230 CenterOnScreen();
231 Show();
232
233 while (acquire_sem(fDoneSem) == B_INTERRUPTED);
234
235 status_t result = fResult;
236 fConfigView->Complete(networkName, authMode, password);
237
238 LockLooper();
239 Quit();
240 return result;
241 }
242
243private:
244 WirelessConfigView* fConfigView;
245 sem_id fDoneSem;
246 status_t fResult;
247};
248
249
250status_t
251wireless_config_dialog(BString& networkName, uint32& authMode,
252 BString& password)
253{
254 WirelessConfigWindow* configWindow
255 = new(std::nothrow) WirelessConfigWindow(BRect(100, 100, 200, 200));
256 if (configWindow == NULL)
257 return B_NO_MEMORY;
258
259 return configWindow->WaitForDialog(networkName, authMode, password);
260}