13c8ac782SAxel Dörfler/*
23c8ac782SAxel Dörfler * Copyright 2015 Haiku, Inc. All rights reserved.
33c8ac782SAxel Dörfler * Distributed under the terms of the MIT License.
43c8ac782SAxel Dörfler *
53c8ac782SAxel Dörfler * Authors:
63c8ac782SAxel Dörfler *		Axel D��rfler, <axeld@pinc-software.de>
73c8ac782SAxel Dörfler */
83c8ac782SAxel Dörfler
93c8ac782SAxel Dörfler
103c8ac782SAxel Dörfler#include "ServiceView.h"
113c8ac782SAxel Dörfler
123c8ac782SAxel Dörfler#include <Button.h>
133c8ac782SAxel Dörfler#include <Catalog.h>
143c8ac782SAxel Dörfler#include <LayoutBuilder.h>
15d45b0180SAxel Dörfler#include <MessageRunner.h>
163c8ac782SAxel Dörfler#include <StringView.h>
173c8ac782SAxel Dörfler#include <TextView.h>
183c8ac782SAxel Dörfler
193c8ac782SAxel Dörfler
203c8ac782SAxel Dörflerstatic const uint32 kMsgToggleService = 'tgls';
21d45b0180SAxel Dörflerstatic const uint32 kMsgEnableToggleButton = 'entg';
22d45b0180SAxel Dörfler
23d45b0180SAxel Dörflerstatic const bigtime_t kDisableDuration = 500000;
243c8ac782SAxel Dörfler
253c8ac782SAxel Dörfler
263c8ac782SAxel Dörfler#undef B_TRANSLATION_CONTEXT
273c8ac782SAxel Dörfler#define B_TRANSLATION_CONTEXT "ServiceView"
283c8ac782SAxel Dörfler
293c8ac782SAxel Dörfler
303c8ac782SAxel DörflerServiceView::ServiceView(const char* name, const char* executable,
313c8ac782SAxel Dörfler	const char* title, const char* description, BNetworkSettings& settings)
323c8ac782SAxel Dörfler	:
333c8ac782SAxel Dörfler	BView("service", 0),
343c8ac782SAxel Dörfler	fName(name),
353c8ac782SAxel Dörfler	fExecutable(executable),
363c8ac782SAxel Dörfler	fSettings(settings)
373c8ac782SAxel Dörfler{
383c8ac782SAxel Dörfler	BStringView* titleView = new BStringView("service", title);
393c8ac782SAxel Dörfler	titleView->SetFont(be_bold_font);
403c8ac782SAxel Dörfler	titleView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
413c8ac782SAxel Dörfler
423c8ac782SAxel Dörfler	BTextView* descriptionView = new BTextView("description");
433c8ac782SAxel Dörfler	descriptionView->SetText(description);
44f0650dc9Slooncraz	descriptionView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
453c8ac782SAxel Dörfler	descriptionView->MakeEditable(false);
463c8ac782SAxel Dörfler
473c8ac782SAxel Dörfler	fEnableButton = new BButton("toggler", B_TRANSLATE("Enable"),
483c8ac782SAxel Dörfler		new BMessage(kMsgToggleService));
493c8ac782SAxel Dörfler
503c8ac782SAxel Dörfler	BLayoutBuilder::Group<>(this, B_VERTICAL)
513c8ac782SAxel Dörfler		.Add(titleView)
523c8ac782SAxel Dörfler		.Add(descriptionView)
5326cc9e9eSHumdinger		.AddGlue()
543c8ac782SAxel Dörfler		.AddGroup(B_HORIZONTAL)
553c8ac782SAxel Dörfler			.AddGlue()
563c8ac782SAxel Dörfler			.Add(fEnableButton);
573c8ac782SAxel Dörfler
583c8ac782SAxel Dörfler	SetExplicitMinSize(BSize(200, B_SIZE_UNSET));
593c8ac782SAxel Dörfler	_UpdateEnableButton();
60d45b0180SAxel Dörfler
61d45b0180SAxel Dörfler	fWasEnabled = IsEnabled();
623c8ac782SAxel Dörfler}
633c8ac782SAxel Dörfler
643c8ac782SAxel Dörfler
653c8ac782SAxel DörflerServiceView::~ServiceView()
663c8ac782SAxel Dörfler{
673c8ac782SAxel Dörfler}
683c8ac782SAxel Dörfler
693c8ac782SAxel Dörfler
70d45b0180SAxel Dörflerbool
716a290205SAxel DörflerServiceView::IsRevertable() const
72d45b0180SAxel Dörfler{
73d45b0180SAxel Dörfler	return IsEnabled() != fWasEnabled;
74d45b0180SAxel Dörfler}
75d45b0180SAxel Dörfler
76d45b0180SAxel Dörfler
776a290205SAxel Dörflerstatus_t
78d45b0180SAxel DörflerServiceView::Revert()
79d45b0180SAxel Dörfler{
80d45b0180SAxel Dörfler	if (IsRevertable())
81d45b0180SAxel Dörfler		_Toggle();
826a290205SAxel Dörfler
836a290205SAxel Dörfler	return B_OK;
84d45b0180SAxel Dörfler}
85d45b0180SAxel Dörfler
86d45b0180SAxel Dörfler
873c8ac782SAxel Dörflervoid
883c8ac782SAxel DörflerServiceView::SettingsUpdated(uint32 which)
893c8ac782SAxel Dörfler{
903c8ac782SAxel Dörfler	if (which == BNetworkSettings::kMsgServiceSettingsUpdated)
913c8ac782SAxel Dörfler		_UpdateEnableButton();
923c8ac782SAxel Dörfler}
933c8ac782SAxel Dörfler
943c8ac782SAxel Dörfler
953c8ac782SAxel Dörflervoid
963c8ac782SAxel DörflerServiceView::AttachedToWindow()
973c8ac782SAxel Dörfler{
983c8ac782SAxel Dörfler	fEnableButton->SetTarget(this);
993c8ac782SAxel Dörfler}
1003c8ac782SAxel Dörfler
1013c8ac782SAxel Dörfler
1023c8ac782SAxel Dörflervoid
1033c8ac782SAxel DörflerServiceView::MessageReceived(BMessage* message)
1043c8ac782SAxel Dörfler{
1053c8ac782SAxel Dörfler	switch (message->what) {
1063c8ac782SAxel Dörfler		case kMsgToggleService:
107d45b0180SAxel Dörfler			_Toggle();
108d45b0180SAxel Dörfler			break;
1093c8ac782SAxel Dörfler
110d45b0180SAxel Dörfler		case kMsgEnableToggleButton:
111d45b0180SAxel Dörfler			fEnableButton->SetEnabled(true);
1123c8ac782SAxel Dörfler			_UpdateEnableButton();
1133c8ac782SAxel Dörfler			break;
114d45b0180SAxel Dörfler
1153c8ac782SAxel Dörfler		default:
1163c8ac782SAxel Dörfler			BView::MessageReceived(message);
1173c8ac782SAxel Dörfler			break;
1183c8ac782SAxel Dörfler	}
1193c8ac782SAxel Dörfler}
1203c8ac782SAxel Dörfler
1213c8ac782SAxel Dörfler
1223c8ac782SAxel Dörflerbool
1233c8ac782SAxel DörflerServiceView::IsEnabled() const
1243c8ac782SAxel Dörfler{
125d45b0180SAxel Dörfler	return fSettings.Service(fName).IsRunning();
1263c8ac782SAxel Dörfler}
1273c8ac782SAxel Dörfler
1283c8ac782SAxel Dörfler
1293c8ac782SAxel Dörflervoid
1303c8ac782SAxel DörflerServiceView::Enable()
1313c8ac782SAxel Dörfler{
1323c8ac782SAxel Dörfler	BNetworkServiceSettings settings;
1333c8ac782SAxel Dörfler	settings.SetName(fName);
1343c8ac782SAxel Dörfler	settings.AddArgument(fExecutable);
1353c8ac782SAxel Dörfler
1363c8ac782SAxel Dörfler	BMessage service;
1373c8ac782SAxel Dörfler	if (settings.GetMessage(service) == B_OK)
1383c8ac782SAxel Dörfler		fSettings.AddService(service);
1393c8ac782SAxel Dörfler}
1403c8ac782SAxel Dörfler
1413c8ac782SAxel Dörfler
1423c8ac782SAxel Dörflervoid
1433c8ac782SAxel DörflerServiceView::Disable()
1443c8ac782SAxel Dörfler{
1453c8ac782SAxel Dörfler	fSettings.RemoveService(fName);
1463c8ac782SAxel Dörfler}
1473c8ac782SAxel Dörfler
1483c8ac782SAxel Dörfler
149d45b0180SAxel Dörflervoid
150d45b0180SAxel DörflerServiceView::_Toggle()
151d45b0180SAxel Dörfler{
152d45b0180SAxel Dörfler	if (IsEnabled())
153d45b0180SAxel Dörfler		Disable();
154d45b0180SAxel Dörfler	else
155d45b0180SAxel Dörfler		Enable();
156d45b0180SAxel Dörfler
157d45b0180SAxel Dörfler	fEnableButton->SetEnabled(false);
158d45b0180SAxel Dörfler	BMessage reenable(kMsgEnableToggleButton);
159d45b0180SAxel Dörfler	BMessageRunner::StartSending(this, &reenable, kDisableDuration, 1);
160d45b0180SAxel Dörfler}
161d45b0180SAxel Dörfler
162d45b0180SAxel Dörfler
1633c8ac782SAxel Dörflervoid
1643c8ac782SAxel DörflerServiceView::_UpdateEnableButton()
1653c8ac782SAxel Dörfler{
1663c8ac782SAxel Dörfler	fEnableButton->SetLabel(IsEnabled()
1673c8ac782SAxel Dörfler		? B_TRANSLATE("Disable") : B_TRANSLATE("Enable"));
1683c8ac782SAxel Dörfler}
169