* some more cleanup and refactoring in EthernetSettingsView

* added Revert button
* added enabling/disabling of text controls according to Mode setting


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22239 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2007-09-17 17:50:06 +00:00
parent 5406d829a8
commit 0fcc16e538
3 changed files with 170 additions and 135 deletions
+160 -126
View File
@@ -53,7 +53,7 @@
#include <string.h>
#include <unistd.h>
#include "AutoDeleter.h"
bool
@@ -69,8 +69,10 @@ EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name)
return true;
}
void
EthernetSettingsView::_GatherInterfaces() {
EthernetSettingsView::_GatherInterfaces()
{
// iterate over all interfaces and retrieve minimal status
ifconf config;
@@ -82,33 +84,30 @@ EthernetSettingsView::_GatherInterfaces() {
if (count == 0)
return;
void *buffer = malloc(count * sizeof(struct ifreq));
void* buffer = malloc(count * sizeof(struct ifreq));
if (buffer == NULL)
return;
MemoryDeleter deleter(buffer);
config.ifc_len = count * sizeof(struct ifreq);
config.ifc_buf = buffer;
if (ioctl(fSocket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
return;
ifreq* interface = (ifreq *)buffer;
ifreq* interface = (ifreq*)buffer;
fInterfaces.MakeEmpty();
for (uint32 i = 0; i < count; i++) {
if (strncmp(interface->ifr_name, "loop", 4) && interface->ifr_name[0])
{
if (strncmp(interface->ifr_name, "loop", 4) && interface->ifr_name[0]) {
fInterfaces.AddItem(new BString(interface->ifr_name));
fSettings.AddItem(new Settings(interface->ifr_name));
}
interface = (ifreq *)((addr_t)interface + IF_NAMESIZE
interface = (ifreq*)((addr_t)interface + IF_NAMESIZE
+ interface->ifr_addr.sa_len);
}
free(buffer);
}
@@ -116,15 +115,12 @@ void
EthernetSettingsView::AttachedToWindow()
{
fApplyButton->SetTarget(this);
fRevertButton->SetTarget(this);
fDeviceMenuField->Menu()->SetTargetForItems(this);
fTypeMenuField->Menu()->SetTargetForItems(this);
// display settigs of first adapter on startup
Settings* settings = fSettings.ItemAt(0);
if (settings) {
BMessage info(kMsgInfo);
info.AddString("interface", settings->GetName());
_ShowConfiguration(&info);
}
_ShowConfiguration(fSettings.ItemAt(0));
}
@@ -136,10 +132,15 @@ EthernetSettingsView::DetachedFromWindow()
EthernetSettingsView::EthernetSettingsView(BRect frame)
: BView(frame, "EthernetSettingsView", B_FOLLOW_ALL, B_WILL_DRAW)
, fInterfaces()
, fSettings()
, fCurrentSettings(NULL)
{
float inset = ceilf(be_plain_font->Size() * 0.8);
frame.OffsetTo(B_ORIGIN);
frame.InsetBy(inset, inset);
frame.OffsetTo(inset, inset);
frame.right = StringWidth("IP Address XXX.XXX.XXX.XXX") + 50;
frame.bottom = frame.top + 15; // just a starting point
BPoint spacing(0, inset);
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
@@ -157,76 +158,93 @@ EthernetSettingsView::EthernetSettingsView(BRect frame)
}
BPopUpMenu* modemenu = new BPopUpMenu("modes");
BMenuItem* staticitem = new BMenuItem("Static", NULL);
modemenu->AddItem(staticitem);
BMenuItem* dhcpitem = new BMenuItem("DHCP", NULL);
modemenu->AddItem(dhcpitem);
modemenu->AddItem(new BMenuItem("Static", new BMessage(kMsgMode)));
modemenu->AddItem(new BMenuItem("DHCP", new BMessage(kMsgMode)));
//BMenuItem* offitem = new BMenuItem("Disconnected", NULL);
//modemenu->AddItem(offitem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu);
fDeviceMenuField->SetDivider(
fDeviceMenuField->StringWidth(fDeviceMenuField->Label()) + 8);
AddChild(fDeviceMenuField);
fDeviceMenuField->ResizeToPreferred();
fTypeMenuField = new BMenuField(frame, "type", "Mode:", modemenu);
fTypeMenuField->SetDivider(
fTypeMenuField->StringWidth(fTypeMenuField->Label()) + 8);
fTypeMenuField->MoveTo(fDeviceMenuField->Frame().LeftBottom() + BPoint(0,10));
fTypeMenuField->MoveTo(fDeviceMenuField->Frame().LeftBottom() + spacing);
AddChild(fTypeMenuField);
fTypeMenuField->ResizeToPreferred();
fIPTextControl = new BTextControl(frame, "ip", "IP Address:", "", NULL,
B_FOLLOW_TOP, B_WILL_DRAW );
fIPTextControl->SetDivider(
fIPTextControl->StringWidth(fIPTextControl->Label()) + 8);
fIPTextControl->MoveTo(fTypeMenuField->Frame().LeftBottom() + BPoint(0,10));
fIPTextControl = new BTextControl(frame, "ip", "IP Address:", "", NULL);
fIPTextControl->MoveTo(fTypeMenuField->Frame().LeftBottom() + spacing);
fIPTextControl->ResizeToPreferred();
AddChild(fIPTextControl);
fNetMaskTextControl = new BTextControl(frame, "mask", "Netmask:", "", NULL,
B_FOLLOW_TOP, B_WILL_DRAW );
fNetMaskTextControl->SetDivider(
fNetMaskTextControl->StringWidth(fNetMaskTextControl->Label()) + 8);
fNetMaskTextControl = new BTextControl(frame, "mask", "Netmask:", "", NULL);
fNetMaskTextControl->MoveTo(
fIPTextControl->Frame().LeftBottom() + BPoint(0,10));
fIPTextControl->Frame().LeftBottom() + spacing);
AddChild(fNetMaskTextControl);
fNetMaskTextControl->ResizeToPreferred();
fGatewayTextControl = new BTextControl(frame, "gateway", "Gateway:", "",
NULL, B_FOLLOW_TOP, B_WILL_DRAW );
fGatewayTextControl->SetDivider(
fGatewayTextControl->StringWidth(fGatewayTextControl->Label()) + 8);
NULL);
fGatewayTextControl->MoveTo(
fNetMaskTextControl->Frame().LeftBottom() + BPoint(0,10));
fNetMaskTextControl->Frame().LeftBottom() + spacing);
AddChild(fGatewayTextControl);
fGatewayTextControl->ResizeToPreferred();
fPrimaryDNSTextControl = new BTextControl(
frame, "dns1", "DNS #1:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW );
fPrimaryDNSTextControl->SetDivider(
fPrimaryDNSTextControl->StringWidth(fPrimaryDNSTextControl->Label()) + 8);
fPrimaryDNSTextControl = new BTextControl(frame, "dns1", "DNS #1:", "",
NULL);
fPrimaryDNSTextControl->MoveTo(
fGatewayTextControl->Frame().LeftBottom() + BPoint(0,10));
fGatewayTextControl->Frame().LeftBottom() + spacing);
AddChild(fPrimaryDNSTextControl);
fPrimaryDNSTextControl->ResizeToPreferred();
fSecondaryDNSTextControl = new BTextControl(
frame, "dns2", "DNS #2:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW );
fSecondaryDNSTextControl->SetDivider(
fSecondaryDNSTextControl->StringWidth(
fSecondaryDNSTextControl->Label()) + 8);
fSecondaryDNSTextControl = new BTextControl(frame, "dns2", "DNS #2:", "",
NULL);
fSecondaryDNSTextControl->MoveTo(
fPrimaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10));
fPrimaryDNSTextControl->Frame().LeftBottom() + spacing);
AddChild(fSecondaryDNSTextControl);
fSecondaryDNSTextControl->ResizeToPreferred();
fRevertButton = new BButton(frame, "revert", "Revert",
new BMessage(kMsgRevert));
fRevertButton->ResizeToPreferred();
fRevertButton->MoveTo(
fSecondaryDNSTextControl->Frame().LeftBottom() + spacing);
AddChild(fRevertButton);
fApplyButton = new BButton(frame, "apply", "Apply", new BMessage(kMsgApply));
fApplyButton->ResizeToPreferred();
fApplyButton->MoveTo(
fSecondaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10));
fSecondaryDNSTextControl->Frame().RightBottom() + spacing
+ BPoint(-fApplyButton->Frame().Width(), 0));
AddChild(fApplyButton);
ResizeTo(frame.Width() + 2 * inset, fApplyButton->Frame().bottom + inset);
// take care of label alignment
float maxLabelWidth
= fDeviceMenuField->StringWidth(fDeviceMenuField->Label());
maxLabelWidth = max_c(maxLabelWidth,
fTypeMenuField->StringWidth(fTypeMenuField->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fIPTextControl->StringWidth(fIPTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fNetMaskTextControl->StringWidth(fNetMaskTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fGatewayTextControl->StringWidth(fGatewayTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fPrimaryDNSTextControl->StringWidth(fPrimaryDNSTextControl->Label()));
maxLabelWidth = max_c(maxLabelWidth,
fSecondaryDNSTextControl->StringWidth(
fSecondaryDNSTextControl->Label()));
fDeviceMenuField->SetDivider(maxLabelWidth + 8);
fTypeMenuField->SetDivider(maxLabelWidth + 8);
fIPTextControl->SetDivider(maxLabelWidth + 8);
fNetMaskTextControl->SetDivider(maxLabelWidth + 8);
fGatewayTextControl->SetDivider(maxLabelWidth + 8);
fPrimaryDNSTextControl->SetDivider(maxLabelWidth + 8);
fSecondaryDNSTextControl->SetDivider(maxLabelWidth + 8);
}
EthernetSettingsView::~EthernetSettingsView()
@@ -235,83 +253,84 @@ EthernetSettingsView::~EthernetSettingsView()
}
void
EthernetSettingsView::_ShowConfiguration(const BMessage* message)
EthernetSettingsView::_ShowConfiguration(Settings* settings)
{
fCurrentSettings = settings;
// Clear the inputs.
fIPTextControl->SetText("");
fGatewayTextControl->SetText("");
fNetMaskTextControl->SetText("");
fPrimaryDNSTextControl->SetText("");
fSecondaryDNSTextControl->SetText("");
const char* name;
if (message->FindString("interface", &name) != B_OK)
return;
for (int32 i = 0; i < fSettings.CountItems(); i++) {
Settings* settings = fSettings.ItemAt(i);
if (strcmp(settings->GetName(), name) != 0)
continue;
fDeviceMenuField->Menu()->FindItem(name)->SetMarked(true);
bool enableControls = false;
fTypeMenuField->SetEnabled(settings != NULL);
if (settings) {
BMenuItem* item = fDeviceMenuField->Menu()->FindItem(
settings->GetName());
if (item)
item->SetMarked(true);
fIPTextControl->SetText(settings->GetIP());
fGatewayTextControl->SetText(settings->GetGateway());
fNetMaskTextControl->SetText(settings->GetNetmask());
if (settings->GetAutoConfigure() == true)
fTypeMenuField->Menu()->FindItem("DHCP")->SetMarked(true);
else
fTypeMenuField->Menu()->FindItem("Static")->SetMarked(true);
// fTypeMenuField->Menu()->SetLabelFromMarked(true);
// fDeviceMenuField->Menu()->SetLabelFromMarked(true);
if (settings->GetAutoConfigure() == true)
item = fTypeMenuField->Menu()->FindItem("DHCP");
else
item = fTypeMenuField->Menu()->FindItem("Static");
if (item)
item->SetMarked(true);
enableControls = settings->GetAutoConfigure() == false;
if (settings->fNameservers.CountItems() >= 2) {
fSecondaryDNSTextControl->SetText(
settings->fNameservers.ItemAt(1)->String());
}
if (settings->fNameservers.CountItems() >= 1) {
fPrimaryDNSTextControl->SetText(
settings->fNameservers.ItemAt(0)->String());
}
}
}
_EnableTextControls(enableControls);
}
void
EthernetSettingsView::_EnableTextControls(bool enable)
{
fIPTextControl->SetEnabled(enable);
fGatewayTextControl->SetEnabled(enable);
fNetMaskTextControl->SetEnabled(enable);
fPrimaryDNSTextControl->SetEnabled(enable);
fSecondaryDNSTextControl->SetEnabled(enable);
}
void
EthernetSettingsView::_ApplyControlsToConfiguration()
{
int i;
for (i = 0; i < fSettings.CountItems(); i++) {
if (!fCurrentSettings)
return;
if (strcmp(fSettings.ItemAt(i)->
GetName(), fDeviceMenuField->Menu()->FindMarked()->Label()) == 0) {
fSettings.ItemAt(i)->
SetIP(fIPTextControl->Text());
fSettings.ItemAt(i)->
SetNetmask(fNetMaskTextControl->Text());
fSettings.ItemAt(i)->
SetGateway(fGatewayTextControl->Text());
if (strcmp(fTypeMenuField->Menu()->FindMarked()->Label(), "DHCP")
== 0)
fSettings.ItemAt(i)->SetAutoConfigure(true);
else
fSettings.ItemAt(i)->SetAutoConfigure(false);
fCurrentSettings->SetIP(fIPTextControl->Text());
fCurrentSettings->SetNetmask(fNetMaskTextControl->Text());
fCurrentSettings->SetGateway(fGatewayTextControl->Text());
fSettings.ItemAt(i)->fNameservers.MakeEmpty();
fSettings.ItemAt(i)->fNameservers.AddItem(new BString(
fPrimaryDNSTextControl->Text()));
fSettings.ItemAt(i)->fNameservers.AddItem(new BString(
fSecondaryDNSTextControl->Text()));
}
}
}
fCurrentSettings->SetAutoConfigure(
strcmp(fTypeMenuField->Menu()->FindMarked()->Label(), "DHCP") == 0);
fCurrentSettings->fNameservers.MakeEmpty();
fCurrentSettings->fNameservers.AddItem(new BString(
fPrimaryDNSTextControl->Text()));
fCurrentSettings->fNameservers.AddItem(new BString(
fSecondaryDNSTextControl->Text()));
}
void
@@ -322,29 +341,29 @@ EthernetSettingsView::_SaveConfiguration()
_SaveAdaptersConfiguration();
}
void
EthernetSettingsView::_SaveDNSConfiguration()
{
FILE* fp;
if ((fp = fopen("/etc/resolv.conf", "w")) != NULL) {
fprintf(fp, "# Generated by Network Preflet\n");
int i;
for (i = 0; i < fSettings.CountItems(); i++) {
// loop all the adapters.
int j;
for (j = 0; j < fSettings.ItemAt(i)->fNameservers.CountItems(); j++) {
if (strcmp(
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String(), "") != 0) {
fprintf(fp, "nameserver\t%s\n",
fSettings.ItemAt(i)->fNameservers.ItemAt(j)->String());
}
}
FILE* fp = fopen("/etc/resolv.conf", "w");
if (fp == NULL)
return;
fprintf(fp, "# Generated by Network Preflet\n");
// loop over all adapters
for (int i = 0; i < fSettings.CountItems(); i++) {
Settings* settings = fSettings.ItemAt(i);
for (int j = 0; j < settings->fNameservers.CountItems(); j++) {
if (settings->fNameservers.ItemAt(j)->Length() > 0) {
fprintf(fp, "nameserver\t%s\n",
settings->fNameservers.ItemAt(j)->String());
}
fclose(fp);
}
}
fclose(fp);
}
void
EthernetSettingsView::_SaveAdaptersConfiguration()
{
@@ -354,14 +373,12 @@ EthernetSettingsView::_SaveAdaptersConfiguration()
return;
FILE* fp = NULL;
bool allDHCP = true;
// loop over all adapters. open the settings file only once,
// append the settins of each non-autoconfiguring adapter
for (int i = 0; i < fSettings.CountItems(); i++) {
if (fSettings.ItemAt(i)->GetAutoConfigure())
continue;
allDHCP = false;
if (fp == NULL) {
fp = fopen(path.Path(), "w");
if (fp == NULL) {
@@ -385,14 +402,13 @@ EthernetSettingsView::_SaveAdaptersConfiguration()
if (fp) {
printf("%s saved.\n", path.Path());
fclose(fp);
}
if (allDHCP) {
} else {
// all configuration is DHCP, so delete interfaces file.
remove(path.Path());
}
}
status_t
EthernetSettingsView::_GetPath(const char* name, BPath& path)
{
@@ -407,12 +423,30 @@ EthernetSettingsView::_GetPath(const char* name, BPath& path)
return B_OK;
}
void
EthernetSettingsView::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgInfo:
_ShowConfiguration(message);
case kMsgMode:
if (BMenuItem* item = fTypeMenuField->Menu()->FindMarked())
_EnableTextControls(strcmp(item->Label(), "DHCP") != 0);
break;
case kMsgInfo: {
const char* name;
if (message->FindString("interface", &name) != B_OK)
break;
for (int32 i = 0; i < fSettings.CountItems(); i++) {
Settings* settings = fSettings.ItemAt(i);
if (strcmp(settings->GetName(), name) == 0) {
_ShowConfiguration(settings);
break;
}
}
break;
}
case kMsgRevert:
_ShowConfiguration(fCurrentSettings);
break;
case kMsgApply:
_SaveConfiguration();
@@ -20,9 +20,11 @@ class BMenuField;
class BTextControl;
static const uint32 kMsgApply = 'aply';
static const uint32 kMsgRevert = 'rvrt';
static const uint32 kMsgClose = 'clse';
static const uint32 kMsgField = 'fild';
static const uint32 kMsgInfo = 'info';
static const uint32 kMsgMode = 'mode';
class EthernetSettingsView : public BView {
public:
@@ -37,8 +39,8 @@ class EthernetSettingsView : public BView {
void LoadProfile(BString profileName);
private:
BButton* fCloseButton;
BButton* fApplyButton;
BButton* fRevertButton;
BMenuField* fDeviceMenuField;
BMenuField* fTypeMenuField;
BTextControl* fIPTextControl;
@@ -48,19 +50,19 @@ class EthernetSettingsView : public BView {
BTextControl* fSecondaryDNSTextControl;
BObjectList<BString> fInterfaces;
BObjectList<Settings> fSettings;
Settings* fCurrentSettings;
int32 fStatus;
int fSocket;
void _GatherInterfaces();
bool _PrepareRequest(struct ifreq& request, const char* name);
void _ShowConfiguration(const BMessage* message);
void _ShowConfiguration(Settings* settings);
void _EnableTextControls(bool enable);
void _SaveConfiguration();
void _SaveDNSConfiguration();
void _SaveAdaptersConfiguration();
void _ApplyControlsToConfiguration();
status_t _GetPath(const char* name, BPath& path);
};
#endif /* ETHERNET_SETTINGS_VIEW_H */
+4 -5
View File
@@ -16,15 +16,14 @@
NetworkWindow::NetworkWindow()
: BWindow(BRect(50, 50, 269, 302), "Network",
B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
| B_NOT_ZOOMABLE)
: BWindow(BRect(50, 50, 269, 302), "Network", B_TITLED_WINDOW,
B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE)
{
fEthView = new EthernetSettingsView(Bounds());
ResizeTo(fEthView->Frame().Width(), fEthView->Frame().Height());
AddChild(fEthView);
}