Skip to content

Commit

Permalink
Added option to specify the project file xml ident character (issue #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobento512 committed Jan 20, 2019
1 parent 18a9202 commit b08fd8f
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 109 deletions.
1 change: 1 addition & 0 deletions Widgets/frequesttreewidgetprojectitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class FRequestTreeWidgetProjectItem : public FRequestTreeWidgetItem
QString projectMainUrl = "http://localhost/";
// can't use optional because it is an abstract base class, using unique_ptr as nullptr as alternative
std::shared_ptr<FRequestAuthentication> authData = nullptr;
UtilFRequest::IdentCharacter saveIdentCharacter = UtilFRequest::IdentCharacter::SPACE; // space by default
private:
QString uuid;
QHash<QString, FRequestTreeWidgetRequestItem *> mapOfChilds_UuidToRequest;
Expand Down
6 changes: 3 additions & 3 deletions XmlParsers/configfilefrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ void ConfigFileFRequest::upgradeConfigFileIfNecessary(){
// Add new attribute that replaces 'askToOpenLastProject'

// use ASK_TO_LOAD_LAST_PROJECT as default
generalNode.append_attribute("onStartupOption").set_value(static_cast<int>(OnStartupOption::ASK_TO_LOAD_LAST_PROJECT));
generalNode.append_attribute("onStartupOption").set_value("0"); // 0 = ASK_TO_LOAD_LAST_PROJECT in 1.1a

// Add new attribute that specifies the max response size for display in ui
generalNode.append_attribute("maxRequestResponseDataSizeToDisplay").set_value(200); // 200 kb default
Expand All @@ -598,7 +598,7 @@ void ConfigFileFRequest::upgradeConfigFileIfNecessary(){
pugi::xml_node generalNode = doc.select_single_node("/FRequestConfig/General").node();

// Add new attribute for themes
generalNode.append_attribute("theme").set_value(static_cast<int>(FRequestTheme::OS_DEFAULT));
generalNode.append_attribute("theme").set_value("0"); // 0 = OS_DEFAULT in 1.1b

fSaveFileAfterUpgrade(this->fileFullPath, versionAfterUpgrade);

Expand Down Expand Up @@ -641,4 +641,4 @@ QString ConfigFileFRequest::getFRequestThemeString(const FRequestTheme currentFR
exit(1);
}
}
}
}
29 changes: 25 additions & 4 deletions XmlParsers/projectfilefrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ ProjectFileFRequest::ProjectData ProjectFileFRequest::readProjectDataFromFile(co
currentProjectData.mainUrl = doc.select_node("/FRequestProject/@mainUrl").attribute().value();
currentProjectData.projectName = doc.select_node("/FRequestProject/@name").attribute().value();
currentProjectData.projectUuid = doc.select_node("/FRequestProject/@uuid").attribute().value();
currentProjectData.saveIdentCharacter = static_cast<UtilFRequest::IdentCharacter>(doc.select_node("/FRequestProject/@saveIdentCharacter").attribute().as_int());

// Check if there is authentication data and load it
pugi::xml_node authNode = doc.select_node("/FRequestProject/Authentication").node();
Expand Down Expand Up @@ -216,6 +217,7 @@ void ProjectFileFRequest::saveProjectDataToFile(const QString &fileFullPath, con
createOrGetPugiXmlAttribute(rootNode, "name").set_value(QSTR_TO_CSTR(newProjectData.projectName));
createOrGetPugiXmlAttribute(rootNode, "mainUrl").set_value(QSTR_TO_CSTR(newProjectData.mainUrl));
createOrGetPugiXmlAttribute(rootNode, "uuid").set_value(QSTR_TO_CSTR(newProjectData.projectUuid));
createOrGetPugiXmlAttribute(rootNode, "saveIdentCharacter").set_value(static_cast<int>(newProjectData.saveIdentCharacter));

// Delete old auth data if exists (we always need to rebuild it
rootNode.remove_child("Authentication");
Expand Down Expand Up @@ -337,7 +339,9 @@ void ProjectFileFRequest::saveProjectDataToFile(const QString &fileFullPath, con

}

if(!doc.save_file(fileFullPath.toUtf8().constData(), PUGIXML_TEXT("\t"), pugi::format_default | pugi::format_write_bom, pugi::xml_encoding::encoding_utf8)){
const pugi::char_t* const identCharacterChar = newProjectData.saveIdentCharacter == UtilFRequest::IdentCharacter::SPACE ? pugiIdentChars::spaceChar : pugiIdentChars::tabChar;

if(!doc.save_file(fileFullPath.toUtf8().constData(), identCharacterChar, pugi::format_default | pugi::format_write_bom, pugi::xml_encoding::encoding_utf8)){
throw std::runtime_error("An error ocurred while trying to save the project file. Please try another path.");
}
}
Expand Down Expand Up @@ -375,8 +379,8 @@ void ProjectFileFRequest::upgradeProjectFileIfNecessary(const QString &filePath)
}
}

