networking: Hostname viewing & setting

addon/network settings/hostname

	allowing the hostname for the system to be set by the user using
	the preferences gui

Change-Id: I676c368070ef89a20435f97ee39e3caa25abf8f9
Reviewed-on: https://review.haiku-os.org/c/870
Reviewed-by: Axel Dörfler <axeld@pinc-software.de>
Reviewed-by: Stephan Aßmus <superstippi@gmx.de>
This commit is contained in:
Rob Gill 2019-01-11 11:40:24 +00:00 committed by Stephan Aßmus
parent b72d60791e
commit 74726e7014
6 changed files with 370 additions and 0 deletions

View File

@ -15,3 +15,5 @@ SubInclude HAIKU_TOP src add-ons network_settings telnetd ;
# VPN add-ons
SubInclude HAIKU_TOP src add-ons network_settings openvpn ;
# Other add-ons
SubInclude HAIKU_TOP src add-ons network_settings hostname ;

View File

@ -0,0 +1,12 @@
resource app_signature "application/x-vnd.Haiku-Hostname";
resource app_version {
major = 1,
middle = 0,
minor = 0,
variety = 0,
internal = 0,
short_info = "1.0.0",
long_info = "Haiku Hostname settings"
};

View File

@ -0,0 +1,152 @@
/*
* Copyright 2019 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, <axeld@pinc-software.de>
* Rob Gill, <rrobgill@protonmail.com>
*/
#include <Catalog.h>
#include <NetworkSettings.h>
#include <NetworkSettingsAddOn.h>
#include <StringItem.h>
#include "HostnameView.h"
using namespace BNetworkKit;
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "HostnameAddOn"
class HostnameAddOn : public BNetworkSettingsAddOn {
public:
HostnameAddOn(image_id image,
BNetworkSettings& settings);
virtual ~HostnameAddOn();
virtual BNetworkSettingsItem*
CreateNextItem(uint32& cookie);
};
class HostnameItem : public BNetworkSettingsItem {
public:
HostnameItem(
BNetworkSettings& settings);
virtual ~HostnameItem();
virtual BNetworkSettingsType
Type() const;
virtual BListItem* ListItem();
virtual BView* View();
virtual status_t Revert();
virtual bool IsRevertable();
private:
BNetworkSettings& fSettings;
BStringItem* fItem;
HostnameView* fView;
};
// #pragma mark -
HostnameItem::HostnameItem(BNetworkSettings& settings)
:
fSettings(settings),
fItem(new BStringItem(B_TRANSLATE("Hostname settings"))),
fView(NULL)
{
}
HostnameItem::~HostnameItem()
{
if (fView->Parent() == NULL)
delete fView;
delete fItem;
}
BNetworkSettingsType
HostnameItem::Type() const
{
return B_NETWORK_SETTINGS_TYPE_OTHER;
}
BListItem*
HostnameItem::ListItem()
{
return fItem;
}
BView*
HostnameItem::View()
{
if (fView == NULL)
fView = new HostnameView(this);
return fView;
}
status_t
HostnameItem::Revert()
{
return fView != NULL ? fView->Revert() : B_OK;
}
bool
HostnameItem::IsRevertable()
{
return fView != NULL ? fView->IsRevertable() : false;
}
// #pragma mark -
HostnameAddOn::HostnameAddOn(image_id image,
BNetworkSettings& settings)
:
BNetworkSettingsAddOn(image, settings)
{
}
HostnameAddOn::~HostnameAddOn()
{
}
BNetworkSettingsItem*
HostnameAddOn::CreateNextItem(uint32& cookie)
{
if (cookie++ == 0)
return new HostnameItem(Settings());
return NULL;
}
// #pragma mark -
extern "C"
BNetworkSettingsAddOn*
instantiate_network_settings_add_on(image_id image, BNetworkSettings& settings)
{
return new HostnameAddOn(image, settings);
}

View File

