Network: WIP of redesign.

* Fletched out new add-on API.
* Moved InterfaceListItem from the interfaces add-on into the
  application.
* Renamed NetworkSetup* to Network* respectively NetworkSettings*.
This commit is contained in:
Axel Dörfler 2015-02-04 18:09:37 +01:00
parent 0690387cd2
commit a7cb9f5f55
17 changed files with 991 additions and 504 deletions

View File

@ -214,8 +214,6 @@ AddFilesToPackage add-ons mail_daemon outbound_filters : Fortune ;
AddFilesToPackage add-ons media : $(SYSTEM_ADD_ONS_MEDIA) ;
AddFilesToPackage add-ons media plugins : $(SYSTEM_ADD_ONS_MEDIA_PLUGINS) ;
AddFilesToPackage add-ons Network\ Setup : Interfaces Services ;
AddFilesToPackage add-ons Tracker
: FileType Mark\ as… Mark\ as\ Read Open\ Target\ Folder
Open\ Terminal ZipOMatic ;

View File

@ -0,0 +1,303 @@
/*
* Copyright 2004-2015 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Alexander von Gluck IV, kallisti5@unixzen.com
* Philippe Houdoin
* Fredrik Modéen
* John Scipione, jscipione@gmail.com
*/
#include "InterfaceListItem.h"
#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <net/if_dl.h>
#include <net/if_media.h>
#include <net/if_types.h>
#include <netinet/in.h>
#include <net_notifications.h>
#include <sys/socket.h>
#include <sys/sockio.h>
#include <Application.h>
#include <Bitmap.h>
#include <Catalog.h>
#include <File.h>
#include <IconUtils.h>
#include <MenuItem.h>
#include <NetworkDevice.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include <OutlineListView.h>
#include <Point.h>
#include <PopUpMenu.h>
#include <Resources.h>
#include <String.h>
#include <SeparatorItem.h>
#include <Window.h>
#include <AutoDeleter.h>
#define ICON_SIZE 37
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "InterfacesListView"
InterfaceListItem::InterfaceListItem(const char* name)
:
BListItem(0, false),
fIcon(NULL)
{
fInterface.SetTo(name);
_Init();
}
InterfaceListItem::~InterfaceListItem()
{
delete fIcon;
}
// #pragma mark - InterfaceListItem public methods
void
InterfaceListItem::DrawItem(BView* owner, BRect /*bounds*/, bool complete)
{
BOutlineListView* list = dynamic_cast<BOutlineListView*>(owner);
if (list == NULL)
return;
owner->PushState();
BRect bounds = list->ItemFrame(list->IndexOf(this));
//rgb_color highColor = list->HighColor();
rgb_color lowColor = list->LowColor();
if (IsSelected() || complete) {
if (IsSelected()) {
list->SetHighColor(ui_color(B_LIST_SELECTED_BACKGROUND_COLOR));
list->SetLowColor(list->HighColor());
} else
list->SetHighColor(lowColor);
list->FillRect(bounds);
}
BString interfaceState;
BBitmap* stateIcon(NULL);
// TODO: only update periodically
bool disabled = (fInterface.Flags() & IFF_UP) == 0;
if (disabled) {
interfaceState = "disabled";
stateIcon = fIconOffline;
} else if (!fInterface.HasLink()) {
interfaceState = "no link";
stateIcon = fIconOffline;
// TODO!
// } else if ((fSettings->IPAddr(AF_INET).IsEmpty()
// && fSettings->IPAddr(AF_INET6).IsEmpty())
// && (fSettings->AutoConfigure(AF_INET)
// || fSettings->AutoConfigure(AF_INET6))) {
// interfaceState = "connecting" B_UTF8_ELLIPSIS;
// stateIcon = fIconPending;
} else {
interfaceState = "connected";
stateIcon = fIconOnline;
}
// Set the initial bounds of item contents
BPoint iconPt = bounds.LeftTop();
BPoint namePt = bounds.LeftTop();
BPoint line2Pt = bounds.LeftTop();
BPoint line3Pt = bounds.LeftTop();
BPoint statePt = bounds.RightTop();
iconPt += BPoint(4, 4);
statePt += BPoint(0, fFirstlineOffset);
namePt += BPoint(ICON_SIZE + 12, fFirstlineOffset);
line2Pt += BPoint(ICON_SIZE + 12, fSecondlineOffset);
line3Pt += BPoint(ICON_SIZE + 12, fThirdlineOffset);
statePt -= BPoint(
be_plain_font->StringWidth(interfaceState.String()) + 4.0f, 0);
if (disabled) {
list->SetDrawingMode(B_OP_ALPHA);
list->SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
list->SetHighColor(0, 0, 0, 32);
} else
list->SetDrawingMode(B_OP_OVER);
list->DrawBitmapAsync(fIcon, iconPt);
list->DrawBitmapAsync(stateIcon, iconPt);
if (disabled) {
rgb_color textColor;
if (IsSelected())
textColor = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
else
textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
if (textColor.red + textColor.green + textColor.blue > 128 * 3)
list->SetHighColor(tint_color(textColor, B_DARKEN_1_TINT));
else
list->SetHighColor(tint_color(textColor, B_LIGHTEN_1_TINT));
} else {
if (IsSelected())
list->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
else
list->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
}
list->SetFont(be_bold_font);
BString name = Name();
name.RemoveFirst("/dev/net/");
list->DrawString(name, namePt);
list->SetFont(be_plain_font);
list->DrawString(interfaceState, statePt);
// TODO!
/* if (!disabled) {
// Render IPv4 Address
BString ipv4Str(B_TRANSLATE_COMMENT("IP:", "IPv4 address label"));
if (fSettings->IPAddr(AF_INET).IsEmpty())
ipv4Str << " " << B_TRANSLATE("None");
else
ipv4Str << " " << BString(fSettings->IP(AF_INET));
list->DrawString(ipv4Str, line2Pt);
}
// Render IPv6 Address (if present)
if (!disabled && !fSettings->IPAddr(AF_INET6).IsEmpty()) {
BString ipv6Str(B_TRANSLATE_COMMENT("IPv6:", "IPv6 address label"));
ipv6Str << " " << BString(fSettings->IP(AF_INET6));
list->DrawString(ipv6Str, line3Pt);
}
*/
owner->PopState();
}
void
InterfaceListItem::Update(BView* owner, const BFont* font)
{
BListItem::Update(owner, font);
font_height height;
font->GetHeight(&height);
float lineHeight = ceilf(height.ascent) + ceilf(height.descent)
+ ceilf(height.leading);
fFirstlineOffset = 2 + ceilf(height.ascent + height.leading / 2);
fSecondlineOffset = fFirstlineOffset + lineHeight;
fThirdlineOffset = fFirstlineOffset + (lineHeight * 2);
SetHeight(std::max(3 * lineHeight + 4, fIcon->Bounds().Height() + 8));
// either to the text height or icon height, whichever is taller
}
// #pragma mark - InterfaceListItem private methods
void
InterfaceListItem::_Init()
{
const char* mediaTypeName = NULL;
BNetworkDevice device(Name());
if (device.IsWireless())
mediaTypeName = "wifi";
else if (device.IsEthernet())
mediaTypeName = "ether";
_PopulateBitmaps(mediaTypeName);
// Load the interface icons
}
void
InterfaceListItem::_PopulateBitmaps(const char* mediaType)
{
const uint8* interfaceHVIF;
const uint8* offlineHVIF;
const uint8* pendingHVIF;
const uint8* onlineHVIF;
BBitmap* interfaceBitmap = NULL;
BResources* resources = BApplication::AppResources();
size_t iconSize;
// Try specific interface icon?
interfaceHVIF = (const uint8*)resources->LoadResource(
B_VECTOR_ICON_TYPE, Name(), &iconSize);
if (interfaceHVIF == NULL && mediaType != NULL)
// Not found, try interface media type?
interfaceHVIF = (const uint8*)resources->LoadResource(
B_VECTOR_ICON_TYPE, mediaType, &iconSize);
if (interfaceHVIF == NULL)
// Not found, try default interface icon?
interfaceHVIF = (const uint8*)resources->LoadResource(
B_VECTOR_ICON_TYPE, "ether", &iconSize);
if (interfaceHVIF) {
// Now build the bitmap
interfaceBitmap = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE),
0, B_RGBA32);
if (BIconUtils::GetVectorIcon(interfaceHVIF,
iconSize, interfaceBitmap) == B_OK)
fIcon = interfaceBitmap;
else
delete interfaceBitmap;
}
// Load possible state icons
offlineHVIF = (const uint8*)resources->LoadResource(
B_VECTOR_ICON_TYPE, "offline", &iconSize);
if (offlineHVIF) {
fIconOffline = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE),
0, B_RGBA32);
BIconUtils::GetVectorIcon(offlineHVIF, iconSize, fIconOffline);
}
pendingHVIF = (const uint8*)resources->LoadResource(
B_VECTOR_ICON_TYPE, "pending", &iconSize);
if (pendingHVIF) {
fIconPending = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE),
0, B_RGBA32);
BIconUtils::GetVectorIcon(pendingHVIF, iconSize, fIconPending);
}
onlineHVIF = (const uint8*)resources->LoadResource(
B_VECTOR_ICON_TYPE, "online", &iconSize);
if (onlineHVIF) {
fIconOnline = new BBitmap(BRect(0, 0, ICON_SIZE, ICON_SIZE),
0, B_RGBA32);
BIconUtils::GetVectorIcon(onlineHVIF, iconSize, fIconOnline);
}
}

