1 /*
2 * Copyright 2015 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 * Axel Dörfler, <axeld@pinc-software.de>
7 */
8
9
10 #include "ServiceView.h"
11
12 #include <Button.h>
13 #include <Catalog.h>
14 #include <LayoutBuilder.h>
15 #include <MessageRunner.h>
16 #include <StringView.h>
17 #include <TextView.h>
18
19
20 static const uint32 kMsgToggleService = 'tgls';
21 static const uint32 kMsgEnableToggleButton = 'entg';
22
23 static const bigtime_t kDisableDuration = 500000;
24
25
26 #undef B_TRANSLATION_CONTEXT
27 #define B_TRANSLATION_CONTEXT "ServiceView"
28
29
ServiceView(const char * name,const char * executable,const char * title,const char * description,BNetworkSettings & settings)30 ServiceView::ServiceView(const char* name, const char* executable,
31 const char* title, const char* description, BNetworkSettings& settings)
32 :
33 BGroupView(B_VERTICAL),
34 fName(name),
35 fExecutable(executable),
36 fSettings(settings)
37 {
38 BStringView* titleView = new BStringView("service", title);
39 titleView->SetFont(be_bold_font);
40 titleView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
41
42 BTextView* descriptionView = new BTextView("description");
43 descriptionView->SetText(description);
44 descriptionView->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
45 descriptionView->MakeEditable(false);
46
47 fEnableButton = new BButton("toggler", B_TRANSLATE("Enable"),
48 new BMessage(kMsgToggleService));
49
50 BLayoutBuilder::Group<>(this, B_VERTICAL)
51 .Add(titleView)
52 .Add(descriptionView)
53 .AddGlue()
54 .AddGroup(B_HORIZONTAL)
55 .AddGlue()
56 .Add(fEnableButton);
57
58 SetExplicitMinSize(BSize(200, B_SIZE_UNSET));
59 _UpdateEnableButton();
60
61 fWasEnabled = IsEnabled();
62 }
63
64
~ServiceView()65 ServiceView::~ServiceView()
66 {
67 }
68
69
70 bool
IsRevertable() const71 ServiceView::IsRevertable() const
72 {
73 return IsEnabled() != fWasEnabled;
74 }
75
76
77 status_t
Revert()78 ServiceView::Revert()
79 {
80 if (IsRevertable())
81 _Toggle();
82
83 return B_OK;
84 }
85
86
87 void
SettingsUpdated(uint32 which)88 ServiceView::SettingsUpdated(uint32 which)
89 {
90 if (which == BNetworkSettings::kMsgServiceSettingsUpdated)
91 _UpdateEnableButton();
92 }
93
94
95 void
AttachedToWindow()96 ServiceView::AttachedToWindow()
97 {
98 fEnableButton->SetTarget(this);
99 }
100
101
102 void
MessageReceived(BMessage * message)103 ServiceView::MessageReceived(BMessage* message)
104 {
105 switch (message->what) {
106 case kMsgToggleService:
107 _Toggle();
108 break;
109
110 case kMsgEnableToggleButton:
111 fEnableButton->SetEnabled(true);
112 _UpdateEnableButton();
113 break;
114
115 default:
116 BView::MessageReceived(message);
117 break;
118 }
119 }
120
121
122 bool
IsEnabled() const123 ServiceView::IsEnabled() const
124 {
125 return fSettings.Service(fName).IsRunning();
126 }
127
128
129 void
Enable()130 ServiceView::Enable()
131 {
132 BNetworkServiceSettings settings;
133 settings.SetName(fName);
134 settings.AddArgument(fExecutable);
135
136 BMessage service;
137 if (settings.GetMessage(service) == B_OK)
138 fSettings.AddService(service);
139 }
140
141
142 void
Disable()143 ServiceView::Disable()
144 {
145 fSettings.RemoveService(fName);
146 }
147
148
149 void
_Toggle()150 ServiceView::_Toggle()
151 {
152 if (IsEnabled())
153 Disable();
154 else
155 Enable();
156
157 fEnableButton->SetEnabled(false);
158 BMessage reenable(kMsgEnableToggleButton);
159 BMessageRunner::StartSending(this, &reenable, kDisableDuration, 1);
160 }
161
162
163 void
_UpdateEnableButton()164 ServiceView::_UpdateEnableButton()
165 {
166 fEnableButton->SetLabel(IsEnabled()
167 ? B_TRANSLATE("Disable") : B_TRANSLATE("Enable"));
168 }
169