Files
haiku-beta6/src/preferences/network/ServicesAddOn/DNSSettingsView.cpp
T

207 lines
4.4 KiB
C++
Raw Normal View History

2014-10-30 11:24:04 +01:00
/*
* Copyright 2014 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Adrien Destugues, [email protected]
*/
#include "DNSSettingsView.h"
#include <Button.h>
2014-10-30 12:23:33 +01:00
#include <File.h>
#include <FindDirectory.h>
2014-10-30 11:24:04 +01:00
#include <LayoutBuilder.h>
#include <ListView.h>
2014-10-30 12:23:33 +01:00
#include <Path.h>
2014-10-30 11:24:04 +01:00
#include <ScrollView.h>
#include <TextControl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <resolv.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
static const int32 kAddServer = 'adds';
static const int32 kDeleteServer = 'dels';
2014-10-30 12:23:33 +01:00
static const int32 kMoveUp = 'mvup';
static const int32 kMoveDown = 'mvdn';
2014-10-30 11:24:04 +01:00
static const int32 kEditServer = 'edit';
DNSSettingsView::DNSSettingsView()
2014-10-30 21:24:23 +01:00
: BGroupView(B_VERTICAL)
2014-10-30 11:24:04 +01:00
{
fServerListView = new BListView("nameservers");
fTextControl = new BTextControl("", "", NULL);
fTextControl->SetModificationMessage(new BMessage(kEditServer));
fAddButton = new BButton("Add", new BMessage(kAddServer));
2014-10-30 12:23:33 +01:00
fUpButton = new BButton("Move up", new BMessage(kMoveUp));
fDownButton = new BButton("Move down", new BMessage(kMoveDown));
2014-10-30 11:24:04 +01:00
fRemoveButton = new BButton("Remove", new BMessage(kDeleteServer));
BLayoutBuilder::Group<>(this)
.AddGrid(B_HORIZONTAL)
.Add(fTextControl, 0, 0)
.Add(fAddButton, 1, 0)
.Add(new BScrollView("scroll", fServerListView, 0, false, true),
0, 1, 1, 4)
.Add(fUpButton, 1, 1)
.Add(fDownButton, 1, 2)
.Add(fRemoveButton, 1, 3)
.End()
2014-10-30 21:24:23 +01:00
.Add(fDomain = new BTextControl("Domain", "", NULL))
2014-10-30 11:24:04 +01:00
.End();
2014-10-30 12:23:33 +01:00
_LoadDNSConfiguration();
2014-10-30 11:24:04 +01:00
}
void
DNSSettingsView::AttachedToWindow()
{
fAddButton->SetTarget(this);
fRemoveButton->SetTarget(this);
fUpButton->SetTarget(this);
fDownButton->SetTarget(this);
fTextControl->SetTarget(this);
}
void
DNSSettingsView::MessageReceived(BMessage* message)
{
switch(message->what) {
case kAddServer:
{
const char* address = fTextControl->Text();
fServerListView->AddItem(new BStringItem(address));
break;
}
case kDeleteServer:
{
2014-10-30 12:23:33 +01:00
delete fServerListView->RemoveItem(
fServerListView->CurrentSelection());
break;
}
case kMoveUp:
{
int index = fServerListView->CurrentSelection();
if (index > 0)
fServerListView->SwapItems(index, index - 1);
break;
}
case kMoveDown:
{
int index = fServerListView->CurrentSelection();
if (index < fServerListView->CountItems() - 1)
fServerListView->SwapItems(index, index + 1);
break;
2014-10-30 11:24:04 +01:00
}
case kEditServer:
{
struct in_addr dummy;
bool success = inet_aton(fTextControl->Text(), &dummy);
fTextControl->MarkAsInvalid(!success);
fAddButton->SetEnabled(success);
2014-10-30 12:23:33 +01:00
break;
2014-10-30 11:24:04 +01:00
}
default:
BGroupView::MessageReceived(message);
}
}
status_t
2014-10-30 12:23:33 +01:00
DNSSettingsView::Apply()
{
return _SaveDNSConfiguration();
}
status_t
DNSSettingsView::Revert()
{
int i;
for(i = 0; i < fRevertList.CountStrings(); i++) {
BStringItem* item = static_cast<BStringItem*>(fServerListView->ItemAt(i));
if (item == NULL) {
item = new BStringItem("");
fServerListView->AddItem(item);
}
item->SetText(fRevertList.StringAt(i));
}
// Now remove any extra item
for(; i < fServerListView->CountItems(); i++)
delete fServerListView->RemoveItem(i);
return B_OK;
}
status_t
DNSSettingsView::_LoadDNSConfiguration()
2014-10-30 11:24:04 +01:00
{
res_init();
res_state state = __res_state();
if (state != NULL) {
for (int i = 0; i < state->nscount; i++) {
2014-10-30 12:23:33 +01:00
char* address = inet_ntoa(state->nsaddr_list[i].sin_addr);
fServerListView->AddItem(new BStringItem(address));
fRevertList.Add(address);
2014-10-30 11:24:04 +01:00
}
2014-10-30 21:24:23 +01:00
fDomain->SetText(state->dnsrch[0]);
2014-10-30 11:24:04 +01:00
return B_OK;
}
return B_ERROR;
}
2014-10-30 12:23:33 +01:00
status_t
DNSSettingsView::_SaveDNSConfiguration()
{
BPath path;
status_t status;
status = find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path);
if (status != B_OK)
return status;
path.Append("network/resolv.conf");
BFile file(path.Path(), B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
if (file.InitCheck() != B_OK) {
fprintf(stderr, "failed to open %s for writing: %s\n", path.Path(),
strerror(file.InitCheck()));
return file.InitCheck();
}
BString content("# Generated by Network Preflet\n");
for (int j = 0; j < fServerListView->CountItems(); j++) {
BString item = ((BStringItem*)fServerListView->ItemAt(j))->Text();
if (item.Length() > 0) {
content << "nameserver\t" << item.String() << "\n";
}
}
2014-10-30 21:24:23 +01:00
if (strlen(fDomain->Text()) > 0)
content << "domain\t" << fDomain->Text() << "\n";
2014-10-30 12:23:33 +01:00
return file.Write(content.String(), content.Length());
}