Implement saving DNS server list.
This makes the preflet good enough for replacing the current one.
This commit is contained in:
@@ -10,8 +10,11 @@
|
|||||||
#include "DNSSettingsView.h"
|
#include "DNSSettingsView.h"
|
||||||
|
|
||||||
#include <Button.h>
|
#include <Button.h>
|
||||||
|
#include <File.h>
|
||||||
|
#include <FindDirectory.h>
|
||||||
#include <LayoutBuilder.h>
|
#include <LayoutBuilder.h>
|
||||||
#include <ListView.h>
|
#include <ListView.h>
|
||||||
|
#include <Path.h>
|
||||||
#include <ScrollView.h>
|
#include <ScrollView.h>
|
||||||
#include <TextControl.h>
|
#include <TextControl.h>
|
||||||
|
|
||||||
@@ -25,6 +28,8 @@
|
|||||||
|
|
||||||
static const int32 kAddServer = 'adds';
|
static const int32 kAddServer = 'adds';
|
||||||
static const int32 kDeleteServer = 'dels';
|
static const int32 kDeleteServer = 'dels';
|
||||||
|
static const int32 kMoveUp = 'mvup';
|
||||||
|
static const int32 kMoveDown = 'mvdn';
|
||||||
static const int32 kEditServer = 'edit';
|
static const int32 kEditServer = 'edit';
|
||||||
|
|
||||||
|
|
||||||
@@ -35,8 +40,8 @@ DNSSettingsView::DNSSettingsView()
|
|||||||
fTextControl->SetModificationMessage(new BMessage(kEditServer));
|
fTextControl->SetModificationMessage(new BMessage(kEditServer));
|
||||||
|
|
||||||
fAddButton = new BButton("Add", new BMessage(kAddServer));
|
fAddButton = new BButton("Add", new BMessage(kAddServer));
|
||||||
fUpButton = new BButton("Move up", NULL);
|
fUpButton = new BButton("Move up", new BMessage(kMoveUp));
|
||||||
fDownButton = new BButton("Move down", NULL);
|
fDownButton = new BButton("Move down", new BMessage(kMoveDown));
|
||||||
fRemoveButton = new BButton("Remove", new BMessage(kDeleteServer));
|
fRemoveButton = new BButton("Remove", new BMessage(kDeleteServer));
|
||||||
|
|
||||||
BLayoutBuilder::Group<>(this)
|
BLayoutBuilder::Group<>(this)
|
||||||
@@ -51,7 +56,7 @@ DNSSettingsView::DNSSettingsView()
|
|||||||
.End()
|
.End()
|
||||||
.End();
|
.End();
|
||||||
|
|
||||||
_ParseResolvConf();
|
_LoadDNSConfiguration();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -79,7 +84,25 @@ DNSSettingsView::MessageReceived(BMessage* message)
|
|||||||
}
|
}
|
||||||
case kDeleteServer:
|
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:
|
case kEditServer:
|
||||||
{
|
{
|
||||||
@@ -87,6 +110,7 @@ DNSSettingsView::MessageReceived(BMessage* message)
|
|||||||
bool success = inet_aton(fTextControl->Text(), &dummy);
|
bool success = inet_aton(fTextControl->Text(), &dummy);
|
||||||
fTextControl->MarkAsInvalid(!success);
|
fTextControl->MarkAsInvalid(!success);
|
||||||
fAddButton->SetEnabled(success);
|
fAddButton->SetEnabled(success);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
BGroupView::MessageReceived(message);
|
BGroupView::MessageReceived(message);
|
||||||
@@ -95,18 +119,88 @@ DNSSettingsView::MessageReceived(BMessage* message)
|
|||||||
|
|
||||||
|
|
||||||
status_t
|
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_init();
|
||||||
res_state state = __res_state();
|
res_state state = __res_state();
|
||||||
|
|
||||||
if (state != NULL) {
|
if (state != NULL) {
|
||||||
for (int i = 0; i < state->nscount; i++) {
|
for (int i = 0; i < state->nscount; i++) {
|
||||||
fServerListView->AddItem(
|
char* address = inet_ntoa(state->nsaddr_list[i].sin_addr);
|
||||||
new BStringItem(inet_ntoa(state->nsaddr_list[i].sin_addr)));
|
fServerListView->AddItem(new BStringItem(address));
|
||||||
|
fRevertList.Add(address);
|
||||||
}
|
}
|
||||||
return B_OK;
|
return B_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
return B_ERROR;
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
|
|
||||||
#include <GroupView.h>
|
#include <GroupView.h>
|
||||||
|
#include <StringList.h>
|
||||||
|
|
||||||
|
|
||||||
class BButton;
|
class BButton;
|
||||||
@@ -22,11 +23,16 @@ public:
|
|||||||
void AttachedToWindow();
|
void AttachedToWindow();
|
||||||
void MessageReceived(BMessage* message);
|
void MessageReceived(BMessage* message);
|
||||||
|
|
||||||
|
status_t Apply();
|
||||||
|
status_t Revert();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _ParseResolvConf();
|
status_t _LoadDNSConfiguration();
|
||||||
|
status_t _SaveDNSConfiguration();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BListView* fServerListView;
|
BListView* fServerListView;
|
||||||
|
BStringList fRevertList;
|
||||||
BTextControl* fTextControl;
|
BTextControl* fTextControl;
|
||||||
|
|
||||||
BButton* fAddButton;
|
BButton* fAddButton;
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ ServicesAddOn::AttachedToWindow()
|
|||||||
void
|
void
|
||||||
ServicesAddOn::MessageReceived(BMessage* message)
|
ServicesAddOn::MessageReceived(BMessage* message)
|
||||||
{
|
{
|
||||||
switch(message->what) {
|
switch (message->what) {
|
||||||
case kSelectionChanged:
|
case kSelectionChanged:
|
||||||
{
|
{
|
||||||
BStringItem* item = static_cast<BStringItem*>(
|
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
|
status_t
|
||||||
ServicesAddOn::_ParseInetd()
|
ServicesAddOn::_ParseInetd()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -23,6 +23,9 @@ class ServicesAddOn : public NetworkSetupAddOn, public BGroupView {
|
|||||||
void AttachedToWindow();
|
void AttachedToWindow();
|
||||||
void MessageReceived(BMessage*);
|
void MessageReceived(BMessage*);
|
||||||
|
|
||||||
|
status_t Save();
|
||||||
|
status_t Revert();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
status_t _ParseInetd();
|
status_t _ParseInetd();
|
||||||
status_t _ParseXinetd();
|
status_t _ParseXinetd();
|
||||||
|
|||||||
Reference in New Issue
Block a user