From 48adb159b8d2446e12fffd7c867da96bf880d7e2 Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Thu, 7 Apr 2022 17:11:50 -0700 Subject: [PATCH] various: consolidated dir exists+mkpath into one function --- app/common/filefunctions.cpp | 23 +++---------------- app/common/filefunctions.h | 3 ++- app/core.cpp | 2 +- app/dialog/export/export.cpp | 2 +- .../preferences/tabs/preferencesdisktab.cpp | 2 +- .../projectproperties/projectproperties.cpp | 2 +- app/render/diskmanager.cpp | 6 ++--- app/render/framehashcache.cpp | 4 ++-- 8 files changed, 14 insertions(+), 30 deletions(-) diff --git a/app/common/filefunctions.cpp b/app/common/filefunctions.cpp index 24de67db0..82878e67b 100644 --- a/app/common/filefunctions.cpp +++ b/app/common/filefunctions.cpp @@ -150,27 +150,10 @@ void FileFunctions::CopyDirectory(const QString &source, const QString &dest, bo } } -bool FileFunctions::DirectoryIsValid(const QString &dir, bool try_to_create) +bool FileFunctions::DirectoryIsValid(const QDir &d, bool try_to_create_if_not_exists) { - // Empty string is invalid - if (dir.isEmpty()) { - return false; - } - - QDir d(dir); - - // If directory already exists, this is valid - if (d.exists()) { - return true; - } - - // If we can create and creation is successful, this is valid - if (try_to_create && d.mkpath(".")) { - return true; - } - - // Otherwise, invalid - return false; + // Return whether the directory exists, or whether it could be created if it doesn't + return d.exists() || d.mkpath(QStringLiteral(".")); } QString FileFunctions::EnsureFilenameExtension(QString fn, const QString &extension) diff --git a/app/common/filefunctions.h b/app/common/filefunctions.h index 122bf076c..abe20be03 100644 --- a/app/common/filefunctions.h +++ b/app/common/filefunctions.h @@ -21,6 +21,7 @@ #ifndef FILEFUNCTIONS_H #define FILEFUNCTIONS_H +#include #include #include "common/define.h" @@ -52,7 +53,7 @@ public: static void CopyDirectory(const QString& source, const QString& dest, bool overwrite = false); - static bool DirectoryIsValid(const QString& dir, bool try_to_create); + static bool DirectoryIsValid(const QDir& dir, bool try_to_create_if_not_exists = true); /** * @brief Ensures a given filename has a certain extension diff --git a/app/core.cpp b/app/core.cpp index 8696d12b3..d4e091959 100644 --- a/app/core.cpp +++ b/app/core.cpp @@ -921,7 +921,7 @@ void Core::SaveAutorecovery() foreach (Project* p, open_projects_) { if (!p->has_autorecovery_been_saved()) { QDir project_autorecovery_dir(QDir(FileFunctions::GetAutoRecoveryRoot()).filePath(p->GetUuid().toString())); - if (project_autorecovery_dir.mkpath(QStringLiteral("."))) { + if (FileFunctions::DirectoryIsValid(project_autorecovery_dir)) { QString this_autorecovery_path = project_autorecovery_dir.filePath(QStringLiteral("%1.ove").arg(QString::number(QDateTime::currentSecsSinceEpoch()))); SaveProjectInternal(p, this_autorecovery_path); diff --git a/app/dialog/export/export.cpp b/app/dialog/export/export.cpp index 7dfcd137f..83f8a0d8d 100644 --- a/app/dialog/export/export.cpp +++ b/app/dialog/export/export.cpp @@ -313,7 +313,7 @@ void ExportDialog::StartExport() // If the directory does not exist, try to create it QDir dest_dir(file_info.path()); - if (!dest_dir.exists() && !dest_dir.mkpath(QStringLiteral("."))) { + if (!FileFunctions::DirectoryIsValid(dest_dir)) { QtUtils::MessageBox(this, QMessageBox::Critical, tr("Failed to create output directory"), tr("The intended output directory doesn't exist and Olive couldn't create it. " "Please choose a different filename.")); diff --git a/app/dialog/preferences/tabs/preferencesdisktab.cpp b/app/dialog/preferences/tabs/preferencesdisktab.cpp index fc6b43d9c..f5258f34e 100644 --- a/app/dialog/preferences/tabs/preferencesdisktab.cpp +++ b/app/dialog/preferences/tabs/preferencesdisktab.cpp @@ -96,7 +96,7 @@ bool PreferencesDiskTab::Validate() } // Check validity of the new path - if (!FileFunctions::DirectoryIsValid(disk_cache_location_->text(), true)) { + if (!FileFunctions::DirectoryIsValid(disk_cache_location_->text())) { QMessageBox::critical(this, tr("Disk Cache"), tr("Failed to set disk cache location. Access was denied.")); diff --git a/app/dialog/projectproperties/projectproperties.cpp b/app/dialog/projectproperties/projectproperties.cpp index 2c75edc9e..c6d92d0fd 100644 --- a/app/dialog/projectproperties/projectproperties.cpp +++ b/app/dialog/projectproperties/projectproperties.cpp @@ -186,7 +186,7 @@ void ProjectPropertiesDialog::accept() bool ProjectPropertiesDialog::VerifyPathAndWarnIfBad(const QString &path) { - if (!FileFunctions::DirectoryIsValid(path, true)) { + if (!FileFunctions::DirectoryIsValid(path)) { QMessageBox mb(this); mb.setWindowModality(Qt::WindowModal); mb.setIcon(QMessageBox::Critical); diff --git a/app/render/diskmanager.cpp b/app/render/diskmanager.cpp index 29c60c925..af67d6bfa 100644 --- a/app/render/diskmanager.cpp +++ b/app/render/diskmanager.cpp @@ -44,7 +44,7 @@ DiskManager::DiskManager() QString default_dir = default_disk_cache_file.readAll(); if (!default_dir.isEmpty()) { - if (FileFunctions::DirectoryIsValid(default_dir, true)) { + if (FileFunctions::DirectoryIsValid(default_dir)) { GetOpenFolder(default_dir); } else { QMessageBox::warning(nullptr, @@ -180,7 +180,7 @@ void DiskManager::ShowDiskCacheSettingsDialog(DiskCacheFolder *folder, QWidget * void DiskManager::ShowDiskCacheSettingsDialog(const QString &path, QWidget *parent) { - if (!FileFunctions::DirectoryIsValid(path, true)) { + if (!FileFunctions::DirectoryIsValid(path)) { QMessageBox::critical(parent, tr("Disk Cache Error"), tr("Failed to open disk cache at \"%1\". Try a different folder.").arg(path)); return; @@ -274,7 +274,7 @@ void DiskCacheFolder::SetPath(const QString &path) // Attempt to load existing index file from path QDir path_dir(path_); - path_dir.mkpath(QStringLiteral(".")); + FileFunctions::DirectoryIsValid(path_dir); index_path_ = path_dir.filePath(QStringLiteral("index")); diff --git a/app/render/framehashcache.cpp b/app/render/framehashcache.cpp index 8cb4b307d..269b5bf19 100644 --- a/app/render/framehashcache.cpp +++ b/app/render/framehashcache.cpp @@ -394,8 +394,8 @@ bool FrameHashCache::SaveCacheFrame(const QString &filename, char *data, const V // Ensure directory is created QDir cache_dir = QFileInfo(filename).dir(); - if (!cache_dir.exists()) { - cache_dir.mkpath("."); + if (!FileFunctions::DirectoryIsValid(cache_dir)) { + return false; } // Floating point types are stored in EXR