View File

@ -0,0 +1,51 @@
/*
* Copyright 2004-2015 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Alexander von Gluck, kallisti5@unixzen.com
* Philippe Houdoin
* Fredrik Modéen
* John Scipione, jscipione@gmail.com
*/
#ifndef INTERFACE_LIST_ITEM_H
#define INTERFACE_LIST_ITEM_H
#include <ListItem.h>
#include <NetworkInterface.h>
class BBitmap;
class InterfaceListItem : public BListItem {
public:
InterfaceListItem(const char* name);
~InterfaceListItem();
void DrawItem(BView* owner,
BRect bounds, bool complete);
void Update(BView* owner, const BFont* font);
inline const char* Name() { return fInterface.Name(); }
private:
void _Init();
void _PopulateBitmaps(const char* mediaType);
BBitmap* fIcon;
BBitmap* fIconOffline;
BBitmap* fIconPending;
BBitmap* fIconOnline;
BNetworkInterface fInterface;
// Hardware Interface
float fFirstlineOffset;
float fSecondlineOffset;
float fThirdlineOffset;
};
#endif // INTERFACE_LIST_ITEM_H

View File

@ -1,33 +1,37 @@
SubDir HAIKU_TOP src preferences network ;
UsePrivateHeaders net shared ;
SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src apps networkstatus ] ;
Preference Network :
NetworkSetup.cpp
NetworkSetupWindow.cpp
NetworkSetupProfile.cpp
NetworkSetupAddOn.cpp
Network.cpp
NetworkWindow.cpp
NetworkProfile.cpp
NetworkSettingsAddOn.cpp
InterfaceListItem.cpp
# from NetworkStatus
RadioView.cpp
WirelessNetworkMenuItem.cpp
: be root [ TargetLibstdc++ ] localestub
: NetworkSetup.rdef
: be root bnetapi [ TargetLibstdc++ ] localestub
: Network.rdef InterfaceIcons.rdef
;
DoCatalogs Network :
x-vnd.Haiku-Network
:
NetworkSetup.cpp
NetworkSetupWindow.cpp
NetworkSetupProfile.cpp
NetworkSetupAddOn.cpp
Network.cpp
NetworkWindow.cpp
NetworkProfile.cpp
NetworkSettingsAddOn.cpp
RadioView.cpp
WirelessNetworkMenuItem.cpp
;
SubInclude HAIKU_TOP src preferences network InterfacesAddOn ;
SubInclude HAIKU_TOP src preferences network ServicesAddOn ;
#SubInclude HAIKU_TOP src preferences network InterfacesAddOn ;
#SubInclude HAIKU_TOP src preferences network ServicesAddOn ;
#SubInclude HAIKU_TOP src preferences network DummyAddOn ;
#SubInclude HAIKU_TOP src preferences network MultipleAddOns ;
#SubInclude HAIKU_TOP src preferences network DialUpAddOn ;

