Network prefs: save interfaces to file.

Fixes #11496.
This commit is contained in:
Adrien Destugues 2014-11-25 14:47:32 +01:00
parent af218cc4e1
commit b2e91cc3e7
5 changed files with 104 additions and 11 deletions

View File

@ -22,10 +22,13 @@
#include <Button.h>
#include <Catalog.h>
#include <ControlLook.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <GroupLayout.h>
#include <GroupLayoutBuilder.h>
#include <ListItem.h>
#include <ListView.h>
#include <Path.h>
#include <ScrollView.h>
@ -99,7 +102,34 @@ InterfacesAddOn::Save()
InterfaceView* settingsView = _SettingsView();
if (settingsView != NULL)
settingsView->Apply();
return fListView->SaveItems();
BString settingsData;
status_t result = fListView->SaveItems(settingsData);
if (result != B_OK)
return result;
if (settingsData.IsEmpty()) {
// Nothing to save, all interfaces are auto-configured
return B_OK;
}
BPath path;
result = find_directory(B_SYSTEM_SETTINGS_DIRECTORY, &path, true);
if (result != B_OK)
return result;
path.Append("network");
create_directory(path.Path(), 0755);
path.Append("interfaces");
BFile settingsFile(path.Path(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
result = settingsFile.Write(settingsData.String(),settingsData.Length());
if (result != settingsData.Length())
return result;
return B_OK;
}

View File

@ -492,7 +492,7 @@ InterfacesListView::FindItem(BPoint where)
status_t
InterfacesListView::SaveItems()
InterfacesListView::SaveItems(BString& settingsData)
{
// Grab each fSettings instance and write it's settings
for (int32 i = CountItems(); i-- > 0;) {
@ -504,8 +504,8 @@ InterfacesListView::SaveItems()
continue;
NetworkSettings* ns = item->GetSettings();
// TODO : SetConfiguration doesn't use the interface settings file
ns->SetConfiguration();
settingsData << ns->GenerateConfiguration();
}
return B_OK;

View File

@ -70,7 +70,7 @@ public:
InterfaceListItem* FindItem(const char* name);
InterfaceListItem* FindItem(BPoint where);
status_t SaveItems();
status_t SaveItems(BString& settingsData);
protected:
virtual void AttachedToWindow();

View File

@ -339,13 +339,76 @@ NetworkSettings::LoadConfiguration()
}
/*! WriteConfiguration reads this classes settings and write them to
the current interface configuration file that NetServer watches for.
/*! GenerateConfiguration reads this classes settings and write them to
a BString. This can then be put in the interfaces settings file to make
the settings persistant.
*/
void
NetworkSettings::WriteConfiguration()
BString
NetworkSettings::GenerateConfiguration()
{
// TODO should use ProtocolVersions to know which protocols are there
bool autoConfigure = true;
if (!IsDisabled()) {
for (int i = 0; i < MAX_PROTOCOLS; i++) {
if (fProtocols[i].present && strlen(IP(fProtocols[i].inet_id)) != 0
&& !AutoConfigure(fProtocols[i].inet_id)) {
autoConfigure = false;
}
}
}
// If the interface is fully autoconfigured, don't include it in the
// settings file.
if (autoConfigure)
return BString();
BString result;
result.SetToFormat("interface %s {\n", Name());
if (IsDisabled())
result << "\tdisabled\ttrue\n";
else {
for (int i = 0; i < MAX_PROTOCOLS; i++) {
if (fProtocols[i].present && strlen(IP(fProtocols[i].inet_id)) != 0
&& !AutoConfigure(fProtocols[i].inet_id)) {
result << "\taddress {\n";
result << "\t\tfamily\t";
if (fProtocols[i].inet_id == AF_INET)
result << "inet\n";
else if (fProtocols[i].inet_id == AF_INET6)
result << "inet6\n";
else {
// FIXME the struct protocols should know the name to use.
debugger("Unknown protocol found!");
}
result << "\t\taddress\t" << IP(fProtocols[i].inet_id) << "\n";
result << "\t\tgateway\t" << Gateway(fProtocols[i].inet_id)
<< "\n";
result << "\t\tmask\t" << Netmask(fProtocols[i].inet_id)
<< "\n";
result << "\t}\n";
}
}
}
#if 0
if (!networks.empty()) {
for (size_t index = 0; index < networks.size(); index++) {
wireless_network& network = networks[index];
fprintf(fp, "\tnetwork %s {\n", network.name);
fprintf(fp, "\t\tmac\t%s\n",
network.address.ToString().String());
fprintf(fp, "\t}\n");
}
}
#endif
result << "}\n\n";
return result;
}

View File

@ -109,10 +109,10 @@ public:
void ReadConfiguration();
void SetConfiguration();
/* Grab and write interface settings from interface configuration
file and let NetServer sort it out */
/* Grab interface settings from interface configuration file */
void LoadConfiguration();
void WriteConfiguration();
/* Generate settings file entry suitable for interfaces settings */
BString GenerateConfiguration();
private:
status_t _DetectProtocols();