Initial checkin of "DialUpPreflet". This will become an add-on for the "Network" preflet.

It is far from finished (all add-ons are missing). The hacks should be removed...


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6856 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Waldemar Kornewald
2004-03-02 16:03:34 +00:00
parent 1a5f7f53c6
commit 6bef9fe1b8
12 changed files with 1482 additions and 0 deletions
+31
View File
@@ -0,0 +1,31 @@
--------------------------------------------------------------------------------------
Notes for add-on developers:
--------------------------------------------------------------------------------------
All add-ons must add a "Valid" boolean value to every recognized parameter. The preflet will check if all values were recognized and if not it will ask the user if he wants to edit the interface anyway...
--------------------------------------------------------------------------------------
Add-on registration:
--------------------------------------------------------------------------------------
Tabs:
"Tab" : pointer to DialUpAddon object
Authenticators:
"Authenticator" : BMessage with the following entries:
{
"KernelModule" : string
"TechnicalName" : string
"FriendlyName" : string
"Position" : int32
}
Devices:
"Device" : pointer to DialUpAddon object
Protocols:
"Protocol" : pointer to DialUpAddon object
--------------------------------------------------------------------------------------
Additional data in the BMessage object for add-ons:
--------------------------------------------------------------------------------------
"Messenger" : BMessenger - This messenger can be used to send messages to the preflet
"TabViewRect" : BRect - This rect should be used for tabs
@@ -0,0 +1,82 @@
/*
This software may be distributed under the OpenBeOS license.
Copyright (c) 2004, OpenBeOS
Dial-Up add-ons must export the following function:
bool register(BMessage *addons);
Most add-ons are simple pointers to a DialUpAddon object.
Note for tab-addons: The BView object is deleted AFTER the DialUpAddon (except you
remove and delete it in the DialUpAddon destructor).
*/
#ifndef _DIAL_UP_ADDON__H
#define _DIAL_UP_ADDON__H
#include <SupportDefs.h>
#include <Point.h>
#define DUN_MAXIMUM_PRIORITY 50
class DialUpAddon {
friend class DialUpView;
public:
DialUpAddon(BMessage *addons) : fAddons(addons) {}
virtual ~DialUpAddon() {}
const BMessage *Addons() const
{ return fAddons; }
bool IsValid() const
{ return fIsValid; }
// This value will be set after all add-ons are registered.
// Add-ons with a non-existant kernel module are invalid!
virtual const char *FriendlyName() const
{ return NULL; }
virtual const char *TechnicalName() const
{ return NULL; }
virtual const char *KernelModuleName() const
{ return NULL; }
// the kernel module for this add-on (if needed)
virtual int32 Position() const
{ return -1; }
// mostly used by tabs to describe where they should appear
virtual int32 Priority() const
{ return 0; }
// allows to set order in which modules are asked to add the settings
virtual bool LoadSettings(BMessage *settings, BMessage *profile)
{ return false; }
// The interface's device is saved to "device" when loading
// settings or when a new device is selected.
virtual void IsModified(bool& settings, bool& profile) const
{ settings = profile = false; }
virtual bool SaveSettings(BMessage *settings, BMessage *profile,
bool saveTemporary)
{ return false; }
// temporary settings are passwords, for example
virtual bool GetPreferredSize(float *width, float *height) const
{ return false; }
// if false is returned your add-on does not want to add a view
virtual BView *CreateView(BPoint leftTop)
{ return NULL; }
private:
virtual void _Reserved1() {}
virtual void _Reserved2() {}
virtual void _Reserved3() {}
virtual void _Reserved4() {}
private:
BMessage *fAddons;
bool fIsValid;
char _reserved[31];
};
#endif
@@ -0,0 +1,56 @@
//-----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//
// Copyright (c) 2003-2004 Waldemar Kornewald, [email protected]
//-----------------------------------------------------------------------
#include <Application.h>
#include <Window.h>
#include "DialUpView.h"
#define DIAL_UP_SIGNATURE "application/x-obos.dial-up-preflet"
class DialUpApplication : public BApplication {
public:
DialUpApplication();
};
class DialUpWindow : public BWindow {
public:
DialUpWindow(BRect frame);
virtual bool QuitRequested()
{ be_app->PostMessage(B_QUIT_REQUESTED); return true; }
};
int main()
{
DialUpApplication *app = new DialUpApplication();
app->Run();
return 0;
}
DialUpApplication::DialUpApplication()
: BApplication(DIAL_UP_SIGNATURE)
{
DialUpWindow *window = new DialUpWindow(BRect(150, 50, 450, 435));
window->Show();
}
DialUpWindow::DialUpWindow(BRect frame)
: BWindow(frame, "DialUp", B_TITLED_WINDOW, B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
{
DialUpView *view = new DialUpView(Bounds());
AddChild(view);
}
@@ -0,0 +1,614 @@
//-----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//
// Copyright (c) 2003-2004 Waldemar Kornewald, [email protected]
//-----------------------------------------------------------------------
#include "DialUpView.h"
#include "DialUpAddon.h"
#include "MessageDriverSettingsUtils.h"
#include "GeneralAddon.h"
#include <PPPInterface.h>
#include <TemplateList.h>
#include <Application.h>
#include <Alert.h>
#include <Button.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <Messenger.h>
#include <PopUpMenu.h>
#include <StringView.h>
#include <TabView.h>
#include <Directory.h>
#include <Entry.h>
#include <File.h>
#include <Path.h>
#include <cstring>
#define MSG_CREATE_NEW 'NEWI'
#define MSG_DELETE_CURRENT 'DELI'
#define MSG_SELECT_INTERFACE 'SELI'
#define MSG_CONNECT_BUTTON 'CONI'
#define LABEL_CREATE_NEW "Create new..."
#define LABEL_DELETE_CURRENT "Delete current"
#define LABEL_CONNECT "Connect"
#define LABEL_DISCONNECT "Disconnect"
#define TEXT_CONNECTING "Connecting..."
#define TEXT_CONNECTION_ESTABLISHED "Connection established."
#define TEXT_NOT_CONNECTED "Not connected."
#define TEXT_DEVICE_ERROR "Device error!"
#define TEXT_AUTHENTICATING "Authenticating..."
#define TEXT_AUTHENTICATION_FAILED "Authentication failed!"
#define TEXT_CONNECTION_LOST "Connection lost!"
#define TEXT_CREATION_ERROR "Error creating interface!"
#define TEXT_OK "OK"
#define ERROR_TITLE "Error"
#define ERROR_NO_PPP_STACK "Error: Could not find the PPP stack!"
#define ERROR_INTERFACE_EXISTS "Error: An interface with this name already exists!"
static
status_t
up_down_thread(void *data)
{
static_cast<DialUpView*>(data)->UpDownThread();
return B_OK;
}
DialUpView::DialUpView(BRect frame)
: BView(frame, "DialUpView", B_FOLLOW_NONE, 0),
fListener(this),
fUpDownThread(-1),
fDriverSettings(NULL),
fCurrentItem(NULL),
fWatching(PPP_UNDEFINED_INTERFACE_ID),
fKeepLabel(false)
{
BRect bounds = Bounds();
// for caching
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
// add messenger to us so add-ons can contact us
BMessenger messenger(this);
fAddons.AddMessenger("Messenger", messenger);
// load integrated add-ons
fAddons.AddPointer("Tab", new GeneralAddon(&fAddons));
// TODO:
// load all add-ons
// register them and check if each one has a kernel module (set fIsValid)
// create pop-up with all interfaces and "New..."/"Delete current" items
fInterfaceMenu = new BPopUpMenu(LABEL_CREATE_NEW);
BRect rect = bounds;
rect.InsetBy(5, 5);
rect.bottom = rect.top + 20;
fMenuField = new BMenuField(rect, "Interfaces", "Interface:", fInterfaceMenu);
fMenuField->SetDivider(StringWidth(fMenuField->Label()) + 10);
fInterfaceMenu->AddSeparatorItem();
fInterfaceMenu->AddItem(new BMenuItem(LABEL_CREATE_NEW,
new BMessage(MSG_CREATE_NEW)));
fInterfaceMenu->AddItem(new BMenuItem(LABEL_DELETE_CURRENT,
new BMessage(MSG_DELETE_CURRENT)));
BDirectory settingsDirectory;
BEntry entry;
BPath path;
GetPPPDirectories(&settingsDirectory, NULL);
while(settingsDirectory.GetNextEntry(&entry) == B_OK) {
if(entry.IsFile()) {
entry.GetPath(&path);
AddInterface(path.Leaf());
}
}
rect.top = rect.bottom + 10;
rect.bottom = bounds.bottom
- 20 // height of bottom controls
- 20; // space for bottom controls
fTabView = new BTabView(rect, "TabView");
BRect tabViewRect = fTabView->Bounds();
tabViewRect.bottom -= fTabView->TabHeight();
fAddons.AddRect("TabViewRect", tabViewRect);
rect.top = rect.bottom + 15;
rect.bottom = rect.top + 15;
rect.right = rect.left + 200;
fStatusView = new BStringView(rect, "StringView", TEXT_NOT_CONNECTED);
rect.InsetBy(0, -5);
rect.left = rect.right + 5;
rect.right = bounds.right - 5;
fConnectButton = new BButton(rect, "ConnectButton", LABEL_CONNECT,
new BMessage(MSG_CONNECT_BUTTON));
AddChild(fMenuField);
AddChild(fTabView);
AddChild(fStatusView);
AddChild(fConnectButton);
CreateTabs();
SelectInterface(0);
}
DialUpView::~DialUpView()
{
SaveSettingsToFile();
int32 tmp;
wait_for_thread(fUpDownThread, &tmp);
// free known add-on types (these should free their known add-on types, etc.)
DialUpAddon *addon;
for(int32 index = 0;
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++)
delete addon;
}
void
DialUpView::AttachedToWindow()
{
fInterfaceMenu->SetTargetForItems(this);
fConnectButton->SetTarget(this);
if(fListener.InitCheck() != B_OK) {
(new BAlert(ERROR_TITLE, ERROR_NO_PPP_STACK, TEXT_OK))->Go();
// be_app->PostMessage(B_QUIT_REQUESTED);
}
}
void
DialUpView::MessageReceived(BMessage *message)
{
switch(message->what) {
case PPP_REPORT_MESSAGE:
HandleReportMessage(message);
break;
case MSG_CREATE_NEW: {
AddInterface("New interface");
if(fCurrentItem)
fCurrentItem->SetMarked(true);
} break;
case MSG_DELETE_CURRENT: {
fInterfaceMenu->RemoveItem(fCurrentItem);
delete fCurrentItem;
fCurrentItem = NULL;
if(CountInterfaces() == 0)
fInterfaceMenu->SetRadioMode(false);
else
SelectInterface(0);
} break;
case MSG_SELECT_INTERFACE: {
int32 index;
message->FindInt32("index", &index);
SelectInterface(index);
} break;
case MSG_CONNECT_BUTTON: {
if(!fCurrentItem || fUpDownThread != -1)
return;
fUpDownThread = spawn_thread(up_down_thread, "up_down_thread",
B_NORMAL_PRIORITY, this);
resume_thread(fUpDownThread);
} break;
default:
BView::MessageReceived(message);
}
}
void
DialUpView::LoadSettings()
{
BMessage settings;
// TODO: load settings
}
void
DialUpView::IsModified(bool& settings, bool& profile)
{
settings = profile = false;
bool addonSettingsChanged, addonProfileChanged;
// for current addon
DialUpAddon *addon;
for(int32 index = 0;
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++) {
if(!addon || !addon->IsValid())
continue;
addon->IsModified(addonSettingsChanged, addonProfileChanged);
if(addonSettingsChanged)
settings = true;
if(addonProfileChanged)
profile = true;
}
}
bool
DialUpView::SaveSettings(BMessage& settings, BMessage& profile, bool saveModified)
{
if(!fCurrentItem)
return false;
// create tabs for all registered and valid "Tab" add-ons
DialUpAddon *addon;
TemplateList<DialUpAddon*> addons;
for(int32 index = 0;
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++) {
if(!addon || !addon->IsValid())
continue;
int32 insertIndex = 0;
for(; insertIndex < addons.CountItems(); insertIndex++)
if(addons.ItemAt(insertIndex)->Priority() <= addon->Priority())
break;
addons.AddItem(addon, insertIndex);
}
settings.AddInt32("Interface", static_cast<int32>(fWatching));
if(fCurrentItem)
settings.AddString("Name", fCurrentItem->Label());
for(int32 index = 0; index < addons.CountItems(); index++)
addons.ItemAt(index)->SaveSettings(&settings, &profile, saveModified);
return true;
}
bool
DialUpView::SaveSettingsToFile()
{
bool settingsChanged, profileChanged;
IsModified(settingsChanged, profileChanged);
if(!settingsChanged && !profileChanged)
return true;
BMessage settings, profile;
if(!SaveSettings(settings, profile, false))
return false;
BDirectory settingsDirectory;
BDirectory profileDirectory;
GetPPPDirectories(&settingsDirectory, &profileDirectory);
if(settingsDirectory.InitCheck() != B_OK || profileDirectory.InitCheck() != B_OK)
return false;
BFile file;
if(settingsChanged) {
settingsDirectory.CreateFile(fCurrentItem->Label(), &file);
WriteMessageDriverSettings(file, settings);
}
if(profileChanged) {
profileDirectory.CreateFile(fCurrentItem->Label(), &file);
WriteMessageDriverSettings(file, profile);
}
return true;
}
void
DialUpView::UpDownThread()
{
PPPInterface interface;
if(fWatching == PPP_UNDEFINED_INTERFACE_ID) {
interface = fListener.Manager().InterfaceWithName(fCurrentItem->Label());
if(interface.InitCheck() != B_OK)
interface = fListener.Manager().CreateInterfaceWithName(
fCurrentItem->Label());
} else
interface = fWatching;
if(interface.InitCheck() != B_OK) {
Window()->Lock();
fStatusView->SetText(TEXT_CREATION_ERROR);
Window()->Unlock();
return;
}
ppp_interface_info_t info;
interface.GetInterfaceInfo(&info);
if(info.info.phase == PPP_DOWN_PHASE)
interface.Up();
else
interface.Down();
fUpDownThread = -1;
}
void
DialUpView::GetPPPDirectories(BDirectory *settingsDirectory,
BDirectory *profileDirectory) const
{
if(settingsDirectory) {
BDirectory settings(PPP_INTERFACE_SETTINGS_PATH);
if(settings.InitCheck() != B_OK) {
create_directory(PPP_INTERFACE_SETTINGS_PATH, 0750);
settings.SetTo(PPP_INTERFACE_SETTINGS_PATH);
}
*settingsDirectory = settings;
}
if(profileDirectory) {
BDirectory profile(PPP_INTERFACE_SETTINGS_PATH "/profile");
if(profile.InitCheck() != B_OK) {
create_directory(PPP_INTERFACE_SETTINGS_PATH "/profile", 0750);
profile.SetTo(PPP_INTERFACE_SETTINGS_PATH "/profile");
}
*profileDirectory = profile;
}
}
void
DialUpView::HandleReportMessage(BMessage *message)
{
thread_id sender;
message->FindInt32("sender", &sender);
send_data(sender, B_OK, NULL, 0);
if(!fCurrentItem)
return;
ppp_interface_id id;
if(message->FindInt32("interface", reinterpret_cast<int32*>(&id)) != B_OK
|| (fWatching != PPP_UNDEFINED_INTERFACE_ID && id != fWatching))
return;
int32 type, code;
message->FindInt32("type", &type);
message->FindInt32("code", &code);
if(type == PPP_MANAGER_REPORT && code == PPP_REPORT_INTERFACE_CREATED) {
PPPInterface interface(id);
if(interface.InitCheck() != B_OK)
return;
ppp_interface_info_t info;
interface.GetInterfaceInfo(&info);
if(strcasecmp(info.info.name, fCurrentItem->Label()))
return;
WatchInterface(id);
} else if(type == PPP_CONNECTION_REPORT) {
if(fWatching == PPP_UNDEFINED_INTERFACE_ID)
return;
UpdateStatus(code);
} else if(type == PPP_DESTRUCTION_REPORT) {
if(fWatching == PPP_UNDEFINED_INTERFACE_ID)
return;
WatchInterface(fListener.Manager().InterfaceWithName(fCurrentItem->Label()));
}
}
void
DialUpView::CreateTabs()
{
// create tabs for all registered and valid "Tab" add-ons
DialUpAddon *addon;
BView *target;
float width, height;
TemplateList<DialUpAddon*> addons;
for(int32 index = 0;
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++) {
if(!addon || !addon->IsValid() || addon->Position() < 0)
continue;
int32 insertIndex = 0;
for(; insertIndex < addons.CountItems(); insertIndex++)
if(addons.ItemAt(insertIndex)->Position() > addon->Position())
break;
addons.AddItem(addon, insertIndex);
}
for(int32 index = 0; index < addons.CountItems(); index++) {
addon = addons.ItemAt(index);
if(!addon->GetPreferredSize(&width, &height))
continue;
target = addon->CreateView(BPoint(0,0));
if(!target)
continue;
fTabView->AddTab(target, NULL);
}
}
void
DialUpView::UpdateStatus(int32 code)
{
switch(code) {
case PPP_REPORT_UP_ABORTED:
case PPP_REPORT_DOWN_SUCCESSFUL:
case PPP_REPORT_CONNECTION_LOST: {
fConnectButton->SetLabel(LABEL_CONNECT);
} break;
default:
fConnectButton->SetLabel(LABEL_DISCONNECT);
}
// maybe the information string must stay
if(fKeepLabel && code != PPP_REPORT_GOING_UP && code != PPP_REPORT_UP_SUCCESSFUL)
return;
switch(code) {
case PPP_REPORT_GOING_UP:
fKeepLabel = false;
fStatusView->SetText(TEXT_CONNECTING);
break;
case PPP_REPORT_UP_SUCCESSFUL:
fKeepLabel = false;
fStatusView->SetText(TEXT_CONNECTION_ESTABLISHED);
break;
case PPP_REPORT_UP_ABORTED:
case PPP_REPORT_DOWN_SUCCESSFUL:
fStatusView->SetText(TEXT_NOT_CONNECTED);
break;
case PPP_REPORT_LOCAL_AUTHENTICATION_REQUESTED:
case PPP_REPORT_PEER_AUTHENTICATION_REQUESTED:
fStatusView->SetText(TEXT_AUTHENTICATING);
break;
case PPP_REPORT_LOCAL_AUTHENTICATION_FAILED:
case PPP_REPORT_PEER_AUTHENTICATION_FAILED:
fKeepLabel = true;
fStatusView->SetText(TEXT_AUTHENTICATION_FAILED);
break;
case PPP_REPORT_CONNECTION_LOST:
fKeepLabel = true;
fStatusView->SetText(TEXT_CONNECTION_LOST);
break;
}
}
void
DialUpView::WatchInterface(ppp_interface_id ID)
{
if(fWatching == ID)
return;
fListener.StopWatchingInterfaces();
if(ID == PPP_UNDEFINED_INTERFACE_ID || fListener.WatchInterface(ID))
fWatching = ID;
// update status
PPPInterface interface(fWatching);
if(interface.InitCheck() != B_OK) {
UpdateStatus(PPP_REPORT_DOWN_SUCCESSFUL);
return;
}
ppp_interface_info_t info;
interface.GetInterfaceInfo(&info);
// transform phase into status
switch(info.info.phase) {
case PPP_DOWN_PHASE:
UpdateStatus(PPP_REPORT_DOWN_SUCCESSFUL);
break;
case PPP_TERMINATION_PHASE:
break;
case PPP_ESTABLISHED_PHASE:
UpdateStatus(PPP_REPORT_UP_SUCCESSFUL);
break;
default:
UpdateStatus(PPP_REPORT_GOING_UP);
}
}
void
DialUpView::AddInterface(const char *name)
{
if(fInterfaceMenu->FindItem(name)) {
(new BAlert(ERROR_TITLE, ERROR_INTERFACE_EXISTS, TEXT_OK))->Go();
return;
}
BMenuItem *item = new BMenuItem(name, new BMessage(MSG_SELECT_INTERFACE));
item->SetTarget(this);
fInterfaceMenu->AddItem(item, CountInterfaces());
if(CountInterfaces() == 1)
fInterfaceMenu->SetLabelFromMarked(true);
SelectInterface(CountInterfaces() - 1);
}
void
DialUpView::SelectInterface(int32 index)
{
BMenuItem *item = fInterfaceMenu->FindMarked();
if(fCurrentItem && item == fCurrentItem)
return;
SaveSettingsToFile();
if(index >= CountInterfaces() || index < 0) {
if(CountInterfaces() > 0)
SelectInterface(0);
else {
fCurrentItem = NULL;
WatchInterface(PPP_UNDEFINED_INTERFACE_ID);
}
} else {
fCurrentItem = fInterfaceMenu->ItemAt(index);
if(!fCurrentItem) {
SelectInterface(0);
return;
}
fCurrentItem->SetMarked(true);
WatchInterface(fListener.Manager().InterfaceWithName(fCurrentItem->Label()));
}
LoadSettings();
}
int32
DialUpView::CountInterfaces() const
{
return fInterfaceMenu->CountItems() - 3;
}
@@ -0,0 +1,65 @@
//-----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//
// Copyright (c) 2003-2004 Waldemar Kornewald, [email protected]
//-----------------------------------------------------------------------
#ifndef _DIAL_UP_VIEW__H
#define _DIAL_UP_VIEW__H
#include <View.h>
#include <Message.h>
#include <PPPInterfaceListener.h>
class DialUpView : public BView {
public:
DialUpView(BRect frame);
virtual ~DialUpView();
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage *message);
void UpDownThread();
private:
void GetPPPDirectories(BDirectory *settingsDirectory,
BDirectory *profileDirectory) const;
void HandleReportMessage(BMessage *message);
void CreateTabs();
void UpdateStatus(int32 code);
void WatchInterface(ppp_interface_id ID);
void LoadSettings();
void IsModified(bool& settings, bool& profile);
bool SaveSettings(BMessage& settings, BMessage& profile, bool saveTemporary);
bool SaveSettingsToFile();
void AddInterface(const char *name);
void SelectInterface(int32 index);
int32 CountInterfaces() const;
private:
PPPInterfaceListener fListener;
thread_id fUpDownThread;
BMessage fAddons, fSettings;
driver_settings *fDriverSettings;
BMenuItem *fCurrentItem;
ppp_interface_id fWatching;
bool fKeepLabel;
BStringView *fStatusView;
BButton *fConnectButton;
BPopUpMenu *fInterfaceMenu;
BMenuField *fMenuField;
BTabView *fTabView;
};
#endif
@@ -0,0 +1,344 @@
//-----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//
// Copyright (c) 2003-2004 Waldemar Kornewald, [email protected]
//-----------------------------------------------------------------------
#include "GeneralAddon.h"
#include "MessageDriverSettingsUtils.h"
#include <Box.h>
#include <Button.h>
#include <MenuField.h>
#include <MenuItem.h>
#include <Messenger.h>
#include <PopUpMenu.h>
#include <StringView.h>
#include <PPPDefs.h>
GeneralAddon::GeneralAddon(BMessage *addons)
: DialUpAddon(addons),
fHasDevice(false),
fHasPassword(false),
fAuthenticatorsCount(0),
fSettings(NULL),
fProfile(NULL)
{
BRect rect;
addons->FindRect("TabViewRect", &rect);
fGeneralView = new GeneralView(this, rect);
}
GeneralAddon::~GeneralAddon()
{
}
bool
GeneralAddon::LoadSettings(BMessage *settings, BMessage *profile)
{
fHasDevice = fHasPassword = false;
fDevice = fUsername = fPassword = "";
fAuthenticatorsCount = 0;
fSettings = settings;
fProfile = profile;
if(!settings)
return true;
if(!LoadDeviceSettings(settings, profile))
return false;
if(!LoadAuthenticationSettings(settings, profile))
return false;
return true;
}
bool
GeneralAddon::LoadDeviceSettings(BMessage *settings, BMessage *profile)
{
fHasDevice = false;
fDevice = "";
return true;
}
bool
GeneralAddon::LoadAuthenticationSettings(BMessage *settings, BMessage *profile)
{
fHasPassword = false;
fUsername = fPassword = "";
fAuthenticatorsCount = 0;
// we only handle the profile (although settings could contain different data)
BMessage authentication, *item = FindMessageParameter(PPP_AUTHENTICATOR_KEY,
*profile);
if(!item)
return true;
// find authenticators (though we load all authenticators, we only use one)
BString name;
for(int32 index = 0; item->FindString("values", index, &name) == B_OK; index++) {
BMessage authenticator;
if(!GetAuthenticator(name, &authenticator))
return false;
// fatal error: we do not know how to handle this authenticator
MarkAuthenticatorAsValid(name);
authentication.AddString("Authenticators", name);
++fAuthenticatorsCount;
}
fSettings->AddMessage("Authentication", &authentication);
bool hasUsername = false;
// a username must be present
// load username and password
BMessage *parameter = FindMessageParameter("username", *item);
if(parameter && parameter->FindString("values", &fUsername) == B_OK) {
hasUsername = true;
parameter->AddBool("Valid", true);
}
parameter = FindMessageParameter("password", *item);
if(parameter && parameter->FindString("values", &fPassword) == B_OK) {
fHasPassword = true;
parameter->AddBool("Valid", true);
}
// tell DUN whether everything is valid
if(hasUsername)
item->AddBool("Valid", true);
return true;
}
void
GeneralAddon::IsModified(bool& settings, bool& profile) const
{
if(!fSettings) {
settings = profile = false;
return;
}
bool deviceSettings, authenticationSettings, deviceProfile, authenticationProfile;
IsDeviceModified(deviceSettings, deviceProfile);
IsAuthenticationModified(authenticationSettings, authenticationProfile);
settings = (deviceSettings || authenticationSettings);
profile = (deviceProfile || authenticationProfile);
}
void
GeneralAddon::IsDeviceModified(bool& settings, bool& profile) const
{
if(fHasDevice)
settings = (!fGeneralView->DeviceName() || fGeneralView->IsDeviceModified());
else
settings = fGeneralView->DeviceName();
profile = false;
}
void
GeneralAddon::IsAuthenticationModified(bool& settings, bool& profile) const
{
// currently we only support selecting one authenticator
if(fAuthenticatorsCount == 0)
settings = fGeneralView->AuthenticatorName();
else {
BMessage authentication;
if(fSettings->FindMessage("Authentication", &authentication) != B_OK) {
settings = profile = false;
return;
// error!
}
BString authenticator;
if(authentication.FindString("Authenticators", &authenticator) != B_OK) {
settings = profile = false;
return;
// error!
}
settings = (!fGeneralView->AuthenticatorName()
|| authenticator != fGeneralView->AuthenticatorName());
}
profile = (fUsername != fGeneralView->Username()
|| (fPassword != fGeneralView->Password() && fHasPassword)
|| fHasPassword != fGeneralView->DoesSavePassword());
}
bool
GeneralAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTemporary)
{
if(!fSettings || !settings)
return false;
// TODO: save settings
return true;
}
bool
GeneralAddon::GetPreferredSize(float *width, float *height) const
{
if(!fSettings)
return false;
BRect rect(0,0,200,300);
// set default values
Addons()->FindRect("TabViewRect", &rect);
if(width)
*width = rect.Width();
if(height)
*height = rect.Height();
return true;
}
BView*
GeneralAddon::CreateView(BPoint leftTop)
{
if(!fSettings)
return NULL;
BRect rect(0,0,200,300);
// set default values
Addons()->FindRect("TabViewRect", &rect);
fGeneralView->Reload();
return fGeneralView;
}
bool
GeneralAddon::GetAuthenticator(const BString& moduleName, BMessage *entry) const
{
if(!entry)
return false;
BString name;
for(int32 index = 0; Addons()->FindMessage("Authenticator", index, entry) == B_OK;
index++) {
entry->FindString("KernelModuleName", &name);
if(name == moduleName)
return true;
}
return false;
}
bool
GeneralAddon::MarkAuthenticatorAsValid(const BString& moduleName)
{
BMessage *authenticator;
int32 index = 0;
BString name;
for(; (authenticator = FindMessageParameter(PPP_AUTHENTICATOR_KEY, *fSettings,
&index)); index++) {
authenticator->FindString("KernelModuleName", &name);
if(name == moduleName) {
authenticator->AddBool("Valid", true);
return true;
}
}
return false;
}
GeneralView::GeneralView(GeneralAddon *addon, BRect frame)
: BView(frame, "General", B_FOLLOW_NONE, 0),
fAddon(addon)
{
BRect rect = Bounds();
rect.InsetBy(5, 5);
rect.bottom = 100;
fDeviceBox = new BBox(rect, "Device");
rect.top = rect.bottom + 10;
rect.bottom = rect.top
+ 25 // space for topmost control
+ 2 * 20 // size of controls
+ 2 * 5; // space beween controls and bottom of box
fAuthenticationBox = new BBox(rect, "Authentication");
BString deviceLabel("Device: ");
deviceLabel << "PPPoE";
// TODO: get real device name
BStringView *deviceLabelView = new BStringView(BRect(0,0,250,25), "Device",
deviceLabel.String());
fDeviceBox->SetLabel(deviceLabelView);
fAuthenticator = new BMenuField(BRect(5,0,250,25), "Authenticator",
"Authenticator:", new BPopUpMenu("No Authenticators Found!"));
fAuthenticator->SetDivider(fAuthenticator->StringWidth(
fAuthenticator->Label()) + 10);
fAuthenticationBox->SetLabel(fAuthenticator);
rect = fAuthenticationBox->Bounds();
rect.InsetBy(10, 0);
rect.top = 25;
rect.bottom = rect.top + 20;
fUsername = new BTextControl(rect, "username", "Name: ", NULL, NULL);
rect.top = rect.bottom + 5;
rect.bottom = rect.top + 20;
fPassword = new BTextControl(rect, "password", "Password: ", NULL, NULL);
fPassword->TextView()->HideTyping(true);
fAuthenticationBox->AddChild(fUsername);
fAuthenticationBox->AddChild(fPassword);
AddChild(fDeviceBox);
AddChild(fAuthenticationBox);
}
GeneralView::~GeneralView()
{
}
void
GeneralView::Reload()
{
}
void
GeneralView::AttachedToWindow()
{
SetViewColor(Parent()->ViewColor());
}
void
GeneralView::MessageReceived(BMessage *message)
{
switch(message->what) {
default:
BView::MessageReceived(message);
}
}
@@ -0,0 +1,98 @@
//-----------------------------------------------------------------------
// This software is part of the OpenBeOS distribution and is covered
// by the OpenBeOS license.
//
// Copyright (c) 2003-2004 Waldemar Kornewald, [email protected]
//-----------------------------------------------------------------------
#ifndef _GENERAL_ADDON__H
#define _GENERAL_ADDON__H
#include <DialUpAddon.h>
#include <View.h>
#include <Message.h>
#include <String.h>
#include <TextControl.h>
class GeneralView;
class GeneralAddon : public DialUpAddon {
public:
GeneralAddon(BMessage *addons);
virtual ~GeneralAddon();
const char *Username() const
{ return fUsername.String(); }
const char *Password() const
{ return fPassword.String(); }
virtual int32 Position() const
{ return 0; }
virtual bool LoadSettings(BMessage *settings, BMessage *profile);
bool LoadDeviceSettings(BMessage *settings, BMessage *profile);
bool LoadAuthenticationSettings(BMessage *settings, BMessage *profile);
virtual void IsModified(bool& settings, bool& profile) const;
void IsDeviceModified(bool& settings, bool& profile) const;
void IsAuthenticationModified(bool& settings, bool& profile) const;
virtual bool SaveSettings(BMessage *settings, BMessage *profile,
bool saveTemporary);
virtual bool GetPreferredSize(float *width, float *height) const;
virtual BView *CreateView(BPoint leftTop);
private:
bool GetAuthenticator(const BString& moduleName, BMessage *entry) const;
bool MarkAuthenticatorAsValid(const BString& moduleName);
private:
bool fHasDevice, fHasPassword;
BString fDevice, fUsername, fPassword;
int32 fAuthenticatorsCount;
BMessage *fSettings, *fProfile;
// saves last settings state
GeneralView *fGeneralView;
};
class GeneralView : public BView {
public:
GeneralView(GeneralAddon *addon, BRect frame);
virtual ~GeneralView();
GeneralAddon *Addon() const
{ return fAddon; }
void Reload();
const char *Username() const
{ return fUsername->Text(); }
const char *Password() const
{ return fPassword->Text(); }
bool DoesSavePassword() const
{ return true; }
// TODO: implement me!
const char *DeviceName() const
{ return NULL; }
// TODO: implement me!
const char *AuthenticatorName() const
{ return NULL; }
// TODO: implement me!
bool IsDeviceModified() const
{ return false; }
// TODO: implement me!
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage *message);
private:
GeneralAddon *fAddon;
BBox *fDeviceBox, *fAuthenticationBox;
BMenuField *fAuthenticator;
BTextControl *fUsername, *fPassword;
};
#endif
+18
View File
@@ -0,0 +1,18 @@
SubDir OBOS_TOP src tests kits net DialUpPreflet ;
UsePrivateHeaders net ;
UseHeaders [ FDirName $(OBOS_TOP) src add-ons kernel network ppp shared libppp headers ] ;
UseHeaders [ FDirName $(OBOS_TOP) src add-ons kernel network ppp shared libkernelppp headers ] ;
SimpleTest DialUpPreflet :
DialUpApplication.cpp
DialUpView.cpp
# utils
MessageDriverSettingsUtils.cpp
# built-in add-ons
GeneralAddon.cpp
;
LinkSharedOSLibs DialUpPreflet : be libppp.a ;
@@ -0,0 +1,142 @@
#include "MessageDriverSettingsUtils.h"
#include <driver_settings.h>
#include <File.h>
#include <Message.h>
#include <String.h>
#include <TypeConstants.h>
#include <cstdio>
BMessage*
FindMessageParameter(const char *name, BMessage& message, int32 *startIndex = NULL)
{
// XXX: this is a hack and should be removed when we can replace BMessage with
// something better
const BMessage *parameter;
BString string;
ssize_t size;
int32 index = startIndex ? *startIndex : 0;
for(; message.FindData("parameters", B_MESSAGE_TYPE, index,
reinterpret_cast<const void**>(&parameter), &size) == B_OK;
index++) {
if(parameter->FindString("name", &string) == B_OK
&& string.ICompare(name)) {
if(startIndex)
*startIndex = index;
return const_cast<BMessage*>(parameter);
}
}
return NULL;
}
static
bool
AddParameter(const driver_parameter *parameter, BMessage& message)
{
if(!parameter)
return false;
if(parameter->name)
message.AddString("name", parameter->name);
else
return false;
for(int32 index = 0; index < parameter->value_count; index++)
if(parameter->values[index])
message.AddString("values", parameter->values[index]);
for(int32 index = 0; index < parameter->parameter_count; index++) {
BMessage parameterMessage;
AddParameter(&parameter->parameters[index], parameterMessage);
message.AddMessage("parameters", &parameterMessage);
}
return true;
}
bool
ReadMessageDriverSettings(const char *name, BMessage& message)
{
if(!name)
return false;
void *handle = load_driver_settings(name);
const driver_settings *settings = get_driver_settings(handle);
if(!settings) {
unload_driver_settings(handle);
return false;
}
for(int32 index = 0; index < settings->parameter_count; index++) {
BMessage parameter;
AddParameter(&settings->parameters[index], parameter);
message.AddMessage("parameters", &parameter);
}
unload_driver_settings(handle);
return true;
}
static
bool
WriteParameter(BFile& file, const BMessage& parameter, int32 level)
{
const char *name;
if(parameter.FindString("name", &name) != B_OK || !name)
return false;
BString line;
line.SetTo('\t', level);
line << name << ' ';
for(int32 index = 0; parameter.FindString("values", index, &name) == B_OK; index++)
if(name)
line << name << ' ';
type_code type;
int32 parameterCount;
parameter.GetInfo("parameters", &type, &parameterCount);
if(parameterCount > 0)
line << '{';
line << '\n';
file.Write(line.String(), line.Length());
if(parameterCount > 0) {
BMessage subParameter;
for(int32 index = 0; parameter.FindMessage("parameters", index,
&subParameter) == B_OK; index++)
WriteParameter(file, subParameter, level + 1);
line.SetTo('\t', level);
line << '\n';
file.Write(line.String(), line.Length());
}
return true;
}
bool
WriteMessageDriverSettings(BFile& file, const BMessage& message)
{
if(file.InitCheck() != B_OK || !file.IsWritable())
return false;
file.Seek(0, SEEK_SET);
BMessage parameter;
for(int32 index = 0; message.FindMessage("parameters", index, &parameter) == B_OK;
index++)
WriteParameter(file, parameter, 0);
return true;
}
@@ -0,0 +1,16 @@
#ifndef _MESSAGE_DRIVER_SETTINGS_UTILS__H
#define _MESSAGE_DRIVER_SETTINGS_UTILS__H
#include <SupportDefs.h>
class BMessage;
class BFile;
extern BMessage *FindMessageParameter(const char *name, BMessage& message,
int32 *startIndex = NULL);
extern bool ReadMessageDriverSettings(const char *name, BMessage& message);
extern bool WriteMessageDriverSettings(BFile& file, const BMessage& message);
#endif
+15
View File
@@ -0,0 +1,15 @@
BUGs/Hacks:
- FIXME: the MessageDriverSettings API uses a little hack to allow adding new entries to sub-messages without replacing the old one
Short-term TODOs:
- add "Protocols" tab
- add "Extras" tab for changing dial-behaviour (auto-redial, dial-on-demand, etc.)
- add "Revert changes" button
- load add-ons
- write IP (IPCP) protocol add-on
- write PAP authenticator add-on
- write PPPoE device add-on
- move DEVNOTES into a doxygen file and document the rest of the API
Long-term TODOs:
- allow selecting multiple authenticators
+1
View File
@@ -13,6 +13,7 @@ SubDir OBOS_TOP src tests kits net ;
# SimpleTest at_client : at_client.c : ;
SubInclude OBOS_TOP src tests kits net DialUpPreflet ;
SubInclude OBOS_TOP src tests kits net netperf ;
# SubInclude OBOS_TOP src tests kits net new_stack ;
SubInclude OBOS_TOP src tests kits net preflet ;