From eb89d62ca58b8e43d35f19871ab1f11c5d5b1a4d Mon Sep 17 00:00:00 2001 From: Waldemar Kornewald Date: Mon, 27 Dec 2004 14:55:07 +0000 Subject: [PATCH] Implemented support for default interface. Not tested. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@10540 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../kits/net/DialUpPreflet/DialUpView.cpp | 121 ++++++++++++++---- src/tests/kits/net/DialUpPreflet/DialUpView.h | 2 + .../kits/net/DialUpPreflet/PTPSettings.cpp | 60 +++++++-- .../kits/net/DialUpPreflet/PTPSettings.h | 5 + 4 files changed, 153 insertions(+), 35 deletions(-) diff --git a/src/tests/kits/net/DialUpPreflet/DialUpView.cpp b/src/tests/kits/net/DialUpPreflet/DialUpView.cpp index ccf5493586..a3810a78aa 100644 --- a/src/tests/kits/net/DialUpPreflet/DialUpView.cpp +++ b/src/tests/kits/net/DialUpPreflet/DialUpView.cpp @@ -3,12 +3,6 @@ * Distributed under the terms of the MIT License. */ -/* - TODO: - - finish |[ ] "Default" | interface handling - - if no interface is default a newly created one becomes default -*/ - #include "DialUpView.h" #include "DialUpAddon.h" @@ -48,6 +42,7 @@ static const uint32 kMsgFinishCreateNew = 'FNEW'; static const uint32 kMsgDeleteCurrent = 'DELI'; static const uint32 kMsgSelectInterface = 'SELI'; static const uint32 kMsgConnectButton = 'CONI'; +static const uint32 kMsgUpdateDefaultInterface = 'UPDT'; // labels #ifdef LANG_GERMAN @@ -158,7 +153,8 @@ DialUpView::DialUpView(BRect frame) rect.bottom -= 2; rect.left = rect.right + 5; rect.right = bounds.right - 5; - fDefaultInterface = new BCheckBox(rect, "Default", kLabelDefaultInterface, NULL); + fDefaultInterface = new BCheckBox(rect, "Default", kLabelDefaultInterface, + new BMessage(kMsgUpdateDefaultInterface)); rect.left = bounds.left + 5; rect.top = rect.bottom + 12; rect.bottom = bounds.bottom @@ -266,6 +262,12 @@ DialUpView::MessageReceived(BMessage *message) fCurrentItem->SetMarked(true); UpdateControls(); + + // a newly created interface is set to default if there is no default one + if(!fSettings.DefaultInterface()) { + fDefaultInterface->SetValue(true); + fSettings.SetDefaultInterface(name); + } } break; // ------------------------------------------------- @@ -273,13 +275,17 @@ DialUpView::MessageReceived(BMessage *message) if(!fCurrentItem) return; + const char *name = fCurrentItem->Message()->FindString("name"); + if(fSettings.DefaultInterface() && !strcmp(fSettings.DefaultInterface(), + name)) + fSettings.SetDefaultInterface(NULL); fInterfaceMenu->RemoveItem(fCurrentItem); BDirectory settings, profile; fSettings.GetPTPDirectories(&settings, &profile); BEntry entry; - settings.FindEntry(fCurrentItem->Label(), &entry); + settings.FindEntry(name, &entry); entry.Remove(); - profile.FindEntry(fCurrentItem->Label(), &entry); + profile.FindEntry(name, &entry); entry.Remove(); delete fCurrentItem; fCurrentItem = NULL; @@ -308,6 +314,10 @@ DialUpView::MessageReceived(BMessage *message) resume_thread(fUpDownThread); } break; + case kMsgUpdateDefaultInterface: + UpdateDefaultInterface(); + break; + default: BView::MessageReceived(message); } @@ -324,16 +334,18 @@ DialUpView::UpDownThread() driver_settings *temporaryProfile = MessageToDriverSettings(profile); PPPInterface interface; - if(fWatching == PPP_UNDEFINED_INTERFACE_ID) { - interface = fListener.Manager().InterfaceWithName(fCurrentItem->Label()); - if(interface.InitCheck() != B_OK) - interface = fListener.Manager().CreateInterfaceWithName( - fCurrentItem->Label(), temporaryProfile); - } else { - interface = fWatching; - interface.SetProfile(temporaryProfile); - } + ppp_interface_info_t info; + // if going up: delete interface in order for the settings change to take effect + interface = fListener.Manager().InterfaceWithName( + fCurrentItem->Message()->FindString("name")); + interface.GetInterfaceInfo(&info); + if(interface.InitCheck() == B_OK && info.info.phase == PPP_DOWN_PHASE) + fListener.Manager().DeleteInterface(interface.ID()); + + interface = fListener.Manager().CreateInterfaceWithName( + fCurrentItem->Message()->FindString("name")); + interface.SetProfile(temporaryProfile); free_driver_settings(temporaryProfile); if(interface.InitCheck() != B_OK) { @@ -343,7 +355,6 @@ DialUpView::UpDownThread() return; } - ppp_interface_info_t info; interface.GetInterfaceInfo(&info); if(info.info.phase == PPP_DOWN_PHASE) interface.Up(); @@ -381,7 +392,7 @@ DialUpView::HandleReportMessage(BMessage *message) ppp_interface_info_t info; interface.GetInterfaceInfo(&info); - if(strcasecmp(info.info.name, fCurrentItem->Label())) + if(strcasecmp(info.info.name, fCurrentItem->Message()->FindString("name"))) return; WatchInterface(id); @@ -394,7 +405,8 @@ DialUpView::HandleReportMessage(BMessage *message) if(fWatching == PPP_UNDEFINED_INTERFACE_ID) return; - WatchInterface(fListener.Manager().InterfaceWithName(fCurrentItem->Label())); + WatchInterface(fListener.Manager().InterfaceWithName( + fCurrentItem->Message()->FindString("name"))); } } @@ -573,13 +585,18 @@ DialUpView::LoadInterfaces() void DialUpView::AddInterface(const char *name, bool isNew = false) { - if(fInterfaceMenu->FindItem(name)) { + if(FindInterface(name)) { (new BAlert(kErrorTitle, kErrorInterfaceExists, kLabelOK, NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go(NULL); return; } - BMenuItem *item = new BMenuItem(name, new BMessage(kMsgSelectInterface)); + BMessage *message = new BMessage(kMsgSelectInterface); + message->AddString("name", name); + BString label(name); + if(fSettings.DefaultInterface() && label == fSettings.DefaultInterface()) + label << " (" << kLabelDefaultInterface << ")"; + BMenuItem *item = new BMenuItem(label.String(), message); item->SetTarget(this); int32 index = FindNextMenuInsertionIndex(fInterfaceMenu, name); if(index > CountInterfaces()) @@ -617,9 +634,14 @@ DialUpView::SelectInterface(int32 index, bool isNew = false) return; } + const char *name = fCurrentItem->Message() ? + fCurrentItem->Message()->FindString("name") : NULL; + fDefaultInterface->SetValue(fSettings.DefaultInterface() && name + && !strcmp(fSettings.DefaultInterface(), name)); fCurrentItem->SetMarked(true); fDeleterItem->SetEnabled(true); - WatchInterface(fListener.Manager().InterfaceWithName(fCurrentItem->Label())); + fInterfaceMenu->Superitem()->SetLabel(name); + WatchInterface(fListener.Manager().InterfaceWithName(name)); } UpdateControls(); @@ -627,11 +649,13 @@ DialUpView::SelectInterface(int32 index, bool isNew = false) if(!fCurrentItem) fSettings.LoadSettings(NULL, false); // tell modules to unload all settings - else if(!isNew && !fSettings.LoadSettings(fCurrentItem->Label(), false)) { + else if(!isNew && !fSettings.LoadSettings( + fCurrentItem->Message()->FindString("name"), false)) { (new BAlert(kErrorTitle, kErrorLoadingFailed, kLabelOK, NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go(NULL); - fSettings.LoadSettings(fCurrentItem->Label(), true); - } else if(isNew && !fSettings.LoadSettings(fCurrentItem->Label(), true)) + fSettings.LoadSettings(fCurrentItem->Message()->FindString("name"), true); + } else if(isNew && !fSettings.LoadSettings( + fCurrentItem->Message()->FindString("name"), true)) (new BAlert(kErrorTitle, kErrorLoadingFailed, kLabelOK, NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go(NULL); } @@ -644,6 +668,23 @@ DialUpView::CountInterfaces() const } +BMenuItem* +DialUpView::FindInterface(const char *name) +{ + if(!name) + return NULL; + + BMenuItem *item; + for(int32 index = 0; index < fInterfaceMenu->CountItems(); index++) { + item = fInterfaceMenu->ItemAt(index); + if(item && !strcmp(item->Message()->FindString("name"), name)) + return item; + } + + return NULL; +} + + void DialUpView::UpdateControls() { @@ -652,17 +693,20 @@ DialUpView::UpdateControls() fStringView->Hide(); fCreateNewButton->Hide(); fTabView->Show(); + fDefaultInterface->Show(); fConnectButton->SetEnabled(true); } else if(!fTabView->IsHidden() && CountInterfaces() == 0) { fDeleterItem->SetEnabled(false); fInterfaceMenu->SetRadioMode(false); fInterfaceMenu->Superitem()->SetLabel(kLabelCreateNew); fTabView->Hide(); + fDefaultInterface->Hide(); fStringView->Show(); fCreateNewButton->Show(); fConnectButton->SetEnabled(false); } + // move default checkbox next to interface menu (its size might have changed) float width = fInterfaceMenu->StringWidth(fMenuField->Label()) + fInterfaceMenu->StringWidth(fInterfaceMenu->Superitem()->Label()) + 30; if(width > kInterfaceFieldWidth) @@ -670,3 +714,26 @@ DialUpView::UpdateControls() fDefaultInterface->MoveTo(fMenuField->Frame().left + width, fDefaultInterface->Frame().top); } + + +void +DialUpView::UpdateDefaultInterface() +{ + const char *name = fCurrentItem->Message()->FindString("name"); + BMenuItem *defaultItem = FindInterface(fSettings.DefaultInterface()); + if(fDefaultInterface->Value()) { + if(!fSettings.SetDefaultInterface(name)) { + fDefaultInterface->SetValue(0); + return; + } + if(defaultItem) + defaultItem->SetLabel(defaultItem->Message()->FindString("name")); + + BString label(name); + label << " (" << kLabelDefaultInterface << ")"; + fCurrentItem->SetLabel(label.String()); + } else { + fSettings.SetDefaultInterface(NULL); + fCurrentItem->SetLabel(name); + } +} diff --git a/src/tests/kits/net/DialUpPreflet/DialUpView.h b/src/tests/kits/net/DialUpPreflet/DialUpView.h index 2d009d7d3b..a27568118d 100644 --- a/src/tests/kits/net/DialUpPreflet/DialUpView.h +++ b/src/tests/kits/net/DialUpPreflet/DialUpView.h @@ -38,8 +38,10 @@ class DialUpView : public BView { void AddInterface(const char *name, bool isNew = false); void SelectInterface(int32 index, bool isNew = false); int32 CountInterfaces() const; + BMenuItem *FindInterface(const char *name); void UpdateControls(); + void UpdateDefaultInterface(); private: PPPInterfaceListener fListener; diff --git a/src/tests/kits/net/DialUpPreflet/PTPSettings.cpp b/src/tests/kits/net/DialUpPreflet/PTPSettings.cpp index 929f9fae88..00d3d16749 100644 --- a/src/tests/kits/net/DialUpPreflet/PTPSettings.cpp +++ b/src/tests/kits/net/DialUpPreflet/PTPSettings.cpp @@ -17,15 +17,22 @@ #include #include +#include PTPSettings::PTPSettings() { + void *handle = load_driver_settings("ptpnet.settings"); + const char *name = get_driver_parameter(handle, "default", NULL, NULL); + fDefaultInterface = name ? strdup(name) : NULL; + unload_driver_settings(handle); } PTPSettings::~PTPSettings() { + free(fDefaultInterface); + // free known add-on types (these should free their known add-on types, etc.) DialUpAddon *addon; for(int32 index = 0; @@ -36,15 +43,47 @@ PTPSettings::~PTPSettings() } +bool +PTPSettings::SetDefaultInterface(const char *name) +{ + // load current settings and replace value of "default" with + BMessage settings; + if(!ReadMessageDriverSettings("ptpnet.settings", &settings)) + return false; + + BMessage parameter; + int32 index = 0; + if(FindMessageParameter("default", settings, ¶meter, &index)) + settings.RemoveData(MDSU_PARAMETERS, index); + + parameter.MakeEmpty(); + if(name) { + parameter.AddString(MDSU_VALUES, name); + settings.AddMessage(MDSU_PARAMETERS, ¶meter); + } + + BFile file(PTP_SETTINGS_PATH, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE); + if(file.InitCheck() != B_OK) + return false; + + if(WriteMessageDriverSettings(file, settings)) { + free(fDefaultInterface); + fDefaultInterface = name ? strdup(name) : NULL; + return true; + } else + return false; +} + + bool PTPSettings::GetPTPDirectories(BDirectory *settingsDirectory, BDirectory *profileDirectory) const { if(settingsDirectory) { - BDirectory settings(PPP_INTERFACE_SETTINGS_PATH); + BDirectory settings(PTP_INTERFACE_SETTINGS_PATH); if(settings.InitCheck() != B_OK) { - create_directory(PPP_INTERFACE_SETTINGS_PATH, 0750); - settings.SetTo(PPP_INTERFACE_SETTINGS_PATH); + create_directory(PTP_INTERFACE_SETTINGS_PATH, 0750); + settings.SetTo(PTP_INTERFACE_SETTINGS_PATH); if(settings.InitCheck() != B_OK) return false; } @@ -53,10 +92,10 @@ PTPSettings::GetPTPDirectories(BDirectory *settingsDirectory, } if(profileDirectory) { - BDirectory profile(PPP_INTERFACE_SETTINGS_PATH "/profile"); + BDirectory profile(PTP_INTERFACE_SETTINGS_PATH "/profile"); if(profile.InitCheck() != B_OK) { - create_directory(PPP_INTERFACE_SETTINGS_PATH "/profile", 0750); - profile.SetTo(PPP_INTERFACE_SETTINGS_PATH "/profile"); + create_directory(PTP_INTERFACE_SETTINGS_PATH "/profile", 0750); + profile.SetTo(PTP_INTERFACE_SETTINGS_PATH "/profile"); if(profile.InitCheck() != B_OK) return false; } @@ -81,11 +120,11 @@ PTPSettings::LoadSettings(const char *interfaceName, bool isNew) *profilePointer = interfaceName ? &fProfile : NULL; if(interfaceName && !isNew) { - BString name("pppidf/"); + BString name("ptpnet/"); name << fCurrent; if(!ReadMessageDriverSettings(name.String(), &fSettings)) return false; - name = "pppidf/profile/"; + name = "ptpnet/profile/"; name << fCurrent; if(!ReadMessageDriverSettings(name.String(), &fProfile)) profilePointer = settingsPointer; @@ -151,6 +190,11 @@ PTPSettings::SaveSettings(BMessage *settings, BMessage *profile, bool saveTempor addons.AddItem(addon, insertIndex); } + // prepare for next release with support different PTP types + BMessage parameter; + parameter.AddString(MDSU_NAME, "PTPType"); + parameter.AddString(MDSU_VALUES, "ppp"); + settings->AddMessage(MDSU_PARAMETERS, ¶meter); settings->AddString("InterfaceName", fCurrent); for(int32 index = 0; index < addons.CountItems(); index++) diff --git a/src/tests/kits/net/DialUpPreflet/PTPSettings.h b/src/tests/kits/net/DialUpPreflet/PTPSettings.h index b5226329aa..53fd02959a 100644 --- a/src/tests/kits/net/DialUpPreflet/PTPSettings.h +++ b/src/tests/kits/net/DialUpPreflet/PTPSettings.h @@ -20,6 +20,10 @@ class PTPSettings { const BString& CurrentInterface() const { return fCurrent; } + bool SetDefaultInterface(const char *name); + const char *DefaultInterface() const + { return fDefaultInterface; } + bool GetPTPDirectories(BDirectory *settingsDirectory, BDirectory *profileDirectory) const; @@ -34,6 +38,7 @@ class PTPSettings { private: BMessage fAddons, fSettings, fProfile; BString fCurrent; + char *fDefaultInterface; };