* patch by Andre Garzia to fix some coding style issues

* fixed a potential memory leak in EthernetSettingsView::_ShowConfiguration()
  when the ioctl didn't succeed


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@21834 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus 2007-08-06 09:46:42 +00:00
parent 55f7db1bd9
commit fe80954c2d
4 changed files with 77 additions and 41 deletions

View File

@ -21,13 +21,15 @@ EthernetSettings::~EthernetSettings()
{ {
} }
void EthernetSettings::ReadyToRun() void
EthernetSettings::ReadyToRun()
{ {
fEthWindow = new EthernetSettingsWindow(); fEthWindow = new EthernetSettingsWindow();
fEthWindow->Show(); fEthWindow->Show();
} }
int main(int argc, char** argv) int
main(int argc, char** argv)
{ {
EthernetSettings app; EthernetSettings app;
app.Run(); app.Run();

View File

@ -50,7 +50,8 @@
bool EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name) bool
EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* name)
{ {
//This function is used for talking direct to the stack. //This function is used for talking direct to the stack.
//It´s used by _ShowConfiguration. //It´s used by _ShowConfiguration.
@ -62,7 +63,9 @@ bool EthernetSettingsView::_PrepareRequest(struct ifreq& request, const char* na
return true; return true;
} }
void EthernetSettingsView::_GatherInterfaces() { void
EthernetSettingsView::_GatherInterfaces()
{
// iterate over all interfaces and retrieve minimal status // iterate over all interfaces and retrieve minimal status
ifconf config; ifconf config;
@ -83,12 +86,13 @@ void EthernetSettingsView::_GatherInterfaces() {
if (ioctl(fSocket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0) if (ioctl(fSocket, SIOCGIFCONF, &config, sizeof(struct ifconf)) < 0)
return; return;
ifreq *interface = (ifreq *)buffer; ifreq* interface = (ifreq *)buffer;
fInterfaces.MakeEmpty(); fInterfaces.MakeEmpty();
for (uint32 i = 0; i < count; i++) { 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)); fInterfaces.AddItem(new BString(interface->ifr_name));
} }
@ -102,20 +106,23 @@ void EthernetSettingsView::_GatherInterfaces() {
} }
void EthernetSettingsView::AttachedToWindow() void
EthernetSettingsView::AttachedToWindow()
{ {
fOKButton->SetTarget(this); fOKButton->SetTarget(this);
fApplyButton->SetTarget(this); fApplyButton->SetTarget(this);
fDeviceMenuField->Menu()->SetTargetForItems(this);
} }
void EthernetSettingsView::DetachedFromWindow() void
EthernetSettingsView::DetachedFromWindow()
{ {
close(fSocket);
} }
EthernetSettingsView::EthernetSettingsView(BRect rect) : BView(rect, "EthernetSettingsView", B_FOLLOW_ALL, B_WILL_DRAW) EthernetSettingsView::EthernetSettingsView(BRect rect)
: BView(rect, "EthernetSettingsView", B_FOLLOW_ALL, B_WILL_DRAW)
{ {
float defaultWidth = 190; float defaultWidth = 190;
float inset = ceilf(be_plain_font->Size() * 0.8); float inset = ceilf(be_plain_font->Size() * 0.8);
@ -135,7 +142,7 @@ EthernetSettingsView::EthernetSettingsView(BRect rect) : BView(rect, "EthernetSe
info->AddString("interface", name.String()); info->AddString("interface", name.String());
BMenuItem* item = new BMenuItem(label.String(), info); BMenuItem* item = new BMenuItem(label.String(), info);
devmenu->AddItem(item); devmenu->AddItem(item);
item->SetTarget(this);
} }
@ -146,50 +153,68 @@ EthernetSettingsView::EthernetSettingsView(BRect rect) : BView(rect, "EthernetSe
modemenu->AddItem(modeitem); modemenu->AddItem(modeitem);
fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu); fDeviceMenuField = new BMenuField(frame, "networkcards", "Adapter:", devmenu);
fDeviceMenuField->SetDivider(fDeviceMenuField->StringWidth(fDeviceMenuField->Label()) + 8); fDeviceMenuField->SetDivider(
fDeviceMenuField->StringWidth(fDeviceMenuField->Label()) + 8);
AddChild(fDeviceMenuField); AddChild(fDeviceMenuField);
fDeviceMenuField->ResizeToPreferred(); fDeviceMenuField->ResizeToPreferred();
fTypeMenuField = new BMenuField(frame, "type", "Mode:", modemenu); fTypeMenuField = new BMenuField(frame, "type", "Mode:", modemenu);
fTypeMenuField->SetDivider(fTypeMenuField->StringWidth(fTypeMenuField->Label()) + 8); fTypeMenuField->SetDivider(
fTypeMenuField->StringWidth(fTypeMenuField->Label()) + 8);
fTypeMenuField->MoveTo(fDeviceMenuField->Frame().LeftBottom() + BPoint(0,10)); fTypeMenuField->MoveTo(fDeviceMenuField->Frame().LeftBottom() + BPoint(0,10));
AddChild(fTypeMenuField); AddChild(fTypeMenuField);
fTypeMenuField->ResizeToPreferred(); fTypeMenuField->ResizeToPreferred();
fIPTextControl = new BTextControl(frame, "ip", "IP Address:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW ); fIPTextControl = new BTextControl(frame, "ip", "IP Address:", "", NULL,
fIPTextControl->SetDivider(fIPTextControl->StringWidth(fIPTextControl->Label()) + 8); B_FOLLOW_TOP, B_WILL_DRAW );
fIPTextControl->SetDivider(
fIPTextControl->StringWidth(fIPTextControl->Label()) + 8);
fIPTextControl->MoveTo(fTypeMenuField->Frame().LeftBottom() + BPoint(0,10)); fIPTextControl->MoveTo(fTypeMenuField->Frame().LeftBottom() + BPoint(0,10));
fIPTextControl->ResizeToPreferred(); fIPTextControl->ResizeToPreferred();
AddChild(fIPTextControl); AddChild(fIPTextControl);
fNetMaskTextControl = new BTextControl(frame, "mask", "Netmask:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW ); fNetMaskTextControl = new BTextControl(frame, "mask", "Netmask:", "", NULL,
fNetMaskTextControl->SetDivider(fNetMaskTextControl->StringWidth(fNetMaskTextControl->Label()) + 8); B_FOLLOW_TOP, B_WILL_DRAW );
fNetMaskTextControl->MoveTo(fIPTextControl->Frame().LeftBottom() + BPoint(0,10)); fNetMaskTextControl->SetDivider(
fNetMaskTextControl->StringWidth(fNetMaskTextControl->Label()) + 8);
fNetMaskTextControl->MoveTo(
fIPTextControl->Frame().LeftBottom() + BPoint(0,10));
AddChild(fNetMaskTextControl); AddChild(fNetMaskTextControl);
fNetMaskTextControl->ResizeToPreferred(); fNetMaskTextControl->ResizeToPreferred();
fGatewayTextControl = new BTextControl(frame, "gateway", "Gateway:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW ); fGatewayTextControl = new BTextControl(frame, "gateway", "Gateway:", "",
fGatewayTextControl->SetDivider(fGatewayTextControl->StringWidth(fGatewayTextControl->Label()) + 8); NULL, B_FOLLOW_TOP, B_WILL_DRAW );
fGatewayTextControl->MoveTo(fNetMaskTextControl->Frame().LeftBottom() + BPoint(0,10)); fGatewayTextControl->SetDivider(
fGatewayTextControl->StringWidth(fGatewayTextControl->Label()) + 8);
fGatewayTextControl->MoveTo(
fNetMaskTextControl->Frame().LeftBottom() + BPoint(0,10));
AddChild(fGatewayTextControl); AddChild(fGatewayTextControl);
fGatewayTextControl->ResizeToPreferred(); fGatewayTextControl->ResizeToPreferred();
fPrimaryDNSTextControl = new BTextControl(frame, "dns1", "DNS #1:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW ); fPrimaryDNSTextControl = new BTextControl(
fPrimaryDNSTextControl->SetDivider(fPrimaryDNSTextControl->StringWidth(fPrimaryDNSTextControl->Label()) + 8); frame, "dns1", "DNS #1:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW );
fPrimaryDNSTextControl->MoveTo(fGatewayTextControl->Frame().LeftBottom() + BPoint(0,10)); fPrimaryDNSTextControl->SetDivider(
fPrimaryDNSTextControl->StringWidth(fPrimaryDNSTextControl->Label()) + 8);
fPrimaryDNSTextControl->MoveTo(
fGatewayTextControl->Frame().LeftBottom() + BPoint(0,10));
AddChild(fPrimaryDNSTextControl); AddChild(fPrimaryDNSTextControl);
fPrimaryDNSTextControl->ResizeToPreferred(); fPrimaryDNSTextControl->ResizeToPreferred();
fSecondaryDNSTextControl = new BTextControl(frame, "dns2", "DNS #2:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW ); fSecondaryDNSTextControl = new BTextControl(
fSecondaryDNSTextControl->SetDivider(fSecondaryDNSTextControl->StringWidth(fSecondaryDNSTextControl->Label()) + 8); frame, "dns2", "DNS #2:", "", NULL, B_FOLLOW_TOP, B_WILL_DRAW );
fSecondaryDNSTextControl->MoveTo(fPrimaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10)); fSecondaryDNSTextControl->SetDivider(
fSecondaryDNSTextControl->StringWidth(
fSecondaryDNSTextControl->Label()) + 8);
fSecondaryDNSTextControl->MoveTo(
fPrimaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10));
AddChild(fSecondaryDNSTextControl); AddChild(fSecondaryDNSTextControl);
fSecondaryDNSTextControl->ResizeToPreferred(); fSecondaryDNSTextControl->ResizeToPreferred();
fApplyButton = new BButton(frame, "apply", "Apply", new BMessage(kMsgApply)); fApplyButton = new BButton(frame, "apply", "Apply", new BMessage(kMsgApply));
fApplyButton->ResizeToPreferred(); fApplyButton->ResizeToPreferred();
fApplyButton->MoveTo(fSecondaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10)); fApplyButton->MoveTo(
fSecondaryDNSTextControl->Frame().LeftBottom() + BPoint(0,10));
AddChild(fApplyButton); AddChild(fApplyButton);
fOKButton = new BButton(frame, "ok", "OK", new BMessage(kMsgOK)); fOKButton = new BButton(frame, "ok", "OK", new BMessage(kMsgOK));
@ -201,12 +226,14 @@ EthernetSettingsView::EthernetSettingsView(BRect rect) : BView(rect, "EthernetSe
EthernetSettingsView::~EthernetSettingsView() EthernetSettingsView::~EthernetSettingsView()
{ {
close(fSocket);
} }
void EthernetSettingsView::_ShowConfiguration(BMessage* message) void
EthernetSettingsView::_ShowConfiguration(BMessage* message)
{ {
const char *name; const char* name;
if (message->FindString("interface", &name) != B_OK) if (message->FindString("interface", &name) != B_OK)
return; return;
@ -248,7 +275,7 @@ void EthernetSettingsView::_ShowConfiguration(BMessage* message)
// Obtain gateway // Obtain gateway
char *gwAddress; char* gwAddress;
ifconf config; ifconf config;
config.ifc_len = sizeof(config.ifc_value); config.ifc_len = sizeof(config.ifc_value);
if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0) if (ioctl(fSocket, SIOCGRTSIZE, &config, sizeof(struct ifconf)) < 0)
@ -265,8 +292,10 @@ void EthernetSettingsView::_ShowConfiguration(BMessage* message)
config.ifc_len = size; config.ifc_len = size;
config.ifc_buf = buffer; config.ifc_buf = buffer;
if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) if (ioctl(fSocket, SIOCGRTTABLE, &config, sizeof(struct ifconf)) < 0) {
free(buffer);
return; return;
}
ifreq *interface = (ifreq *)buffer; ifreq *interface = (ifreq *)buffer;
ifreq *end = (ifreq *)((uint8 *)buffer + size); ifreq *end = (ifreq *)((uint8 *)buffer + size);
@ -292,11 +321,12 @@ void EthernetSettingsView::_ShowConfiguration(BMessage* message)
if (route.gateway != NULL) if (route.gateway != NULL)
addressSize += route.gateway->sa_len; addressSize += route.gateway->sa_len;
interface = (ifreq *)((addr_t)interface + IF_NAMESIZE + sizeof(route_entry) + addressSize); interface = (ifreq *)((addr_t)interface +
IF_NAMESIZE + sizeof(route_entry) + addressSize);
} }
free(buffer); free(buffer);
// Obtain DNS // Obtain DNS
@ -327,7 +357,8 @@ void EthernetSettingsView::_ShowConfiguration(BMessage* message)
} }
void EthernetSettingsView::MessageReceived(BMessage* message) void
EthernetSettingsView::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case kMsgInfo: case kMsgInfo:

