From fa71797ed23ec6d7a87a96ff36f92970d06d346f Mon Sep 17 00:00:00 2001 From: Bruno Herbelin Date: Mon, 6 Dec 2021 11:41:03 +0100 Subject: [PATCH] Unified implementation saving and loading settings history files --- Settings.cpp | 222 ++++++++++++++------------------------------------- 1 file changed, 61 insertions(+), 161 deletions(-) diff --git a/Settings.cpp b/Settings.cpp index d712f51..b7e8f2b 100644 --- a/Settings.cpp +++ b/Settings.cpp @@ -34,6 +34,25 @@ using namespace tinyxml2; Settings::Application Settings::application; 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) { // impose C locale for all app @@ -201,66 +220,19 @@ void Settings::Save(uint64_t runtime) XMLElement *recent = xmlDoc.NewElement( "Recent" ); // recent session filenames - XMLElement *recentsession = xmlDoc.NewElement( "Session" ); - 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->InsertEndChild( save_history(application.recentSessions, "Session", xmlDoc)); // recent session folders - XMLElement *recentfolder = xmlDoc.NewElement( "Folder" ); - 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->InsertEndChild( save_history(application.recentFolders, "Folder", xmlDoc)); - // recent media uri - XMLElement *recentmedia = xmlDoc.NewElement( "Import" ); - 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 media uri + recent->InsertEndChild( save_history(application.recentImport, "Import", xmlDoc)); // recent import folders - XMLElement *recentimportfolder = xmlDoc.NewElement( "ImportFolder" ); - 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->InsertEndChild( save_history(application.recentImportFolders, "ImportFolder", xmlDoc)); // recent recordings - XMLElement *recentrecord = xmlDoc.NewElement( "Record" ); - 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->InsertEndChild( save_history(application.recentRecordings, "Record", xmlDoc)); // recent dialog path 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() { // impose C locale for all app @@ -490,88 +490,19 @@ void Settings::Load() if (pElement) { // recent session filenames - XMLElement * pSession = pElement->FirstChildElement("Session"); - 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); - } + load_history(application.recentSessions, "Session", pElement); + // recent session folders - XMLElement * pFolder = pElement->FirstChildElement("Folder"); - 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) ); - } - } + load_history(application.recentFolders, "Folder", pElement); + // recent media uri - XMLElement * pImport = pElement->FirstChildElement("Import"); - 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) ); - } - } + load_history(application.recentImport, "Import", pElement); // recent import folders - XMLElement * pImportFolder = pElement->FirstChildElement("ImportFolder"); - 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) ); - } - } + load_history(application.recentImportFolders, "ImportFolder", pElement); + // recent recordings - XMLElement * pRecord = pElement->FirstChildElement("Record"); - 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); - } + load_history(application.recentRecordings, "Record", pElement); // recent dialog path 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() {