2 * Copyright 2005, Waldemar Kornewald <wkornew@gmx.net>
3 * Distributed under the terms of the MIT License.
6 #include "ConnectionView.h"
7 #include "PPPDeskbarReplicant.h"
8 #include <MessageDriverSettingsUtils.h>
10 #include <Application.h>
17 #include <StringView.h>
18 #include <TextControl.h>
21 #include <PPPInterface.h>
22 #include <settings_tools.h>
29 static const uint32 kDefaultButtonWidth
= 80;
32 static const uint32 kMsgCancel
= 'CANC';
33 static const uint32 kMsgConnect
= 'CONN';
34 static const uint32 kMsgUpdate
= 'MUPD';
37 static const char *kLabelSavePassword
= "Save password";
38 static const char *kLabelName
= "Username: ";
39 static const char *kLabelPassword
= "Password: ";
40 static const char *kLabelConnect
= "Connect";
41 static const char *kLabelCancel
= "Cancel";
42 static const char *kLabelAuthentication
= "Authentication";
44 // connection status strings
45 static const char *kTextConnecting
= "Connecting...";
46 static const char *kTextConnectionEstablished
= "Connection established.";
47 static const char *kTextNotConnected
= "Not connected.";
48 static const char *kTextDeviceUpFailed
= "Failed to connect.";
49 static const char *kTextAuthenticating
= "Authenticating...";
50 static const char *kTextAuthenticationFailed
= "Authentication failed!";
51 static const char *kTextConnectionLost
= "Connection lost!";
54 ConnectionView::ConnectionView(BRect rect
, const BString
& interfaceName
)
55 : BView(rect
, "ConnectionView", B_FOLLOW_NONE
, 0),
57 fInterfaceName(interfaceName
),
60 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
64 rect
.bottom
= rect
.top
65 + 25 // space for topmost control
66 + 3 * 20 // size of controls
67 + 3 * 5; // space beween controls and bottom of box
68 BBox
*authenticationBox
= new BBox(rect
, "Authentication");
69 authenticationBox
->SetLabel(kLabelAuthentication
);
70 rect
= authenticationBox
->Bounds();
72 rect
.bottom
= rect
.top
+ 20;
73 fUsername
= new BTextControl(rect
, "username", kLabelName
, NULL
, NULL
);
74 rect
.top
= rect
.bottom
+ 5;
75 rect
.bottom
= rect
.top
+ 20;
76 fPassword
= new BTextControl(rect
, "password", kLabelPassword
, NULL
, NULL
);
77 fPassword
->TextView()->HideTyping(true);
80 float width
= max(StringWidth(fUsername
->Label()),
81 StringWidth(fPassword
->Label()));
82 fUsername
->SetDivider(width
+ 5);
83 fPassword
->SetDivider(width
+ 5);
85 rect
.top
= rect
.bottom
+ 5;
86 rect
.bottom
= rect
.top
+ 20;
87 fSavePassword
= new BCheckBox(rect
, "SavePassword", kLabelSavePassword
, NULL
);
89 authenticationBox
->AddChild(fUsername
);
90 authenticationBox
->AddChild(fPassword
);
91 authenticationBox
->AddChild(fSavePassword
);
92 AddChild(authenticationBox
);
94 rect
= authenticationBox
->Frame();
95 rect
.top
= rect
.bottom
+ 10;
96 rect
.bottom
= rect
.top
+ 15;
97 fAttemptView
= new BStringView(rect
, "AttemptView", "");
98 AddChild(fAttemptView
);
101 rect
.top
= rect
.bottom
+ 5;
102 rect
.bottom
= rect
.top
+ 15;
103 fStatusView
= new BStringView(rect
, "StatusView", "");
104 AddChild(fStatusView
);
106 // add "Connect" and "Cancel" buttons
107 rect
.top
= rect
.bottom
+ 10;
108 rect
.bottom
= rect
.top
+ 25;
109 rect
.right
= rect
.left
+ kDefaultButtonWidth
;
110 fConnectButton
= new BButton(rect
, "ConnectButton", kLabelConnect
,
111 new BMessage(kMsgConnect
));
113 rect
.left
= rect
.right
+ 10;
114 rect
.right
= rect
.left
+ kDefaultButtonWidth
;
115 fCancelButton
= new BButton(rect
, "CancelButton", kLabelCancel
,
116 new BMessage(kMsgCancel
));
118 AddChild(fConnectButton
);
119 AddChild(fCancelButton
);
124 ConnectionView::AttachedToWindow()
127 fListener
.WatchManager();
128 WatchInterface(fListener
.Manager().InterfaceWithName(fInterfaceName
.String()));
130 Window()->SetDefaultButton(fConnectButton
);
131 fConnectButton
->SetTarget(this);
132 fCancelButton
->SetTarget(this);
137 ConnectionView::MessageReceived(BMessage
*message
)
139 switch(message
->what
) {
140 case PPP_REPORT_MESSAGE
:
141 HandleReportMessage(message
);
153 BView::MessageReceived(message
);
158 // update authentication UI
160 ConnectionView::Reload()
162 // load username and password
163 BString
path("ptpnet/");
164 path
<< fInterfaceName
;
165 fSettings
.MakeEmpty();
166 ReadMessageDriverSettings(path
.String(), &fSettings
);
168 fHasUsername
= fHasPassword
= false;
169 BString username
, password
;
172 int32 parameterIndex
= 0;
173 if(FindMessageParameter(PPP_USERNAME_KEY
, fSettings
, ¶meter
, ¶meterIndex
)
174 && parameter
.FindString(MDSU_VALUES
, &username
) == B_OK
)
178 if(FindMessageParameter(PPP_PASSWORD_KEY
, fSettings
, ¶meter
, ¶meterIndex
)
179 && parameter
.FindString(MDSU_VALUES
, &password
) == B_OK
)
182 fUsername
->SetText(username
.String());
183 fPassword
->SetText(password
.String());
184 fSavePassword
->SetValue(fHasPassword
);
186 fUsername
->SetEnabled(fHasUsername
);
187 fPassword
->SetEnabled(fHasUsername
);
188 fSavePassword
->SetEnabled(fHasUsername
);
193 ConnectionView::Connect()
195 PPPInterface
interface(PPPManager().CreateInterfaceWithName(
196 fInterfaceName
.String()));
197 interface
.SetUsername(Username());
198 interface
.SetPassword(Password());
199 interface
.SetAskBeforeConnecting(false);
206 if(FindMessageParameter(PPP_USERNAME_KEY
, fSettings
, ¶meter
, &index
))
207 fSettings
.RemoveData(MDSU_PARAMETERS
, index
);
208 parameter
.MakeEmpty();
209 parameter
.AddString(MDSU_NAME
, PPP_USERNAME_KEY
);
210 parameter
.AddString(MDSU_VALUES
, Username());
211 fSettings
.AddMessage(MDSU_PARAMETERS
, ¶meter
);
214 if(FindMessageParameter(PPP_PASSWORD_KEY
, fSettings
, ¶meter
, &index
))
215 fSettings
.RemoveData(MDSU_PARAMETERS
, index
);
216 if(DoesSavePassword()) {
217 parameter
.MakeEmpty();
218 parameter
.AddString(MDSU_NAME
, PPP_PASSWORD_KEY
);
219 parameter
.AddString(MDSU_VALUES
, Password());
220 fSettings
.AddMessage(MDSU_PARAMETERS
, ¶meter
);
224 if(interface
.GetSettingsEntry(&entry
) == B_OK
) {
225 BFile
file(&entry
, B_WRITE_ONLY
);
226 WriteMessageDriverSettings(file
, fSettings
);
235 ConnectionView::Cancel()
237 PPPInterface
interface(fListener
.Interface());
239 ppp_interface_info_t info
;
240 if(interface
.GetInterfaceInfo(&info
) && info
.info
.phase
< PPP_ESTABLISHMENT_PHASE
)
248 // Clean up before our window quits (called by ConnectionWindow).
250 ConnectionView::CleanUp()
252 fListener
.StopWatchingInterface();
253 fListener
.StopWatchingManager();
258 ConnectionView::AttemptString() const
260 PPPInterface
interface(fListener
.Interface());
261 ppp_interface_info_t info
;
262 if(!interface
.GetInterfaceInfo(&info
))
265 attempt
<< "Attempt " << info
.info
.connectAttempt
<< " of " <<
266 info
.info
.connectRetriesLimit
+ 1;
273 ConnectionView::HandleReportMessage(BMessage
*message
)
276 if(message
->FindInt32("interface", reinterpret_cast<int32
*>(&id
)) != B_OK
277 || (fListener
.Interface() != PPP_UNDEFINED_INTERFACE_ID
278 && id
!= fListener
.Interface()))
282 message
->FindInt32("type", &type
);
283 message
->FindInt32("code", &code
);
285 if(type
== PPP_MANAGER_REPORT
&& code
== PPP_REPORT_INTERFACE_CREATED
) {
286 PPPInterface
interface(id
);
287 if(interface
.InitCheck() != B_OK
|| fInterfaceName
!= interface
.Name())
292 if(((fHasUsername
&& !fHasPassword
) || fAskBeforeConnecting
)
293 && Window()->IsHidden())
295 } else if(type
== PPP_CONNECTION_REPORT
)
297 else if(type
== PPP_DESTRUCTION_REPORT
)
298 fListener
.StopWatchingInterface();
303 ConnectionView::UpdateStatus(int32 code
)
305 BString attemptString
= AttemptString();
306 fAttemptView
->SetText(attemptString
.String());
308 if(code
== PPP_REPORT_UP_SUCCESSFUL
) {
309 fStatusView
->SetText(kTextConnectionEstablished
);
310 PPPDeskbarReplicant
*item
= new PPPDeskbarReplicant(fListener
.Interface());
311 BDeskbar().AddItem(item
);
317 // maybe the status string must not be changed (codes that set fKeepLabel to false
318 // should still be handled)
319 if(fKeepLabel
&& code
!= PPP_REPORT_GOING_UP
&& code
!= PPP_REPORT_UP_SUCCESSFUL
)
322 if(fListener
.InitCheck() != B_OK
) {
323 fStatusView
->SetText(kTextConnectionLost
);
327 // only errors should set fKeepLabel to true
329 case PPP_REPORT_GOING_UP
:
331 fStatusView
->SetText(kTextConnecting
);
334 case PPP_REPORT_DOWN_SUCCESSFUL
:
335 fStatusView
->SetText(kTextNotConnected
);
338 case PPP_REPORT_DEVICE_UP_FAILED
:
340 fStatusView
->SetText(kTextDeviceUpFailed
);
343 case PPP_REPORT_AUTHENTICATION_REQUESTED
:
344 fStatusView
->SetText(kTextAuthenticating
);
347 case PPP_REPORT_AUTHENTICATION_FAILED
:
349 fStatusView
->SetText(kTextAuthenticationFailed
);
352 case PPP_REPORT_CONNECTION_LOST
:
354 fStatusView
->SetText(kTextConnectionLost
);
361 ConnectionView::WatchInterface(ppp_interface_id ID
)
363 fListener
.WatchInterface(ID
);
367 PPPInterface
interface(fListener
.Interface());
368 ppp_interface_info_t info
;
369 if(!interface
.GetInterfaceInfo(&info
)) {
370 UpdateStatus(PPP_REPORT_DOWN_SUCCESSFUL
);
371 fAskBeforeConnecting
= false;
373 fAskBeforeConnecting
= info
.info
.askBeforeConnecting
;