@ -0,0 +1,136 @@
/*
* Copyright 2019 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Adrien Destugues, pulkomandy@pulkomandy.tk
* Rob Gill, <rrobgill@protonmail.com>
*/
#include "HostnameView.h"
#include <stdio.h>
#include <string.h>
#include <Box.h>
#include <Button.h>
#include <Catalog.h>
#include <ControlLook.h>
#include <LayoutBuilder.h>
#include <SeparatorView.h>
#include <StringView.h>
static const int32 kMsgApply = 'aply';
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "HostnameView"
HostnameView::HostnameView(BNetworkSettingsItem* item)
:
BView("hostname", 0),
fItem(item)
{
BStringView* titleView = new BStringView("title",
B_TRANSLATE("Hostname settings"));
titleView->SetFont(be_bold_font);
titleView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
fHostname = new BTextControl(B_TRANSLATE("Hostname:"), "", NULL);
fApplyButton = new BButton(B_TRANSLATE("Apply"), new BMessage(kMsgApply));
BLayoutBuilder::Group<>(this, B_VERTICAL)
.Add(titleView)
.Add(fHostname)
.AddGlue()
.AddGroup(B_HORIZONTAL)
.AddGlue()
.Add(fApplyButton);
_LoadHostname();
}
HostnameView::~HostnameView()
{
}
status_t
HostnameView::Revert()
{
return B_OK;
}
bool
HostnameView::IsRevertable() const
{
return false;
}
void
HostnameView::AttachedToWindow()
{
fApplyButton->SetTarget(this);
}
void
HostnameView::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgApply:
if (_SaveHostname() == B_OK)
fItem->NotifySettingsUpdated();
break;
default:
BView::MessageReceived(message);
break;
}
}
status_t
HostnameView::_LoadHostname()
{
BString fHostnameString;
char hostname[MAXHOSTNAMELEN];
if (gethostname(hostname, MAXHOSTNAMELEN) == 0) {
fHostnameString.SetTo(hostname, MAXHOSTNAMELEN);
fHostname->SetText(fHostnameString);
return B_OK;
} else
return B_ERROR;
}
status_t
HostnameView::_SaveHostname()
{
BString hostnamestring("");
size_t hostnamelen(strlen(fHostname->Text()));
if (hostnamelen == 0)
return B_ERROR;
if (hostnamelen > MAXHOSTNAMELEN) {
hostnamestring.Truncate(MAXHOSTNAMELEN);
hostnamelen = MAXHOSTNAMELEN;
}
hostnamestring << fHostname->Text();
if (sethostname(hostnamestring, hostnamelen) == 0)
return B_OK;
return B_ERROR;
}

View File

@ -0,0 +1,48 @@
/*
* Copyright 2014-2019 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Adrien Destugues, pulkomandy@pulkomandy.tk
* Rob Gill, rrobgill@protonmail.com
*/
#ifndef HOSTNAME_VIEW_H
#define HOSTNAME_VIEW_H
#include <NetworkSettingsAddOn.h>
#include <StringList.h>
#include <View.h>
using namespace BNetworkKit;
class BButton;
class BTextControl;
class HostnameView : public BView {
public:
HostnameView(BNetworkSettingsItem* item);
~HostnameView();
status_t Revert();
bool IsRevertable() const;
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message);
private:
status_t _LoadHostname();
status_t _SaveHostname();
private:
BNetworkSettingsItem*
fItem;
BTextControl* fHostname;
BButton* fApplyButton;
};
#endif // HOSTNAME_VIEW_H

View File

@ -0,0 +1,20 @@
SubDir HAIKU_TOP src add-ons network_settings hostname ;
UsePublicHeaders [ FDirName add-ons network_settings ] ;
UsePrivateHeaders app libroot kernel net ;
UseHeaders [ FDirName $(HAIKU_TOP) src preferences network ] : false ;
AddResources Hostname : Hostname.rdef ;
Addon Hostname :
HostnameAddOn.cpp
HostnameView.cpp
: be network bnetapi <nogrist>Network [ TargetLibsupc++ ]
[ TargetLibstdc++ ] localestub
;
DoCatalogs Hostname : x-vnd.Haiku-Hostname :
HostnameAddOn.cpp
HostnameView.cpp
;