View File

@ -15,19 +15,21 @@
#include "EthernetSettingsView.h" #include "EthernetSettingsView.h"
EthernetSettingsWindow::EthernetSettingsWindow() : BWindow(BRect(50, 50, 269, 302), EthernetSettingsWindow::EthernetSettingsWindow()
"Ethernet Settings", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE) : BWindow(BRect(50, 50, 269, 302), "Ethernet Settings",
B_TITLED_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS
| B_NOT_ZOOMABLE)
{ {
fEthView = new EthernetSettingsView(Bounds()); fEthView = new EthernetSettingsView(Bounds());
AddChild(fEthView); AddChild(fEthView);
Show();
} }
void EthernetSettingsWindow::MessageReceived(BMessage* message) void
EthernetSettingsWindow::MessageReceived(BMessage* message)
{ {
switch (message->what) { switch (message->what) {
case kMsgInfo: case kMsgInfo:
@ -44,7 +46,8 @@ EthernetSettingsWindow::~EthernetSettingsWindow()
} }
bool EthernetSettingsWindow::QuitRequested() bool
EthernetSettingsWindow::QuitRequested()
{ {
be_app->PostMessage(B_QUIT_REQUESTED); be_app->PostMessage(B_QUIT_REQUESTED);
return true; return true;

View File

@ -19,7 +19,7 @@ class EthernetSettingsWindow : public BWindow {
EthernetSettingsWindow(); EthernetSettingsWindow();
virtual ~EthernetSettingsWindow(); virtual ~EthernetSettingsWindow();
virtual bool QuitRequested(); virtual bool QuitRequested();
virtual void EthernetSettingsWindow::MessageReceived(BMessage* mesage); virtual void MessageReceived(BMessage* mesage);
private: private:
EthernetSettingsView *fEthView; EthernetSettingsView *fEthView;