diff --git a/Core/Core.hh b/Core/Core.hh index 6d649b8843b537f37f93531097a39d6e5477e6a7..dd782491d4b3418a34e03a34d77e568d2a19ef70 100644 --- a/Core/Core.hh +++ b/Core/Core.hh @@ -1019,19 +1019,21 @@ private slots: * @param _saveSystemSettings Choose if you also want to save system settings into the ini file. * @param _savePluginSettings Choose if you want to save per Plugin global settings into the ini file. * @param _saveObjectInfo If you want to store information about all open objects this has to be true + * @param _fileMappings If multiple files have the same name, all duplicates get renamed. Mapping: Id -> new file name. */ void writeIniFile( QString _filename, bool _relativePaths, bool _targetOnly, bool _saveSystemSettings, bool _savePluginSettings , - bool _saveObjectInfo ); + bool _saveObjectInfo, + std::map& _fileMapping); /** \brief Write current status to obj file (Application and File Options) * * Writes the complete status to an obj file ( All open objects and their Information ) */ - void writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly); + void writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly, std::map& _fileMapping); /// Called if app is closed and writes all information to ini file void writeOnExit(); diff --git a/Core/ParseIni.cc b/Core/ParseIni.cc index dc3403f57b16260249696f4ceaa1d51d5de2d690..3212b6b8827aaebe550e233f2d00126802a5342e 100644 --- a/Core/ParseIni.cc +++ b/Core/ParseIni.cc @@ -610,7 +610,8 @@ void Core::writeIniFile(QString _filename, bool _targetOnly, bool _saveSystemSettings, bool _savePluginSettings , - bool _saveObjectInfo ) { + bool _saveObjectInfo, + std::map& _fileMapping) { INIFile ini; @@ -646,7 +647,14 @@ void Core::writeIniFile(QString _filename, QString sectionName; for ( PluginFunctions::ObjectIterator o_it(restriction) ; o_it != PluginFunctions::objectsEnd(); ++o_it) { - QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name(); + + QString file; + std::map::iterator f = _fileMapping.find(o_it->id()); + if(f == _fileMapping.end()) { + file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name(); + } else { + file = f->second; + } // Don't save default light source objects LightObject* light = 0; diff --git a/Core/ParseObj.cc b/Core/ParseObj.cc index 318fa6232cd0571c029927494955883d3bca1af0..ef9ec9a1110757bed8a5fde0d5751dbcf84a3b95 100644 --- a/Core/ParseObj.cc +++ b/Core/ParseObj.cc @@ -69,7 +69,7 @@ //== IMPLEMENTATION ========================================================== /// \todo After obj writing to one file is implemented in obj plugin, remove this Function and the whole File! -void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly) +void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly, std::map& _fileMapping) { // open file std::string fname = _filename.toStdString(); @@ -97,7 +97,13 @@ void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly for ( PluginFunctions::ObjectIterator o_it (restriction) ; o_it != PluginFunctions::objectsEnd(); ++o_it) { - QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name(); + QString file; + std::map::iterator f = _fileMapping.find(o_it->id()); + if(f == _fileMapping.end()) { + file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name(); + } else { + file = f->second; + } if (QFile(file).exists()) { diff --git a/Core/saveSettings.cc b/Core/saveSettings.cc index 17acfcc5b7f0f4e274b172538a740579ba26c9c3..250679917efcb859fe49d94b8388dc4ef5f65c23 100644 --- a/Core/saveSettings.cc +++ b/Core/saveSettings.cc @@ -55,6 +55,8 @@ #include #include +#include + /// Save Settings (slot is called from CoreWidget's File-Menu) void Core::saveSettings(){ @@ -168,6 +170,9 @@ void Core::saveSettings(){ // ======================================================================================== // Depending on the checkbox iterate over all objects or only the selected ones. + // Memorize saved files new file names + std::map savedFiles; + if ( saveObjectInfo->isChecked() ) { PluginFunctions::IteratorRestriction restriction; @@ -176,6 +181,9 @@ void Core::saveSettings(){ else restriction = PluginFunctions::ALL_OBJECTS; + // Store saved file's original names (in order to get number of duplicates) + std::multiset originalFiles; + //Iterate over opened objects and save them for ( PluginFunctions::ObjectIterator o_it(restriction); o_it != PluginFunctions::objectsEnd(); ++o_it) @@ -220,12 +228,29 @@ void Core::saveSettings(){ if(light->defaultLight()) continue; } + // Store original file name + originalFiles.insert(filename); + + // If a file with the same name already has been saved, + // rename it. + unsigned int c = originalFiles.count(filename); + if(c > 1) { + QFileInfo finfo(filename); + filename = finfo.absolutePath() + OpenFlipper::Options::dirSeparator(); + filename += finfo.baseName() + QString("_%1").arg((c-1)) + "."; + filename += finfo.completeSuffix(); + } + + // decide whether to use saveObject or saveObjectTo if ( !QFile(filename).exists() || !askOverwrite->isChecked() ) saveObject( o_it->id(), filename); else saveObjectTo(o_it->id(), filename); + // Store saved file's name + savedFiles.insert(std::pair(o_it->id(),filename)); + } } @@ -241,12 +266,13 @@ void Core::saveSettings(){ targetOnly->isChecked(), saveProgramSettings->isChecked(), savePluginSettings->isChecked(), - saveObjectInfo->isChecked()); + saveObjectInfo->isChecked(), + savedFiles); } else if ( complete_name.endsWith("obj") ) { //write to obj - writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked() ); + writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked(), savedFiles); }