From 331ff73d3f5d1f3279e593aee339752a0e86038d Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 29 Oct 2024 12:48:25 -0400 Subject: [PATCH] Make sure to create directories for drag-and-drop scripts Signed-off-by: Geoff Hutchison --- avogadro/mainwindow.cpp | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/avogadro/mainwindow.cpp b/avogadro/mainwindow.cpp index 3be18ece..53da793a 100644 --- a/avogadro/mainwindow.cpp +++ b/avogadro/mainwindow.cpp @@ -874,7 +874,25 @@ bool MainWindow::addScript(const QString& filePath) QFileInfo info(filePath); - QString destinationPath(stdPaths[0] + '/' + typePath + '/' + info.fileName()); + // check if the directory structure exists first + // and if not, create what we need + int i = 0; + bool createDir = true; + for (i = 0; i < stdPaths.size(); ++i) { + if (QDir(stdPaths[i] + '/' + typePath).exists()) { + createDir = false; + break; + } + } + if (createDir) { + // find a path we can create (e.g., first path might be admin-only) + for (i = 0; i < stdPaths.size(); ++i) { + if (QDir().mkpath(stdPaths[i] + '/' + typePath)) + break; + } + } + + QString destinationPath(stdPaths[i] + '/' + typePath + '/' + info.fileName()); qDebug() << " copying " << filePath << " to " << destinationPath; QFile::remove(destinationPath); // silently fail if there's nothing to remove QFile::copy(filePath, destinationPath);