View File

@ -1,16 +1,16 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
*/
#include <Alert.h>
#include <Application.h>
#include <Catalog.h>
#include <Locale.h>
#include <Window.h>
#include "NetworkSetupWindow.h"
#include "NetworkWindow.h"
static const char* kSignature = "application/x-vnd.Haiku-Network";
@ -35,7 +35,7 @@ Application::Application()
void
Application::ReadyToRun()
{
NetworkSetupWindow* window = new NetworkSetupWindow();
NetworkWindow* window = new NetworkWindow();
window->Show();
}

View File

@ -0,0 +1,160 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "NetworkProfile.h"
#include <stdlib.h>
using namespace BNetworkKit;
BNetworkProfile::BNetworkProfile()
:
fIsDefault(false),
fIsCurrent(false),
fName(NULL)
{
}
BNetworkProfile::BNetworkProfile(const char* path)
:
fIsDefault(false),
fIsCurrent(false)
{
SetTo(path);
}
BNetworkProfile::BNetworkProfile(const entry_ref& ref)
:
fIsDefault(false),
fIsCurrent(false)
{
SetTo(ref);
}
BNetworkProfile::BNetworkProfile(const BEntry& entry)
:
fIsDefault(false),
fIsCurrent(false)
{
SetTo(entry);
}
BNetworkProfile::~BNetworkProfile()
{
}
status_t
BNetworkProfile::SetTo(const char* path)
{
status_t status = fEntry.SetTo(path, true);
if (status != B_OK)
return status;
fPath.Unset();
fName = NULL;
return B_OK;
}
status_t
BNetworkProfile::SetTo(const entry_ref& ref)
{
status_t status = fEntry.SetTo(&ref);
if (status != B_OK)
return status;
fPath.Unset();
fName = ref.name;
return B_OK;
}
status_t
BNetworkProfile::SetTo(const BEntry& entry)
{
fEntry = entry;
fPath.Unset();
fName = NULL;
return B_OK;
}
const char*
BNetworkProfile::Name()
{
if (fName == NULL) {
if (fEntry.GetPath(&fPath) == B_OK)
fName = fPath.Leaf();
}
return fName;
}
status_t
BNetworkProfile::SetName(const char* name)
{
return B_OK;
}
bool
BNetworkProfile::Exists()
{
return fEntry.Exists();
}
status_t
BNetworkProfile::Delete()
{
return B_ERROR;
}
bool
BNetworkProfile::IsDefault()
{
return fIsDefault;
}
bool
BNetworkProfile::IsCurrent()
{
return fIsCurrent;
}
status_t
BNetworkProfile::MakeCurrent()
{
return B_ERROR;
}
// #pragma mark -
BNetworkProfile*
BNetworkProfile::Default()
{
return NULL;
}
BNetworkProfile*
BNetworkProfile::Current()
{
return NULL;
}

