diff --git a/src/add-ons/network_settings/sshd/Jamfile b/src/add-ons/network_settings/sshd/Jamfile index 68de3239f9..55e0dc1604 100644 --- a/src/add-ons/network_settings/sshd/Jamfile +++ b/src/add-ons/network_settings/sshd/Jamfile @@ -5,6 +5,7 @@ UsePrivateHeaders app libroot kernel net shared ; Addon SSHService : SSHServiceAddOn.cpp + ServiceListItem.cpp ServiceView.cpp : be bnetapi libshared.a Network [ TargetLibsupc++ ] @@ -13,5 +14,6 @@ Addon SSHService : DoCatalogs SSHService : x-vnd.Haiku-SSHService : SSHServiceAddOn.cpp + ServiceListItem.cpp ServiceView.cpp ; diff --git a/src/add-ons/network_settings/sshd/SSHServiceAddOn.cpp b/src/add-ons/network_settings/sshd/SSHServiceAddOn.cpp index 954dcfd722..661769a013 100644 --- a/src/add-ons/network_settings/sshd/SSHServiceAddOn.cpp +++ b/src/add-ons/network_settings/sshd/SSHServiceAddOn.cpp @@ -15,7 +15,6 @@ #include #include #include -#include #include #include @@ -24,6 +23,7 @@ #include #include +#include "ServiceListItem.h" #include "ServiceView.h" @@ -76,7 +76,7 @@ public: private: BNetworkSettings& fSettings; - BStringItem* fItem; + BListItem* fItem; ServiceView* fView; }; @@ -171,7 +171,7 @@ SSHServiceView::Enable() SSHServiceItem::SSHServiceItem(BNetworkSettings& settings) : fSettings(settings), - fItem(new BStringItem(B_TRANSLATE("SSH server"))), + fItem(new ServiceListItem("ssh", B_TRANSLATE("SSH server"), settings)), fView(NULL) { } diff --git a/src/add-ons/network_settings/sshd/ServiceListItem.cpp b/src/add-ons/network_settings/sshd/ServiceListItem.cpp new file mode 100644 index 0000000000..8b8d0321d6 --- /dev/null +++ b/src/add-ons/network_settings/sshd/ServiceListItem.cpp @@ -0,0 +1,126 @@ +/* + * Copyright 2015 Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Axel Dörfler, + */ + + +#include "ServiceListItem.h" + +#include +#include + + +static const char* kEnabledState = "on"; +static const char* kDisabledState = "off"; + + +#undef B_TRANSLATION_CONTEXT +#define B_TRANSLATION_CONTEXT "ServiceListItem" + + +ServiceListItem::ServiceListItem(const char* name, const char* label, + const BNetworkSettings& settings) + : + fName(name), + fLabel(label), + fSettings(settings), + fEnabled(false) +{ +} + + +ServiceListItem::~ServiceListItem() +{ +} + + +void +ServiceListItem::DrawItem(BView* owner, BRect bounds, bool complete) +{ + owner->PushState(); + + rgb_color lowColor = owner->LowColor(); + + if (IsSelected() || complete) { + if (IsSelected()) { + owner->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR)); + owner->SetLowColor(owner->HighColor()); + } else + owner->SetHighColor(lowColor); + + owner->FillRect(bounds); + } + + const char* stateText = fEnabled ? B_TRANSLATE(kEnabledState) + : B_TRANSLATE(kDisabledState); + + // Set the initial bounds of item contents + BPoint statePoint = bounds.RightTop() + BPoint(0, fLineOffset) + - BPoint(be_plain_font->StringWidth(stateText) + + be_control_look->DefaultLabelSpacing(), 0); + BPoint namePoint = bounds.LeftTop() + + BPoint(be_control_look->DefaultLabelSpacing(), fLineOffset); + + owner->SetDrawingMode(B_OP_OVER); + + rgb_color textColor; + if (IsSelected()) + textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR); + else + textColor = ui_color(B_LIST_ITEM_TEXT_COLOR); + + owner->SetHighColor(textColor); + owner->DrawString(fLabel, namePoint); + + if (!fEnabled) { + if (textColor.red + textColor.green + textColor.blue > 128 * 3) + owner->SetHighColor(tint_color(textColor, B_DARKEN_1_TINT)); + else + owner->SetHighColor(tint_color(textColor, B_LIGHTEN_1_TINT)); + } + owner->DrawString(stateText, statePoint); + + owner->PopState(); +} + + +void +ServiceListItem::Update(BView* owner, const BFont* font) +{ + fOwner = owner; + fEnabled = IsEnabled(); + + BListItem::Update(owner, font); + font_height height; + font->GetHeight(&height); + + fLineOffset = 2 + ceilf(height.ascent + height.leading / 2); + + float maxStateWidth = std::max(font->StringWidth(B_TRANSLATE(kEnabledState)), + font->StringWidth(B_TRANSLATE(kDisabledState))); + SetWidth(font->StringWidth(fLabel) + + 3 * be_control_look->DefaultLabelSpacing() + maxStateWidth); + SetHeight(4 + ceilf(height.ascent + height.leading + height.descent)); +} + + +void +ServiceListItem::SettingsUpdated(uint32 type) +{ + if (type == BNetworkSettings::kMsgServiceSettingsUpdated) { + bool wasEnabled = fEnabled; + fEnabled = IsEnabled(); + if (wasEnabled != fEnabled) + fOwner->Invalidate(); + } +} + + +bool +ServiceListItem::IsEnabled() +{ + return fSettings.Service(fName).IsRunning(); +} diff --git a/src/add-ons/network_settings/sshd/ServiceListItem.h b/src/add-ons/network_settings/sshd/ServiceListItem.h new file mode 100644 index 0000000000..4e70d1b2cc --- /dev/null +++ b/src/add-ons/network_settings/sshd/ServiceListItem.h @@ -0,0 +1,51 @@ +/* + * Copyright 2015 Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Axel Dörfler, + */ +#ifndef SERVICE_LIST_ITEM_H +#define SERVICE_LIST_ITEM_H + + +#include +#include +#include + + +using namespace BNetworkKit; + + +class ServiceListItem : public BListItem, + public BNetworkKit::BNetworkSettingsListener { +public: + ServiceListItem(const char* name, + const char* label, + const BNetworkSettings& settings); + virtual ~ServiceListItem(); + + virtual void DrawItem(BView* owner, + BRect bounds, bool complete); + virtual void Update(BView* owner, const BFont* font); + + inline const char* Name() const { return fName; } + + virtual void SettingsUpdated(uint32 type); + +protected: + virtual bool IsEnabled(); + +private: + const char* fName; + const char* fLabel; + const BNetworkSettings& + fSettings; + + BView* fOwner; + float fLineOffset; + bool fEnabled; +}; + + +#endif // SERVICE_LIST_ITEM_H