Commit f86f7754 authored by Mike Kremer's avatar Mike Kremer

Rename files if multiple files have the same name when saving settings file.

git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@11574 383ad7c9-94d9-4d36-a494-682f7c89f535
parent 61538420
...@@ -1019,19 +1019,21 @@ private slots: ...@@ -1019,19 +1019,21 @@ private slots:
* @param _saveSystemSettings Choose if you also want to save system settings into the ini file. * @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 _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 _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, void writeIniFile( QString _filename,
bool _relativePaths, bool _relativePaths,
bool _targetOnly, bool _targetOnly,
bool _saveSystemSettings, bool _saveSystemSettings,
bool _savePluginSettings , bool _savePluginSettings ,
bool _saveObjectInfo ); bool _saveObjectInfo,
std::map<int,QString>& _fileMapping);
/** \brief Write current status to obj file (Application and File Options) /** \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 ) * 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<int,QString>& _fileMapping);
/// Called if app is closed and writes all information to ini file /// Called if app is closed and writes all information to ini file
void writeOnExit(); void writeOnExit();
......
...@@ -610,7 +610,8 @@ void Core::writeIniFile(QString _filename, ...@@ -610,7 +610,8 @@ void Core::writeIniFile(QString _filename,
bool _targetOnly, bool _targetOnly,
bool _saveSystemSettings, bool _saveSystemSettings,
bool _savePluginSettings , bool _savePluginSettings ,
bool _saveObjectInfo ) { bool _saveObjectInfo,
std::map<int,QString>& _fileMapping) {
INIFile ini; INIFile ini;
...@@ -646,7 +647,14 @@ void Core::writeIniFile(QString _filename, ...@@ -646,7 +647,14 @@ void Core::writeIniFile(QString _filename,
QString sectionName; QString sectionName;
for ( PluginFunctions::ObjectIterator o_it(restriction) ; for ( PluginFunctions::ObjectIterator o_it(restriction) ;
o_it != PluginFunctions::objectsEnd(); ++o_it) { o_it != PluginFunctions::objectsEnd(); ++o_it) {
QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name();
QString file;
std::map<int,QString>::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 // Don't save default light source objects
LightObject* light = 0; LightObject* light = 0;
......
...@@ -69,7 +69,7 @@ ...@@ -69,7 +69,7 @@
//== IMPLEMENTATION ========================================================== //== IMPLEMENTATION ==========================================================
/// \todo After obj writing to one file is implemented in obj plugin, remove this Function and the whole File! /// \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<int,QString>& _fileMapping)
{ {
// open file // open file
std::string fname = _filename.toStdString(); std::string fname = _filename.toStdString();
...@@ -97,7 +97,13 @@ void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly ...@@ -97,7 +97,13 @@ void Core::writeObjFile(QString _filename, bool _relativePaths, bool _targetOnly
for ( PluginFunctions::ObjectIterator o_it (restriction) ; for ( PluginFunctions::ObjectIterator o_it (restriction) ;
o_it != PluginFunctions::objectsEnd(); ++o_it) o_it != PluginFunctions::objectsEnd(); ++o_it)
{ {
QString file = o_it->path() + OpenFlipper::Options::dirSeparator() + o_it->name(); QString file;
std::map<int,QString>::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()) if (QFile(file).exists())
{ {
......
...@@ -55,6 +55,8 @@ ...@@ -55,6 +55,8 @@
#include <QGroupBox> #include <QGroupBox>
#include <QBoxLayout> #include <QBoxLayout>
#include <set>
/// Save Settings (slot is called from CoreWidget's File-Menu) /// Save Settings (slot is called from CoreWidget's File-Menu)
void Core::saveSettings(){ void Core::saveSettings(){
...@@ -168,6 +170,9 @@ void Core::saveSettings(){ ...@@ -168,6 +170,9 @@ void Core::saveSettings(){
// ======================================================================================== // ========================================================================================
// Depending on the checkbox iterate over all objects or only the selected ones. // Depending on the checkbox iterate over all objects or only the selected ones.
// Memorize saved files new file names
std::map<int,QString> savedFiles;
if ( saveObjectInfo->isChecked() ) { if ( saveObjectInfo->isChecked() ) {
PluginFunctions::IteratorRestriction restriction; PluginFunctions::IteratorRestriction restriction;
...@@ -176,6 +181,9 @@ void Core::saveSettings(){ ...@@ -176,6 +181,9 @@ void Core::saveSettings(){
else else
restriction = PluginFunctions::ALL_OBJECTS; restriction = PluginFunctions::ALL_OBJECTS;
// Store saved file's original names (in order to get number of duplicates)
std::multiset<QString> originalFiles;
//Iterate over opened objects and save them //Iterate over opened objects and save them
for ( PluginFunctions::ObjectIterator o_it(restriction); for ( PluginFunctions::ObjectIterator o_it(restriction);
o_it != PluginFunctions::objectsEnd(); ++o_it) o_it != PluginFunctions::objectsEnd(); ++o_it)
...@@ -220,12 +228,29 @@ void Core::saveSettings(){ ...@@ -220,12 +228,29 @@ void Core::saveSettings(){
if(light->defaultLight()) continue; 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 // decide whether to use saveObject or saveObjectTo
if ( !QFile(filename).exists() || !askOverwrite->isChecked() ) if ( !QFile(filename).exists() || !askOverwrite->isChecked() )
saveObject( o_it->id(), filename); saveObject( o_it->id(), filename);
else else
saveObjectTo(o_it->id(), filename); saveObjectTo(o_it->id(), filename);
// Store saved file's name
savedFiles.insert(std::pair<int,QString>(o_it->id(),filename));
} }
} }
...@@ -241,12 +266,13 @@ void Core::saveSettings(){ ...@@ -241,12 +266,13 @@ void Core::saveSettings(){
targetOnly->isChecked(), targetOnly->isChecked(),
saveProgramSettings->isChecked(), saveProgramSettings->isChecked(),
savePluginSettings->isChecked(), savePluginSettings->isChecked(),
saveObjectInfo->isChecked()); saveObjectInfo->isChecked(),
savedFiles);
} else if ( complete_name.endsWith("obj") ) { } else if ( complete_name.endsWith("obj") ) {
//write to obj //write to obj
writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked() ); writeObjFile(complete_name, saveAllBox->isChecked(), targetOnly->isChecked(), savedFiles);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment