From 3dbb75a6767a195d41320109569e7d8e3c58b2fc Mon Sep 17 00:00:00 2001 From: Waldemar Kornewald Date: Thu, 25 Mar 2004 11:59:34 +0000 Subject: [PATCH] Added the Protocols tab. Could not test it because currently there is no IPCP addon. Replaced direct identifier specification for BMessage with #define'd values, so typos cannot arise. Moved some functions into InterfaceUtils so they are available for all add-ons (this will later become part of a static library for the DialUpAddon API). git-svn-id: file:///srv/svn/repos/haiku/trunk/current@7065 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/net/DialUpPreflet/DEVNOTES | 18 +- .../kits/net/DialUpPreflet/DialUpAddon.h | 15 +- .../kits/net/DialUpPreflet/DialUpView.cpp | 49 +- src/tests/kits/net/DialUpPreflet/DialUpView.h | 2 - .../kits/net/DialUpPreflet/GeneralAddon.cpp | 148 ++---- .../kits/net/DialUpPreflet/GeneralAddon.h | 8 +- .../kits/net/DialUpPreflet/InterfaceUtils.cpp | 74 +++ .../kits/net/DialUpPreflet/InterfaceUtils.h | 26 + src/tests/kits/net/DialUpPreflet/Jamfile | 2 + .../MessageDriverSettingsUtils.cpp | 29 +- .../MessageDriverSettingsUtils.h | 12 + .../kits/net/DialUpPreflet/PPPoEAddon.cpp | 55 +- src/tests/kits/net/DialUpPreflet/PPPoEAddon.h | 5 +- .../kits/net/DialUpPreflet/ProtocolsAddon.cpp | 468 ++++++++++++++++++ .../kits/net/DialUpPreflet/ProtocolsAddon.h | 100 ++++ src/tests/kits/net/DialUpPreflet/TODO | 5 +- 16 files changed, 836 insertions(+), 180 deletions(-) create mode 100644 src/tests/kits/net/DialUpPreflet/InterfaceUtils.cpp create mode 100644 src/tests/kits/net/DialUpPreflet/InterfaceUtils.h create mode 100644 src/tests/kits/net/DialUpPreflet/ProtocolsAddon.cpp create mode 100644 src/tests/kits/net/DialUpPreflet/ProtocolsAddon.h diff --git a/src/tests/kits/net/DialUpPreflet/DEVNOTES b/src/tests/kits/net/DialUpPreflet/DEVNOTES index bbd824a4c9..78b861c978 100644 --- a/src/tests/kits/net/DialUpPreflet/DEVNOTES +++ b/src/tests/kits/net/DialUpPreflet/DEVNOTES @@ -7,10 +7,10 @@ All add-ons must add a "Valid" boolean value to every recognized parameter. The Add-on registration: -------------------------------------------------------------------------------------- Tabs: -"Tab" : pointer to DialUpAddon object +DUN_TAB_ADDON_TYPE : pointer to DialUpAddon object Authenticators: -"Authenticator" : BMessage with the following entries: +DUN_AUTHENTICATOR_ADDON_TYPE : BMessage with the following entries: { "KernelModule" : string "TechnicalName" : string @@ -19,18 +19,20 @@ Authenticators: } Devices: -"Device" : pointer to DialUpAddon object +DUN_DEVICE_ADDON_TYPE : pointer to DialUpAddon object Protocols: -"Protocol" : pointer to DialUpAddon object +Note: CreateView() should NOT return a view, but open a preferences window (B_MODAL_WINDOW)! +DUN_PROTOCOL_ADDON_TYPE : 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) +DUN_DELETE_ON_QUIT : pointer to DialUpAddon object - this will be deleted when the preflet quits + (you should register all your addons here, too) -------------------------------------------------------------------------------------- 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 +DUN_MESSENGER : BMessenger - This messenger can be used to send messages to the preflet +DUN_TAB_VIEW_RECT : BRect - This rect should be used for tabs +DUN_DEVICE_VIEW_WIDTH : float - Maximum width a device add-on's view may have diff --git a/src/tests/kits/net/DialUpPreflet/DialUpAddon.h b/src/tests/kits/net/DialUpPreflet/DialUpAddon.h index 9db79d36a2..01150bbb53 100644 --- a/src/tests/kits/net/DialUpPreflet/DialUpAddon.h +++ b/src/tests/kits/net/DialUpPreflet/DialUpAddon.h @@ -18,7 +18,20 @@ #include -#define DUN_MAXIMUM_PRIORITY 50 +#define DUN_MAXIMUM_PRIORITY 50 + +// add-on types +#define DUN_TAB_ADDON_TYPE "Tab" +#define DUN_AUTHENTICATOR_ADDON_TYPE "Authenticator" +#define DUN_DEVICE_ADDON_TYPE "Device" +#define DUN_PROTOCOL_ADDON_TYPE "Protocol" + +// other information contained in the add-ons BMessage object +#define DUN_DELETE_ON_QUIT "DeleteMe" + // the DialUpAddon objects in this list will be deleted when the preflet quits +#define DUN_MESSENGER "Messenger" +#define DUN_TAB_VIEW_RECT "TabViewRect" +#define DUN_DEVICE_VIEW_WIDTH "DeviceViewWidth" class DialUpAddon { diff --git a/src/tests/kits/net/DialUpPreflet/DialUpView.cpp b/src/tests/kits/net/DialUpPreflet/DialUpView.cpp index 115c8338fb..7303a65315 100644 --- a/src/tests/kits/net/DialUpPreflet/DialUpView.cpp +++ b/src/tests/kits/net/DialUpPreflet/DialUpView.cpp @@ -8,11 +8,13 @@ #include "DialUpView.h" #include "DialUpAddon.h" +#include "InterfaceUtils.h" #include "MessageDriverSettingsUtils.h" // built-in add-ons #include "GeneralAddon.h" #include "PPPoEAddon.h" +#include "ProtocolsAddon.h" #include @@ -90,7 +92,7 @@ DialUpView::DialUpView(BRect frame) // add messenger to us so add-ons can contact us BMessenger messenger(this); - fAddons.AddMessenger("Messenger", messenger); + fAddons.AddMessenger(DUN_MESSENGER, messenger); // create pop-up with all interfaces and "New..."/"Delete current" items fInterfaceMenu = new BPopUpMenu(LABEL_CREATE_NEW); @@ -107,7 +109,7 @@ DialUpView::DialUpView(BRect frame) fTabView = new BTabView(rect, "TabView"); BRect tabViewRect = fTabView->Bounds(); tabViewRect.bottom -= fTabView->TabHeight(); - fAddons.AddRect("TabViewRect", tabViewRect); + fAddons.AddRect(DUN_TAB_VIEW_RECT, tabViewRect); rect.top = rect.bottom + 15; rect.bottom = rect.top + 15; @@ -145,7 +147,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("DeleteMe", index, + fAddons.FindPointer(DUN_DELETE_ON_QUIT, index, reinterpret_cast(&addon)) == B_OK; index++) delete addon; @@ -232,7 +234,7 @@ DialUpView::LoadSettings(bool isNew) } DialUpAddon *addon; - for(int32 index = 0; fAddons.FindPointer("Tab", index, + for(int32 index = 0; fAddons.FindPointer(DUN_TAB_ADDON_TYPE, index, reinterpret_cast(&addon)) == B_OK; index++) { if(!addon) continue; @@ -255,7 +257,7 @@ DialUpView::IsModified(bool& settings, bool& profile) // for current addon DialUpAddon *addon; - for(int32 index = 0; fAddons.FindPointer("Tab", index, + for(int32 index = 0; fAddons.FindPointer(DUN_TAB_ADDON_TYPE, index, reinterpret_cast(&addon)) == B_OK; index++) { if(!addon) continue; @@ -278,7 +280,7 @@ DialUpView::SaveSettings(BMessage& settings, BMessage& profile, bool saveModifie DialUpAddon *addon; TemplateList addons; for(int32 index = 0; - fAddons.FindPointer("Tab", index, + fAddons.FindPointer(DUN_TAB_ADDON_TYPE, index, reinterpret_cast(&addon)) == B_OK; index++) { if(!addon) @@ -440,14 +442,14 @@ DialUpView::HandleReportMessage(BMessage *message) void DialUpView::CreateTabs() { - // create tabs for all registered and valid "Tab" add-ons + // create tabs for all registered and valid tab add-ons DialUpAddon *addon; BView *target; float width, height; TemplateList addons; for(int32 index = 0; - fAddons.FindPointer("Tab", index, + fAddons.FindPointer(DUN_TAB_ADDON_TYPE, index, reinterpret_cast(&addon)) == B_OK; index++) { if(!addon || addon->Position() < 0) @@ -598,19 +600,23 @@ DialUpView::LoadAddons() // Load integrated add-ons: // "General" tab GeneralAddon *generalAddon = new GeneralAddon(&fAddons); - fAddons.AddPointer("Tab", generalAddon); - fAddons.AddPointer("DeleteMe", generalAddon); + fAddons.AddPointer(DUN_TAB_ADDON_TYPE, generalAddon); + fAddons.AddPointer(DUN_DELETE_ON_QUIT, generalAddon); // "PPPoE" device PPPoEAddon *pppoeAddon = new PPPoEAddon(&fAddons); - fAddons.AddPointer("Device", pppoeAddon); - fAddons.AddPointer("DeleteMe", pppoeAddon); + fAddons.AddPointer(DUN_DEVICE_ADDON_TYPE, pppoeAddon); + fAddons.AddPointer(DUN_DELETE_ON_QUIT, pppoeAddon); + // "Protocols" tab + ProtocolsAddon *protocolsAddon = new ProtocolsAddon(&fAddons); + fAddons.AddPointer(DUN_TAB_ADDON_TYPE, protocolsAddon); + fAddons.AddPointer(DUN_DELETE_ON_QUIT, protocolsAddon); // "PAP" authenticator BMessage addon; addon.AddString("KernelModuleName", "pap"); addon.AddString("FriendlyName", "Plain-text Authentication"); addon.AddString("TechnicalName", "PAP"); - fAddons.AddMessage("Authenticator", &addon); - // addon.MakeEmpty(); + fAddons.AddMessage(DUN_AUTHENTICATOR_ADDON_TYPE, &addon); + // addon.MakeEmpty(); // for next authenticator // TODO: // load all add-ons from the add-ons folder @@ -687,18 +693,3 @@ DialUpView::CountInterfaces() const { return fInterfaceMenu->CountItems() - 3; } - - -int32 -DialUpView::FindNextMenuInsertionIndex(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; -} diff --git a/src/tests/kits/net/DialUpPreflet/DialUpView.h b/src/tests/kits/net/DialUpPreflet/DialUpView.h index 1d650c6a96..eedd2307ec 100644 --- a/src/tests/kits/net/DialUpPreflet/DialUpView.h +++ b/src/tests/kits/net/DialUpPreflet/DialUpView.h @@ -45,8 +45,6 @@ class DialUpView : public BView { void AddInterface(const char *name, bool isNew = false); void SelectInterface(int32 index, bool isNew = false); int32 CountInterfaces() const; - int32 FindNextMenuInsertionIndex(BMenu *menu, const BString& name, - int32 index = 0); private: PPPInterfaceListener fListener; diff --git a/src/tests/kits/net/DialUpPreflet/GeneralAddon.cpp b/src/tests/kits/net/DialUpPreflet/GeneralAddon.cpp index 56834c2f42..191903abf0 100644 --- a/src/tests/kits/net/DialUpPreflet/GeneralAddon.cpp +++ b/src/tests/kits/net/DialUpPreflet/GeneralAddon.cpp @@ -10,13 +10,13 @@ #include "GeneralAddon.h" +#include "InterfaceUtils.h" #include "MessageDriverSettingsUtils.h" #include #include #include #include -#include #include #include @@ -27,6 +27,10 @@ #define MSG_SELECT_AUTHENTICATOR 'SELA' +#define GENERAL_TAB_AUTHENTICATION "Authentication" +#define GENERAL_TAB_AUTHENTICATORS "Authenticators" + + GeneralAddon::GeneralAddon(BMessage *addons) : DialUpAddon(addons), fHasPassword(false), @@ -47,7 +51,7 @@ DialUpAddon* GeneralAddon::FindDevice(const BString& moduleName) const { DialUpAddon *addon; - for(int32 index = 0; Addons()->FindPointer("Device", index, + for(int32 index = 0; Addons()->FindPointer(DUN_DEVICE_ADDON_TYPE, index, reinterpret_cast(&addon)) == B_OK; index++) if(addon && moduleName == addon->KernelModuleName()) return addon; @@ -74,10 +78,10 @@ GeneralAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew) if(!settings || !profile || isNew) return true; - if(!LoadDeviceSettings(settings, profile)) + if(!LoadDeviceSettings()) return false; - if(!LoadAuthenticationSettings(settings, profile)) + if(!LoadAuthenticationSettings()) return false; if(fGeneralView) @@ -89,53 +93,53 @@ GeneralAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew) bool -GeneralAddon::LoadDeviceSettings(BMessage *settings, BMessage *profile) +GeneralAddon::LoadDeviceSettings() { int32 index = 0; BMessage device; - if(!FindMessageParameter(PPP_DEVICE_KEY, *settings, device, &index)) + if(!FindMessageParameter(PPP_DEVICE_KEY, *fSettings, device, &index)) return false; // TODO: tell user that device specification is missing - if(device.FindString("Values", &fDeviceName) != B_OK) + if(device.FindString(MDSU_VALUES, &fDeviceName) != B_OK) return false; // TODO: tell user that device specification is missing - device.AddBool("Valid", true); - settings->ReplaceMessage("Parameters", index, &device); + device.AddBool(MDSU_VALID, true); + fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &device); fDeviceAddon = FindDevice(fDeviceName); if(!fDeviceAddon) return false; - return fDeviceAddon->LoadSettings(settings, profile, false); + return fDeviceAddon->LoadSettings(fSettings, fProfile, false); } bool -GeneralAddon::LoadAuthenticationSettings(BMessage *settings, BMessage *profile) +GeneralAddon::LoadAuthenticationSettings() { // we only handle the profile (although settings could contain different data) int32 itemIndex = 0; BMessage authentication, item; - if(!FindMessageParameter(PPP_AUTHENTICATOR_KEY, *profile, item, &itemIndex)) + 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("Values", index, &name) == B_OK; index++) { + 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("Authenticators", name); + authentication.AddString(GENERAL_TAB_AUTHENTICATORS, name); ++fAuthenticatorsCount; } - fSettings->AddMessage("Authentication", &authentication); + fSettings->AddMessage(GENERAL_TAB_AUTHENTICATION, &authentication); bool hasUsername = false; // a username must be present @@ -144,25 +148,25 @@ GeneralAddon::LoadAuthenticationSettings(BMessage *settings, BMessage *profile) BMessage parameter; int32 parameterIndex = 0; if(FindMessageParameter("User", item, parameter, ¶meterIndex) - && parameter.FindString("Values", &fUsername) == B_OK) { + && parameter.FindString(MDSU_VALUES, &fUsername) == B_OK) { hasUsername = true; - parameter.AddBool("Valid", true); - item.ReplaceMessage("Parameters", parameterIndex, ¶meter); + parameter.AddBool(MDSU_VALID, true); + item.ReplaceMessage(MDSU_PARAMETERS, parameterIndex, ¶meter); } parameterIndex = 0; if(FindMessageParameter("Password", item, parameter, ¶meterIndex) - && parameter.FindString("Values", &fPassword) == B_OK) { + && parameter.FindString(MDSU_VALUES, &fPassword) == B_OK) { fHasPassword = true; - parameter.AddBool("Valid", true); - item.ReplaceMessage("Parameters", parameterIndex, ¶meter); + parameter.AddBool(MDSU_VALID, true); + item.ReplaceMessage(MDSU_PARAMETERS, parameterIndex, ¶meter); } // tell DUN whether everything is valid if(hasUsername) - item.AddBool("Valid", true); + item.AddBool(MDSU_VALID, true); - profile->ReplaceMessage("Parameters", itemIndex, &item); + fProfile->ReplaceMessage(MDSU_PARAMETERS, itemIndex, &item); return true; } @@ -213,14 +217,14 @@ GeneralAddon::IsAuthenticationModified(bool& settings, bool& profile) const settings = fGeneralView->AuthenticatorName(); else { BMessage authentication; - if(fSettings->FindMessage("Authentication", &authentication) != B_OK) { + if(fSettings->FindMessage(GENERAL_TAB_AUTHENTICATION, &authentication) != B_OK) { settings = profile = false; return; // error! } BString authenticator; - if(authentication.FindString("Authenticators", &authenticator) != B_OK) { + if(authentication.FindString(GENERAL_TAB_AUTHENTICATORS, &authenticator) != B_OK) { settings = profile = false; return; // error! @@ -248,24 +252,24 @@ GeneralAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTempo if(fGeneralView->AuthenticatorName()) { BMessage authenticator; - authenticator.AddString("Name", PPP_AUTHENTICATOR_KEY); - authenticator.AddString("Values", fGeneralView->AuthenticatorName()); - settings->AddMessage("Parameters", &authenticator); + authenticator.AddString(MDSU_NAME, PPP_AUTHENTICATOR_KEY); + authenticator.AddString(MDSU_VALUES, fGeneralView->AuthenticatorName()); + settings->AddMessage(MDSU_PARAMETERS, &authenticator); BMessage username; - username.AddString("Name", "User"); - username.AddString("Values", fGeneralView->Username()); - authenticator.AddMessage("Parameters", &username); + username.AddString(MDSU_NAME, "User"); + username.AddString(MDSU_VALUES, fGeneralView->Username()); + authenticator.AddMessage(MDSU_PARAMETERS, &username); if(saveTemporary || fGeneralView->DoesSavePassword()) { // save password, too BMessage password; - password.AddString("Name", "Password"); - password.AddString("Values", fGeneralView->Password()); - authenticator.AddMessage("Parameters", &password); + password.AddString(MDSU_NAME, "Password"); + password.AddString(MDSU_VALUES, fGeneralView->Password()); + authenticator.AddMessage(MDSU_PARAMETERS, &password); } - profile->AddMessage("Parameters", &authenticator); + profile->AddMessage(MDSU_PARAMETERS, &authenticator); } return true; @@ -276,7 +280,7 @@ bool GeneralAddon::GetPreferredSize(float *width, float *height) const { BRect rect; - if(Addons()->FindRect("TabViewRect", &rect) != B_OK) + if(Addons()->FindRect(DUN_TAB_VIEW_RECT, &rect) != B_OK) rect.Set(0, 0, 200, 300); // set default values @@ -294,7 +298,7 @@ GeneralAddon::CreateView(BPoint leftTop) { if(!fGeneralView) { BRect rect; - Addons()->FindRect("TabViewRect", &rect); + Addons()->FindRect(DUN_TAB_VIEW_RECT, &rect); fGeneralView = new GeneralView(this, rect); } @@ -311,8 +315,8 @@ GeneralAddon::GetAuthenticator(const BString& moduleName, BMessage *entry) const return false; BString name; - for(int32 index = 0; Addons()->FindMessage("Authenticator", index, entry) == B_OK; - index++) { + for(int32 index = 0; Addons()->FindMessage(DUN_AUTHENTICATOR_ADDON_TYPE, index, + entry) == B_OK; index++) { entry->FindString("KernelModuleName", &name); if(name == moduleName) return true; @@ -333,8 +337,8 @@ GeneralAddon::MarkAuthenticatorAsValid(const BString& moduleName) &index); index++) { authenticator.FindString("KernelModuleName", &name); if(name == moduleName) { - authenticator.AddBool("Valid", true); - fSettings->ReplaceMessage("Parameters", index, &authenticator); + authenticator.AddBool(MDSU_VALID, true); + fSettings->ReplaceMessage(MDSU_PARAMETERS, index, &authenticator); return true; } } @@ -344,14 +348,15 @@ GeneralAddon::MarkAuthenticatorAsValid(const BString& moduleName) GeneralView::GeneralView(GeneralAddon *addon, BRect frame) - : BView(frame, "GeneralView", B_FOLLOW_NONE, 0), + : BView(frame, "General", B_FOLLOW_NONE, 0), fAddon(addon) { BRect rect = Bounds(); rect.InsetBy(5, 5); rect.bottom = 100; fDeviceBox = new BBox(rect, "Device"); - Addon()->Addons()->AddFloat("DeviceViewWidth", fDeviceBox->Bounds().Width() - 10); + Addon()->Addons()->AddFloat(DUN_DEVICE_VIEW_WIDTH, + fDeviceBox->Bounds().Width() - 10); rect.top = rect.bottom + 10; rect.bottom = rect.top + 25 // space for topmost control @@ -436,8 +441,9 @@ GeneralView::Reload() if(Addon()->CountAuthenticators() > 0) { BString kernelModule, authenticator; BMessage authentication; - if(Addon()->Settings()->FindMessage("Authentication", &authentication) == B_OK) - authentication.FindString("Authenticators", &authenticator); + if(Addon()->Settings()->FindMessage(GENERAL_TAB_AUTHENTICATION, + &authentication) == B_OK) + authentication.FindString(GENERAL_TAB_AUTHENTICATORS, &authenticator); BMenu *menu = fAuthenticatorField->Menu(); for(int32 index = 0; index < menu->CountItems(); index++) { item = menu->ItemAt(index); @@ -575,7 +581,8 @@ GeneralView::ReloadDeviceView() void GeneralView::AddDevices() { - AddAddonsToMenu(fDeviceField->Menu(), "Device", MSG_SELECT_DEVICE); + AddAddonsToMenu(Addon()->Addons(), fDeviceField->Menu(), DUN_DEVICE_ADDON_TYPE, + MSG_SELECT_DEVICE); } @@ -589,8 +596,8 @@ GeneralView::AddAuthenticators() BMessage addon; for(int32 index = 0; - Addon()->Addons()->FindMessage("Authenticator", index, &addon) == B_OK; - index++) { + Addon()->Addons()->FindMessage(DUN_AUTHENTICATOR_ADDON_TYPE, index, + &addon) == B_OK; index++) { BMessage *message = new BMessage(MSG_SELECT_AUTHENTICATOR); message->AddString("KernelModuleName", addon.FindString("KernelModuleName")); @@ -616,48 +623,3 @@ GeneralView::AddAuthenticators() insertAt); } } - - -void -GeneralView::AddAddonsToMenu(BMenu *menu, const char *type, uint32 what) -{ - DialUpAddon *addon; - for(int32 index = 0; Addon()->Addons()->FindPointer(type, index, - reinterpret_cast(&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 = FindNextMenuInsertionIndex(menu, name); - menu->AddItem(new BMenuItem(name.String(), message), insertAt); - } -} - - -int32 -GeneralView::FindNextMenuInsertionIndex(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; -} diff --git a/src/tests/kits/net/DialUpPreflet/GeneralAddon.h b/src/tests/kits/net/DialUpPreflet/GeneralAddon.h index e6b81d49f6..df34ac6bbc 100644 --- a/src/tests/kits/net/DialUpPreflet/GeneralAddon.h +++ b/src/tests/kits/net/DialUpPreflet/GeneralAddon.h @@ -14,7 +14,6 @@ #include #include -#include #include #include @@ -53,8 +52,8 @@ class GeneralAddon : public DialUpAddon { virtual int32 Position() const { return 0; } virtual bool LoadSettings(BMessage *settings, BMessage *profile, bool isNew); - bool LoadDeviceSettings(BMessage *settings, BMessage *profile); - bool LoadAuthenticationSettings(BMessage *settings, BMessage *profile); + bool LoadDeviceSettings(); + bool LoadAuthenticationSettings(); virtual bool HasTemporaryProfile() const; virtual void IsModified(bool& settings, bool& profile) const; @@ -109,9 +108,6 @@ class GeneralView : public BView { void AddDevices(); void AddAuthenticators(); - void AddAddonsToMenu(BMenu *menu, const char *type, uint32 what); - int32 FindNextMenuInsertionIndex(BMenu *menu, const BString& name, - int32 index = 0); private: GeneralAddon *fAddon; diff --git a/src/tests/kits/net/DialUpPreflet/InterfaceUtils.cpp b/src/tests/kits/net/DialUpPreflet/InterfaceUtils.cpp new file mode 100644 index 0000000000..c9f3e3267d --- /dev/null +++ b/src/tests/kits/net/DialUpPreflet/InterfaceUtils.cpp @@ -0,0 +1,74 @@ +//----------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de +//----------------------------------------------------------------------- + +#include "InterfaceUtils.h" + +#include "DialUpAddon.h" +#include +#include +#include +#include +#include + + +int32 +FindNextMenuInsertionIndex(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; +} + + +int32 +FindNextListInsertionIndex(BListView *list, const BString& name) +{ + int32 index = 0; + BStringItem *item; + for(; index < list->CountItems(); index++) { + item = static_cast(list->ItemAt(index)); + if(item && name.ICompare(item->Text()) <= 0) + return index; + } + + return index; +} + + +void +AddAddonsToMenu(const BMessage *source, BMenu *menu, const char *type, uint32 what) +{ + DialUpAddon *addon; + for(int32 index = 0; source->FindPointer(type, index, + reinterpret_cast(&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 = FindNextMenuInsertionIndex(menu, name); + menu->AddItem(new BMenuItem(name.String(), message), insertAt); + } +} diff --git a/src/tests/kits/net/DialUpPreflet/InterfaceUtils.h b/src/tests/kits/net/DialUpPreflet/InterfaceUtils.h new file mode 100644 index 0000000000..e249ba9c88 --- /dev/null +++ b/src/tests/kits/net/DialUpPreflet/InterfaceUtils.h @@ -0,0 +1,26 @@ +//----------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de +//----------------------------------------------------------------------- + +#ifndef _INTERFACE_UTILS__H +#define _INTERFACE_UTILS__H + +#include + +class DialUpAddon; +class BListView; +class BMenu; +class BString; + + +extern int32 FindNextMenuInsertionIndex(BMenu *menu, const BString& name, + int32 index = 0); +extern int32 FindNextListInsertionIndex(BListView *list, const BString& name); +extern void AddAddonsToMenu(const BMessage *source, BMenu *menu, const char *type, + uint32 what); + + +#endif diff --git a/src/tests/kits/net/DialUpPreflet/Jamfile b/src/tests/kits/net/DialUpPreflet/Jamfile index e2da2c968b..fecd5b9e65 100644 --- a/src/tests/kits/net/DialUpPreflet/Jamfile +++ b/src/tests/kits/net/DialUpPreflet/Jamfile @@ -12,11 +12,13 @@ SimpleTest DialUpPreflet : DialUpView.cpp # utils + InterfaceUtils.cpp MessageDriverSettingsUtils.cpp # built-in add-ons GeneralAddon.cpp PPPoEAddon.cpp + ProtocolsAddon.cpp ; LinkSharedOSLibs DialUpPreflet : libppp.a be ; diff --git a/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.cpp b/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.cpp index 5df4b85218..2296befa02 100644 --- a/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.cpp +++ b/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.cpp @@ -1,3 +1,10 @@ +//----------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de +//----------------------------------------------------------------------- + #include "MessageDriverSettingsUtils.h" #include @@ -15,8 +22,8 @@ FindMessageParameter(const char *name, BMessage& message, BMessage& save, // XXX: this should be removed when we can replace BMessage with something better BString string; int32 index = startIndex ? *startIndex : 0; - for(; message.FindMessage("Parameters", index, &save) == B_OK; index++) { - if(save.FindString("Name", &string) == B_OK && string.ICompare(name) == 0) { + for(; message.FindMessage(MDSU_PARAMETERS, index, &save) == B_OK; index++) { + if(save.FindString(MDSU_NAME, &string) == B_OK && string.ICompare(name) == 0) { if(startIndex) *startIndex = index; return true; @@ -35,18 +42,18 @@ AddParameter(const driver_parameter *parameter, BMessage& message) return false; if(parameter->name) - message.AddString("Name", parameter->name); + message.AddString(MDSU_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(MDSU_VALUES, parameter->values[index]); for(int32 index = 0; index < parameter->parameter_count; index++) { BMessage parameterMessage; AddParameter(¶meter->parameters[index], parameterMessage); - message.AddMessage("Parameters", ¶meterMessage); + message.AddMessage(MDSU_PARAMETERS, ¶meterMessage); } return true; @@ -71,7 +78,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", ¶meter); + message.AddMessage(MDSU_PARAMETERS, ¶meter); } unload_driver_settings(handle); @@ -96,7 +103,7 @@ bool WriteParameter(BFile& file, const BMessage& parameter, int32 level) { const char *name; - if(parameter.FindString("Name", &name) != B_OK || !name) + if(parameter.FindString(MDSU_NAME, &name) != B_OK || !name) return false; BString line, word(name); @@ -110,7 +117,7 @@ WriteParameter(BFile& file, const BMessage& parameter, int32 level) line << '\"'; line << ' '; - for(int32 index = 0; parameter.FindString("Values", index, &name) == B_OK; index++) + for(int32 index = 0; parameter.FindString(MDSU_VALUES, index, &name) == B_OK; index++) if(name) { word = name; EscapeWord(word); @@ -125,7 +132,7 @@ WriteParameter(BFile& file, const BMessage& parameter, int32 level) type_code type; int32 parameterCount; - parameter.GetInfo("Parameters", &type, ¶meterCount); + parameter.GetInfo(MDSU_PARAMETERS, &type, ¶meterCount); if(parameterCount > 0) line << '{'; @@ -135,7 +142,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(MDSU_PARAMETERS, index, &subParameter) == B_OK; index++) WriteParameter(file, subParameter, level + 1); @@ -158,7 +165,7 @@ WriteMessageDriverSettings(BFile& file, const BMessage& message) file.Seek(0, SEEK_SET); BMessage parameter; - for(int32 index = 0; message.FindMessage("Parameters", index, ¶meter) == B_OK; + for(int32 index = 0; message.FindMessage(MDSU_PARAMETERS, index, ¶meter) == B_OK; index++) { if(index > 0) file.Write("\n", 1); diff --git a/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.h b/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.h index e303b1589f..58940369c0 100644 --- a/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.h +++ b/src/tests/kits/net/DialUpPreflet/MessageDriverSettingsUtils.h @@ -1,3 +1,10 @@ +//----------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2004 Waldemar Kornewald, Waldemar.Kornewald@web.de +//----------------------------------------------------------------------- + #ifndef _MESSAGE_DRIVER_SETTINGS_UTILS__H #define _MESSAGE_DRIVER_SETTINGS_UTILS__H @@ -6,6 +13,11 @@ class BMessage; class BFile; +#define MDSU_NAME "Name" +#define MDSU_VALUES "Values" +#define MDSU_PARAMETERS "Parameters" +#define MDSU_VALID "Valid" + extern bool FindMessageParameter(const char *name, BMessage& message, BMessage& save, int32 *startIndex = NULL); diff --git a/src/tests/kits/net/DialUpPreflet/PPPoEAddon.cpp b/src/tests/kits/net/DialUpPreflet/PPPoEAddon.cpp index a1be7d1c6d..8f8d3d77f5 100644 --- a/src/tests/kits/net/DialUpPreflet/PPPoEAddon.cpp +++ b/src/tests/kits/net/DialUpPreflet/PPPoEAddon.cpp @@ -49,6 +49,8 @@ PPPoEAddon::PPPoEAddon(BMessage *addons) PPPoEAddon::~PPPoEAddon() { + delete fPPPoEView; + // this may have been set to NULL from the view's destructor! } @@ -93,41 +95,41 @@ PPPoEAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew) // error: no device BString name; - if(device.FindString("Values", &name) != B_OK || name != kKernelModuleName) + if(device.FindString(MDSU_VALUES, &name) != B_OK || name != kKernelModuleName) return false; // error: no device BMessage parameter; int32 index = 0; if(!FindMessageParameter(PPPoE_INTERFACE_KEY, device, parameter, &index) - || parameter.FindString("Values", &fInterfaceName) != B_OK) + || parameter.FindString(MDSU_VALUES, &fInterfaceName) != B_OK) return false; // error: no interface else { - parameter.AddBool("Valid", true); - device.ReplaceMessage("Parameters", index, ¶meter); + parameter.AddBool(MDSU_VALID, true); + device.ReplaceMessage(MDSU_PARAMETERS, index, ¶meter); } index = 0; if(!FindMessageParameter(PPPoE_AC_NAME_KEY, device, parameter, &index) - || parameter.FindString("Values", &fACName) != B_OK) + || parameter.FindString(MDSU_VALUES, &fACName) != B_OK) fACName = ""; else { - parameter.AddBool("Valid", true); - device.ReplaceMessage("Parameters", index, ¶meter); + parameter.AddBool(MDSU_VALID, true); + device.ReplaceMessage(MDSU_PARAMETERS, index, ¶meter); } index = 0; if(!FindMessageParameter(PPPoE_SERVICE_NAME_KEY, device, parameter, &index) - || parameter.FindString("Values", &fServiceName) != B_OK) + || parameter.FindString(MDSU_VALUES, &fServiceName) != B_OK) fServiceName = ""; else { - parameter.AddBool("Valid", true); - device.ReplaceMessage("Parameters", index, ¶meter); + parameter.AddBool(MDSU_VALID, true); + device.ReplaceMessage(MDSU_PARAMETERS, index, ¶meter); } - device.AddBool("Valid", true); - fSettings->ReplaceMessage("Parameters", deviceIndex, &device); + device.AddBool(MDSU_VALID, true); + fSettings->ReplaceMessage(MDSU_PARAMETERS, deviceIndex, &device); return true; } @@ -157,30 +159,30 @@ PPPoEAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTempora // TODO: tell user that an interface is needed (if we fail because of this) BMessage device, interface; - device.AddString("Name", PPP_DEVICE_KEY); - device.AddString("Values", kKernelModuleName); + device.AddString(MDSU_NAME, PPP_DEVICE_KEY); + device.AddString(MDSU_VALUES, kKernelModuleName); - interface.AddString("Name", PPPoE_INTERFACE_KEY); - interface.AddString("Values", fPPPoEView->InterfaceName()); - device.AddMessage("Parameters", &interface); + interface.AddString(MDSU_NAME, PPPoE_INTERFACE_KEY); + interface.AddString(MDSU_VALUES, fPPPoEView->InterfaceName()); + device.AddMessage(MDSU_PARAMETERS, &interface); if(fPPPoEView->ACName() && strlen(fPPPoEView->ACName()) > 0) { // save access concentrator, too BMessage ac; - ac.AddString("Name", PPPoE_AC_NAME_KEY); - ac.AddString("Values", fPPPoEView->ACName()); - device.AddMessage("Parameters", &ac); + ac.AddString(MDSU_NAME, PPPoE_AC_NAME_KEY); + ac.AddString(MDSU_VALUES, fPPPoEView->ACName()); + device.AddMessage(MDSU_PARAMETERS, &ac); } if(fPPPoEView->ServiceName() && strlen(fPPPoEView->ServiceName()) > 0) { // save service name, too BMessage service; - service.AddString("Name", PPPoE_SERVICE_NAME_KEY); - service.AddString("Values", fPPPoEView->ServiceName()); - device.AddMessage("Parameters", &service); + service.AddString(MDSU_NAME, PPPoE_SERVICE_NAME_KEY); + service.AddString(MDSU_VALUES, fPPPoEView->ServiceName()); + device.AddMessage(MDSU_PARAMETERS, &service); } - settings->AddMessage("Parameters", &device); + settings->AddMessage(MDSU_PARAMETERS, &device); return true; } @@ -190,7 +192,7 @@ bool PPPoEAddon::GetPreferredSize(float *width, float *height) const { float viewWidth; - if(Addons()->FindFloat("DeviceViewWidth", &viewWidth) != B_OK) + if(Addons()->FindFloat(DUN_DEVICE_VIEW_WIDTH, &viewWidth) != B_OK) viewWidth = 270; // default value @@ -208,7 +210,7 @@ PPPoEAddon::CreateView(BPoint leftTop) { if(!fPPPoEView) { float width; - if(!Addons()->FindFloat("DeviceViewWidth", &width)) + if(!Addons()->FindFloat(DUN_DEVICE_VIEW_WIDTH, &width)) width = 270; // default value @@ -303,6 +305,7 @@ PPPoEView::PPPoEView(PPPoEAddon *addon, BRect frame) PPPoEView::~PPPoEView() { + Addon()->UnregisterView(); } diff --git a/src/tests/kits/net/DialUpPreflet/PPPoEAddon.h b/src/tests/kits/net/DialUpPreflet/PPPoEAddon.h index 344bb29894..334eb22b7c 100644 --- a/src/tests/kits/net/DialUpPreflet/PPPoEAddon.h +++ b/src/tests/kits/net/DialUpPreflet/PPPoEAddon.h @@ -13,8 +13,6 @@ #include -#include -#include #include #include @@ -53,6 +51,9 @@ class PPPoEAddon : public DialUpAddon { bool saveTemporary); virtual bool GetPreferredSize(float *width, float *height) const; virtual BView *CreateView(BPoint leftTop); + + void UnregisterView() + { fPPPoEView = NULL; } private: bool fIsNew; diff --git a/src/tests/kits/net/DialUpPreflet/ProtocolsAddon.cpp b/src/tests/kits/net/DialUpPreflet/ProtocolsAddon.cpp new file mode 100644 index 0000000000..134e790880 --- /dev/null +++ b/src/tests/kits/net/DialUpPreflet/ProtocolsAddon.cpp @@ -0,0 +1,468 @@ +//----------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de +//----------------------------------------------------------------------- +// ProtocolsAddon saves the loaded settings. +// ProtocolsView saves the current settings. +//----------------------------------------------------------------------- + +#include "ProtocolsAddon.h" + +#include "InterfaceUtils.h" +#include "MessageDriverSettingsUtils.h" + +#include +#include +#include +#include +#include +#include +#include + +#include + + +#define MSG_ADD_PROTOCOL 'ADDP' +#define MSG_REMOVE_PROTOCOL 'REMP' +#define MSG_SHOW_PREFERENCES 'SHOW' +#define MSG_UPDATE_BUTTONS 'UBTN' + + +#define PROTOCOLS_TAB_PROTOCOLS "Protocols" + + +ProtocolsAddon::ProtocolsAddon(BMessage *addons) + : DialUpAddon(addons), + fProtocolsCount(0), + fSettings(NULL), + fProfile(NULL), + fProtocolsView(NULL) +{ +} + + +ProtocolsAddon::~ProtocolsAddon() +{ +} + + +bool +ProtocolsAddon::LoadSettings(BMessage *settings, BMessage *profile, bool isNew) +{ + fIsNew = isNew; + fProtocolsCount = 0; + fSettings = settings; + fProfile = profile; + + if(fProtocolsView) + fProtocolsView->Reload(); + // reset all views (empty settings) + + if(!settings || !profile || isNew) + return true; + + // ask protocols to load their settings + BMessage parameter; + for(int32 index = 0; FindMessageParameter(PPP_PROTOCOL_KEY, *fProfile, parameter, + &index); index++) + if(!LoadProtocolSettings(parameter)) + return false; + // error: some protocol did not accept its settings + + if(fProtocolsView) + fProtocolsView->Reload(); + // reload new settings + + return true; +} + + +bool +ProtocolsAddon::LoadProtocolSettings(const BMessage& parameter) +{ + // TODO: Maybe we should only load the first add-on because we cannot know + // whether the settings are acceptable for all listed protocols. + // In addition to this, maybe the protocol itself chose this format because + // it consists of two modules. + + // get protocols and ask them to load their settings + BString name; + for(int32 index = 0; parameter.FindString(MDSU_VALUES, index, &name) == B_OK; + index++) { + DialUpAddon *protocol; + if(!GetProtocol(name, &protocol)) + return false; + // fatal error: we do not know how to handle this protocol + + if(!protocol->LoadSettings(fSettings, fProfile, false)) + return false; + // error: protocol did not accept its settings + + fSettings->AddPointer(PROTOCOLS_TAB_PROTOCOLS, protocol); + ++fProtocolsCount; + } + + return true; +} + + +bool +ProtocolsAddon::HasTemporaryProfile() const +{ + if(!fProtocolsView) + return false; + + DialUpAddon *protocol; + for(int32 index = 0; index < fProtocolsView->CountProtocols(); index++) { + protocol = fProtocolsView->ProtocolAt(index); + if(protocol && protocol->HasTemporaryProfile()) + return true; + } + + return false; +} + + +void +ProtocolsAddon::IsModified(bool& settings, bool& profile) const +{ + settings = profile = false; + + if(!fSettings || !fProtocolsView) + return; + + if(CountProtocols() != fProtocolsView->CountProtocols()) { + settings = profile = true; + return; + } + + DialUpAddon *protocol; + bool protocolSettingsChanged, protocolProfileChanged; + // for current protocol + for(int32 index = 0; fSettings->FindPointer(PROTOCOLS_TAB_PROTOCOLS, index, + reinterpret_cast(&protocol)) == B_OK; index++) { + if(!protocol->KernelModuleName() // this is actually an error + || !fProtocolsView->HasProtocol(protocol->KernelModuleName())) { + settings = profile = true; + return; + } + + protocol->IsModified(protocolSettingsChanged, protocolProfileChanged); + if(protocolSettingsChanged) + settings = true; + if(protocolProfileChanged) + profile = true; + } +} + + +bool +ProtocolsAddon::SaveSettings(BMessage *settings, BMessage *profile, bool saveTemporary) +{ + if(!fSettings || !settings) + return false; + + DialUpAddon *protocol; + for(int32 index = 0; index < fProtocolsView->CountProtocols(); index++) { + protocol = fProtocolsView->ProtocolAt(index); + if(!protocol->SaveSettings(settings, profile, saveTemporary)) + return false; + } + + return true; +} + + +bool +ProtocolsAddon::GetPreferredSize(float *width, float *height) const +{ + BRect rect; + if(Addons()->FindRect(DUN_TAB_VIEW_RECT, &rect) != B_OK) + rect.Set(0, 0, 200, 300); + // set default values + + if(width) + *width = rect.Width(); + if(height) + *height = rect.Height(); + + return true; +} + + +BView* +ProtocolsAddon::CreateView(BPoint leftTop) +{ + if(!fProtocolsView) { + BRect rect; + Addons()->FindRect(DUN_TAB_VIEW_RECT, &rect); + fProtocolsView = new ProtocolsView(this, rect); + } + + fProtocolsView->MoveTo(leftTop); + fProtocolsView->Reload(); + return fProtocolsView; +} + + +bool +ProtocolsAddon::GetProtocol(const BString& moduleName, DialUpAddon **protocol) const +{ + if(!protocol) + return false; + + for(int32 index = 0; Addons()->FindPointer(DUN_PROTOCOL_ADDON_TYPE, index, + reinterpret_cast(protocol)) == B_OK; index++) + if((*protocol)->KernelModuleName() + && moduleName == (*protocol)->KernelModuleName()) + return true; + + return false; +} + + +// we need a simple BListItem that can hold a pointer to the DialUpAddon it represents +class AddonItem : public BStringItem { + public: + AddonItem(const char *label, DialUpAddon *addon) : BStringItem(label), + fAddon(addon) {} + + DialUpAddon *Addon() const + { return fAddon; } + + private: + DialUpAddon *fAddon; +}; + + +ProtocolsView::ProtocolsView(ProtocolsAddon *addon, BRect frame) + : BView(frame, "Protocols", B_FOLLOW_NONE, 0), + fAddon(addon) +{ + BRect rect = Bounds(); + rect.InsetBy(10, 10); + rect.bottom = 200; + BRect listViewRect(rect); + listViewRect.right -= B_V_SCROLL_BAR_WIDTH; + fListView = new BListView(listViewRect, "Protocols"); + fListView->SetInvocationMessage(new BMessage(MSG_SHOW_PREFERENCES)); + fListView->SetSelectionMessage(new BMessage(MSG_UPDATE_BUTTONS)); + + rect.top = rect.bottom + 10; + rect.bottom = rect.top + 25; + float buttonWidth = (rect.Width() - 20) / 3; + rect.right = rect.left + buttonWidth; + fAddButton = new BButton(rect, "AddButton", "Add Protocol", + new BMessage(MSG_ADD_PROTOCOL)); + rect.left = rect.right + 10; + rect.right = rect.left + buttonWidth; + fRemoveButton = new BButton(rect, "RemoveButton", "Remove", + new BMessage(MSG_REMOVE_PROTOCOL)); + rect.left = rect.right + 10; + rect.right = rect.left + buttonWidth; + fPreferencesButton = new BButton(rect, "PreferencesButton", "Preferences", + new BMessage(MSG_SHOW_PREFERENCES)); + + AddChild(new BScrollView("ScrollView", fListView, B_FOLLOW_NONE, 0, false, true)); + AddChild(fAddButton); + AddChild(fRemoveButton); + AddChild(fPreferencesButton); + + fProtocolsMenu = new BPopUpMenu("UnregisteredProtocols", false, false); + AddAddonsToMenu(Addon()->Addons(), fProtocolsMenu, DUN_PROTOCOL_ADDON_TYPE, 0); +} + + +ProtocolsView::~ProtocolsView() +{ + delete fProtocolsMenu; + + AddonItem *item; + while(fListView->CountItems() > 0) { + item = dynamic_cast(fListView->RemoveItem((int32) 0)); + delete item; + } +} + + +void +ProtocolsView::Reload() +{ + // move all protocols back to the menu + while(CountProtocols() > 0) + UnregisterProtocol(0); + + if(!Addon()->Settings()) + return; + + // move all registered protocols to the list + DialUpAddon *protocol; + for(int32 index = 0; Addon()->Settings()->FindPointer(PROTOCOLS_TAB_PROTOCOLS, + index, reinterpret_cast(&protocol)) == B_OK; index++) + RegisterProtocol(protocol); + + fListView->Select(0); + + UpdateButtons(); +} + + +void +ProtocolsView::AttachedToWindow() +{ + SetViewColor(Parent()->ViewColor()); + fListView->SetTarget(this); + fAddButton->SetTarget(this); + fRemoveButton->SetTarget(this); + fPreferencesButton->SetTarget(this); +} + + +void +ProtocolsView::MessageReceived(BMessage *message) +{ + switch(message->what) { + case MSG_ADD_PROTOCOL: { + BMenuItem *selected = fProtocolsMenu->Go(fAddButton->ConvertToScreen( + fAddButton->Bounds().RightTop())); + + RegisterProtocol(fProtocolsMenu->IndexOf(selected)); + + UpdateButtons(); + } break; + + case MSG_REMOVE_PROTOCOL: { + AddonItem *selected = dynamic_cast( + fListView->RemoveItem(fListView->CurrentSelection())); + + UnregisterProtocol(fListView->IndexOf(selected)); + + UpdateButtons(); + } break; + + case MSG_SHOW_PREFERENCES: { + AddonItem *selected = dynamic_cast( + fListView->ItemAt(fListView->CurrentSelection())); + + if(selected) { + DialUpAddon *addon = selected->Addon(); + addon->CreateView(ConvertToScreen(Bounds().LeftTop())); + // show the preferences window + } + } break; + + case MSG_UPDATE_BUTTONS: + UpdateButtons(); + break; + + default: + BView::MessageReceived(message); + } +} + + +DialUpAddon* +ProtocolsView::ProtocolAt(int32 index) const +{ + AddonItem *item = dynamic_cast(fListView->ItemAt(index)); + if(item) + return item->Addon(); + + return NULL; +} + + +bool +ProtocolsView::HasProtocol(const BString& moduleName) const +{ + AddonItem *item; + for(int32 index = 0; index < CountProtocols(); index++) { + item = dynamic_cast(fListView->ItemAt(index)); + if(item && moduleName == item->Addon()->KernelModuleName()) + return true; + } + + return false; +} + + +void +ProtocolsView::RegisterProtocol(const DialUpAddon *protocol) +{ + if(!protocol) + return; + + DialUpAddon *addon; + BMenuItem *item; + for(int32 index = 0; index < fProtocolsMenu->CountItems(); index++) { + item = fProtocolsMenu->ItemAt(index); + if(item && item->Message()->FindPointer("Addon", + reinterpret_cast(addon)) == B_OK && addon == protocol) { + RegisterProtocol(index); + return; + } + } +} + + +void +ProtocolsView::RegisterProtocol(int32 index) +{ + DialUpAddon *addon; + BMenuItem *remove = fProtocolsMenu->ItemAt(index); + if(!remove || remove->Message()->FindPointer("Addon", + reinterpret_cast(addon)) != B_OK) + return; + + const char *label = remove->Label(); + AddonItem *item = new AddonItem(label, addon); + + index = FindNextListInsertionIndex(fListView, label); + fListView->AddItem(item, index); + fProtocolsMenu->RemoveItem(remove); + delete remove; +} + + +void +ProtocolsView::UnregisterProtocol(int32 index) +{ + AddonItem *remove = dynamic_cast(fListView->ItemAt(index)); + if(!remove) + return; + + const char *label = remove->Text(); + BMessage *message = new BMessage(MSG_ADD_PROTOCOL); + // the 'what' field is merely set to get around the compiler warning + message->AddPointer("Addon", remove->Addon()); + BMenuItem *item = new BMenuItem(label, message); + + index = FindNextMenuInsertionIndex(fProtocolsMenu, label); + fProtocolsMenu->AddItem(item, index); + delete remove; +} + + +void +ProtocolsView::UpdateButtons() +{ + if(fProtocolsMenu->CountItems() == 0) + fAddButton->SetEnabled(false); + else + fAddButton->SetEnabled(true); + + if(CountProtocols() == 0) + fRemoveButton->SetEnabled(false); + else + fRemoveButton->SetEnabled(true); + + AddonItem *item = dynamic_cast(fListView->ItemAt( + fListView->CurrentSelection())); + float width, height; + if(!item || !item->Addon()->GetPreferredSize(&width, &height)) + fPreferencesButton->SetEnabled(false); + else + fPreferencesButton->SetEnabled(true); +} diff --git a/src/tests/kits/net/DialUpPreflet/ProtocolsAddon.h b/src/tests/kits/net/DialUpPreflet/ProtocolsAddon.h new file mode 100644 index 0000000000..650a69fd65 --- /dev/null +++ b/src/tests/kits/net/DialUpPreflet/ProtocolsAddon.h @@ -0,0 +1,100 @@ +//----------------------------------------------------------------------- +// This software is part of the OpenBeOS distribution and is covered +// by the OpenBeOS license. +// +// Copyright (c) 2003-2004 Waldemar Kornewald, Waldemar.Kornewald@web.de +//----------------------------------------------------------------------- +// ProtocolsAddon saves the loaded settings. +// ProtocolsView saves the current settings. +//----------------------------------------------------------------------- + +#ifndef _PROTOCOLS_ADDON__H +#define _PROTOCOLS_ADDON__H + +#include + +#include +#include + +class ProtocolsView; + + +class ProtocolsAddon : public DialUpAddon { + public: + ProtocolsAddon(BMessage *addons); + virtual ~ProtocolsAddon(); + + bool IsNew() const + { return fIsNew; } + + int32 CountProtocols() const + { return fProtocolsCount; } + + BMessage *Settings() const + { return fSettings; } + BMessage *Profile() const + { return fProfile; } + + virtual int32 Position() const + { return 10; } + virtual bool LoadSettings(BMessage *settings, BMessage *profile, bool isNew); + bool LoadProtocolSettings(const BMessage& parameter); + + virtual bool HasTemporaryProfile() const; + virtual void IsModified(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 GetProtocol(const BString& moduleName, DialUpAddon **protocol) const; + + private: + bool fIsNew; + int32 fProtocolsCount; + BMessage *fSettings, *fProfile; + // saves last settings state + ProtocolsView *fProtocolsView; +}; + + +class ProtocolsView : public BView { + public: + ProtocolsView(ProtocolsAddon *addon, BRect frame); + virtual ~ProtocolsView(); + + ProtocolsAddon *Addon() const + { return fAddon; } + void Reload(); + + bool HasTemporaryProfile() const; + + virtual void AttachedToWindow(); + virtual void MessageReceived(BMessage *message); + + int32 CountProtocols() const + { return fListView->CountItems(); } + DialUpAddon *ProtocolAt(int32 index) const; + bool HasProtocol(const BString& moduleName) const; + + private: + void RegisterProtocol(const DialUpAddon *protocol); + void RegisterProtocol(int32 index); + // moves the protocol from the pop-up menu to the list view + void UnregisterProtocol(int32 index); + // moves the protocol from the list view to the pop-up menu + + void UpdateButtons(); + // enables/disables buttons depending on the current state + + private: + ProtocolsAddon *fAddon; + BButton *fAddButton, *fRemoveButton, *fPreferencesButton; + BListView *fListView; + BPopUpMenu *fProtocolsMenu; +}; + + +#endif diff --git a/src/tests/kits/net/DialUpPreflet/TODO b/src/tests/kits/net/DialUpPreflet/TODO index 19b22bed19..6fb71c71d4 100644 --- a/src/tests/kits/net/DialUpPreflet/TODO +++ b/src/tests/kits/net/DialUpPreflet/TODO @@ -1,12 +1,13 @@ 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 - load add-ons - write IP (IPCP) protocol add-on - move DEVNOTES into a doxygen file and document the rest of the API - use dynamic window positioning based on screen size instead of static coordinates +- really delete interface description files when removing an interface +- open dialog asking for interface name when creating a new interface Long-term TODOs: -- allow selecting multiple authenticators +- (maybe) allow selecting multiple authenticators (in expert-mode)