From 7b79abd0ec8d409e3b20c08ed57b50fb16395df5 Mon Sep 17 00:00:00 2001 From: Waldemar Kornewald Date: Sun, 21 Nov 2004 12:47:40 +0000 Subject: [PATCH] Forgot to commit some files. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10114 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/apps/bin/ppp_up/PPPUpAddon.cpp | 293 +++++++++++++++++++++++++++++ src/apps/bin/ppp_up/PPPUpAddon.h | 84 +++++++++ 2 files changed, 377 insertions(+) create mode 100644 src/apps/bin/ppp_up/PPPUpAddon.cpp create mode 100644 src/apps/bin/ppp_up/PPPUpAddon.h diff --git a/src/apps/bin/ppp_up/PPPUpAddon.cpp b/src/apps/bin/ppp_up/PPPUpAddon.cpp new file mode 100644 index 0000000000..bfb4358e5b --- /dev/null +++ b/src/apps/bin/ppp_up/PPPUpAddon.cpp @@ -0,0 +1,293 @@ +/* + * Copyright 2003-2004, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#include "ConnectionView.h" + +#include "InterfaceUtils.h" +#include "MessageDriverSettingsUtils.h" + +#include +#include +#include +#include +#include +#include + +#include + + +PPPUpAddon::PPPUpAddon(BMessage *addons, ConnectionView *connectionView) + : DialUpAddon(addons), + fHasPassword(false), + fAuthenticatorsCount(0), + fSettings(NULL), + fProfile(NULL), + fConnectionView(connectionView) +{ +} + + +PPPUpAddon::~PPPUpAddon() +{ +} + + +DialUpAddon* +PPPUpAddon::FindDevice(const BString& moduleName) const +{ + DialUpAddon *addon; + for(int32 index = 0; Addons()->FindPointer(DUN_DEVICE_ADDON_TYPE, index, + reinterpret_cast(&addon)) == B_OK; index++) + if(addon && moduleName == addon->KernelModuleName()) + return addon; + + return NULL; +} + + +bool +PPPUpAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew) +{ + fIsNew = isNew; + fHasPassword = false; + fDeviceName = fUsername = fPassword = ""; + fDeviceAddon = NULL; + fAuthenticatorsCount = 0; + fSettings = settings; + fProfile = profile; + + fConnectionView->Reload(); + + if(!settings || !profile || isNew) + return true; + + if(!LoadDeviceSettings()) + return false; + + if(!LoadAuthenticationSettings()) + return false; + + fConnectionView->Reload(); + // reload new settings + + return true; +} + + +bool +PPPUpAddon::LoadDeviceSettings() +{ + int32 index = 0; + BMessage device; + if(!FindMessageParameter(PPP_DEVICE_KEY, *fSettings, &device, &index)) + return false; + + if(device.FindString(MDSU_VALUES, &fDeviceName) != B_OK) + return false; + + device.AddBool(MDSU_VALID, true); + fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &device); + + fDeviceAddon = FindDevice(fDeviceName); + if(!fDeviceAddon) + return false; + + return fDeviceAddon->LoadSettings(fSettings, fProfile, false); +} + + +bool +PPPUpAddon::LoadAuthenticationSettings() +{ + // we only handle the profile (although settings could contain different data) + int32 itemIndex = 0; + BMessage authentication, item; + + if(!FindMessageParameter(PPP_AUTHENTICATOR_KEY, *fProfile, &item, &itemIndex)) + return true; + + // find authenticators (though we load all authenticators, we only use one) + BString name; + for(int32 index = 0; item.FindString(MDSU_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(PPP_UP_AUTHENTICATORS, name); + fAuthenticatorName = name; + ++fAuthenticatorsCount; + } + + fSettings->AddMessage(PPP_UP_AUTHENTICATION, &authentication); + + bool hasUsername = false; + // a username must be present + + // load username and password + BMessage parameter; + int32 parameterIndex = 0; + if(FindMessageParameter("User", item, ¶meter, ¶meterIndex) + && parameter.FindString(MDSU_VALUES, &fUsername) == B_OK) { + hasUsername = true; + parameter.AddBool(MDSU_VALID, true); + item.ReplaceMessage(MDSU_PARAMETERS, parameterIndex, ¶meter); + } + + parameterIndex = 0; + if(FindMessageParameter("Password", item, ¶meter, ¶meterIndex) + && parameter.FindString(MDSU_VALUES, &fPassword) == B_OK) { + fHasPassword = true; + parameter.AddBool(MDSU_VALID, true); + item.ReplaceMessage(MDSU_PARAMETERS, parameterIndex, ¶meter); + } + + // tell DUN whether everything is valid + if(hasUsername) + item.AddBool(MDSU_VALID, true); + + fProfile->ReplaceMessage(MDSU_PARAMETERS, itemIndex, &item); + + return true; +} + + +bool +PPPUpAddon::HasTemporaryProfile() const +{ + return fConnectionView->HasTemporaryProfile(); +} + + +void +PPPUpAddon::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 +PPPUpAddon::IsDeviceModified(bool *settings, bool *profile) const +{ + fConnectionView->IsDeviceModified(settings, profile); +} + + +void +PPPUpAddon::IsAuthenticationModified(bool *settings, bool *profile) const +{ + // currently we only support selecting one authenticator + *profile = (*settings || fUsername != fConnectionView->Username() + || (fPassword != fConnectionView->Password() && fHasPassword) + || fHasPassword != fConnectionView->DoesSavePassword()); +} + + +bool +PPPUpAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTemporary) +{ + if(!fSettings || !settings) + return false; + // TODO: tell user that a device is needed (if we fail because of this) + + if(!DeviceAddon()->SaveSettings(settings, profile, saveTemporary)) + return false; + + if(CountAuthenticators() > 0) { + BMessage authenticator; + authenticator.AddString(MDSU_NAME, PPP_AUTHENTICATOR_KEY); + authenticator.AddString(MDSU_VALUES, AuthenticatorName()); + settings->AddMessage(MDSU_PARAMETERS, &authenticator); + + BMessage username; + username.AddString(MDSU_NAME, "User"); + username.AddString(MDSU_VALUES, fConnectionView->Username()); + authenticator.AddMessage(MDSU_PARAMETERS, &username); + + if(saveTemporary || fConnectionView->DoesSavePassword()) { + // save password, too + BMessage password; + password.AddString(MDSU_NAME, "Password"); + password.AddString(MDSU_VALUES, fConnectionView->Password()); + authenticator.AddMessage(MDSU_PARAMETERS, &password); + } + + profile->AddMessage(MDSU_PARAMETERS, &authenticator); + } + + return true; +} + + +bool +PPPUpAddon::GetPreferredSize(float *width, float *height) const +{ + // this method is not used, just set values + if(width) + *width = 200; + if(height) + *height = 300; + + return true; +} + + +BView* +PPPUpAddon::CreateView(BPoint leftTop) +{ + return NULL; + // ConnectionView is responsible for our UI +} + + +bool +PPPUpAddon::GetAuthenticator(const BString& moduleName, BMessage *entry) const +{ + if(!entry) + return false; + + BString name; + for(int32 index = 0; Addons()->FindMessage(DUN_AUTHENTICATOR_ADDON_TYPE, index, + entry) == B_OK; index++) { + entry->FindString("KernelModuleName", &name); + if(name == moduleName) + return true; + } + + return false; +} + + +bool +PPPUpAddon::MarkAuthenticatorAsValid(const BString& moduleName) +{ + BMessage authenticator; + int32 index = 0; + BString name; + + for(; FindMessageParameter(PPP_AUTHENTICATOR_KEY, *fSettings, &authenticator, + &index); index++) { + authenticator.FindString("KernelModuleName", &name); + if(name == moduleName) { + authenticator.AddBool(MDSU_VALID, true); + fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &authenticator); + return true; + } + } + + return false; +} diff --git a/src/apps/bin/ppp_up/PPPUpAddon.h b/src/apps/bin/ppp_up/PPPUpAddon.h new file mode 100644 index 0000000000..1cd65436d3 --- /dev/null +++ b/src/apps/bin/ppp_up/PPPUpAddon.h @@ -0,0 +1,84 @@ +/* + * Copyright 2003-2004, Waldemar Kornewald + * Distributed under the terms of the MIT License. + */ + +#ifndef PPP_UP_ADDON__H +#define PPP_UP_ADDON__H + +#include + +#include +#include +#include + +class ConnectionView; + +// string constants for information saved in the settings message +#define PPP_UP_AUTHENTICATION "Authentication" +#define PPP_UP_AUTHENTICATORS "Authenticators" + + +class PPPUpAddon : public DialUpAddon { + public: + PPPUpAddon(BMessage *addons, ConnectionView *connectionView); + virtual ~PPPUpAddon(); + + bool IsNew() const + { return fIsNew; } + + const char *DeviceName() const + { return fDeviceName.String(); } + const char *AuthenticatorName() const + { return fAuthenticatorName.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, bool isNew); + bool LoadDeviceSettings(); + bool LoadAuthenticationSettings(); + + virtual bool HasTemporaryProfile() const; + 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 fIsNew, fHasPassword; + BString fDeviceName, fAuthenticatorName, fUsername, fPassword; + DialUpAddon *fDeviceAddon; + int32 fAuthenticatorsCount; + BMessage *fSettings, *fProfile; + // saves last settings state + ConnectionView *fConnectionView; +}; + + +#endif