diff --git a/src/ExportManager.cpp b/src/ExportManager.cpp --- a/src/ExportManager.cpp +++ b/src/ExportManager.cpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include #include #include @@ -198,7 +200,7 @@ if (mGrabMode == ImageGrabber::GrabMode::ActiveWindow || mGrabMode == ImageGrabber::GrabMode::TransientWithParent || mGrabMode == ImageGrabber::GrabMode::WindowUnderCursor) { - title = mWindowTitle; + title = mWindowTitle.replace(QLatin1String("/"), QLatin1String("_")); // POSIX doesn't allow "/" in filenames } else { // Remove '%T' with separators around it const auto wordSymbol = QStringLiteral(R"(\p{L}\p{M}\p{N})"); @@ -214,8 +216,16 @@ .replace(QLatin1String("%H"), timestamp.toString(QStringLiteral("hh"))) .replace(QLatin1String("%m"), timestamp.toString(QStringLiteral("mm"))) .replace(QLatin1String("%S"), timestamp.toString(QStringLiteral("ss"))) - .replace(QLatin1String("%T"), title) - .replace(QLatin1String("/"), QLatin1String("_")); // POSIX doesn't allow "/" in filenames + .replace(QLatin1String("%T"), title); + + // Remove leading and trailing '/' + while (result.startsWith(QLatin1Char('/'))) { + result.remove(0, 1); + } + while (result.endsWith(QLatin1Char('/'))) { + result.chop(1); + } + if (result.isEmpty()) { result = QStringLiteral("Screenshot"); } @@ -269,6 +279,18 @@ bool ExportManager::localSave(const QUrl &url, const QString &mimetype) { + // Create save directory if it doesn't exist + const QUrl dirPath(url.adjusted(QUrl::RemoveFilename)); + const QDir dir(dirPath.path()); + + if (!dir.mkpath(QLatin1String("."))) { + emit errorMessage(xi18nc("@info", + "Cannot save screenshot, because creating " + "the directory failed:%1", + dirPath.path())); + return false; + } + QFile outputFile(url.toLocalFile()); outputFile.open(QFile::WriteOnly); @@ -281,6 +303,26 @@ bool ExportManager::remoteSave(const QUrl &url, const QString &mimetype) { + + // Check if remote save directory exists + const QUrl dirPath(url.adjusted(QUrl::RemoveFilename)); + KIO::ListJob *listJob = KIO::listDir(dirPath); + listJob->exec(); + + if (listJob->error() != KJob::NoError) { + // Create remote save directory + KIO::MkpathJob *mkpathJob = KIO::mkpath(dirPath, QUrl(saveLocation())); + mkpathJob->exec(); + + if (mkpathJob->error() != KJob::NoError) { + emit errorMessage(xi18nc("@info", + "Cannot save screenshot, because creating the " + "remote directory failed:%1", + dirPath.path())); + return false; + } + } + QTemporaryFile tmpFile; if (tmpFile.open()) { diff --git a/src/Gui/SettingsDialog/SaveOptionsPage.cpp b/src/Gui/SettingsDialog/SaveOptionsPage.cpp --- a/src/Gui/SettingsDialog/SaveOptionsPage.cpp +++ b/src/Gui/SettingsDialog/SaveOptionsPage.cpp @@ -104,6 +104,12 @@ "%S: Second
" "%T: Window title" "" + + "

To save to a sub-folder, use slashes to describe the desired path, e.g.:

" + + "
" + "%Y/%M/Screenshot_%Y%M%D_%H%m%S" + "
" ); QLabel *fmtHelpText = new QLabel(helpText, this);