From 85e845d83f6031f650341fb7002bb6cd4677dc0a Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sat, 26 Oct 2019 21:59:27 +0200 Subject: [PATCH] If the most recent input or output directory does not exist anymore, try to find a still existing "ancestor" directory, before falling back to the default directory. --- src/Config.h | 2 +- src/Model_Settings.cpp | 36 +++++++++++++++++++++++++++++------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/Config.h b/src/Config.h index 2c32d004..36ceb629 100644 --- a/src/Config.h +++ b/src/Config.h @@ -35,7 +35,7 @@ #define VER_LAMEXP_MINOR_LO 8 #define VER_LAMEXP_TYPE Beta #define VER_LAMEXP_PATCH 7 -#define VER_LAMEXP_BUILD 2234 +#define VER_LAMEXP_BUILD 2235 #define VER_LAMEXP_CONFG 2188 /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Model_Settings.cpp b/src/Model_Settings.cpp index 537fc1cb..e686a189 100644 --- a/src/Model_Settings.cpp +++ b/src/Model_Settings.cpp @@ -176,7 +176,27 @@ quint32 SettingsModel::OPT##Default(void) { return (DEF); } } \ while(0) -#define DIR_EXISTS(PATH) (QFileInfo(PATH).exists() && QFileInfo(PATH).isDir()) +//////////////////////////////////////////////////////////// +// Utility functions +//////////////////////////////////////////////////////////// + +static bool dir_exists(const QString &path) +{ + const QFileInfo info(path); + return info.exists() && info.isDir(); +} + +static QString find_existing_ancestor(const QString &path) +{ + for (QString parentPath = path; !parentPath.isEmpty(); parentPath = MUtils::parent_path(parentPath)) + { + if (dir_exists(parentPath)) + { + return parentPath; /*existing parent found*/ + } + } + return QString(); +} //////////////////////////////////////////////////////////// // Constants @@ -427,16 +447,18 @@ void SettingsModel::validate(void) } } - if(this->outputDir().isEmpty() || (!DIR_EXISTS(this->outputDir()))) + if(this->outputDir().isEmpty() || (!dir_exists(this->outputDir()))) { - qWarning("Output directory not set yet or does NOT exist anymore -> Resetting"); - this->outputDir(defaultDirectory()); + qWarning("Output directory not set yet or does NOT exist anymore -> resetting!"); + const QString outputDir = find_existing_ancestor(this->outputDir()); + this->outputDir((!outputDir.isEmpty()) ? outputDir : defaultDirectory()); } - if(this->mostRecentInputPath().isEmpty() || (!DIR_EXISTS(this->mostRecentInputPath()))) + if(this->mostRecentInputPath().isEmpty() || (!dir_exists(this->mostRecentInputPath()))) { - qWarning("Most recent input directory not set yet or does NOT exist anymore -> Resetting"); - this->mostRecentInputPath(defaultDirectory()); + qWarning("Most recent input directory not set yet or does NOT exist anymore -> resetting!"); + const QString inputPath = find_existing_ancestor(this->mostRecentInputPath()); + this->mostRecentInputPath((!inputPath.isEmpty()) ? inputPath : defaultDirectory()); } if(!this->currentLanguageFile().isEmpty())