2 * Copyright 2016-2017, Rene Gollent, rene@gollent.com.
3 * Distributed under the terms of the MIT License.
5 #include "ConnectionConfigWindow.h"
7 #include <Application.h>
10 #include <MenuField.h>
11 #include <LayoutBuilder.h>
13 #include <AutoDeleter.h>
14 #include <AutoLocker.h>
16 #include "AppMessageCodes.h"
17 #include "ConnectionConfigHandlerRoster.h"
19 #include "TargetHostInterfaceInfo.h"
20 #include "TargetHostInterfaceRoster.h"
21 #include "TargetHostInterface.h"
25 MSG_SWITCH_CONNECTION_TYPE
= 'swct',
26 MSG_CREATE_CONNECTION
= 'crco'
30 ConnectionConfigWindow::ConnectionConfigWindow()
32 BWindow(BRect(), "Create new connection", B_TITLED_WINDOW
,
33 B_AUTO_UPDATE_SIZE_LIMITS
| B_CLOSE_ON_ESCAPE
),
34 ConnectionConfigView::Listener(),
35 fConnectionTypeField(NULL
),
36 fConfigGroupView(NULL
),
39 fCurrentSettings(NULL
),
45 ConnectionConfigWindow::~ConnectionConfigWindow()
47 if (fCurrentSettings
!= NULL
)
48 fCurrentSettings
->ReleaseReference();
52 ConnectionConfigWindow
*
53 ConnectionConfigWindow::Create()
55 ConnectionConfigWindow
* self
= new ConnectionConfigWindow();
70 ConnectionConfigWindow::Show()
78 ConnectionConfigWindow::QuitRequested()
80 return be_app_messenger
.SendMessage(MSG_CONNECTION_CONFIG_WINDOW_CLOSED
)
86 ConnectionConfigWindow::ConfigurationChanged(Settings
* settings
)
88 if (fCurrentSettings
!= NULL
)
89 fCurrentSettings
->ReleaseReference();
90 fCurrentSettings
= settings
;
91 fCurrentSettings
->AcquireReference();
93 fConnectButton
->SetEnabled(fActiveInfo
->IsConfigured(settings
));
98 ConnectionConfigWindow::MessageReceived(BMessage
* message
)
100 switch (message
->what
) {
101 case MSG_CREATE_CONNECTION
:
103 TargetHostInterfaceRoster
* roster
104 = TargetHostInterfaceRoster::Default();
105 AutoLocker
<TargetHostInterfaceRoster
> rosterLocker(roster
);
107 TargetHostInterface
* interface
;
108 status_t error
= roster
->CreateInterface(fActiveInfo
,
109 fCurrentSettings
, interface
);
111 // TODO: notify user.
115 PostMessage(B_QUIT_REQUESTED
);
119 BWindow::MessageReceived(message
);
126 ConnectionConfigWindow::_Init()
128 BMenu
* menu
= _BuildTypeMenu();
130 fConfigGroupView
= new BGroupView(B_HORIZONTAL
);
132 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
133 .SetInsets(B_USE_DEFAULT_SPACING
)
134 .Add(fConnectionTypeField
= new BMenuField("Type:", menu
))
135 .AddGroup(fConfigGroupView
)
136 // this group is a placeholder to contain
137 // the actual config view
139 .AddGroup(B_HORIZONTAL
)
140 .SetInsets(B_USE_SMALL_SPACING
)
142 .Add(fCloseButton
= new BButton("Close",
143 new BMessage(B_QUIT_REQUESTED
)))
144 .Add(fConnectButton
= new BButton("Connect",
145 new BMessage(MSG_CREATE_CONNECTION
)))
148 fConnectionTypeField
->Menu()->SetLabelFromMarked(true);
149 fConnectionTypeField
->Menu()->SetTargetForItems(this);
151 fCloseButton
->SetTarget(this);
152 fConnectButton
->SetTarget(this);
153 fConnectButton
->SetEnabled(false);
155 if (menu
->CountItems() > 0) {
156 BMenuItem
* item
= menu
->ItemAt(0);
157 BMessage
* message
= item
->Message();
158 TargetHostInterfaceInfo
* info
= NULL
;
159 if (message
->FindPointer("info", reinterpret_cast<void**>(&info
))
161 _UpdateActiveConfig(info
);
162 menu
->ItemAt(0)->SetMarked(true);
169 ConnectionConfigWindow::_BuildTypeMenu()
171 BMenu
* menu
= new BMenu("Types");
172 TargetHostInterfaceRoster
* roster
= TargetHostInterfaceRoster::Default();
174 AutoLocker
<TargetHostInterfaceRoster
> rosterLocker(roster
);
176 for (int32 i
= 0; i
< roster
->CountInterfaceInfos(); i
++) {
177 TargetHostInterfaceInfo
* info
= roster
->InterfaceInfoAt(i
);
181 BMenuItem
* item
= new BMenuItem(info
->Name(), new BMessage(
182 MSG_SWITCH_CONNECTION_TYPE
));
183 item
->Message()->AddPointer("info", info
);
192 ConnectionConfigWindow::_UpdateActiveConfig(TargetHostInterfaceInfo
* info
)
194 if (fConfigGroupView
->CountChildren() > 0) {
195 BView
* view
= fConfigGroupView
->ChildAt(0);
196 fConfigGroupView
->RemoveChild(view
);
200 ConnectionConfigHandlerRoster
* roster
201 = ConnectionConfigHandlerRoster::Default();
203 if (roster
->HasHandlerFor(info
)) {
204 ConnectionConfigView
* view
= NULL
;
205 status_t error
= roster
->CreateConfigView(info
, this, view
);
209 ObjectDeleter
<ConnectionConfigView
> viewDeleter(view
);
210 BLayoutItem
* item
= fConfigGroupView
->GroupLayout()->AddView(view
);
212 viewDeleter
.Detach();
217 fConfigGroupView
->InvalidateLayout();