Network: style cleanup.

This commit is contained in:
Axel Dörfler
2015-01-28 17:05:06 +01:00
parent 2d9c195093
commit be883bad52
7 changed files with 197 additions and 207 deletions
+27 -27
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2004-2007 Haiku Inc. All rights reserved. * Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
*/ */
@@ -13,41 +13,41 @@
#include "NetworkSetupWindow.h" #include "NetworkSetupWindow.h"
#define SOFTWARE_EDITOR "Haiku" static const char* kSignature = "application/x-vnd.Haiku-Network";
#define NAME "Network"
#define SOFTWARE_VERSION_LABEL "1.0.0 alpha"
#define APPLICATION_SIGNATURE "application/x-vnd." SOFTWARE_EDITOR "-" NAME
class Application : public BApplication class Application : public BApplication {
{ public:
public: Application();
Application();
public: public:
void ReadyToRun(void); virtual void ReadyToRun();
}; };
int main() Application::Application()
:
BApplication(kSignature)
{
}
void
Application::ReadyToRun()
{
NetworkSetupWindow* window = new NetworkSetupWindow();
window->Show();
}
// #pragma mark -
int
main()
{ {
Application* app = new Application(); Application* app = new Application();
app->Run(); app->Run();
delete app; delete app;
return 0; return 0;
} }
Application::Application()
: BApplication(APPLICATION_SIGNATURE)
{
}
void
Application::ReadyToRun(void)
{
NetworkSetupWindow* window = new NetworkSetupWindow(NAME);
window->Show();
}
+14 -18
View File
@@ -1,15 +1,11 @@
/* /*
* Copyright 2004-2011 Haiku Inc. All rights reserved. * Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
*/ */
#include "NetworkSetupAddOn.h" #include "NetworkSetupAddOn.h"
#include <kernel/image.h>
#include <storage/Resources.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -18,15 +14,15 @@ NetworkSetupAddOn::NetworkSetupAddOn(image_id image)
: :
fIsDirty(false), fIsDirty(false),
fProfile(NULL), fProfile(NULL),
fAddonImage(image), fImage(image),
fAddonResources(NULL) fResources(NULL)
{ {
} }
NetworkSetupAddOn::~NetworkSetupAddOn() NetworkSetupAddOn::~NetworkSetupAddOn()
{ {
delete fAddonResources; delete fResources;
} }
@@ -45,9 +41,9 @@ NetworkSetupAddOn::Revert()
status_t status_t
NetworkSetupAddOn::ProfileChanged(NetworkSetupProfile* new_profile) NetworkSetupAddOn::ProfileChanged(NetworkSetupProfile* newProfile)
{ {
fProfile = new_profile; fProfile = newProfile;
return B_OK; return B_OK;
} }
@@ -76,7 +72,7 @@ NetworkSetupAddOn::Profile()
image_id image_id
NetworkSetupAddOn::ImageId() NetworkSetupAddOn::ImageId()
{ {
return fAddonImage; return fImage;
} }
@@ -90,17 +86,17 @@ NetworkSetupAddOn::Name()
BResources* BResources*
NetworkSetupAddOn::Resources() NetworkSetupAddOn::Resources()
{ {
if (!fAddonResources) { if (fResources == NULL) {
image_info info; image_info info;
if (get_image_info(fAddonImage, &info) != B_OK) if (get_image_info(fImage, &info) != B_OK)
return NULL; return NULL;
BResources *resources = new BResources(); BResources* resources = new BResources();
BFile addon_file(info.name, O_RDONLY); BFile file(info.name, B_READ_ONLY);
if (resources->SetTo(&addon_file) == B_OK) if (resources->SetTo(&file) == B_OK)
fAddonResources = resources; fResources = resources;
else else
delete resources; delete resources;
} }
return fAddonResources; return fResources;
} }
+28 -27
View File
@@ -1,45 +1,47 @@
/* /*
* Copyright 2004-2011 Haiku Inc. All rights reserved. * Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
*/ */
#ifndef NETWORKSETUPADDON_H #ifndef NETWORK_SETUP_ADD_ON_H
#define NETWORKSETUPADDON_H #define NETWORK_SETUP_ADD_ON_H
#include <interface/View.h> #include <image.h>
#include <kernel/image.h> // for image_id #include <Resources.h>
#include <storage/Resources.h> #include <View.h>
class NetworkSetupProfile; class NetworkSetupProfile;
class NetworkSetupAddOn { class NetworkSetupAddOn {
public: public:
NetworkSetupAddOn(image_id addon_image); NetworkSetupAddOn(image_id image);
virtual ~NetworkSetupAddOn(); virtual ~NetworkSetupAddOn();
virtual BView * CreateView() = 0; virtual BView* CreateView() = 0;
virtual status_t Save(); virtual status_t Save();
virtual status_t Revert(); virtual status_t Revert();
virtual const char * Name(); virtual const char* Name();
virtual status_t ProfileChanged(NetworkSetupProfile* virtual status_t ProfileChanged(NetworkSetupProfile* newProfile);
new_profile);
NetworkSetupProfile * Profile(); NetworkSetupProfile*
bool IsDirty(); Profile();
void SetDirty(bool dirty = true); bool IsDirty();
image_id ImageId(); void SetDirty(bool dirty = true);
BResources* Resources(); image_id ImageId();
BResources* Resources();
private: private:
bool fIsDirty; bool fIsDirty;
NetworkSetupProfile* fProfile; NetworkSetupProfile*
image_id fAddonImage; fProfile;
BResources* fAddonResources; image_id fImage;
BResources* fResources;
}; };
extern "C" { extern "C" {
#define NETWORK_SETUP_ADDON_INSTANCIATE_FUNC_NAME "get_nth_addon" #define NETWORK_SETUP_ADDON_INSTANCIATE_FUNC_NAME "get_nth_addon"
@@ -51,5 +53,4 @@ extern NetworkSetupAddOn* get_nth_addon(image_id image, int index);
} }
#endif // ifdef NETWORKSETUPADDON_H #endif // NETWORKSETUPADDON_H
@@ -1,13 +1,13 @@
/* /*
* Copyright 2004-2011 Haiku Inc. All rights reserved. * Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
*/ */
#include <stdlib.h>
#include "NetworkSetupProfile.h" #include "NetworkSetupProfile.h"
#include <stdlib.h>
NetworkSetupProfile::NetworkSetupProfile() NetworkSetupProfile::NetworkSetupProfile()
: :
@@ -77,7 +77,7 @@ NetworkSetupProfile::SetTo(const entry_ref* ref)
status_t status_t
NetworkSetupProfile::SetTo(BEntry *entry) NetworkSetupProfile::SetTo(BEntry* entry)
{ {
delete fRoot; delete fRoot;
delete fPath; delete fPath;
@@ -91,7 +91,7 @@ NetworkSetupProfile::SetTo(BEntry *entry)
const char* const char*
NetworkSetupProfile::Name() NetworkSetupProfile::Name()
{ {
if (!fName) { if (fName == NULL) {
fRoot->GetPath(fPath); fRoot->GetPath(fPath);
fName = fPath->Leaf(); fName = fPath->Leaf();
} }
@@ -100,7 +100,7 @@ NetworkSetupProfile::Name()
} }
status_t status_t
NetworkSetupProfile::SetName(const char* name) NetworkSetupProfile::SetName(const char* name)
{ {
return B_OK; return B_OK;
@@ -143,6 +143,8 @@ NetworkSetupProfile::MakeCurrent()
// #pragma mark - // #pragma mark -
NetworkSetupProfile* NetworkSetupProfile*
NetworkSetupProfile::Default() NetworkSetupProfile::Default()
{ {
+36 -33
View File
@@ -1,49 +1,52 @@
/* /*
* Copyright 2004-2011 Haiku Inc. All rights reserved. * Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
*
*/ */
#ifndef NETWORK_SETUP_PROFILE_H
#define NETWORK_SETUP_PROFILE_H
#ifndef NETWORKSETUPPROFILE_H
#define NETWORKSETUPPROFILE_H
#include <StorageKit.h> #include <StorageKit.h>
class NetworkSetupProfile { class NetworkSetupProfile {
public: public:
NetworkSetupProfile(); NetworkSetupProfile();
NetworkSetupProfile(const char* path); NetworkSetupProfile(const char* path);
NetworkSetupProfile(const entry_ref* ref); NetworkSetupProfile(const entry_ref* ref);
NetworkSetupProfile(BEntry *entry); NetworkSetupProfile(BEntry* entry);
virtual ~NetworkSetupProfile(); virtual ~NetworkSetupProfile();
status_t SetTo(const char* path);
status_t SetTo(const entry_ref *ref);
status_t SetTo(BEntry *entry);
bool Exists();
const char* Name();
status_t SetName(const char *name);
bool IsDefault();
bool IsCurrent();
status_t MakeCurrent(); status_t SetTo(const char* path);
status_t Delete(); status_t SetTo(const entry_ref* ref);
status_t SetTo(BEntry* entry);
static NetworkSetupProfile* Default(); bool Exists();
static NetworkSetupProfile* Current();
const char* Name();
status_t SetName(const char* name);
bool IsDefault();
bool IsCurrent();
status_t MakeCurrent();
status_t Delete();
static NetworkSetupProfile*
Default();
static NetworkSetupProfile*
Current();
private: private:
BEntry* fRoot; BEntry* fRoot;
BPath* fPath; BPath* fPath;
bool fIsDefault; bool fIsDefault;
bool fIsCurrent; bool fIsCurrent;
const char* fName; const char* fName;
static BDirectory* fProfilesRoot; static BDirectory* fProfilesRoot;
}; };
#endif // ifdef NETWORKSETUPPROFILE_H
#endif // NETWORK_SETUP_PROFILE_H
+63 -74
View File
@@ -32,12 +32,11 @@
#define B_TRANSLATION_CONTEXT "NetworkSetupWindow" #define B_TRANSLATION_CONTEXT "NetworkSetupWindow"
// -------------------------------------------------------------- NetworkSetupWindow::NetworkSetupWindow()
NetworkSetupWindow::NetworkSetupWindow(const char *title)
: :
BWindow(BRect(100, 100, 300, 300), title, B_TITLED_WINDOW, BWindow(BRect(100, 100, 300, 300), B_TRANSLATE("Network"), B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS), B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fAddonCount(0) fAddOnCount(0)
{ {
// ---- Profiles section // ---- Profiles section
#if 0 #if 0
@@ -94,7 +93,7 @@ NetworkSetupWindow::NetworkSetupWindow(const char *title)
_BuildShowTabView(); _BuildShowTabView();
fAddonView = NULL; fAddOnView = NULL;
CenterOnScreen(); CenterOnScreen();
} }
@@ -120,43 +119,39 @@ NetworkSetupWindow::MessageReceived(BMessage* message)
case kMsgProfileNew: case kMsgProfileNew:
break; break;
case kMsgProfileSelected: { case kMsgProfileSelected:
BPath name; {
const char *path; const char* path;
bool is_default;
bool is_current;
if (message->FindString("path", &path) != B_OK) if (message->FindString("path", &path) != B_OK)
break; break;
name.SetTo(path); BPath name(path);
bool isCurrent = strcmp(name.Leaf(), "current") == 0;
is_default = (strcmp(name.Leaf(), "default") == 0); fApplyButton->SetEnabled(!isCurrent);
is_current = (strcmp(name.Leaf(), "current") == 0);
fApplyButton->SetEnabled(!is_current);
break; break;
} }
case kMsgRevert: { case kMsgRevert:
for (int addonIndex = 0; addonIndex < fAddonCount; addonIndex++) { {
NetworkSetupAddOn* addon for (int index = 0; index < fAddOnCount; index++) {
= fNetworkAddOnMap[addonIndex]; NetworkSetupAddOn* addOn = fNetworkAddOnMap[index];
addon->Revert(); addOn->Revert();
} }
break; break;
} }
case kMsgApply: { case kMsgApply:
for (int addonIndex = 0; addonIndex < fAddonCount; addonIndex++) { {
NetworkSetupAddOn* addon for (int index = 0; index < fAddOnCount; index++) {
= fNetworkAddOnMap[addonIndex]; NetworkSetupAddOn* addOn = fNetworkAddOnMap[index];
addon->Save(); addOn->Save();
} }
break; break;
} }
case kMsgToggleReplicant: { case kMsgToggleReplicant:
{
_ShowReplicant(message->GetInt32("be:value", B_CONTROL_OFF) _ShowReplicant(message->GetInt32("be:value", B_CONTROL_OFF)
== B_CONTROL_ON); == B_CONTROL_ON);
break; break;
@@ -169,18 +164,15 @@ NetworkSetupWindow::MessageReceived(BMessage* message)
void void
NetworkSetupWindow::_BuildProfilesMenu(BMenu* menu, int32 msg_what) NetworkSetupWindow::_BuildProfilesMenu(BMenu* menu, int32 what)
{ {
BMenuItem* item; char currentProfile[256] = { 0 };
char current_profile[256] = { 0 };
menu->SetRadioMode(true); menu->SetRadioMode(true);
BDirectory dir("/boot/system/settings/network/profiles"); BDirectory dir("/boot/system/settings/network/profiles");
if (dir.InitCheck() == B_OK) { if (dir.InitCheck() == B_OK) {
BEntry entry; BEntry entry;
BMessage* msg;
dir.Rewind(); dir.Rewind();
while (dir.GetNextEntry(&entry) >= 0) { while (dir.GetNextEntry(&entry) >= 0) {
@@ -195,17 +187,17 @@ NetworkSetupWindow::_BuildProfilesMenu(BMenu* menu, int32 msg_what)
// oh oh, sorry, wrong symlink... // oh oh, sorry, wrong symlink...
continue; continue;
symlink.ReadLink(current_profile, sizeof(current_profile)); symlink.ReadLink(currentProfile, sizeof(currentProfile));
continue; continue;
}; };
if (!entry.IsDirectory()) if (!entry.IsDirectory())
continue; continue;
msg = new BMessage(msg_what); BMessage* message = new BMessage(what);
msg->AddString("path", name.Path()); message->AddString("path", name.Path());
item = new BMenuItem(name.Leaf(), msg); BMenuItem* item = new BMenuItem(name.Leaf(), message);
menu->AddItem(item); menu->AddItem(item);
} }
} }
@@ -216,11 +208,11 @@ NetworkSetupWindow::_BuildProfilesMenu(BMenu* menu, int32 msg_what)
menu->AddItem(new BMenuItem(B_TRANSLATE("Manage" B_UTF8_ELLIPSIS), menu->AddItem(new BMenuItem(B_TRANSLATE("Manage" B_UTF8_ELLIPSIS),
new BMessage(kMsgProfileManage))); new BMessage(kMsgProfileManage)));
if (strlen(current_profile)) { if (currentProfile[0] != '\0') {
item = menu->FindItem(current_profile); BMenuItem* item = menu->FindItem(currentProfile);
if (item) { if (item != NULL) {
BString label; // TODO: translate
label << item->Label(); BString label(item->Label());
label << " (current)"; label << " (current)";
item->SetLabel(label.String()); item->SetLabel(label.String());
item->SetMarked(true); item->SetMarked(true);
@@ -233,32 +225,32 @@ void
NetworkSetupWindow::_BuildShowTabView() NetworkSetupWindow::_BuildShowTabView()
{ {
BPath path; BPath path;
BPath addon_path; BPath addOnPath;
BDirectory dir; BDirectory dir;
BEntry entry; BEntry entry;
char* search_paths = getenv("ADDON_PATH"); char* searchPaths = getenv("ADDON_PATH");
if (!search_paths) if (!searchPaths)
return; return;
search_paths = strdup(search_paths); searchPaths = strdup(searchPaths);
char* next_path_token; char* nextPathToken;
char* search_path = strtok_r(search_paths, ":", &next_path_token); char* searchPath = strtok_r(searchPaths, ":", &nextPathToken);
while (search_path) { while (searchPath) {
if (strncmp(search_path, "%A/", 3) == 0) { if (strncmp(searchPath, "%A/", 3) == 0) {
app_info ai; app_info ai;
be_app->GetAppInfo(&ai); be_app->GetAppInfo(&ai);
entry.SetTo(&ai.ref); entry.SetTo(&ai.ref);
entry.GetPath(&path); entry.GetPath(&path);
path.GetParent(&path); path.GetParent(&path);
path.Append(search_path + 3); path.Append(searchPath + 3);
} else { } else {
path.SetTo(search_path); path.SetTo(searchPath);
path.Append("Network Setup"); path.Append("Network Setup");
} }
search_path = strtok_r(NULL, ":", &next_path_token); searchPath = strtok_r(NULL, ":", &nextPathToken);
dir.SetTo(path.Path()); dir.SetTo(path.Path());
if (dir.InitCheck() != B_OK) if (dir.InitCheck() != B_OK)
@@ -269,16 +261,16 @@ NetworkSetupWindow::_BuildShowTabView()
if (entry.IsDirectory()) if (entry.IsDirectory())
continue; continue;
entry.GetPath(&addon_path); entry.GetPath(&addOnPath);
image_id addon_id = load_add_on(addon_path.Path()); image_id image = load_add_on(addOnPath.Path());
if (addon_id < 0) { if (image < 0) {
printf("Failed to load %s addon: %s.\n", addon_path.Path(), printf("Failed to load %s addon: %s.\n", addOnPath.Path(),
strerror(addon_id)); strerror(image));
continue; continue;
} }
network_setup_addon_instantiate get_nth_addon; network_setup_addon_instantiate get_nth_addon;
status_t status = get_image_symbol(addon_id, "get_nth_addon", status_t status = get_image_symbol(image, "get_nth_addon",
B_SYMBOL_TYPE_TEXT, (void **) &get_nth_addon); B_SYMBOL_TYPE_TEXT, (void **) &get_nth_addon);
int tabCount = 0; int tabCount = 0;
@@ -286,22 +278,21 @@ NetworkSetupWindow::_BuildShowTabView()
if (status != B_OK) { if (status != B_OK) {
// No "addon instantiate function" symbol found in this addon // No "addon instantiate function" symbol found in this addon
printf("No symbol \"get_nth_addon\" found in %s addon: not a " printf("No symbol \"get_nth_addon\" found in %s addon: not a "
"network setup addon!\n", addon_path.Path()); "network setup addon!\n", addOnPath.Path());
unload_add_on(addon_id); unload_add_on(image);
continue; continue;
} }
while ((fNetworkAddOnMap[fAddonCount] while ((fNetworkAddOnMap[fAddOnCount]
= get_nth_addon(addon_id, tabCount)) != NULL) { = get_nth_addon(image, tabCount)) != NULL) {
printf("Adding Tab: %d\n", fAddonCount); printf("Adding Tab: %d\n", fAddOnCount);
BView* addon_view BView* view = fNetworkAddOnMap[fAddOnCount]->CreateView();
= fNetworkAddOnMap[fAddonCount]->CreateView();
// FIXME rework this: we don't want to use a tab view here, // FIXME rework this: we don't want to use a tab view here,
// instead add-ons should populate the "interfaces" list with // instead add-ons should populate the "interfaces" list with
// interfaces, services, etc. // interfaces, services, etc.
fPanel->AddTab(addon_view); fPanel->AddTab(view);
fAddonCount++; fAddOnCount++;
// Number of tab addons total // Number of tab addons total
tabCount++; tabCount++;
// Tabs for *this* addon // Tabs for *this* addon
@@ -309,7 +300,7 @@ NetworkSetupWindow::_BuildShowTabView()
} }
} }
free(search_paths); free(searchPaths);
} }
@@ -317,16 +308,14 @@ void
NetworkSetupWindow::_ShowReplicant(bool show) NetworkSetupWindow::_ShowReplicant(bool show)
{ {
if (show) { if (show) {
char* argv[] = {const_cast<char *>("--deskbar"), NULL}; const char* argv[] = {"--deskbar", NULL};
status_t ret = be_roster->Launch( status_t status = be_roster->Launch(be_app->Signature(), 1, argv);
"application/x-vnd.Haiku-NetworkStatus", 1, argv); if (status != B_OK) {
if (ret != B_OK) {
BString errorMessage; BString errorMessage;
errorMessage.SetToFormat( errorMessage.SetToFormat(
B_TRANSLATE("Installing NetworkStatus in Deskbar failed: %s"), B_TRANSLATE("Installing NetworkStatus in Deskbar failed: %s"),
strerror(ret)); strerror(status));
BAlert* alert = new BAlert(B_TRANSLATE("launch error"), BAlert* alert = new BAlert(B_TRANSLATE("launch error"),
errorMessage, B_TRANSLATE("Ok")); errorMessage, B_TRANSLATE("Ok"));
alert->Go(NULL); alert->Go(NULL);
+21 -22
View File
@@ -1,38 +1,31 @@
/* /*
* Copyright 2004-2011 Haiku Inc. All rights reserved. * Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
* Alexander von Gluck, <[email protected]> * Alexander von Gluck, <[email protected]>
*/ */
#ifndef NETWORKSETUPWINDOW_H #ifndef NETWORK_SETUP_WINDOW_H
#define NETWORKSETUPWINDOW_H #define NETWORK_SETUP_WINDOW_H
#include "NetworkSetupAddOn.h" #include "NetworkSetupAddOn.h"
#include <map> #include <map>
#include <Window.h>
typedef std::map<int, NetworkSetupAddOn*> NetworkAddOnMap; typedef std::map<int, NetworkSetupAddOn*> NetworkAddOnMap;
class NetworkSetupWindow; class NetworkSetupWindow;
#include <Window.h>
class BTabView; class BTabView;
class BButton; class BButton;
class BMenu; class BMenu;
class NetworkSetupWindow : public BWindow
{
public:
NetworkSetupWindow(const char *title);
~NetworkSetupWindow();
typedef BWindow inherited;
class NetworkSetupWindow : public BWindow {
public:
static const uint32 kMsgProfileSelected = 'prof'; static const uint32 kMsgProfileSelected = 'prof';
static const uint32 kMsgProfileManage = 'mngp'; static const uint32 kMsgProfileManage = 'mngp';
static const uint32 kMsgProfileNew = 'newp'; static const uint32 kMsgProfileNew = 'newp';
@@ -40,26 +33,32 @@ class NetworkSetupWindow : public BWindow
static const uint32 kMsgRevert = 'rvrt'; static const uint32 kMsgRevert = 'rvrt';
static const uint32 kMsgToggleReplicant = 'trep'; static const uint32 kMsgToggleReplicant = 'trep';
bool QuitRequested(); public:
void MessageReceived(BMessage* msg); NetworkSetupWindow();
virtual ~NetworkSetupWindow();
private: bool QuitRequested();
void _BuildProfilesMenu(BMenu* menu, int32 msg); void MessageReceived(BMessage* message);
private:
typedef BWindow inherited;
void _BuildProfilesMenu(BMenu* menu, int32 what);
void _BuildShowTabView(); void _BuildShowTabView();
bool _IsReplicantInstalled(); bool _IsReplicantInstalled();
void _ShowReplicant(bool show); void _ShowReplicant(bool show);
private:
BButton* fRevertButton; BButton* fRevertButton;
BButton* fApplyButton; BButton* fApplyButton;
NetworkAddOnMap fNetworkAddOnMap; NetworkAddOnMap fNetworkAddOnMap;
BTabView* fPanel; BTabView* fPanel;
BView* fAddonView; BView* fAddOnView;
int fAddonCount; int fAddOnCount;
}; };
#endif // ifdef NETWORKSETUPWINDOW_H #endif // NETWORK_SETUP_WINDOW_H