Unified implementation saving and loading settings history files

This commit is contained in:
Bruno Herbelin
2021-12-06 11:41:03 +01:00
parent 8c63552573
commit fa71797ed2

View File

@@ -34,6 +34,25 @@ using namespace tinyxml2;
Settings::Application Settings::application; Settings::Application Settings::application;
string settingsFilename = ""; string settingsFilename = "";
XMLElement *save_history(Settings::History &h, const char *nodename, XMLDocument &xmlDoc)
{
XMLElement *pElement = xmlDoc.NewElement( nodename );
pElement->SetAttribute("path", h.path.c_str());
pElement->SetAttribute("autoload", h.load_at_start);
pElement->SetAttribute("autosave", h.save_on_exit);
pElement->SetAttribute("valid", h.front_is_valid);
for(auto it = h.filenames.cbegin();
it != h.filenames.cend(); ++it) {
XMLElement *fileNode = xmlDoc.NewElement("path");
XMLText *text = xmlDoc.NewText( (*it).c_str() );
fileNode->InsertEndChild( text );
pElement->InsertFirstChild(fileNode);
}
return pElement;
}
void Settings::Save(uint64_t runtime) void Settings::Save(uint64_t runtime)
{ {
// impose C locale for all app // impose C locale for all app
@@ -201,66 +220,19 @@ void Settings::Save(uint64_t runtime)
XMLElement *recent = xmlDoc.NewElement( "Recent" ); XMLElement *recent = xmlDoc.NewElement( "Recent" );
// recent session filenames // recent session filenames
XMLElement *recentsession = xmlDoc.NewElement( "Session" ); recent->InsertEndChild( save_history(application.recentSessions, "Session", xmlDoc));
recentsession->SetAttribute("path", application.recentSessions.path.c_str());
recentsession->SetAttribute("autoload", application.recentSessions.load_at_start);
recentsession->SetAttribute("autosave", application.recentSessions.save_on_exit);
recentsession->SetAttribute("valid", application.recentSessions.front_is_valid);
for(auto it = application.recentSessions.filenames.cbegin();
it != application.recentSessions.filenames.cend(); ++it) {
XMLElement *fileNode = xmlDoc.NewElement("path");
XMLText *text = xmlDoc.NewText( (*it).c_str() );
fileNode->InsertEndChild( text );
recentsession->InsertFirstChild(fileNode);
};
recent->InsertEndChild(recentsession);
// recent session folders // recent session folders
XMLElement *recentfolder = xmlDoc.NewElement( "Folder" ); recent->InsertEndChild( save_history(application.recentFolders, "Folder", xmlDoc));
for(auto it = application.recentFolders.filenames.cbegin();
it != application.recentFolders.filenames.cend(); ++it) {
XMLElement *fileNode = xmlDoc.NewElement("path");
XMLText *text = xmlDoc.NewText( (*it).c_str() );
fileNode->InsertEndChild( text );
recentfolder->InsertFirstChild(fileNode);
};
recent->InsertEndChild(recentfolder);
// recent media uri // recent import media uri
XMLElement *recentmedia = xmlDoc.NewElement( "Import" ); recent->InsertEndChild( save_history(application.recentImport, "Import", xmlDoc));
recentmedia->SetAttribute("path", application.recentImport.path.c_str());
for(auto it = application.recentImport.filenames.cbegin();
it != application.recentImport.filenames.cend(); ++it) {
XMLElement *fileNode = xmlDoc.NewElement("path");
XMLText *text = xmlDoc.NewText( (*it).c_str() );
fileNode->InsertEndChild( text );
recentmedia->InsertFirstChild(fileNode);
}
recent->InsertEndChild(recentmedia);
// recent import folders // recent import folders
XMLElement *recentimportfolder = xmlDoc.NewElement( "ImportFolder" ); recent->InsertEndChild( save_history(application.recentImportFolders, "ImportFolder", xmlDoc));
recentmedia->SetAttribute("path", application.recentImportFolders.path.c_str());
for(auto it = application.recentImportFolders.filenames.cbegin();
it != application.recentImportFolders.filenames.cend(); ++it) {
XMLElement *pathNode = xmlDoc.NewElement("path");
XMLText *text = xmlDoc.NewText( (*it).c_str() );
pathNode->InsertEndChild( text );
recentimportfolder->InsertFirstChild(pathNode);
}
recent->InsertEndChild(recentimportfolder);
// recent recordings // recent recordings
XMLElement *recentrecord = xmlDoc.NewElement( "Record" ); recent->InsertEndChild( save_history(application.recentRecordings, "Record", xmlDoc));
recentrecord->SetAttribute("autoload", application.recentRecordings.load_at_start);
for(auto it = application.recentRecordings.filenames.cbegin();
it != application.recentRecordings.filenames.cend(); ++it) {
XMLElement *pathNode = xmlDoc.NewElement("path");
XMLText *text = xmlDoc.NewText( (*it).c_str() );
pathNode->InsertEndChild( text );
recentrecord->InsertFirstChild(pathNode);
}
recent->InsertEndChild(recentrecord);
// recent dialog path // recent dialog path
XMLElement *recentdialogpath = xmlDoc.NewElement( "Dialog" ); XMLElement *recentdialogpath = xmlDoc.NewElement( "Dialog" );
@@ -296,6 +268,34 @@ void Settings::Save(uint64_t runtime)
} }
void load_history(Settings::History &h, const char *nodename, XMLElement *root)
{
XMLElement * pElement = root->FirstChildElement(nodename);
if (pElement)
{
// list of path
h.filenames.clear();
XMLElement* path = pElement->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())
{
const char *p = path->GetText();
if (p)
h.push( std::string (p) );
}
// path attribute
const char *path_ = pElement->Attribute("path");
if (path_)
h.path = std::string(path_);
else
h.path = SystemToolkit::home_path();
// other attritutes
pElement->QueryBoolAttribute("autoload", &h.load_at_start);
pElement->QueryBoolAttribute("autosave", &h.save_on_exit);
pElement->QueryBoolAttribute("valid", &h.front_is_valid);
}
}
void Settings::Load() void Settings::Load()
{ {
// impose C locale for all app // impose C locale for all app
@@ -490,88 +490,19 @@ void Settings::Load()
if (pElement) if (pElement)
{ {
// recent session filenames // recent session filenames
XMLElement * pSession = pElement->FirstChildElement("Session"); load_history(application.recentSessions, "Session", pElement);
if (pSession)
{
const char *path_ = pSession->Attribute("path");
if (path_)
application.recentSessions.path = std::string(path_);
else
application.recentSessions.path = SystemToolkit::home_path();
application.recentSessions.filenames.clear();
XMLElement* path = pSession->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())
{
const char *p = path->GetText();
if (p)
application.recentSessions.push( std::string (p) );
}
pSession->QueryBoolAttribute("autoload", &application.recentSessions.load_at_start);
pSession->QueryBoolAttribute("autosave", &application.recentSessions.save_on_exit);
pSession->QueryBoolAttribute("valid", &application.recentSessions.front_is_valid);
}
// recent session folders // recent session folders
XMLElement * pFolder = pElement->FirstChildElement("Folder"); load_history(application.recentFolders, "Folder", pElement);
if (pFolder)
{
application.recentFolders.filenames.clear();
XMLElement* path = pFolder->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())
{
const char *p = path->GetText();
if (p)
application.recentFolders.push( std::string (p) );
}
}
// recent media uri // recent media uri
XMLElement * pImport = pElement->FirstChildElement("Import"); load_history(application.recentImport, "Import", pElement);
if (pImport)
{
const char *path_ = pImport->Attribute("path");
if (path_)
application.recentImport.path = std::string(path_);
else
application.recentImport.path = SystemToolkit::home_path();
application.recentImport.filenames.clear();
XMLElement* path = pImport->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())
{
const char *p = path->GetText();
if (p)
application.recentImport.push( std::string (p) );
}
}
// recent import folders // recent import folders
XMLElement * pImportFolder = pElement->FirstChildElement("ImportFolder"); load_history(application.recentImportFolders, "ImportFolder", pElement);
if (pImportFolder)
{
const char *path_ = pImportFolder->Attribute("path");
if (path_)
application.recentImportFolders.path = std::string(path_);
application.recentImportFolders.filenames.clear();
XMLElement* path = pImportFolder->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())
{
const char *p = path->GetText();
if (p)
application.recentImportFolders.push( std::string (p) );
}
}
// recent recordings // recent recordings
XMLElement * pRecord = pElement->FirstChildElement("Record"); load_history(application.recentRecordings, "Record", pElement);
if (pRecord)
{
application.recentRecordings.filenames.clear();
XMLElement* path = pRecord->FirstChildElement("path");
for( ; path ; path = path->NextSiblingElement())
{
const char *p = path->GetText();
if (p)
application.recentRecordings.push( std::string (p) );
}
pRecord->QueryBoolAttribute("autoload", &application.recentRecordings.load_at_start);
}
// recent dialog path // recent dialog path
XMLElement * pDialog = pElement->FirstChildElement("Dialog"); XMLElement * pDialog = pElement->FirstChildElement("Dialog");
@@ -638,37 +569,6 @@ void Settings::History::validate()
} }
} }
// TODO unified history function saving
//void load_history(Settings::History &h, XMLElement *root)
//{
// XMLElement * pElement = root->FirstChildElement("Import");
// if (pElement)
// {
// h.filenames.clear();
// XMLElement* path = pElement->FirstChildElement("path");
// for( ; path ; path = path->NextSiblingElement())
// {
// const char *p = path->GetText();
// if (p)
// h.push( std::string (p) );
// }
// }
//}
//XMLElement *save_history(Settings::History &h, XMLDocument &xmlDoc)
//{
// XMLElement *pElement = xmlDoc.NewElement( "Import" );
// for(auto it = h.filenames.cbegin();
// it != h.filenames.cend(); ++it) {
// XMLElement *fileNode = xmlDoc.NewElement("path");
// XMLText *text = xmlDoc.NewText( (*it).c_str() );
// fileNode->InsertEndChild( text );
// pElement->InsertFirstChild(fileNode);
// }
// return pElement;
//}
void Settings::Lock() void Settings::Lock()
{ {