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 30 ServiceView::ServiceView(const char* name, const char* executable, 31 const char* title, const char* description, BNetworkSettings& settings) 32 : 33 BView("service", 0), 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->SetViewColor(ui_color(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 .AddGroup(B_HORIZONTAL) 54 .AddGlue() 55 .Add(fEnableButton); 56 57 SetExplicitMinSize(BSize(200, B_SIZE_UNSET)); 58 _UpdateEnableButton(); 59 60 fWasEnabled = IsEnabled(); 61 } 62 63 64 ServiceView::~ServiceView() 65 { 66 } 67 68 69 bool 70 ServiceView::IsRevertable() const 71 { 72 return IsEnabled() != fWasEnabled; 73 } 74 75 76 status_t 77 ServiceView::Revert() 78 { 79 if (IsRevertable()) 80 _Toggle(); 81 82 return B_OK; 83 } 84 85 86 void 87 ServiceView::SettingsUpdated(uint32 which) 88 { 89 if (which == BNetworkSettings::kMsgServiceSettingsUpdated) 90 _UpdateEnableButton(); 91 } 92 93 94 void 95 ServiceView::AttachedToWindow() 96 { 97 fEnableButton->SetTarget(this); 98 } 99 100 101 void 102 ServiceView::MessageReceived(BMessage* message) 103 { 104 switch (message->what) { 105 case kMsgToggleService: 106 _Toggle(); 107 break; 108 109 case kMsgEnableToggleButton: 110 fEnableButton->SetEnabled(true); 111 _UpdateEnableButton(); 112 break; 113 114 default: 115 BView::MessageReceived(message); 116 break; 117 } 118 } 119 120 121 bool 122 ServiceView::IsEnabled() const 123 { 124 return fSettings.Service(fName).IsRunning(); 125 } 126 127 128 void 129 ServiceView::Enable() 130 { 131 BNetworkServiceSettings settings; 132 settings.SetName(fName); 133 settings.AddArgument(fExecutable); 134 135 BMessage service; 136 if (settings.GetMessage(service) == B_OK) 137 fSettings.AddService(service); 138 } 139 140 141 void 142 ServiceView::Disable() 143 { 144 fSettings.RemoveService(fName); 145 } 146 147 148 void 149 ServiceView::_Toggle() 150 { 151 if (IsEnabled()) 152 Disable(); 153 else 154 Enable(); 155 156 fEnableButton->SetEnabled(false); 157 BMessage reenable(kMsgEnableToggleButton); 158 BMessageRunner::StartSending(this, &reenable, kDisableDuration, 1); 159 } 160 161 162 void 163 ServiceView::_UpdateEnableButton() 164 { 165 fEnableButton->SetLabel(IsEnabled() 166 ? B_TRANSLATE("Disable") : B_TRANSLATE("Enable")); 167 } 168