Some more work on the DialUpPreflet code.

git-svn-id: file:///srv/svn/repos/haiku/trunk/current@6927 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Waldemar Kornewald 2004-03-07 13:51:57 +00:00
parent 44c1e167fc
commit bb713c7c5b
9 changed files with 556 additions and 164 deletions

View File

@ -24,6 +24,11 @@ Devices:
Protocols:
"Protocol" : pointer to DialUpAddon object
Addons that must be deleted:
"DeleteMe" : pointer to DialUpAddon object - this will be deleted on exit (you should register all your
addons here, too)
--------------------------------------------------------------------------------------
Additional data in the BMessage object for add-ons:
--------------------------------------------------------------------------------------

View File

@ -30,10 +30,6 @@ class 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; }
@ -49,10 +45,8 @@ class DialUpAddon {
{ return 0; }
// allows to set order in which modules are asked to add the settings
virtual bool LoadSettings(BMessage *settings, BMessage *profile)
virtual bool LoadSettings(BMessage *settings, BMessage *profile, bool isNew)
{ 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,
@ -73,7 +67,6 @@ class DialUpAddon {
private:
BMessage *fAddons;
bool fIsValid;
char _reserved[31];
};

View File

@ -54,10 +54,14 @@
#define TEXT_AUTHENTICATION_FAILED "Authentication failed!"
#define TEXT_CONNECTION_LOST "Connection lost!"
#define TEXT_CREATION_ERROR "Error creating interface!"
#define TEXT_NO_INTERFACE_SELECTED "No interface selected..."
#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!"
#define ERROR_INTERFACE_EXISTS "Error: An interface with this name already " \
"exists!"
#define ERROR_LOADING_FAILED "Error: Failed loading interface! The current " \
"settings will be deleted."
static
@ -86,13 +90,6 @@ DialUpView::DialUpView(BRect frame)
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;
@ -101,23 +98,6 @@ DialUpView::DialUpView(BRect frame)
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
@ -127,10 +107,16 @@ DialUpView::DialUpView(BRect frame)
tabViewRect.bottom -= fTabView->TabHeight();
fAddons.AddRect("TabViewRect", tabViewRect);
// BRect noInterfacesRect(rect);
// noInterfacesRect.top = rect.top + rect.Width() / 2;
// noInterfacesRect.bottom = noInterfacesRect.top + 15;
// fNoInterfacesStringView = new BStringView(noInterfacesRect, "NoInterfacesView",
// TEXT_NO_INTERFACE_SELECTED);
rect.top = rect.bottom + 15;
rect.bottom = rect.top + 15;
rect.right = rect.left + 200;
fStatusView = new BStringView(rect, "StringView", TEXT_NOT_CONNECTED);
fStatusView = new BStringView(rect, "StatusView", TEXT_NOT_CONNECTED);
rect.InsetBy(0, -5);
rect.left = rect.right + 5;
@ -143,8 +129,12 @@ DialUpView::DialUpView(BRect frame)
AddChild(fStatusView);
AddChild(fConnectButton);
// initialize
LoadInterfaces();
LoadAddons();
CreateTabs();
fCurrentItem = NULL;
// reset, otherwise SelectInterface will not load the settings
SelectInterface(0);
}
@ -159,7 +149,7 @@ DialUpView::~DialUpView()
// free known add-on types (these should free their known add-on types, etc.)
DialUpAddon *addon;
for(int32 index = 0;
fAddons.FindPointer("Tab", index,
fAddons.FindPointer("DeleteMe", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++)
delete addon;
@ -188,7 +178,7 @@ DialUpView::MessageReceived(BMessage *message)
break;
case MSG_CREATE_NEW: {
AddInterface("New interface");
AddInterface("New interface", true);
if(fCurrentItem)
fCurrentItem->SetMarked(true);
} break;
@ -225,12 +215,38 @@ DialUpView::MessageReceived(BMessage *message)
}
void
DialUpView::LoadSettings()
bool
DialUpView::LoadSettings(bool isNew)
{
BMessage settings;
fSettings.MakeEmpty();
fProfile.MakeEmpty();
BMessage *settingsPointer = fCurrentItem ? &fSettings : NULL,
*profilePointer = fCurrentItem ? &fProfile : NULL;
// TODO: load settings
if(fCurrentItem && !isNew) {
BString name("pppidf/");
name << fCurrentItem->Label();
if(!ReadMessageDriverSettings(name.String(), fSettings))
return false;
name = "pppidf/profile/";
name << fCurrentItem->Label();
if(!ReadMessageDriverSettings(name.String(), fProfile))
profilePointer = settingsPointer;
}
DialUpAddon *addon;
for(int32 index = 0; fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK; index++) {
if(!addon)
continue;
if(!addon->LoadSettings(settingsPointer, profilePointer, isNew))
return false;
}
// TODO: check if settings are valid
return true;
}
@ -242,11 +258,9 @@ DialUpView::IsModified(bool& settings, bool& profile)
// for current addon
DialUpAddon *addon;
for(int32 index = 0;
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++) {
if(!addon || !addon->IsValid())
for(int32 index = 0; fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK; index++) {
if(!addon)
continue;
addon->IsModified(addonSettingsChanged, addonProfileChanged);
@ -271,7 +285,7 @@ DialUpView::SaveSettings(BMessage& settings, BMessage& profile, bool saveModifie
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++) {
if(!addon || !addon->IsValid())
if(!addon)
continue;
int32 insertIndex = 0;
@ -284,7 +298,7 @@ DialUpView::SaveSettings(BMessage& settings, BMessage& profile, bool saveModifie
settings.AddInt32("Interface", static_cast<int32>(fWatching));
if(fCurrentItem)
settings.AddString("Name", fCurrentItem->Label());
settings.AddString("InterfaceName", fCurrentItem->Label());
for(int32 index = 0; index < addons.CountItems(); index++)
addons.ItemAt(index)->SaveSettings(&settings, &profile, saveModified);
@ -440,7 +454,7 @@ DialUpView::CreateTabs()
fAddons.FindPointer("Tab", index,
reinterpret_cast<void**>(&addon)) == B_OK;
index++) {
if(!addon || !addon->IsValid() || addon->Position() < 0)
if(!addon || addon->Position() < 0)
continue;
int32 insertIndex = 0;
@ -522,13 +536,14 @@ DialUpView::UpdateStatus(int32 code)
void
DialUpView::WatchInterface(ppp_interface_id ID)
{
if(fWatching == ID)
return;
// This method can be used to update the interface's connection status.
if(fWatching != ID) {
fListener.StopWatchingInterfaces();
if(ID == PPP_UNDEFINED_INTERFACE_ID || fListener.WatchInterface(ID))
fWatching = ID;
}
// update status
PPPInterface interface(fWatching);
@ -560,7 +575,50 @@ DialUpView::WatchInterface(ppp_interface_id ID)
void
DialUpView::AddInterface(const char *name)
DialUpView::LoadInterfaces()
{
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(), true);
}
}
}
void
DialUpView::LoadAddons()
{
// Load integrated add-ons:
// "General" tab
GeneralAddon *generalAddon = new GeneralAddon(&fAddons);
fAddons.AddPointer("Tab", generalAddon);
fAddons.AddPointer("DeleteMe", generalAddon);
// "PAP" authenticator
BMessage addon;
addon.AddString("KernelModuleName", "pap");
addon.AddString("FriendlyName", "Plain-text Authentication");
addon.AddString("TechnicalName", "PAP");
fAddons.AddMessage("Authenticator", &addon);
// addon.MakeEmpty();
// TODO:
// load all add-ons from the add-ons folder
}
void
DialUpView::AddInterface(const char *name, bool isNew = false)
{
if(fInterfaceMenu->FindItem(name)) {
(new BAlert(ERROR_TITLE, ERROR_INTERFACE_EXISTS, TEXT_OK))->Go();
@ -572,12 +630,12 @@ DialUpView::AddInterface(const char *name)
fInterfaceMenu->AddItem(item, CountInterfaces());
if(CountInterfaces() == 1)
fInterfaceMenu->SetLabelFromMarked(true);
SelectInterface(CountInterfaces() - 1);
SelectInterface(CountInterfaces() - 1, isNew);
}
void
DialUpView::SelectInterface(int32 index)
DialUpView::SelectInterface(int32 index, bool isNew = false)
{
BMenuItem *item = fInterfaceMenu->FindMarked();
if(fCurrentItem && item == fCurrentItem)
@ -593,6 +651,13 @@ DialUpView::SelectInterface(int32 index)
WatchInterface(PPP_UNDEFINED_INTERFACE_ID);
}
} else {
if(!fCurrentItem) {
fTabView->Show();
// fTabView->MoveBy(-Bounds().Width(), 0);
// fNoInterfacesStringView->MoveBy(Bounds().Width(), 0);
fConnectButton->SetEnabled(true);
}
fCurrentItem = fInterfaceMenu->ItemAt(index);
if(!fCurrentItem) {
SelectInterface(0);
@ -603,7 +668,21 @@ DialUpView::SelectInterface(int32 index)
WatchInterface(fListener.Manager().InterfaceWithName(fCurrentItem->Label()));
}
LoadSettings();
if(!fCurrentItem) {
LoadSettings(false);
// tell modules to unload all settings
fTabView->Hide();
// fTabView->MoveBy(Bounds().Width(), 0);
// fNoInterfacesStringView->MoveBy(-Bounds().Width(), 0);
fConnectButton->SetEnabled(false);
} else if(isNew && !LoadSettings(true)) {
(new BAlert(ERROR_TITLE, ERROR_LOADING_FAILED, TEXT_OK))->Go();
LoadSettings(true);
} else if(!LoadSettings(false)) {
(new BAlert(ERROR_TITLE, ERROR_LOADING_FAILED, TEXT_OK))->Go();
LoadSettings(true);
}
}

View File

@ -34,13 +34,16 @@ class DialUpView : public BView {
void UpdateStatus(int32 code);
void WatchInterface(ppp_interface_id ID);
void LoadSettings();
bool LoadSettings(bool isNew);
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);
void LoadInterfaces();
void LoadAddons();
void AddInterface(const char *name, bool isNew = false);
void SelectInterface(int32 index, bool isNew = false);
int32 CountInterfaces() const;
private:
@ -48,7 +51,7 @@ class DialUpView : public BView {
thread_id fUpDownThread;
BMessage fAddons, fSettings;
BMessage fAddons, fSettings, fProfile;
driver_settings *fDriverSettings;
BMenuItem *fCurrentItem;
ppp_interface_id fWatching;
@ -59,6 +62,7 @@ class DialUpView : public BView {
BPopUpMenu *fInterfaceMenu;
BMenuField *fMenuField;
BTabView *fTabView;
// BStringView *fNoInterfacesStringView;
};

View File

@ -4,6 +4,9 @@
//
// Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de
//-----------------------------------------------------------------------
// GeneralAddon saves the loaded settings.
// GeneralView saves the current settings.
//-----------------------------------------------------------------------
#include "GeneralAddon.h"
@ -20,9 +23,12 @@
#include <PPPDefs.h>
#define MSG_SELECT_DEVICE 'SELD'
#define MSG_SELECT_AUTHENTICATOR 'SELA'
GeneralAddon::GeneralAddon(BMessage *addons)
: DialUpAddon(addons),
fHasDevice(false),
fHasPassword(false),
fAuthenticatorsCount(0),
fSettings(NULL),
@ -39,15 +45,30 @@ GeneralAddon::~GeneralAddon()
}
bool
GeneralAddon::LoadSettings(BMessage *settings, BMessage *profile)
DialUpAddon*
GeneralAddon::FindDevice(const BString& moduleName) const
{
fHasDevice = fHasPassword = false;
fDevice = fUsername = fPassword = "";
DialUpAddon *addon;
for(int32 index = 0; Addons()->FindPointer("Device", index,
reinterpret_cast<void**>(addon)); index++)
if(addon && moduleName == addon->KernelModuleName())
return addon;
return NULL;
}
bool
GeneralAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew)
{
fIsNew = isNew;
fHasPassword = false;
fDeviceName = fUsername = fPassword = "";
fDeviceAddon = NULL;
fAuthenticatorsCount = 0;
fSettings = settings;
fProfile = profile;
if(!settings)
if(!settings || !profile || isNew)
return true;
if(!LoadDeviceSettings(settings, profile))
@ -63,30 +84,40 @@ GeneralAddon::LoadSettings(BMessage *settings, BMessage *profile)
bool
GeneralAddon::LoadDeviceSettings(BMessage *settings, BMessage *profile)
{
fHasDevice = false;
fDevice = "";
int32 index = 0;
BMessage device;
if(!FindMessageParameter(PPP_DEVICE_KEY, *settings, device, &index))
return false;
// TODO: tell user that device specification is missing
return true;
if(!device.FindString("Values", &fDeviceName))
return false;
// TODO: tell user that device specification is missing
device.AddBool("Valid", true);
settings->ReplaceMessage("Parameters", index, &device);
fDeviceAddon = FindDevice(fDeviceName);
if(!fDeviceAddon)
return false;
return fDeviceAddon->LoadSettings(settings, profile, false);
}
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);
int32 itemIndex = 0;
BMessage authentication, item;
if(!item)
if(!FindMessageParameter(PPP_AUTHENTICATOR_KEY, *profile, item, &itemIndex))
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++) {
for(int32 index = 0; item.FindString("Values", index, &name) == B_OK; index++) {
BMessage authenticator;
if(!GetAuthenticator(name, &authenticator))
return false;
@ -103,21 +134,28 @@ GeneralAddon::LoadAuthenticationSettings(BMessage *settings, BMessage *profile)
// a username must be present
// load username and password
BMessage *parameter = FindMessageParameter("username", *item);
if(parameter && parameter->FindString("values", &fUsername) == B_OK) {
BMessage parameter;
int32 parameterIndex = 0;
if(FindMessageParameter("User", item, parameter, &parameterIndex)
&& parameter.FindString("Values", &fUsername) == B_OK) {
hasUsername = true;
parameter->AddBool("Valid", true);
parameter.AddBool("Valid", true);
item.ReplaceMessage("Parameters", parameterIndex, &parameter);
}
parameter = FindMessageParameter("password", *item);
if(parameter && parameter->FindString("values", &fPassword) == B_OK) {
parameterIndex = 0;
if(FindMessageParameter("Password", item, parameter, &parameterIndex)
&& parameter.FindString("Values", &fPassword) == B_OK) {
fHasPassword = true;
parameter->AddBool("Valid", true);
parameter.AddBool("Valid", true);
item.ReplaceMessage("Parameters", parameterIndex, &parameter);
}
// tell DUN whether everything is valid
if(hasUsername)
item->AddBool("Valid", true);
item.AddBool("Valid", true);
profile->ReplaceMessage("Parameters", itemIndex, &item);
return true;
}
@ -144,7 +182,7 @@ GeneralAddon::IsModified(bool& settings, bool& profile) const
void
GeneralAddon::IsDeviceModified(bool& settings, bool& profile) const
{
if(fHasDevice)
if(fDeviceAddon)
settings = (!fGeneralView->DeviceName() || fGeneralView->IsDeviceModified());
else
settings = fGeneralView->DeviceName();
@ -187,22 +225,41 @@ GeneralAddon::IsAuthenticationModified(bool& settings, bool& profile) const
bool
GeneralAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTemporary)
{
if(!fSettings || !settings)
if(!fSettings || !settings || !fGeneralView->DeviceName())
return false;
// TODO: tell user that a device is needed (if we fail because of this)
if(fGeneralView->AuthenticatorName()) {
BMessage authenticator;
authenticator.AddString("Name", PPP_AUTHENTICATOR_KEY);
authenticator.AddString("Values", fGeneralView->AuthenticatorName());
BMessage username;
username.AddString("Name", "User");
username.AddString("Values", fGeneralView->Username());
authenticator.AddMessage("Parameters", &username);
if(saveTemporary || fGeneralView->DoesSavePassword()) {
// save password, too
BMessage password;
password.AddString("Name", "Password");
password.AddString("Values", fGeneralView->Password());
authenticator.AddMessage("Parameters", &password);
}
}
DialUpAddon *addon = FindDevice(fGeneralView->DeviceName());
if(!addon)
return false;
// TODO: save settings
return true;
return addon->SaveSettings(settings, profile, saveTemporary);
}
bool
GeneralAddon::GetPreferredSize(float *width, float *height) const
{
if(!fSettings)
return false;
BRect rect(0,0,200,300);
BRect rect(0, 0, 200, 300);
// set default values
Addons()->FindRect("TabViewRect", &rect);
@ -218,13 +275,12 @@ GeneralAddon::GetPreferredSize(float *width, float *height) const
BView*
GeneralAddon::CreateView(BPoint leftTop)
{
if(!fSettings)
return NULL;
BRect rect(0,0,200,300);
BRect rect;
if(Addons()->FindRect("TabViewRect", &rect) != B_OK);
rect.Set(0, 0, 200, 300);
// set default values
Addons()->FindRect("TabViewRect", &rect);
fGeneralView->MoveTo(leftTop);
fGeneralView->Reload();
return fGeneralView;
}
@ -251,15 +307,16 @@ GeneralAddon::GetAuthenticator(const BString& moduleName, BMessage *entry) const
bool
GeneralAddon::MarkAuthenticatorAsValid(const BString& moduleName)
{
BMessage *authenticator;
BMessage authenticator;
int32 index = 0;
BString name;
for(; (authenticator = FindMessageParameter(PPP_AUTHENTICATOR_KEY, *fSettings,
&index)); index++) {
authenticator->FindString("KernelModuleName", &name);
for(; FindMessageParameter(PPP_AUTHENTICATOR_KEY, *fSettings, authenticator,
&index); index++) {
authenticator.FindString("KernelModuleName", &name);
if(name == moduleName) {
authenticator->AddBool("Valid", true);
authenticator.AddBool("Valid", true);
fSettings->ReplaceMessage("Parameters", index, &authenticator);
return true;
}
}
@ -279,22 +336,24 @@ GeneralView::GeneralView(GeneralAddon *addon, BRect frame)
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
+ 3 * 20 // size of controls
+ 3 * 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);
fDeviceField = new BMenuField(BRect(5, 0, 250, 25), "Device",
"Device:", new BPopUpMenu("No Devices Found!"));
fDeviceField->SetDivider(fDeviceField->StringWidth(fDeviceField->Label()) + 10);
fDeviceField->Menu()->SetRadioMode(true);
AddDevices();
fDeviceBox->SetLabel(fDeviceField);
fAuthenticator = new BMenuField(BRect(5,0,250,25), "Authenticator",
fAuthenticatorField = new BMenuField(BRect(5, 0, 250, 25), "Authenticator",
"Authenticator:", new BPopUpMenu("No Authenticators Found!"));
fAuthenticator->SetDivider(fAuthenticator->StringWidth(
fAuthenticator->Label()) + 10);
fAuthenticationBox->SetLabel(fAuthenticator);
fAuthenticatorField->SetDivider(fAuthenticatorField->StringWidth(
fAuthenticatorField->Label()) + 10);
fAuthenticatorField->Menu()->SetRadioMode(true);
AddAuthenticators();
fAuthenticationBox->SetLabel(fAuthenticatorField);
rect = fAuthenticationBox->Bounds();
rect.InsetBy(10, 0);
@ -305,9 +364,13 @@ GeneralView::GeneralView(GeneralAddon *addon, BRect frame)
rect.bottom = rect.top + 20;
fPassword = new BTextControl(rect, "password", "Password: ", NULL, NULL);
fPassword->TextView()->HideTyping(true);
rect.top = rect.bottom + 5;
rect.bottom = rect.top + 20;
fSavePassword = new BCheckBox(rect, "SavePassword", "Save Password", NULL);
fAuthenticationBox->AddChild(fUsername);
fAuthenticationBox->AddChild(fPassword);
fAuthenticationBox->AddChild(fSavePassword);
AddChild(fDeviceBox);
AddChild(fAuthenticationBox);
@ -316,13 +379,95 @@ GeneralView::GeneralView(GeneralAddon *addon, BRect frame)
GeneralView::~GeneralView()
{
// TODO: delete device add-on
}
void
GeneralView::Reload()
{
// TODO: reload settings:
// - select device and authenticator
// - add device view (and move authenticator view to fit)
// - set username, password, and checkbox
fDeviceAddon = NULL;
BMenuItem *item;
for(int32 index = 0; index < fDeviceField->Menu()->CountItems(); index++) {
item = fDeviceField->Menu()->ItemAt(index);
if(item && item->Message() && item->Message()->FindPointer("Addon",
reinterpret_cast<void**>(&fDeviceAddon)) == B_OK
&& fDeviceAddon == Addon()->DeviceAddon())
break;
}
if(fDeviceAddon == Addon()->DeviceAddon()) {
item->SetMarked(true);
ReloadDeviceView();
} else {
fDeviceAddon = NULL;
item = fDeviceField->Menu()->FindMarked();
if(item)
item->SetMarked(false);
}
if(Addon()->CountAuthenticators() > 0) {
BString kernelModule, authenticator;
BMessage authentication;
if(Addon()->Settings()->FindMessage("Authentication", &authentication) == B_OK)
authentication.FindString("Authenticators", &authenticator);
for(int32 index = 0; index < fDeviceField->Menu()->CountItems(); index++) {
item = fDeviceField->Menu()->ItemAt(index);
if(item && item->Message()
&& item->Message()->FindString("KernelModuleName",
&kernelModule) == B_OK && kernelModule == authenticator) {
item->SetMarked(true);
break;
}
}
} else
fAuthenticatorNone->SetMarked(true);
fUsername->SetText(Addon()->Username());
fPassword->SetText(Addon()->Password());
fSavePassword->SetValue(Addon()->HasPassword());
}
const char*
GeneralView::DeviceName() const
{
if(fDeviceAddon)
return fDeviceAddon->KernelModuleName();
return NULL;
}
const char*
GeneralView::AuthenticatorName() const
{
BMenuItem *marked = fAuthenticatorField->Menu()->FindMarked();
if(marked && marked != fAuthenticatorNone)
return marked->Message()->FindString("KernelModuleName");
return NULL;
}
bool
GeneralView::IsDeviceModified() const
{
if(fDeviceAddon != Addon()->DeviceAddon())
return true;
else if(fDeviceAddon) {
bool settings, profile;
fDeviceAddon->IsModified(settings, profile);
return settings;
}
return false;
}
@ -330,6 +475,10 @@ void
GeneralView::AttachedToWindow()
{
SetViewColor(Parent()->ViewColor());
fDeviceField->Menu()->SetTargetForItems(this);
fAuthenticatorField->Menu()->SetTargetForItems(this);
fUsername->SetTarget(this);
fPassword->SetTarget(this);
}
@ -337,8 +486,146 @@ void
GeneralView::MessageReceived(BMessage *message)
{
switch(message->what) {
case MSG_SELECT_DEVICE:
if(message->FindPointer("Addon", reinterpret_cast<void**>(&fDeviceAddon))
!= B_OK)
fDeviceAddon = NULL;
else {
if(fDeviceAddon != Addon()->DeviceAddon())
fDeviceAddon->LoadSettings(Addon()->Settings(), Addon()->Profile(),
Addon()->IsNew());
ReloadDeviceView();
}
break;
case MSG_SELECT_AUTHENTICATOR:
break;
default:
BView::MessageReceived(message);
}
}
void
GeneralView::ReloadDeviceView()
{
// first remove existing device view(s)
while(fDeviceBox->CountChildren() > 0)
fDeviceBox->RemoveChild(fDeviceBox->ChildAt(0));
BRect rect(fDeviceBox->Bounds());
rect.top = 25;
float width, height;
if(!fDeviceAddon->GetPreferredSize(&width, &height)) {
width = 50;
height = 50;
}
if(width > rect.Width())
width = rect.Width();
if(height < 10)
height = 10;
rect.InsetBy((rect.Width() - width) / 2, 0);
rect.bottom = rect.top + height;
float deltaY = rect.Height() - fDeviceBox->Bounds().Height();
fDeviceBox->ResizeBy(0, deltaY);
fAuthenticationBox->MoveBy(0, deltaY);
BView *deviceView = fDeviceAddon->CreateView(rect.LeftTop());
if(deviceView)
fDeviceBox->AddChild(deviceView);
}
void
GeneralView::AddDevices()
{
AddAddonsToMenu(fDeviceField->Menu(), "Device", MSG_SELECT_DEVICE);
}
void
GeneralView::AddAuthenticators()
{
fAuthenticatorNone = new BMenuItem("None", new BMessage(MSG_SELECT_AUTHENTICATOR));
fAuthenticatorField->Menu()->AddItem(fAuthenticatorNone);
fAuthenticatorNone->SetMarked(true);
fAuthenticatorField->Menu()->AddSeparatorItem();
BMessage addon;
for(int32 index = 0;
Addon()->Addons()->FindMessage("Authenticator", index, &addon) == B_OK;
index++) {
BMessage *message = new BMessage(MSG_SELECT_AUTHENTICATOR);
message->AddString("Authenticator", addon.FindString("KernelModuleName"));
BString name, technicalName, friendlyName;
bool hasTechnicalName
= (addon.FindString("TechnicalName", &technicalName) == B_OK);
bool hasFriendlyName
= (addon.FindString("FriendlyName", &friendlyName) == B_OK);
if(hasTechnicalName) {
name << technicalName;
if(hasFriendlyName)
name << " (";
}
if(hasFriendlyName) {
name << friendlyName;
if(hasTechnicalName)
name << ")";
}
int32 insertAt = FindNextAddonInsertionIndex(fAuthenticatorField->Menu(),
name, 2);
fAuthenticatorField->Menu()->AddItem(new BMenuItem(name.String(), message),
insertAt);
}
}
void
GeneralView::AddAddonsToMenu(BMenu *menu, const char *type, uint32 what)
{
DialUpAddon *addon;
for(int32 index = 0; Addon()->Addons()->FindPointer(type, index,
reinterpret_cast<void**>(&addon)) == B_OK; index++) {
if(!addon || (!addon->FriendlyName() && !addon->TechnicalName()))
continue;
BMessage *message = new BMessage(what);
message->AddPointer("Addon", addon);
BString name;
if(addon->TechnicalName()) {
name << addon->TechnicalName();
if(addon->FriendlyName())
name << " (";
}
if(addon->FriendlyName()) {
name << addon->FriendlyName();
if(addon->TechnicalName())
name << ")";
}
int32 insertAt = FindNextAddonInsertionIndex(menu, name);
menu->AddItem(new BMenuItem(name.String(), message), insertAt);
}
}
int32
GeneralView::FindNextAddonInsertionIndex(BMenu *menu, const BString& name,
int32 index = 0)
{
BMenuItem *item;
for(; index < menu->CountItems(); index++) {
item = menu->ItemAt(index);
if(item && name.ICompare(item->Label()) <= 0)
return index;
}
return index;
}

View File

@ -4,13 +4,16 @@
//
// Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de
//-----------------------------------------------------------------------
// GeneralAddon saves the loaded settings.
// GeneralView saves the current settings.
//-----------------------------------------------------------------------
#ifndef _GENERAL_ADDON__H
#define _GENERAL_ADDON__H
#include <DialUpAddon.h>
#include <View.h>
#include <CheckBox.h>
#include <Message.h>
#include <String.h>
#include <TextControl.h>
@ -23,14 +26,33 @@ class GeneralAddon : public DialUpAddon {
GeneralAddon(BMessage *addons);
virtual ~GeneralAddon();
bool IsNew() const
{ return fIsNew; }
const char *DeviceName() const
{ return fDeviceName.String(); }
const char *Username() const
{ return fUsername.String(); }
const char *Password() const
{ return fPassword.String(); }
bool HasPassword() const
{ return fHasPassword; }
DialUpAddon *FindDevice(const BString& moduleName) const;
DialUpAddon *DeviceAddon() const
{ return fDeviceAddon; }
int32 CountAuthenticators() const
{ return fAuthenticatorsCount; }
BMessage *Settings() const
{ return fSettings; }
BMessage *Profile() const
{ return fProfile; }
virtual int32 Position() const
{ return 0; }
virtual bool LoadSettings(BMessage *settings, BMessage *profile);
virtual bool LoadSettings(BMessage *settings, BMessage *profile, bool isNew);
bool LoadDeviceSettings(BMessage *settings, BMessage *profile);
bool LoadAuthenticationSettings(BMessage *settings, BMessage *profile);
@ -48,8 +70,9 @@ class GeneralAddon : public DialUpAddon {
bool MarkAuthenticatorAsValid(const BString& moduleName);
private:
bool fHasDevice, fHasPassword;
BString fDevice, fUsername, fPassword;
bool fIsNew, fHasPassword;
BString fDeviceName, fUsername, fPassword;
DialUpAddon *fDeviceAddon;
int32 fAuthenticatorsCount;
BMessage *fSettings, *fProfile;
// saves last settings state
@ -71,27 +94,32 @@ class GeneralView : public BView {
const char *Password() const
{ return fPassword->Text(); }
bool DoesSavePassword() const
{ return true; }
// TODO: implement me!
{ return fSavePassword->Value(); }
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!
const char *DeviceName() const;
const char *AuthenticatorName() const;
bool IsDeviceModified() const;
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage *message);
private:
void ReloadDeviceView();
void AddDevices();
void AddAuthenticators();
void AddAddonsToMenu(BMenu *menu, const char *type, uint32 what);
int32 FindNextAddonInsertionIndex(BMenu *menu, const BString& name,
int32 index = 0);
private:
GeneralAddon *fAddon;
DialUpAddon *fDeviceAddon;
BBox *fDeviceBox, *fAuthenticationBox;
BMenuField *fAuthenticator;
BMenuField *fDeviceField, *fAuthenticatorField;
BMenuItem *fAuthenticatorNone;
BTextControl *fUsername, *fPassword;
BCheckBox *fSavePassword;
};

View File

@ -9,27 +9,22 @@
#include <cstdio>
BMessage*
FindMessageParameter(const char *name, BMessage& message, int32 *startIndex = NULL)
bool
FindMessageParameter(const char *name, BMessage& message, BMessage& save,
int32 *startIndex = NULL)
{
// XXX: this is a hack and should be removed when we can replace BMessage with
// something better
const BMessage *parameter;
// XXX: this should be removed when we can replace BMessage with something better
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)) {
for(; message.FindMessage("Parameters", index, &save) == B_OK; index++) {
if(save.FindString("Name", &string) == B_OK && string.ICompare(name) == 0) {
if(startIndex)
*startIndex = index;
return const_cast<BMessage*>(parameter);
return true;
}
}
return NULL;
return false;
}
@ -41,18 +36,18 @@ AddParameter(const driver_parameter *parameter, BMessage& message)
return false;
if(parameter->name)
message.AddString("name", 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]);
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);
message.AddMessage("Parameters", &parameterMessage);
}
return true;
@ -75,7 +70,7 @@ ReadMessageDriverSettings(const char *name, BMessage& message)
for(int32 index = 0; index < settings->parameter_count; index++) {
BMessage parameter;
AddParameter(&settings->parameters[index], parameter);
message.AddMessage("parameters", &parameter);
message.AddMessage("Parameters", &parameter);
}
unload_driver_settings(handle);
@ -89,20 +84,20 @@ bool
WriteParameter(BFile& file, const BMessage& parameter, int32 level)
{
const char *name;
if(parameter.FindString("name", &name) != B_OK || !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++)
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);
parameter.GetInfo("Parameters", &type, &parameterCount);
if(parameterCount > 0)
line << '{';
@ -112,7 +107,7 @@ WriteParameter(BFile& file, const BMessage& parameter, int32 level)
if(parameterCount > 0) {
BMessage subParameter;
for(int32 index = 0; parameter.FindMessage("parameters", index,
for(int32 index = 0; parameter.FindMessage("Parameters", index,
&subParameter) == B_OK; index++)
WriteParameter(file, subParameter, level + 1);
@ -134,7 +129,7 @@ WriteMessageDriverSettings(BFile& file, const BMessage& message)
file.Seek(0, SEEK_SET);
BMessage parameter;
for(int32 index = 0; message.FindMessage("parameters", index, &parameter) == B_OK;
for(int32 index = 0; message.FindMessage("Parameters", index, &parameter) == B_OK;
index++)
WriteParameter(file, parameter, 0);

View File

@ -7,8 +7,8 @@ class BMessage;
class BFile;
extern BMessage *FindMessageParameter(const char *name, BMessage& message,
int32 *startIndex = NULL);
extern bool FindMessageParameter(const char *name, BMessage& message,
BMessage& save, int32 *startIndex = NULL);
extern bool ReadMessageDriverSettings(const char *name, BMessage& message);
extern bool WriteMessageDriverSettings(BFile& file, const BMessage& message);

View File

@ -2,9 +2,10 @@ 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:
- see if we can reuse some code from the original DUN preflet
- add "Protocols" tab
- add "Extras" tab for changing dial-behaviour (auto-redial, dial-on-demand, etc.)
- add "Revert changes" button
- add "Revert Changes" button
- load add-ons
- write IP (IPCP) protocol add-on
- write PAP authenticator add-on