auto fSaveFileAfterUpgrade = [&doc](const QString &fileFullPath, const QString &upgradeVersion){
if(!doc.save_file(QSTR_TO_CSTR(fileFullPath), PUGIXML_TEXT("\t"), pugi::format_default | pugi::format_write_bom, pugi::xml_encoding::encoding_utf8)){
auto fSaveFileAfterUpgrade = [&doc](const QString &fileFullPath, const QString &upgradeVersion, const pugi::char_t* const identChar){
if(!doc.save_file(QSTR_TO_CSTR(fileFullPath), identChar, pugi::format_default | pugi::format_write_bom, pugi::xml_encoding::encoding_utf8)){
throw std::runtime_error(QSTR_TO_CSTR("Error while saving: '" + fileFullPath + "'. After file version upgrade. (to version " + upgradeVersion + " )"));
}
};
Expand All @@ -402,7 +406,24 @@ void ProjectFileFRequest::upgradeProjectFileIfNecessary(const QString &filePath)
formKeyValuesNodes[i].node().append_child("Type").append_child(pugi::node_pcdata).set_value("0"); // 0 is Text in 1.1
}

fSaveFileAfterUpgrade(filePath, versionAfterUpgrade);
fSaveFileAfterUpgrade(filePath, versionAfterUpgrade, pugiIdentChars::tabChar);

projectVersion = versionAfterUpgrade;
}
if(projectVersion == "1.1"){
const QString versionAfterUpgrade = "1.1c";

pugi::xml_node projectNode = doc.select_single_node("/FRequestProject").node();

// Update version
projectNode.attribute("frequestVersion").set_value(QSTR_TO_CSTR(versionAfterUpgrade));

// Set the default ident character
// Previous to 1.1c all versions use tab as separator, so we want to keep that
// until user changes, even though now space is the default
projectNode.append_attribute("saveIdentCharacter").set_value("1"); // 0 is tab in 1.1c

fSaveFileAfterUpgrade(filePath, versionAfterUpgrade, pugiIdentChars::tabChar);

projectVersion = versionAfterUpgrade;
}
Expand Down
16 changes: 11 additions & 5 deletions XmlParsers/projectfilefrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,26 @@ class ProjectFileFRequest

struct ProjectData{
QString projectName;
QString mainUrl;
QString mainUrl;
QVector<UtilFRequest::RequestInfo> projectRequests;
QString projectUuid;
QString projectUuid;
std::shared_ptr<FRequestAuthentication> authData = nullptr;
bool retryLoginIfError401 = false;
UtilFRequest::IdentCharacter saveIdentCharacter;
};

public:
ProjectFileFRequest() = delete;
static ProjectFileFRequest::ProjectData readProjectDataFromFile(const QString &fileFullPath);
static ProjectFileFRequest::ProjectData readProjectDataFromFile(const QString &fileFullPath);
static void saveProjectDataToFile(const QString &fileFullPath, const ProjectData &newProjectData, const QVector<QString> &uuidsToCleanUp);
private:
static pugi::xml_attribute createOrGetPugiXmlAttribute(pugi::xml_node &mainNode, const char *name);
static void upgradeProjectFileIfNecessary(const QString &filePath);
static pugi::xml_attribute createOrGetPugiXmlAttribute(pugi::xml_node &mainNode, const char *name);
static void upgradeProjectFileIfNecessary(const QString &filePath);
};

namespace pugiIdentChars {
static constexpr pugi::char_t spaceChar[] = PUGIXML_TEXT(" "); // we use 4 spaces as default
static constexpr pugi::char_t tabChar[] = PUGIXML_TEXT("\t");
}

#endif // PROJECTFILEFREQUEST_H
2 changes: 2 additions & 0 deletions mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,7 @@ void MainWindow::loadProjectState(const QString &filePath)
this->currentProjectItem->projectName = projectData->projectName;
this->currentProjectItem->projectMainUrl = projectData->mainUrl;
this->currentProjectItem->authData = projectData->authData;
this->currentProjectItem->saveIdentCharacter = projectData->saveIdentCharacter;

// Order them by the correct order
std::sort(
Expand Down Expand Up @@ -1345,6 +1346,7 @@ ProjectFileFRequest::ProjectData MainWindow::fetchCurrentProjectData(){
currentProjectData.mainUrl = this->currentProjectItem->projectMainUrl;
currentProjectData.projectUuid = this->currentProjectItem->getUuid();
currentProjectData.authData = this->currentProjectItem->authData;
currentProjectData.saveIdentCharacter = this->currentProjectItem->saveIdentCharacter;

// Save by the current tree order
for(int i=0; i<this->currentProjectItem->childCount(); i++){
Expand Down
Loading

0 comments on commit b08fd8f

Please sign in to comment.