changeset 723:69e893792522

[fix] Bug ID 2942381: When loading an indenter configuration file, the settings displayed in th docking widget were correctly updated, but the code wasn't. - Changed the created shell scripts indenter config file reference. Now each created shell script has a corresponding config file being created in the same dir as the shell script, with the same name but different suffix. But since the config file is referenced by "./", when calling the script the working directory must be the same as the directory containing the config file. - Slight code improvements and refactoring. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@996 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Sun, 21 Feb 2010 22:38:30 +0000
parents 10ac3864c9a1
children 6cca48ee1557
files src/IndentHandler.cpp src/IndentHandler.h
diffstat 2 files changed, 39 insertions(+), 36 deletions(-) [+]
line wrap: on
line diff
--- a/src/IndentHandler.cpp	Fri Feb 19 22:16:47 2010 +0000
+++ b/src/IndentHandler.cpp	Sun Feb 21 22:38:30 2010 +0000
@@ -238,7 +238,7 @@
     \brief Creates the content for a shell script that can be used as a external tool call
     to indent an as parameter defined file.
  */
-QString IndentHandler::generateCommandlineCall() {
+QString IndentHandler::generateShellScript(const QString &configFilename) {
     QString indenterCompleteCallString;
     QString parameterInputFile;
     QString parameterOuputFile;
@@ -252,13 +252,6 @@
     QString shellParameterPlaceholder = "$1";
 #endif
 
-    // Generate the parameter string that will be saved to the indenters config file.
-    QString parameterString = getParameterString();
-
-    if ( !configFilename.isEmpty() ) {
-        saveConfigFile( indenterDirctoryStr + "/" + configFilename, parameterString );
-    }
-
     parameterInputFile = " " + inputFileParameter + "\"" + shellParameterPlaceholder + "\"";
 
     if ( outputFileParameter != "none" && outputFileParameter != "stdout" ) {
@@ -271,12 +264,12 @@
     }
 
     // If the config file name is empty it is assumed that all parameters are sent via command line call
-    if ( configFilename.isEmpty() ) {
-        parameterParameterFile = " " + parameterString;
+    if ( globalConfigFilename_.isEmpty() ) {
+        parameterParameterFile = " " + getParameterString();
     }
     // else if needed add the parameter to the indenter call string where the config file can be found.
     else if (useCfgFileParameter != "none") {
-        parameterParameterFile = " " + useCfgFileParameter + "\"" + indenterDirctoryStr + "/" + configFilename + "\"";
+        parameterParameterFile = " " + useCfgFileParameter + "\"./" + configFilename + "\"";
     }
 
     // Assemble indenter call string for parameters according to the set order.
@@ -398,8 +391,8 @@
     // Generate the parameter string that will be saved to the indenters config file
     QString parameterString = getParameterString();
 
-    if ( !configFilename.isEmpty() ) {
-        saveConfigFile( tempDirctoryStr + "/" + configFilename, parameterString );
+    if ( !globalConfigFilename_.isEmpty() ) {
+        saveConfigFile( tempDirctoryStr + "/" + globalConfigFilename_, parameterString );
     }
 
     // Only add a dot to file extension if the string is not empty
@@ -484,12 +477,12 @@
 #endif
 
     // If the config file name is empty it is assumed that all parameters are sent via command line call
-    if ( configFilename.isEmpty() ) {
+    if ( globalConfigFilename_.isEmpty() ) {
         parameterParameterFile = " " + parameterString;
     }
     // if needed add the parameter to the indenter call string where the config file can be found
     else if (useCfgFileParameter != "none") {
-        parameterParameterFile = " " + useCfgFileParameter + "\"" + tempDirctoryStr + "/" + configFilename + "\"";
+        parameterParameterFile = " " + useCfgFileParameter + "\"" + tempDirctoryStr + "/" + globalConfigFilename_ + "\"";
     }
 
     // Assemble indenter call string for parameters according to the set order.
@@ -901,7 +894,7 @@
 
     indenterName = indenterSettings->value("header/indenterName").toString();
     indenterFileName = indenterSettings->value("header/indenterFileName").toString();
-    configFilename = indenterSettings->value("header/configFilename").toString();
+    globalConfigFilename_ = indenterSettings->value("header/configFilename").toString();
     useCfgFileParameter = indenterSettings->value("header/useCfgFileParameter").toString();
     cfgFileParameterEnding = indenterSettings->value("header/cfgFileParameterEnding").toString();
     if ( cfgFileParameterEnding == "cr" ) {
@@ -1291,7 +1284,7 @@
     \brief Returns the path and filename of the current indenter config file.
  */
 QString IndentHandler::getIndenterCfgFile() {
-    QFileInfo fileInfo( indenterDirctoryStr + "/" + configFilename );
+    QFileInfo fileInfo( indenterDirctoryStr + "/" + globalConfigFilename_ );
     return fileInfo.absoluteFilePath();
 }
 
@@ -1507,7 +1500,9 @@
     configFilePath = QFileDialog::getOpenFileName( NULL, tr("Choose indenter config file"), getIndenterCfgFile(), "All files (*.*)" );
 
     if (configFilePath != "") {
-        loadConfigFile(configFilePath);
+        // If the config file was loaded successfully, inform any who is interested about it.
+        if ( loadConfigFile(configFilePath) )
+            handleChangedIndenterSettings();
     }
 }
 
@@ -1540,9 +1535,6 @@
     other application and open a save dialog for saving the shell script.
 */
 void IndentHandler::createIndenterCallShellScript() {
-    // Get the content of the shell/batch script.
-    QString indenterCallShellScript = generateCommandlineCall();
-
     QString shellScriptExtension;
 #if defined(Q_OS_WIN32)
     shellScriptExtension = "bat";
@@ -1555,27 +1547,38 @@
     QString currentIndenterName = getCurrentIndenterName();
     currentIndenterName = currentIndenterName.replace(" ", "_");
 
-    //QString openedSourceFileContent = openFileDialog( tr("Choose source code file"), "./", fileExtensions );
-    QString fileName = QFileDialog::getSaveFileName( this, tr("Save shell script"), "call_"+currentIndenterName+"."+shellScriptExtension, fileExtensions);
+    QString shellScriptFileName = QFileDialog::getSaveFileName( this, tr("Save shell script"), "call_"+currentIndenterName+"."+shellScriptExtension, fileExtensions);
 
     // Saving has been canceled if the filename is empty
-    if ( fileName.isEmpty() ) {
+    if ( shellScriptFileName.isEmpty() ) {
         return;
     }
 
-    // Replace placeholder for script name in script template.
-    indenterCallShellScript = indenterCallShellScript.replace( "__INDENTERCALLSTRINGSCRIPTNAME__", QFileInfo(fileName).fileName() );
+    // Delete any old file, write the new contents and set executable permissions.
+    QFile::remove(shellScriptFileName);
+    QFile outSrcFile(shellScriptFileName);
+    if ( outSrcFile.open( QFile::ReadWrite | QFile::Text ) ) {
+        QString shellScriptConfigFilename = QFileInfo(shellScriptFileName).baseName() + "." + QFileInfo(globalConfigFilename_).suffix();
+
+        // Get the content of the shell/batch script.
+        QString indenterCallShellScript = generateShellScript(shellScriptConfigFilename);
+
+        // Replace placeholder for script name in script template.
+        indenterCallShellScript = indenterCallShellScript.replace( "__INDENTERCALLSTRINGSCRIPTNAME__", QFileInfo(shellScriptFileName).fileName() );
 
-    // Delete any old file, write the new contents and set executable permissions.
-    QFile::remove(fileName);
-    QFile outSrcFile(fileName);
-    outSrcFile.open( QFile::ReadWrite | QFile::Text );
-    outSrcFile.write( indenterCallShellScript.toAscii() );
+        outSrcFile.write( indenterCallShellScript.toAscii() );
 #if !defined(Q_OS_WIN32)
-    // For none Windows systems set the files executable flag
-    outSrcFile.setPermissions( outSrcFile.permissions() | QFile::ExeOwner | QFile::ExeUser| QFile::ExeGroup );
+        // For none Windows systems set the files executable flag
+        outSrcFile.setPermissions( outSrcFile.permissions() | QFile::ExeOwner | QFile::ExeUser| QFile::ExeGroup );
 #endif
-    outSrcFile.close();
+        outSrcFile.close();
+
+        // Save the indenter config file to the same directory, where the shell srcipt was saved to,
+        // because the script will reference it there via "./".
+        if ( !globalConfigFilename_.isEmpty() ) {
+            saveConfigFile( QFileInfo(shellScriptFileName).path() + "/" + shellScriptConfigFilename, getParameterString() );
+        }
+    }
 }
 
 
--- a/src/IndentHandler.h	Fri Feb 19 22:16:47 2010 +0000
+++ b/src/IndentHandler.h	Sun Feb 21 22:38:30 2010 +0000
@@ -61,7 +61,7 @@
     IndentHandler(int indenterID, QWidget *mainWindow = NULL, QWidget *parent = NULL);
     ~IndentHandler();
 
-    QString generateCommandlineCall();
+    QString generateShellScript(const QString &configFilename);
     QString callIndenter(QString sourceCode, QString inputFileExtension);
     bool loadConfigFile(QString filePathName);
     void resetToDefaultValues();
@@ -168,7 +168,7 @@
     QString settingsDirctoryStr;
     QStringList indenterIniFileList;
     QString parameterOrder;
-    QString configFilename;
+    QString globalConfigFilename_;
     QString cfgFileParameterEnding;
     QString inputFileParameter;
     QString inputFileName;