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:
* @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<int,QString>& _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<int,QString>& _fileMapping);
/// Called if app is closed and writes all information to ini file
void writeOnExit();
......
......@@ -610,7 +610,8 @@ void Core::writeIniFile(QString _filename,
bool _targetOnly,
bool _saveSystemSettings,
bool _savePluginSettings ,
bool _saveObjectInfo ) {
bool _saveObjectInfo,
std::map<int,QString>& _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<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
LightObject* light = 0;
......
......@@ -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<int,QString>& _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<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())
{
......
......@@ -55,6 +55,8 @@
#include <QGroupBox>
#include <QBoxLayout>
#include <set>
/// 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<int,QString> 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<QString> 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<int,QString>(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);
}
......
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