Implement saving DNS server list.

This makes the preflet good enough for replacing the current one.
This commit is contained in:
Adrien Destugues 2014-10-30 12:23:33 +01:00
parent 5cd3ae7c0d
commit 446094bcb3
4 changed files with 142 additions and 9 deletions

View File

@ -10,8 +10,11 @@
#include "DNSSettingsView.h"
#include <Button.h>
#include <File.h>
#include <FindDirectory.h>
#include <LayoutBuilder.h>
#include <ListView.h>
#include <Path.h>
#include <ScrollView.h>
#include <TextControl.h>
@ -25,6 +28,8 @@
static const int32 kAddServer = 'adds';
static const int32 kDeleteServer = 'dels';
static const int32 kMoveUp = 'mvup';
static const int32 kMoveDown = 'mvdn';
static const int32 kEditServer = 'edit';
@ -35,8 +40,8 @@ DNSSettingsView::DNSSettingsView()
fTextControl->SetModificationMessage(new BMessage(kEditServer));
fAddButton = new BButton("Add", new BMessage(kAddServer));
fUpButton = new BButton("Move up", NULL);
fDownButton = new BButton("Move down", NULL);
fUpButton = new BButton("Move up", new BMessage(kMoveUp));
fDownButton = new BButton("Move down", new BMessage(kMoveDown));
fRemoveButton = new BButton("Remove", new BMessage(kDeleteServer));
BLayoutBuilder::Group<>(this)
@ -51,7 +56,7 @@ DNSSettingsView::DNSSettingsView()
.End()
.End();
_ParseResolvConf();
_LoadDNSConfiguration();
}
@ -79,7 +84,25 @@ DNSSettingsView::MessageReceived(BMessage* message)
}
case kDeleteServer:
{
fServerListView->RemoveItem(fServerListView->CurrentSelection());
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;
}
case kEditServer:
{
@ -87,6 +110,7 @@ DNSSettingsView::MessageReceived(BMessage* message)
bool success = inet_aton(fTextControl->Text(), &dummy);
fTextControl->MarkAsInvalid(!success);
fAddButton->SetEnabled(success);
break;
}
default:
BGroupView::MessageReceived(message);
@ -95,18 +119,88 @@ DNSSettingsView::MessageReceived(BMessage* message)
status_t
DNSSettingsView::_ParseResolvConf()
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()
{
res_init();
res_state state = __res_state();
if (state != NULL) {
for (int i = 0; i < state->nscount; i++) {
fServerListView->AddItem(
new BStringItem(inet_ntoa(state->nsaddr_list[i].sin_addr)));
char* address = inet_ntoa(state->nsaddr_list[i].sin_addr);
fServerListView->AddItem(new BStringItem(address));
fRevertList.Add(address);
}
return B_OK;
}
return B_ERROR;
}
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";
}
}
#if 0
if (strlen(settings->Domain()) > 0) {
content << "domain\t"
<< settings->Domain()
<< "\n";
}
#endif
return file.Write(content.String(), content.Length());
}

View File

@ -8,6 +8,7 @@
#include <GroupView.h>
#include <StringList.h>
class BButton;
@ -22,11 +23,16 @@ public:
void AttachedToWindow();
void MessageReceived(BMessage* message);
status_t Apply();
status_t Revert();
private:
status_t _ParseResolvConf();
status_t _LoadDNSConfiguration();
status_t _SaveDNSConfiguration();
private:
BListView* fServerListView;
BStringList fRevertList;
BTextControl* fTextControl;
BButton* fAddButton;

View File

@ -79,7 +79,7 @@ ServicesAddOn::AttachedToWindow()
void
ServicesAddOn::MessageReceived(BMessage* message)
{
switch(message->what) {
switch (message->what) {
case kSelectionChanged:
{
BStringItem* item = static_cast<BStringItem*>(
@ -111,6 +111,36 @@ ServicesAddOn::MessageReceived(BMessage* message)
}
status_t
ServicesAddOn::Save()
{
BView* panel = Window()->FindView("panel");
DNSSettingsView* settingsView = dynamic_cast<DNSSettingsView*>(
panel->ChildAt(0));
// View not active - nothing to save
if (settingsView == NULL)
return B_OK;
return settingsView->Apply();
}
status_t
ServicesAddOn::Revert()
{
BView* panel = Window()->FindView("panel");
DNSSettingsView* settingsView = dynamic_cast<DNSSettingsView*>(
panel->ChildAt(0));
// View not active - nothing to revert
if (settingsView == NULL)
return B_OK;
return settingsView->Revert();
}
status_t
ServicesAddOn::_ParseInetd()
{

View File

@ -23,6 +23,9 @@ class ServicesAddOn : public NetworkSetupAddOn, public BGroupView {
void AttachedToWindow();
void MessageReceived(BMessage*);
status_t Save();
status_t Revert();
private:
status_t _ParseInetd();
status_t _ParseXinetd();