11953ec9dSRene Gollent/*
2a1afac4dSRene Gollent * Copyright 2013-2016, Rene Gollent, rene@gollent.com.
31953ec9dSRene Gollent * Distributed under the terms of the MIT License.
41953ec9dSRene Gollent */
51953ec9dSRene Gollent#include "StartTeamWindow.h"
61953ec9dSRene Gollent
71953ec9dSRene Gollent#include <Alert.h>
81953ec9dSRene Gollent#include <Application.h>
91953ec9dSRene Gollent#include <Button.h>
101953ec9dSRene Gollent#include <FilePanel.h>
111953ec9dSRene Gollent#include <LayoutBuilder.h>
121953ec9dSRene Gollent#include <Path.h>
131953ec9dSRene Gollent#include <String.h>
141953ec9dSRene Gollent#include <StringView.h>
151953ec9dSRene Gollent#include <TextControl.h>
161953ec9dSRene Gollent
1710ba3348SRene Gollent#include "AppMessageCodes.h"
181953ec9dSRene Gollent#include "UserInterface.h"
191953ec9dSRene Gollent
201953ec9dSRene Gollent
211953ec9dSRene Gollentenum {
221953ec9dSRene Gollent	MSG_BROWSE_TEAM		= 'brte',
231953ec9dSRene Gollent	MSG_SET_TEAM_PATH	= 'setp'
241953ec9dSRene Gollent};
251953ec9dSRene Gollent
261953ec9dSRene Gollent
27a1afac4dSRene GollentStartTeamWindow::StartTeamWindow(TargetHostInterface* hostInterface)
281953ec9dSRene Gollent	:
291953ec9dSRene Gollent	BWindow(BRect(), "Start new team", B_TITLED_WINDOW,
301953ec9dSRene Gollent		B_AUTO_UPDATE_SIZE_LIMITS | B_CLOSE_ON_ESCAPE),
311953ec9dSRene Gollent	fGuideText(NULL),
321953ec9dSRene Gollent	fTeamTextControl(NULL),
331953ec9dSRene Gollent	fArgumentsTextControl(NULL),
341953ec9dSRene Gollent	fBrowseTeamButton(NULL),
351953ec9dSRene Gollent	fBrowseTeamPanel(NULL),
361953ec9dSRene Gollent	fStartButton(NULL),
37a1afac4dSRene Gollent	fCancelButton(NULL),
38a1afac4dSRene Gollent	fTargetHostInterface(hostInterface)
391953ec9dSRene Gollent{
401953ec9dSRene Gollent}
411953ec9dSRene Gollent
421953ec9dSRene Gollent
431953ec9dSRene GollentStartTeamWindow::~StartTeamWindow()
441953ec9dSRene Gollent{
451953ec9dSRene Gollent	delete fBrowseTeamPanel;
461953ec9dSRene Gollent}
471953ec9dSRene Gollent
481953ec9dSRene Gollent
491953ec9dSRene GollentStartTeamWindow*
50a1afac4dSRene GollentStartTeamWindow::Create(TargetHostInterface* hostInterface)
511953ec9dSRene Gollent{
52a1afac4dSRene Gollent	StartTeamWindow* self = new StartTeamWindow(hostInterface);
531953ec9dSRene Gollent
541953ec9dSRene Gollent	try {
551953ec9dSRene Gollent		self->_Init();
561953ec9dSRene Gollent	} catch (...) {
571953ec9dSRene Gollent		delete self;
581953ec9dSRene Gollent		throw;
591953ec9dSRene Gollent	}
601953ec9dSRene Gollent
611953ec9dSRene Gollent	return self;
621953ec9dSRene Gollent
631953ec9dSRene Gollent}
641953ec9dSRene Gollent
65918f4a07SRene Gollent
661953ec9dSRene Gollentvoid
671953ec9dSRene GollentStartTeamWindow::_Init()
681953ec9dSRene Gollent{
691953ec9dSRene Gollent	fGuideText = new BStringView("guide", "Set new team parameters below.");
701953ec9dSRene Gollent	fTeamTextControl = new BTextControl("Path: ", NULL, NULL);
711953ec9dSRene Gollent	fArgumentsTextControl = new BTextControl("Arguments: ", NULL, NULL);
721953ec9dSRene Gollent	fBrowseTeamButton = new BButton("Browse" B_UTF8_ELLIPSIS, new BMessage(
731953ec9dSRene Gollent			MSG_BROWSE_TEAM));
741953ec9dSRene Gollent	fStartButton = new BButton("Start team", new BMessage(MSG_START_NEW_TEAM));
751953ec9dSRene Gollent	fCancelButton = new BButton("Cancel", new BMessage(B_QUIT_REQUESTED));
761953ec9dSRene Gollent
771953ec9dSRene Gollent	BLayoutBuilder::Group<>(this, B_VERTICAL)
78447f975cSRene Gollent		.SetInsets(B_USE_DEFAULT_SPACING)
791953ec9dSRene Gollent		.Add(fGuideText)
801953ec9dSRene Gollent		.AddGroup(B_HORIZONTAL, 4.0f)
811953ec9dSRene Gollent			.Add(fTeamTextControl)
821953ec9dSRene Gollent			.Add(fBrowseTeamButton)
831953ec9dSRene Gollent		.End()
841953ec9dSRene Gollent		.AddGroup(B_HORIZONTAL, 4.0f)
851953ec9dSRene Gollent			.Add(fArgumentsTextControl)
861953ec9dSRene Gollent		.End()
871953ec9dSRene Gollent		.AddGroup(B_HORIZONTAL, 4.0f)
881953ec9dSRene Gollent			.AddGlue()
891953ec9dSRene Gollent			.Add(fCancelButton)
901953ec9dSRene Gollent			.Add(fStartButton)
911953ec9dSRene Gollent		.End();
921953ec9dSRene Gollent
937c96721bSOlivier Coursière	fTeamTextControl->SetExplicitMinSize(BSize(300.0, B_SIZE_UNSET));
9457745c4eSRene Gollent	fGuideText->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
951953ec9dSRene Gollent
961953ec9dSRene Gollent	fStartButton->SetTarget(this);
971953ec9dSRene Gollent	fCancelButton->SetTarget(this);
981953ec9dSRene Gollent}
991953ec9dSRene Gollent
100918f4a07SRene Gollent
1011953ec9dSRene Gollentvoid
1021953ec9dSRene GollentStartTeamWindow::Show()
1031953ec9dSRene Gollent{
1041953ec9dSRene Gollent	CenterOnScreen();
1051953ec9dSRene Gollent	BWindow::Show();
1061953ec9dSRene Gollent}
1071953ec9dSRene Gollent
108918f4a07SRene Gollent
1091953ec9dSRene Gollentvoid
1101953ec9dSRene GollentStartTeamWindow::MessageReceived(BMessage* message)
1111953ec9dSRene Gollent{
1121953ec9dSRene Gollent	switch (message->what) {
1131953ec9dSRene Gollent		case MSG_BROWSE_TEAM:
1141953ec9dSRene Gollent		{
1151953ec9dSRene Gollent			if (fBrowseTeamPanel == NULL) {
1161953ec9dSRene Gollent				fBrowseTeamPanel = new(std::nothrow) BFilePanel(B_OPEN_PANEL,
1171953ec9dSRene Gollent					new BMessenger(this));
1181953ec9dSRene Gollent				if (fBrowseTeamPanel == NULL)
1191953ec9dSRene Gollent					break;
1201953ec9dSRene Gollent				BMessage* message = new(std::nothrow) BMessage(
1211953ec9dSRene Gollent					MSG_SET_TEAM_PATH);
1221953ec9dSRene Gollent				if (message == NULL) {
1231953ec9dSRene Gollent					delete fBrowseTeamPanel;
1241953ec9dSRene Gollent					fBrowseTeamPanel = NULL;
1251953ec9dSRene Gollent					break;
1261953ec9dSRene Gollent				}
1271953ec9dSRene Gollent				fBrowseTeamPanel->SetMessage(message);
1281953ec9dSRene Gollent			}
1291953ec9dSRene Gollent
1307c96721bSOlivier Coursière			fBrowseTeamPanel->SetPanelDirectory(fTeamTextControl->TextView()
1317c96721bSOlivier Coursière					->Text());
1327c96721bSOlivier Coursière
1331953ec9dSRene Gollent			fBrowseTeamPanel->Show();
1341953ec9dSRene Gollent			break;
1351953ec9dSRene Gollent		}
1361953ec9dSRene Gollent		case MSG_SET_TEAM_PATH:
1371953ec9dSRene Gollent		{
1381953ec9dSRene Gollent			entry_ref ref;
1391953ec9dSRene Gollent			if (message->FindRef("refs", &ref) == B_OK) {
1401953ec9dSRene Gollent				BPath path(&ref);
1411953ec9dSRene Gollent				fTeamTextControl->TextView()->SetText(path.Path());
1421953ec9dSRene Gollent			}
1431953ec9dSRene Gollent			break;
1441953ec9dSRene Gollent		}
1451953ec9dSRene Gollent		case MSG_START_NEW_TEAM:
1461953ec9dSRene Gollent		{
1471953ec9dSRene Gollent			BMessage appMessage(MSG_START_NEW_TEAM);
1481953ec9dSRene Gollent			appMessage.AddString("path", fTeamTextControl->TextView()->Text());
1491953ec9dSRene Gollent			appMessage.AddString("arguments", fArgumentsTextControl->TextView()
1501953ec9dSRene Gollent					->Text());
151a1afac4dSRene Gollent			appMessage.AddPointer("interface", fTargetHostInterface);
1521953ec9dSRene Gollent			BMessage reply;
1531953ec9dSRene Gollent			be_app_messenger.SendMessage(&appMessage, &reply);
1541953ec9dSRene Gollent			status_t error = reply.FindInt32("status");
1551953ec9dSRene Gollent			if (error != B_OK) {
1561953ec9dSRene Gollent				BString messageString;
1571953ec9dSRene Gollent				messageString.SetToFormat("Failed to start team: %s.",
1581953ec9dSRene Gollent					strerror(error));
1591953ec9dSRene Gollent				BAlert* alert = new(std::nothrow) BAlert("Start team failed",
1601953ec9dSRene Gollent					messageString.String(), "Close");
1611953ec9dSRene Gollent				if (alert != NULL)
1621953ec9dSRene Gollent					alert->Go();
163edf74e6cSRene Gollent			} else {
164edf74e6cSRene Gollent				be_app->PostMessage(MSG_START_TEAM_WINDOW_CLOSED);
1651953ec9dSRene Gollent				PostMessage(B_QUIT_REQUESTED);
166edf74e6cSRene Gollent			}
1671953ec9dSRene Gollent			break;
1681953ec9dSRene Gollent		}
1691953ec9dSRene Gollent		default:
1701953ec9dSRene Gollent			BWindow::MessageReceived(message);
1711953ec9dSRene Gollent			break;
1721953ec9dSRene Gollent	}
1731953ec9dSRene Gollent
1741953ec9dSRene Gollent}
175