1/* 2 * Copyright 2019 Haiku Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Adrien Destugues, pulkomandy@pulkomandy.tk 7 * Rob Gill, <rrobgill@protonmail.com> 8 */ 9 10 11#include "HostnameView.h" 12 13#include <stdio.h> 14#include <string.h> 15 16#include <Box.h> 17#include <Button.h> 18#include <Catalog.h> 19#include <ControlLook.h> 20#include <LayoutBuilder.h> 21#include <SeparatorView.h> 22#include <StringView.h> 23 24 25static const int32 kMsgApply = 'aply'; 26 27 28#undef B_TRANSLATION_CONTEXT 29#define B_TRANSLATION_CONTEXT "HostnameView" 30 31 32HostnameView::HostnameView(BNetworkSettingsItem* item) 33 : 34 BView("hostname", 0), 35 fItem(item) 36{ 37 BStringView* titleView = new BStringView("title", 38 B_TRANSLATE("Hostname settings")); 39 titleView->SetFont(be_bold_font); 40 titleView->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET)); 41 42 fHostname = new BTextControl(B_TRANSLATE("Hostname:"), "", NULL); 43 fApplyButton = new BButton(B_TRANSLATE("Apply"), new BMessage(kMsgApply)); 44 45 BLayoutBuilder::Group<>(this, B_VERTICAL) 46 .Add(titleView) 47 .Add(fHostname) 48 .AddGlue() 49 .AddGroup(B_HORIZONTAL) 50 .AddGlue() 51 .Add(fApplyButton); 52 53 _LoadHostname(); 54} 55 56 57HostnameView::~HostnameView() 58{ 59} 60 61status_t 62HostnameView::Revert() 63{ 64 return B_OK; 65} 66 67bool 68HostnameView::IsRevertable() const 69{ 70 return false; 71} 72 73 74void 75HostnameView::AttachedToWindow() 76{ 77 fApplyButton->SetTarget(this); 78} 79 80 81void 82HostnameView::MessageReceived(BMessage* message) 83{ 84 switch (message->what) { 85 case kMsgApply: 86 if (_SaveHostname() == B_OK) 87 fItem->NotifySettingsUpdated(); 88 break; 89 90 default: 91 BView::MessageReceived(message); 92 break; 93 } 94} 95 96 97status_t 98HostnameView::_LoadHostname() 99{ 100 BString fHostnameString; 101 char hostname[MAXHOSTNAMELEN]; 102 103 if (gethostname(hostname, MAXHOSTNAMELEN) == 0) { 104 105 fHostnameString.SetTo(hostname, MAXHOSTNAMELEN); 106 fHostname->SetText(fHostnameString); 107 108 return B_OK; 109 } 110 111 return B_ERROR; 112} 113 114 115status_t 116HostnameView::_SaveHostname() 117{ 118 BString hostnamestring(""); 119 120 size_t hostnamelen(strlen(fHostname->Text())); 121 122 if (hostnamelen == 0) 123 return B_ERROR; 124 125 if (hostnamelen > MAXHOSTNAMELEN) { 126 hostnamestring.Truncate(MAXHOSTNAMELEN); 127 hostnamelen = MAXHOSTNAMELEN; 128 } 129 130 hostnamestring << fHostname->Text(); 131 132 if (sethostname(hostnamestring, hostnamelen) == 0) 133 return B_OK; 134 135 return B_ERROR; 136} 137