docs/ikteam: Delete most files.
[haiku.git] / src / preferences / network / ServiceView.cpp
blob4b4dc01212003b62e8d7dfc86af978a5892eedbd
1 /*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, <axeld@pinc-software.de>
7 */
10 #include "ServiceView.h"
12 #include <Button.h>
13 #include <Catalog.h>
14 #include <LayoutBuilder.h>
15 #include <MessageRunner.h>
16 #include <StringView.h>
17 #include <TextView.h>
20 static const uint32 kMsgToggleService = 'tgls';
21 static const uint32 kMsgEnableToggleButton = 'entg';
23 static const bigtime_t kDisableDuration = 500000;
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "ServiceView"
30 ServiceView::ServiceView(const char* name, const char* executable,
31 const char* title, const char* description, BNetworkSettings& settings)
33 BView("service", 0),
34 fName(name),
35 fExecutable(executable),
36 fSettings(settings)
38 BStringView* titleView = new BStringView("service", title);
39 titleView->SetFont(be_bold_font);
40 titleView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
42 BTextView* descriptionView = new BTextView("description");
43 descriptionView->SetText(description);
44 descriptionView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
45 descriptionView->MakeEditable(false);
47 fEnableButton = new BButton("toggler", B_TRANSLATE("Enable"),
48 new BMessage(kMsgToggleService));
50 BLayoutBuilder::Group<>(this, B_VERTICAL)
51 .Add(titleView)
52 .Add(descriptionView)
53 .AddGlue()
54 .AddGroup(B_HORIZONTAL)
55 .AddGlue()
56 .Add(fEnableButton);
58 SetExplicitMinSize(BSize(200, B_SIZE_UNSET));
59 _UpdateEnableButton();
61 fWasEnabled = IsEnabled();
65 ServiceView::~ServiceView()
70 bool
71 ServiceView::IsRevertable() const
73 return IsEnabled() != fWasEnabled;
77 status_t
78 ServiceView::Revert()
80 if (IsRevertable())
81 _Toggle();
83 return B_OK;
87 void
88 ServiceView::SettingsUpdated(uint32 which)
90 if (which == BNetworkSettings::kMsgServiceSettingsUpdated)
91 _UpdateEnableButton();
95 void
96 ServiceView::AttachedToWindow()
98 fEnableButton->SetTarget(this);
102 void
103 ServiceView::MessageReceived(BMessage* message)
105 switch (message->what) {
106 case kMsgToggleService:
107 _Toggle();
108 break;
110 case kMsgEnableToggleButton:
111 fEnableButton->SetEnabled(true);
112 _UpdateEnableButton();
113 break;
115 default:
116 BView::MessageReceived(message);
117 break;
122 bool
123 ServiceView::IsEnabled() const
125 return fSettings.Service(fName).IsRunning();
129 void
130 ServiceView::Enable()
132 BNetworkServiceSettings settings;
133 settings.SetName(fName);
134 settings.AddArgument(fExecutable);
136 BMessage service;
137 if (settings.GetMessage(service) == B_OK)
138 fSettings.AddService(service);
142 void
143 ServiceView::Disable()
145 fSettings.RemoveService(fName);
149 void
150 ServiceView::_Toggle()
152 if (IsEnabled())
153 Disable();
154 else
155 Enable();
157 fEnableButton->SetEnabled(false);
158 BMessage reenable(kMsgEnableToggleButton);
159 BMessageRunner::StartSending(this, &reenable, kDisableDuration, 1);
163 void
164 ServiceView::_UpdateEnableButton()
166 fEnableButton->SetLabel(IsEnabled()
167 ? B_TRANSLATE("Disable") : B_TRANSLATE("Enable"));