View File

@ -0,0 +1,56 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _NETWORK_PROFILE_H
#define _NETWORK_PROFILE_H
#include <Entry.h>
#include <Path.h>
namespace BNetworkKit {
class BNetworkProfile {
public:
BNetworkProfile();
BNetworkProfile(const char* path);
BNetworkProfile(const entry_ref& ref);
BNetworkProfile(const BEntry& entry);
virtual ~BNetworkProfile();
status_t SetTo(const char* path);
status_t SetTo(const entry_ref& ref);
status_t SetTo(const BEntry& entry);
bool Exists();
const char* Name();
status_t SetName(const char* name);
bool IsDefault();
bool IsCurrent();
status_t MakeCurrent();
status_t Delete();
static BNetworkProfile* Default();
static BNetworkProfile* Current();
private:
BEntry fEntry;
BPath fPath;
bool fIsDefault;
bool fIsCurrent;
const char* fName;
static BDirectory* fProfilesRoot;
};
} // namespace BNetworkKit
#endif // _NETWORK_PROFILE_H

View File

@ -0,0 +1,123 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "NetworkSettingsAddOn.h"
#include <stdio.h>
#include <stdlib.h>
using namespace BNetworkKit;
BNetworkSettingsItem::BNetworkSettingsItem()
:
fProfile(NULL)
{
}
BNetworkSettingsItem::~BNetworkSettingsItem()
{
}
status_t
BNetworkSettingsItem::ProfileChanged(const BNetworkProfile* newProfile)
{
fProfile = newProfile;
return B_OK;
}
const BNetworkProfile*
BNetworkSettingsItem::Profile() const
{
return fProfile;
}
// #pragma mark -
BNetworkSettingsInterfaceItem::BNetworkSettingsInterfaceItem(
const char* interface)
:
fInterface(interface)
{
}
BNetworkSettingsType
BNetworkSettingsInterfaceItem::Type() const
{
return B_NETWORK_SETTINGS_TYPE_INTERFACE;
}
const char*
BNetworkSettingsInterfaceItem::Interface() const
{
return fInterface;
}
// #pragma mark -
BNetworkSettingsAddOn::BNetworkSettingsAddOn(image_id image)
:
fImage(image),
fResources(NULL)
{
}
BNetworkSettingsAddOn::~BNetworkSettingsAddOn()
{
delete fResources;
}
BNetworkSettingsInterfaceItem*
BNetworkSettingsAddOn::CreateNextInterfaceItem(uint32& cookie,
const char* interface)
{
return NULL;
}
BNetworkSettingsItem*
BNetworkSettingsAddOn::CreateNextItem(uint32& cookie)
{
return NULL;
}
image_id
BNetworkSettingsAddOn::Image()
{
return fImage;
}
BResources*
BNetworkSettingsAddOn::Resources()
{
if (fResources == NULL) {
image_info info;
if (get_image_info(fImage, &info) != B_OK)
return NULL;
BResources* resources = new BResources();
BFile file(info.name, B_READ_ONLY);
if (resources->SetTo(&file) == B_OK)
fResources = resources;
else
delete resources;
}
return fResources;
}

View File

@ -0,0 +1,94 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
*/
#ifndef _NETWORK_SETTINGS_ADD_ON_H
#define _NETWORK_SETTINGS_ADD_ON_H
#include <image.h>
#include <ListItem.h>
#include <Resources.h>
#include <View.h>
namespace BNetworkKit {
enum BNetworkSettingsType {
B_NETWORK_SETTINGS_TYPE_INTERFACE = 'intf',
B_NETWORK_SETTINGS_TYPE_SERVICE = 'serv',
B_NETWORK_SETTINGS_TYPE_DIAL_UP = 'dial',
B_NETWORK_SETTINGS_TYPE_OTHER = 'othr'
};
class BNetworkProfile;
class BNetworkSettingsItem {
public:
BNetworkSettingsItem();
virtual ~BNetworkSettingsItem();
virtual BNetworkSettingsType
Type() const = 0;
virtual BListItem* CreateListItem() = 0;
virtual status_t ProfileChanged(
const BNetworkProfile* newProfile);
const BNetworkProfile*
Profile() const;
virtual status_t Save() = 0;
virtual status_t Revert() = 0;
private:
const BNetworkProfile*
fProfile;
};
class BNetworkSettingsInterfaceItem : public BNetworkSettingsItem {
public:
BNetworkSettingsInterfaceItem(
const char* interface);
virtual BNetworkSettingsType
Type() const;
const char* Interface() const;
private:
const char* fInterface;
};
class BNetworkSettingsAddOn {
public:
BNetworkSettingsAddOn(image_id image);
virtual ~BNetworkSettingsAddOn();
virtual BNetworkSettingsInterfaceItem*
CreateNextInterfaceItem(uint32& cookie,
const char* interface);
virtual BNetworkSettingsItem*
CreateNextItem(uint32& cookie);
image_id Image();
BResources* Resources();
private:
image_id fImage;
BResources* fResources;
};
// Your add-on needs to export this hook in order to be picked up
extern "C" BNetworkSettingsAddOn* instantiate_network_settings_add_on(
image_id image);
} // namespace BNetworkKit
#endif // _NETWORK_SETTINGS_ADD_ON_H

