Some code clean up. Use an enum type for the "overwrite" mode instead of two separate flags.

This commit is contained in:
LoRd_MuldeR 2014-05-30 16:52:34 +02:00
parent b7b4fa31bd
commit 1bb96ba6db
2 changed files with 26 additions and 19 deletions

View File

@ -63,8 +63,7 @@ ProcessThread::ProcessThread(const AudioFileModel &audioFile, const QString &out
m_jobId(QUuid::createUuid()), m_jobId(QUuid::createUuid()),
m_prependRelativeSourcePath(prependRelativeSourcePath), m_prependRelativeSourcePath(prependRelativeSourcePath),
m_renamePattern("<BaseName>"), m_renamePattern("<BaseName>"),
m_overwriteSkipExistingFile(false), m_overwriteMode(OverwriteMode_KeepBoth),
m_overwriteReplacesExisting(false),
m_initialized(-1), m_initialized(-1),
m_aborted(false), m_aborted(false),
m_propDetect(new WaveProperties()) m_propDetect(new WaveProperties())
@ -438,7 +437,7 @@ int ProcessThread::generateOutFileName(QString &outFileName)
outFileName = QString("%1/%2.%3").arg(targetDir.canonicalPath(), fileName, m_encoder->extension()); outFileName = QString("%1/%2.%3").arg(targetDir.canonicalPath(), fileName, m_encoder->extension());
//Skip file, if target file exists (optional!) //Skip file, if target file exists (optional!)
if(m_overwriteSkipExistingFile && QFileInfo(outFileName).exists()) if((m_overwriteMode == OverwriteMode_SkipExisting) && QFileInfo(outFileName).exists())
{ {
handleMessage(QString("%1\n%2\n").arg(tr("Target output file already exists, going to skip this file:"), QDir::toNativeSeparators(outFileName))); handleMessage(QString("%1\n%2\n").arg(tr("Target output file already exists, going to skip this file:"), QDir::toNativeSeparators(outFileName)));
handleMessage(tr("If you don't want existing files to be skipped, please change the overwrite mode!")); handleMessage(tr("If you don't want existing files to be skipped, please change the overwrite mode!"));
@ -446,11 +445,11 @@ int ProcessThread::generateOutFileName(QString &outFileName)
} }
//Delete file, if target file exists (optional!) //Delete file, if target file exists (optional!)
if(m_overwriteReplacesExisting && QFileInfo(outFileName).exists() && QFileInfo(outFileName).isFile()) if((m_overwriteMode == OverwriteMode_Overwrite) && QFileInfo(outFileName).exists() && QFileInfo(outFileName).isFile())
{
if(sourceFile.canonicalFilePath().compare(QFileInfo(outFileName).absoluteFilePath(), Qt::CaseInsensitive) != 0)
{ {
handleMessage(QString("%1\n%2\n").arg(tr("Target output file already exists, going to delete existing file:"), QDir::toNativeSeparators(outFileName))); handleMessage(QString("%1\n%2\n").arg(tr("Target output file already exists, going to delete existing file:"), QDir::toNativeSeparators(outFileName)));
if(sourceFile.canonicalFilePath().compare(QFileInfo(outFileName).absoluteFilePath(), Qt::CaseInsensitive) != 0)
{
for(int i = 0; i < 16; i++) for(int i = 0; i < 16; i++)
{ {
if(QFile::remove(outFileName)) if(QFile::remove(outFileName))
@ -459,12 +458,12 @@ int ProcessThread::generateOutFileName(QString &outFileName)
} }
lamexp_sleep(125); lamexp_sleep(125);
} }
}
if(QFileInfo(outFileName).exists()) if(QFileInfo(outFileName).exists())
{ {
handleMessage(QString("%1\n").arg(tr("Failed to delete existing target file, will save to another file name!"))); handleMessage(QString("%1\n").arg(tr("Failed to delete existing target file, will save to another file name!")));
} }
} }
}
int n = 1; int n = 1;
@ -679,17 +678,19 @@ void ProcessThread::setRenamePattern(const QString &pattern)
if(!newPattern.isEmpty()) m_renamePattern = newPattern; if(!newPattern.isEmpty()) m_renamePattern = newPattern;
} }
void ProcessThread::setOverwriteMode(const bool bSkipExistingFile, const bool bReplacesExisting) void ProcessThread::setOverwriteMode(const bool &bSkipExistingFile, const bool &bReplacesExisting)
{ {
if(bSkipExistingFile && bReplacesExisting) if(bSkipExistingFile && bReplacesExisting)
{ {
qWarning("Inconsistent overwrite flags, reverting to default!"); qWarning("Inconsistent overwrite flags -> reverting to default!");
m_overwriteSkipExistingFile = false; m_overwriteMode = OverwriteMode_KeepBoth;
m_overwriteReplacesExisting = false; }
else
{
m_overwriteMode = OverwriteMode_KeepBoth;
if(bSkipExistingFile) m_overwriteMode = OverwriteMode_SkipExisting;
if(bReplacesExisting) m_overwriteMode = OverwriteMode_Overwrite;
} }
m_overwriteSkipExistingFile = bSkipExistingFile;
m_overwriteReplacesExisting = bReplacesExisting;
} }
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -47,7 +47,7 @@ public:
QUuid getId(void) { return m_jobId; } QUuid getId(void) { return m_jobId; }
void setRenamePattern(const QString &pattern); void setRenamePattern(const QString &pattern);
void setOverwriteMode(const bool bSkipExistingFile, const bool ReplacesExisting = false); void setOverwriteMode(const bool &bSkipExistingFile, const bool &bReplacesExisting = false);
void addFilter(AbstractFilter *filter); void addFilter(AbstractFilter *filter);
public slots: public slots:
@ -77,6 +77,13 @@ private:
UnknownStep = 4 UnknownStep = 4
}; };
enum OverwriteMode
{
OverwriteMode_KeepBoth = 0,
OverwriteMode_SkipExisting = 1,
OverwriteMode_Overwrite = 2,
};
void processFile(); void processFile();
int generateOutFileName(QString &outFileName); int generateOutFileName(QString &outFileName);
QString applyRenamePattern(const QString &baseName, const AudioFileModel_MetaInfo &metaInfo); QString applyRenamePattern(const QString &baseName, const AudioFileModel_MetaInfo &metaInfo);
@ -97,8 +104,7 @@ private:
const bool m_prependRelativeSourcePath; const bool m_prependRelativeSourcePath;
QList<AbstractFilter*> m_filters; QList<AbstractFilter*> m_filters;
QString m_renamePattern; QString m_renamePattern;
bool m_overwriteSkipExistingFile; int m_overwriteMode;
bool m_overwriteReplacesExisting;
WaveProperties *m_propDetect; WaveProperties *m_propDetect;
QString m_outFileName; QString m_outFileName;
}; };