Network: SSH list item now indicates a running server.

* This will be moved into the network preferences, so that it can be
  used from other add-ons, as well.
This commit is contained in:
Axel Dörfler 2015-03-16 21:53:49 +00:00
parent 30811f5d73
commit d95c5011bd
4 changed files with 182 additions and 3 deletions

View File

@ -5,6 +5,7 @@ UsePrivateHeaders app libroot kernel net shared ;
Addon SSHService :
SSHServiceAddOn.cpp
ServiceListItem.cpp
ServiceView.cpp
: be bnetapi libshared.a <nogrist>Network [ TargetLibsupc++ ]
@ -13,5 +14,6 @@ Addon SSHService :
DoCatalogs SSHService : x-vnd.Haiku-SSHService :
SSHServiceAddOn.cpp
ServiceListItem.cpp
ServiceView.cpp
;

View File

@ -15,7 +15,6 @@
#include <LayoutBuilder.h>
#include <NetworkSettings.h>
#include <NetworkSettingsAddOn.h>
#include <StringItem.h>
#include <StringView.h>
#include <TextView.h>
@ -24,6 +23,7 @@
#include <user_group.h>
#include <util/KMessage.h>
#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)
{
}

View File

@ -0,0 +1,126 @@
/*
* Copyright 2015 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, <axeld@pinc-software.de>
*/
#include "ServiceListItem.h"
#include <Catalog.h>
#include <ControlLook.h>
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();
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2015 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, <axeld@pinc-software.de>
*/
#ifndef SERVICE_LIST_ITEM_H
#define SERVICE_LIST_ITEM_H
#include <ListItem.h>
#include <NetworkSettings.h>
#include <NetworkSettingsAddOn.h>
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