View File

@ -1,102 +0,0 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "NetworkSetupAddOn.h"
#include <stdio.h>
#include <stdlib.h>
NetworkSetupAddOn::NetworkSetupAddOn(image_id image)
:
fIsDirty(false),
fProfile(NULL),
fImage(image),
fResources(NULL)
{
}
NetworkSetupAddOn::~NetworkSetupAddOn()
{
delete fResources;
}
status_t
NetworkSetupAddOn::Save()
{
return B_OK;
}
status_t
NetworkSetupAddOn::Revert()
{
return B_OK;
}
status_t
NetworkSetupAddOn::ProfileChanged(NetworkSetupProfile* newProfile)
{
fProfile = newProfile;
return B_OK;
}
bool
NetworkSetupAddOn::IsDirty()
{
return fIsDirty;
}
void
NetworkSetupAddOn::SetDirty(bool dirty)
{
fIsDirty = dirty;
}
NetworkSetupProfile*
NetworkSetupAddOn::Profile()
{
return fProfile;
}
image_id
NetworkSetupAddOn::ImageId()
{
return fImage;
}
const char*
NetworkSetupAddOn::Name()
{
return "Dummy NetworkSetupAddon";
}
BResources*
NetworkSetupAddOn::Resources()
{
if (fResources == NULL) {
image_info info;
if (get_image_info(fImage, &info) != B_OK)
return NULL;
BResources* resources = new BResources();
BFile file(info.name, B_READ_ONLY);
if (resources->SetTo(&file) == B_OK)
fResources = resources;
else
delete resources;
}
return fResources;
}

View File

@ -1,56 +0,0 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*
*/
#ifndef NETWORK_SETUP_ADD_ON_H
#define NETWORK_SETUP_ADD_ON_H
#include <image.h>
#include <Resources.h>
#include <View.h>
class NetworkSetupProfile;
class NetworkSetupAddOn {
public:
NetworkSetupAddOn(image_id image);
virtual ~NetworkSetupAddOn();
virtual BView* CreateView() = 0;
virtual status_t Save();
virtual status_t Revert();
virtual const char* Name();
virtual status_t ProfileChanged(NetworkSetupProfile* newProfile);
NetworkSetupProfile*
Profile();
bool IsDirty();
void SetDirty(bool dirty = true);
image_id ImageId();
BResources* Resources();
private:
bool fIsDirty;
NetworkSetupProfile*
fProfile;
image_id fImage;
BResources* fResources;
};
extern "C" {
#define NETWORK_SETUP_ADDON_INSTANCIATE_FUNC_NAME "get_nth_addon"
typedef NetworkSetupAddOn* (*network_setup_addon_instantiate)(image_id image,
int index);
extern NetworkSetupAddOn* get_nth_addon(image_id image, int index);
}
#endif // NETWORKSETUPADDON_H

View File

@ -1,159 +0,0 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#include "NetworkSetupProfile.h"
#include <stdlib.h>
NetworkSetupProfile::NetworkSetupProfile()
:
fRoot(new BEntry()),
fPath(new BPath()),
fIsDefault(false),
fIsCurrent(false),
fName(NULL)
{
}
NetworkSetupProfile::NetworkSetupProfile(const char* path)
:
fRoot(NULL),
fPath(NULL),
fIsDefault(false),
fIsCurrent(false)
{
SetTo(path);
}
NetworkSetupProfile::NetworkSetupProfile(const entry_ref* ref)
:
fRoot(NULL),
fPath(NULL),
fIsDefault(false),
fIsCurrent(false)
{
SetTo(ref);
}
NetworkSetupProfile::NetworkSetupProfile(BEntry* entry)
:
fRoot(NULL),
fPath(NULL),
fIsDefault(false),
fIsCurrent(false)
{
SetTo(entry);
}
NetworkSetupProfile::~NetworkSetupProfile()
{
delete fRoot;
delete fPath;
}
status_t
NetworkSetupProfile::SetTo(const char* path)
{
SetTo(new BEntry(path));
return B_OK;
}
status_t
NetworkSetupProfile::SetTo(const entry_ref* ref)
{
SetTo(new BEntry(ref));
return B_OK;
}
status_t
NetworkSetupProfile::SetTo(BEntry* entry)
{
delete fRoot;
delete fPath;
fRoot = entry;
fPath = NULL;
fName = NULL;
return B_OK;
}
const char*
NetworkSetupProfile::Name()
{
if (fName == NULL) {
fRoot->GetPath(fPath);
fName = fPath->Leaf();
}
return fName;
}
status_t
NetworkSetupProfile::SetName(const char* name)
{
return B_OK;
}
bool
NetworkSetupProfile::Exists()
{
return fRoot->Exists();
}
status_t
NetworkSetupProfile::Delete()
{
return B_ERROR;
}
bool
NetworkSetupProfile::IsDefault()
{
return fIsDefault;
}
bool
NetworkSetupProfile::IsCurrent()
{
return fIsCurrent;
}
status_t
NetworkSetupProfile::MakeCurrent()
{
return B_ERROR;
}
// #pragma mark -
NetworkSetupProfile*
NetworkSetupProfile::Default()
{
return NULL;
}
NetworkSetupProfile*
NetworkSetupProfile::Current()
{
return NULL;
}

