various: consolidated dir exists+mkpath into one function

This commit is contained in:
itsmattkc
2022-04-07 17:11:50 -07:00
parent 20f74919a3
commit 48adb159b8
8 changed files with 14 additions and 30 deletions
+3 -20
View File
@@ -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 // Return whether the directory exists, or whether it could be created if it doesn't
if (dir.isEmpty()) { return d.exists() || d.mkpath(QStringLiteral("."));
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;
} }
QString FileFunctions::EnsureFilenameExtension(QString fn, const QString &extension) QString FileFunctions::EnsureFilenameExtension(QString fn, const QString &extension)
+2 -1
View File
@@ -21,6 +21,7 @@
#ifndef FILEFUNCTIONS_H #ifndef FILEFUNCTIONS_H
#define FILEFUNCTIONS_H #define FILEFUNCTIONS_H
#include <QDir>
#include <QString> #include <QString>
#include "common/define.h" #include "common/define.h"
@@ -52,7 +53,7 @@ public:
static void CopyDirectory(const QString& source, const QString& dest, bool overwrite = false); 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 * @brief Ensures a given filename has a certain extension
+1 -1
View File
@@ -921,7 +921,7 @@ void Core::SaveAutorecovery()
foreach (Project* p, open_projects_) { foreach (Project* p, open_projects_) {
if (!p->has_autorecovery_been_saved()) { if (!p->has_autorecovery_been_saved()) {
QDir project_autorecovery_dir(QDir(FileFunctions::GetAutoRecoveryRoot()).filePath(p->GetUuid().toString())); 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()))); QString this_autorecovery_path = project_autorecovery_dir.filePath(QStringLiteral("%1.ove").arg(QString::number(QDateTime::currentSecsSinceEpoch())));
SaveProjectInternal(p, this_autorecovery_path); SaveProjectInternal(p, this_autorecovery_path);
+1 -1
View File
@@ -313,7 +313,7 @@ void ExportDialog::StartExport()
// If the directory does not exist, try to create it // If the directory does not exist, try to create it
QDir dest_dir(file_info.path()); 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"), 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. " tr("The intended output directory doesn't exist and Olive couldn't create it. "
"Please choose a different filename.")); "Please choose a different filename."));
@@ -96,7 +96,7 @@ bool PreferencesDiskTab::Validate()
} }
// Check validity of the new path // Check validity of the new path
if (!FileFunctions::DirectoryIsValid(disk_cache_location_->text(), true)) { if (!FileFunctions::DirectoryIsValid(disk_cache_location_->text())) {
QMessageBox::critical(this, QMessageBox::critical(this,
tr("Disk Cache"), tr("Disk Cache"),
tr("Failed to set disk cache location. Access was denied.")); tr("Failed to set disk cache location. Access was denied."));
@@ -186,7 +186,7 @@ void ProjectPropertiesDialog::accept()
bool ProjectPropertiesDialog::VerifyPathAndWarnIfBad(const QString &path) bool ProjectPropertiesDialog::VerifyPathAndWarnIfBad(const QString &path)
{ {
if (!FileFunctions::DirectoryIsValid(path, true)) { if (!FileFunctions::DirectoryIsValid(path)) {
QMessageBox mb(this); QMessageBox mb(this);
mb.setWindowModality(Qt::WindowModal); mb.setWindowModality(Qt::WindowModal);
mb.setIcon(QMessageBox::Critical); mb.setIcon(QMessageBox::Critical);
+3 -3
View File
@@ -44,7 +44,7 @@ DiskManager::DiskManager()
QString default_dir = default_disk_cache_file.readAll(); QString default_dir = default_disk_cache_file.readAll();
if (!default_dir.isEmpty()) { if (!default_dir.isEmpty()) {
if (FileFunctions::DirectoryIsValid(default_dir, true)) { if (FileFunctions::DirectoryIsValid(default_dir)) {
GetOpenFolder(default_dir); GetOpenFolder(default_dir);
} else { } else {
QMessageBox::warning(nullptr, QMessageBox::warning(nullptr,
@@ -180,7 +180,7 @@ void DiskManager::ShowDiskCacheSettingsDialog(DiskCacheFolder *folder, QWidget *
void DiskManager::ShowDiskCacheSettingsDialog(const QString &path, QWidget *parent) void DiskManager::ShowDiskCacheSettingsDialog(const QString &path, QWidget *parent)
{ {
if (!FileFunctions::DirectoryIsValid(path, true)) { if (!FileFunctions::DirectoryIsValid(path)) {
QMessageBox::critical(parent, tr("Disk Cache Error"), QMessageBox::critical(parent, tr("Disk Cache Error"),
tr("Failed to open disk cache at \"%1\". Try a different folder.").arg(path)); tr("Failed to open disk cache at \"%1\". Try a different folder.").arg(path));
return; return;
@@ -274,7 +274,7 @@ void DiskCacheFolder::SetPath(const QString &path)
// Attempt to load existing index file from path // Attempt to load existing index file from path
QDir path_dir(path_); QDir path_dir(path_);
path_dir.mkpath(QStringLiteral(".")); FileFunctions::DirectoryIsValid(path_dir);
index_path_ = path_dir.filePath(QStringLiteral("index")); index_path_ = path_dir.filePath(QStringLiteral("index"));
+2 -2
View File
@@ -394,8 +394,8 @@ bool FrameHashCache::SaveCacheFrame(const QString &filename, char *data, const V
// Ensure directory is created // Ensure directory is created
QDir cache_dir = QFileInfo(filename).dir(); QDir cache_dir = QFileInfo(filename).dir();
if (!cache_dir.exists()) { if (!FileFunctions::DirectoryIsValid(cache_dir)) {
cache_dir.mkpath("."); return false;
} }
// Floating point types are stored in EXR // Floating point types are stored in EXR