174726e70SRob Gill /*
274726e70SRob Gill * Copyright 2019 Haiku Inc. All rights reserved.
374726e70SRob Gill * Distributed under the terms of the MIT License.
474726e70SRob Gill *
574726e70SRob Gill * Authors:
674726e70SRob Gill * Adrien Destugues, pulkomandy@pulkomandy.tk
774726e70SRob Gill * Rob Gill, <rrobgill@protonmail.com>
874726e70SRob Gill */
974726e70SRob Gill
1074726e70SRob Gill
1174726e70SRob Gill #include "HostnameView.h"
1274726e70SRob Gill
1374726e70SRob Gill #include <stdio.h>
1474726e70SRob Gill #include <string.h>
1574726e70SRob Gill
1674726e70SRob Gill #include <Box.h>
1774726e70SRob Gill #include <Button.h>
1874726e70SRob Gill #include <Catalog.h>
1974726e70SRob Gill #include <ControlLook.h>
2074726e70SRob Gill #include <LayoutBuilder.h>
2174726e70SRob Gill #include <SeparatorView.h>
2274726e70SRob Gill #include <StringView.h>
2374726e70SRob Gill
2474726e70SRob Gill
2574726e70SRob Gill static const int32 kMsgApply = 'aply';
2674726e70SRob Gill
2774726e70SRob Gill
2874726e70SRob Gill #undef B_TRANSLATION_CONTEXT
2974726e70SRob Gill #define B_TRANSLATION_CONTEXT "HostnameView"
3074726e70SRob Gill
3174726e70SRob Gill
HostnameView(BNetworkSettingsItem * item)3274726e70SRob Gill HostnameView::HostnameView(BNetworkSettingsItem* item)
3374726e70SRob Gill :
3474726e70SRob Gill BView("hostname", 0),
3574726e70SRob Gill fItem(item)
3674726e70SRob Gill {
3774726e70SRob Gill BStringView* titleView = new BStringView("title",
3874726e70SRob Gill B_TRANSLATE("Hostname settings"));
3974726e70SRob Gill titleView->SetFont(be_bold_font);
4074726e70SRob Gill titleView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
4174726e70SRob Gill
4274726e70SRob Gill fHostname = new BTextControl(B_TRANSLATE("Hostname:"), "", NULL);
4374726e70SRob Gill fApplyButton = new BButton(B_TRANSLATE("Apply"), new BMessage(kMsgApply));
4474726e70SRob Gill
4574726e70SRob Gill BLayoutBuilder::Group<>(this, B_VERTICAL)
4674726e70SRob Gill .Add(titleView)
4774726e70SRob Gill .Add(fHostname)
4874726e70SRob Gill .AddGlue()
4974726e70SRob Gill .AddGroup(B_HORIZONTAL)
5074726e70SRob Gill .AddGlue()
5174726e70SRob Gill .Add(fApplyButton);
5274726e70SRob Gill
5374726e70SRob Gill _LoadHostname();
5474726e70SRob Gill }
5574726e70SRob Gill
5674726e70SRob Gill
~HostnameView()5774726e70SRob Gill HostnameView::~HostnameView()
5874726e70SRob Gill {
5974726e70SRob Gill }
6074726e70SRob Gill
6174726e70SRob Gill status_t
Revert()6274726e70SRob Gill HostnameView::Revert()
6374726e70SRob Gill {
6474726e70SRob Gill return B_OK;
6574726e70SRob Gill }
6674726e70SRob Gill
6774726e70SRob Gill bool
IsRevertable() const6874726e70SRob Gill HostnameView::IsRevertable() const
6974726e70SRob Gill {
7074726e70SRob Gill return false;
7174726e70SRob Gill }
7274726e70SRob Gill
7374726e70SRob Gill
7474726e70SRob Gill void
AttachedToWindow()7574726e70SRob Gill HostnameView::AttachedToWindow()
7674726e70SRob Gill {
7774726e70SRob Gill fApplyButton->SetTarget(this);
7874726e70SRob Gill }
7974726e70SRob Gill
8074726e70SRob Gill
8174726e70SRob Gill void
MessageReceived(BMessage * message)8274726e70SRob Gill HostnameView::MessageReceived(BMessage* message)
8374726e70SRob Gill {
8474726e70SRob Gill switch (message->what) {
8574726e70SRob Gill case kMsgApply:
8674726e70SRob Gill if (_SaveHostname() == B_OK)
8774726e70SRob Gill fItem->NotifySettingsUpdated();
8874726e70SRob Gill break;
8974726e70SRob Gill
9074726e70SRob Gill default:
9174726e70SRob Gill BView::MessageReceived(message);
9274726e70SRob Gill break;
9374726e70SRob Gill }
9474726e70SRob Gill }
9574726e70SRob Gill
9674726e70SRob Gill
9774726e70SRob Gill status_t
_LoadHostname()9874726e70SRob Gill HostnameView::_LoadHostname()
9974726e70SRob Gill {
10074726e70SRob Gill BString fHostnameString;
10174726e70SRob Gill char hostname[MAXHOSTNAMELEN];
10274726e70SRob Gill
10374726e70SRob Gill if (gethostname(hostname, MAXHOSTNAMELEN) == 0) {
10474726e70SRob Gill
10574726e70SRob Gill fHostnameString.SetTo(hostname, MAXHOSTNAMELEN);
10674726e70SRob Gill fHostname->SetText(fHostnameString);
10774726e70SRob Gill
10874726e70SRob Gill return B_OK;
109*57657d22SAdrien Destugues }
11074726e70SRob Gill
11174726e70SRob Gill return B_ERROR;
11274726e70SRob Gill }
11374726e70SRob Gill
11474726e70SRob Gill
11574726e70SRob Gill status_t
_SaveHostname()11674726e70SRob Gill HostnameView::_SaveHostname()
11774726e70SRob Gill {
11874726e70SRob Gill BString hostnamestring("");
11974726e70SRob Gill
12074726e70SRob Gill size_t hostnamelen(strlen(fHostname->Text()));
12174726e70SRob Gill
12274726e70SRob Gill if (hostnamelen == 0)
12374726e70SRob Gill return B_ERROR;
12474726e70SRob Gill
12574726e70SRob Gill if (hostnamelen > MAXHOSTNAMELEN) {
12674726e70SRob Gill hostnamestring.Truncate(MAXHOSTNAMELEN);
12774726e70SRob Gill hostnamelen = MAXHOSTNAMELEN;
12874726e70SRob Gill }
12974726e70SRob Gill
13074726e70SRob Gill hostnamestring << fHostname->Text();
13174726e70SRob Gill
13274726e70SRob Gill if (sethostname(hostnamestring, hostnamelen) == 0)
13374726e70SRob Gill return B_OK;
13474726e70SRob Gill
13574726e70SRob Gill return B_ERROR;
13674726e70SRob Gill }
137