View File

@ -1,52 +0,0 @@
/*
* Copyright 2004-2015 Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef NETWORK_SETUP_PROFILE_H
#define NETWORK_SETUP_PROFILE_H
#include <StorageKit.h>
class NetworkSetupProfile {
public:
NetworkSetupProfile();
NetworkSetupProfile(const char* path);
NetworkSetupProfile(const entry_ref* ref);
NetworkSetupProfile(BEntry* entry);
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 Delete();
static NetworkSetupProfile*
Default();
static NetworkSetupProfile*
Current();
private:
BEntry* fRoot;
BPath* fPath;
bool fIsDefault;
bool fIsCurrent;
const char* fName;
static BDirectory* fProfilesRoot;
};
#endif // NETWORK_SETUP_PROFILE_H

View File

@ -8,8 +8,9 @@
*/
#include "NetworkSetupWindow.h"
#include "NetworkWindow.h"
#include <net/if.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -21,30 +22,35 @@
#include <CheckBox.h>
#include <ControlLook.h>
#include <Deskbar.h>
#include <Directory.h>
#include <LayoutBuilder.h>
#include <Locale.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include <OutlineListView.h>
#include <Path.h>
#include <PathFinder.h>
#include <Roster.h>
#include <StorageKit.h>
#include <SupportKit.h>
#include <TabView.h>
#include <ScrollView.h>
#include <SymLink.h>
#define ENABLE_PROFILES 0
#if ENABLE_PROFILES
# include <PopUpMenu.h>
#endif
#include "InterfaceListItem.h"
const char* kNetworkStatusSignature = "application/x-vnd.Haiku-NetworkStatus";
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "NetworkSetupWindow"
#define B_TRANSLATION_CONTEXT "NetworkWindow"
NetworkSetupWindow::NetworkSetupWindow()
NetworkWindow::NetworkWindow()
:
BWindow(BRect(100, 100, 300, 300), B_TRANSLATE("Network"), B_TITLED_WINDOW,
B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS),
fAddOnCount(0)
B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS)
{
// ---- Profiles section
#if ENABLE_PROFILES
@ -60,8 +66,6 @@ NetworkSetupWindow::NetworkSetupWindow()
// ---- Settings section
fPanel = new BTabView("tabs", B_WIDTH_FROM_LABEL);
fApplyButton = new BButton("apply", B_TRANSLATE("Apply"),
new BMessage(kMsgApply));
SetDefaultButton(fApplyButton);
@ -73,8 +77,14 @@ NetworkSetupWindow::NetworkSetupWindow()
BMessage* message = new BMessage(kMsgToggleReplicant);
BCheckBox* replicantStatus = new BCheckBox("replicantStatus",
B_TRANSLATE("Show network status in Deskbar"), message);
replicantStatus->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
replicantStatus->SetValue(_IsReplicantInstalled());
fListView = new BOutlineListView("list");
BScrollView* scrollView = new BScrollView("ScrollView",
fListView, 0/*B_WILL_DRAW | B_FRAME_EVENTS*/, false, true);
// Build the layout
BLayoutBuilder::Group<>(this, B_VERTICAL)
.SetInsets(B_USE_DEFAULT_SPACING)
@ -86,9 +96,9 @@ NetworkSetupWindow::NetworkSetupWindow()
.End()
#endif
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fPanel)
.Add(new BGroupView("panel"))
.End()
.Add(scrollView)
.AddGlue()
.End()
.Add(replicantStatus)
.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
.Add(fRevertButton)
@ -96,7 +106,8 @@ NetworkSetupWindow::NetworkSetupWindow()
.Add(fApplyButton)
.End();
_BuildShowTabView();
_ScanInterfaces();
_ScanAddOns();
fAddOnView = NULL;
@ -104,13 +115,13 @@ NetworkSetupWindow::NetworkSetupWindow()
}
NetworkSetupWindow::~NetworkSetupWindow()
NetworkWindow::~NetworkWindow()
{
}
bool
NetworkSetupWindow::QuitRequested()
NetworkWindow::QuitRequested()
{
be_app->PostMessage(B_QUIT_REQUESTED);
return true;
@ -118,7 +129,7 @@ NetworkSetupWindow::QuitRequested()
void
NetworkSetupWindow::MessageReceived(BMessage* message)
NetworkWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kMsgProfileNew:
@ -139,26 +150,22 @@ NetworkSetupWindow::MessageReceived(BMessage* message)
case kMsgRevert:
{
for (int index = 0; index < fAddOnCount; index++) {
NetworkSetupAddOn* addOn = fNetworkAddOnMap[index];
addOn->Revert();
}
for (int index = 0; index < fItems.CountItems(); index++)
fItems.ItemAt(index)->Revert();
break;
}
case kMsgApply:
{
for (int index = 0; index < fAddOnCount; index++) {
NetworkSetupAddOn* addOn = fNetworkAddOnMap[index];
addOn->Save();
}
for (int index = 0; index < fItems.CountItems(); index++)
fItems.ItemAt(index)->Save();
break;
}
case kMsgToggleReplicant:
{
_ShowReplicant(message->GetInt32("be:value", B_CONTROL_OFF)
== B_CONTROL_ON);
_ShowReplicant(
message->GetInt32("be:value", B_CONTROL_OFF) == B_CONTROL_ON);
break;
}
@ -169,7 +176,7 @@ NetworkSetupWindow::MessageReceived(BMessage* message)
void
NetworkSetupWindow::_BuildProfilesMenu(BMenu* menu, int32 what)
NetworkWindow::_BuildProfilesMenu(BMenu* menu, int32 what)
{
char currentProfile[256] = { 0 };
@ -227,90 +234,137 @@ NetworkSetupWindow::_BuildProfilesMenu(BMenu* menu, int32 what)
void
NetworkSetupWindow::_BuildShowTabView()
NetworkWindow::_ScanInterfaces()
{
BPath path;
BPath addOnPath;
BDirectory dir;
BEntry entry;
// Try existing devices first
BNetworkRoster& roster = BNetworkRoster::Default();
BNetworkInterface interface;
uint32 cookie = 0;
char* searchPaths = getenv("ADDON_PATH");
if (!searchPaths)
return;
searchPaths = strdup(searchPaths);
char* nextPathToken;
char* searchPath = strtok_r(searchPaths, ":", &nextPathToken);
while (searchPath) {
if (strncmp(searchPath, "%A/", 3) == 0) {
app_info ai;
be_app->GetAppInfo(&ai);
entry.SetTo(&ai.ref);
entry.GetPath(&path);
path.GetParent(&path);
path.Append(searchPath + 3);
} else {
path.SetTo(searchPath);
path.Append("Network Setup");
}
searchPath = strtok_r(NULL, ":", &nextPathToken);
dir.SetTo(path.Path());
if (dir.InitCheck() != B_OK)
while (roster.GetNextInterface(&cookie, interface) == B_OK) {
if ((interface.Flags() & IFF_LOOPBACK) != 0)
continue;
dir.Rewind();
while (dir.GetNextEntry(&entry) >= 0) {
if (entry.IsDirectory())
continue;
entry.GetPath(&addOnPath);
image_id image = load_add_on(addOnPath.Path());
if (image < 0) {
printf("Failed to load %s addon: %s.\n", addOnPath.Path(),
strerror(image));
continue;
}
network_setup_addon_instantiate get_nth_addon;
status_t status = get_image_symbol(image, "get_nth_addon",
B_SYMBOL_TYPE_TEXT, (void **) &get_nth_addon);
int tabCount = 0;
if (status != B_OK) {
// No "addon instantiate function" symbol found in this addon
printf("No symbol \"get_nth_addon\" found in %s addon: not a "
"network setup addon!\n", addOnPath.Path());
unload_add_on(image);
continue;
}
while ((fNetworkAddOnMap[fAddOnCount]
= get_nth_addon(image, tabCount)) != NULL) {
printf("Adding Tab: %d\n", fAddOnCount);
BView* view = fNetworkAddOnMap[fAddOnCount]->CreateView();
// FIXME rework this: we don't want to use a tab view here,
// instead add-ons should populate the "interfaces" list with
// interfaces, services, etc.
fPanel->AddTab(view);
fAddOnCount++;
// Number of tab addons total
tabCount++;
// Tabs for *this* addon
}
}
InterfaceListItem* item = new InterfaceListItem(interface.Name());
fInterfaceItemMap.insert(std::pair<BString, BListItem*>(
BString(interface.Name()), item));
fListView->AddItem(item);
}
free(searchPaths);
// TODO: Then consider those from the settings (for example, for USB)
}
void
NetworkSetupWindow::_ShowReplicant(bool show)
NetworkWindow::_ScanAddOns()
{
BStringList paths;
BPathFinder::FindPaths(B_FIND_PATH_ADD_ONS_DIRECTORY, "Network Settings",
paths);
for (int32 i = 0; i < paths.CountStrings(); i++) {
BDirectory directory(paths.StringAt(i));
BEntry entry;
while (directory.GetNextEntry(&entry) == B_OK) {
BPath path;
if (entry.GetPath(&path) != B_OK)
continue;
image_id image = load_add_on(path.Path());
if (image < 0) {
printf("Failed to load %s addon: %s.\n", path.Path(),
strerror(image));
continue;
}
BNetworkSettingsAddOn* (*instantiateAddOn)(image_id image);
status_t status = get_image_symbol(image,
"instantiate_network_settings_add_on",
B_SYMBOL_TYPE_TEXT, (void**)&instantiateAddOn);
if (status != B_OK) {
// No "addon instantiate function" symbol found in this addon
printf("No symbol \"instantiate_network_settings_add_on\" "
"found in %s addon: not a network setup addon!\n",
path.Path());
unload_add_on(image);
continue;
}
BNetworkSettingsAddOn* addOn = instantiateAddOn(image);
if (addOn == NULL) {
unload_add_on(image);
continue;
}
fAddOns.AddItem(addOn);
// Per interface items
ItemMap::const_iterator iterator = fInterfaceItemMap.begin();
for (; iterator != fInterfaceItemMap.end(); iterator++) {
const BString& interface = iterator->first;
BListItem* interfaceItem = iterator->second;
uint32 cookie = 0;
while (true) {
BNetworkSettingsItem* item = addOn->CreateNextInterfaceItem(
cookie, interface.String());
if (item == NULL)
break;
fItems.AddItem(item);
// TODO: sort
fListView->AddUnder(interfaceItem, item->CreateListItem());
}
}
// Generic items
uint32 cookie = 0;
while (true) {
BNetworkSettingsItem* item = addOn->CreateNextItem(cookie);
if (item == NULL)
break;
fItems.AddItem(item);
// TODO: sort
fListView->AddUnder(_ItemFor(item->Type()),
item->CreateListItem());
}
}
}
}
BListItem*
NetworkWindow::_ItemFor(BNetworkSettingsType type)
{
switch (type) {
case B_NETWORK_SETTINGS_TYPE_SERVICE:
if (fServicesItem == NULL)
fServicesItem = new BStringItem(B_TRANSLATE("Services"));
return fServicesItem;
case B_NETWORK_SETTINGS_TYPE_DIAL_UP:
if (fDialUpItem == NULL)
fDialUpItem = new BStringItem(B_TRANSLATE("Dial Up"));
return fDialUpItem;
case B_NETWORK_SETTINGS_TYPE_OTHER:
if (fOtherItem == NULL)
fOtherItem = new BStringItem(B_TRANSLATE("Other"));
return fOtherItem;
default:
return NULL;
}
}
void
NetworkWindow::_ShowReplicant(bool show)
{
if (show) {
const char* argv[] = {"--deskbar", NULL};
@ -333,7 +387,7 @@ NetworkSetupWindow::_ShowReplicant(bool show)
bool
NetworkSetupWindow::_IsReplicantInstalled()
NetworkWindow::_IsReplicantInstalled()
{
BDeskbar deskbar;
return deskbar.HasItem("NetworkStatus");

View File

@ -5,26 +5,27 @@
* Authors:
* Alexander von Gluck, <kallisti5@unixzen.com>
*/
#ifndef NETWORK_SETUP_WINDOW_H
#define NETWORK_SETUP_WINDOW_H
#ifndef NETWORK_WINDOW_H
#define NETWORK_WINDOW_H
#include "NetworkSetupAddOn.h"
#include <map>
#include <ObjectList.h>
#include <Window.h>
#include "NetworkSettingsAddOn.h"
using namespace BNetworkKit;
typedef std::map<int, NetworkSetupAddOn*> NetworkAddOnMap;
class NetworkSetupWindow;
class BTabView;
class BButton;
class BMenu;
class NetworkSetupWindow : public BWindow {
class NetworkWindow : public BWindow {
public:
static const uint32 kMsgProfileSelected = 'prof';
static const uint32 kMsgProfileManage = 'mngp';
@ -34,8 +35,8 @@ public:
static const uint32 kMsgToggleReplicant = 'trep';
public:
NetworkSetupWindow();
virtual ~NetworkSetupWindow();
NetworkWindow();
virtual ~NetworkWindow();
bool QuitRequested();
void MessageReceived(BMessage* message);
@ -44,21 +45,33 @@ private:
typedef BWindow inherited;
void _BuildProfilesMenu(BMenu* menu, int32 what);
void _BuildShowTabView();
void _ScanInterfaces();
void _ScanAddOns();
BListItem* _ItemFor(BNetworkSettingsType type);
bool _IsReplicantInstalled();
void _ShowReplicant(bool show);
private:
typedef BObjectList<BNetworkSettingsAddOn> AddOnList;
typedef BObjectList<BNetworkSettingsItem> ItemList;
typedef std::map<BString, BListItem*> ItemMap;
BButton* fRevertButton;
BButton* fApplyButton;
NetworkAddOnMap fNetworkAddOnMap;
AddOnList fAddOns;
BOutlineListView* fListView;
ItemMap fInterfaceItemMap;
BListItem* fServicesItem;
BListItem* fDialUpItem;
BListItem* fOtherItem;
ItemList fItems;
BTabView* fPanel;
BView* fAddOnView;
int fAddOnCount;
};
#endif // NETWORK_SETUP_WINDOW_H
#endif // NETWORK_WINDOW_H