From 1b0c0f26beddc4e7b11a99a2c7e970712c64c5f7 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sat, 20 Apr 2019 17:10:18 +0800 Subject: [PATCH] fix: check path existence Change-Id: I5d5c043a616aef787dc4e8b6ed3f36f7605c75eb --- src/mainwindow.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 3d63ab7a..8c1847f2 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -32,6 +32,7 @@ #include #include #include +#include #include @@ -1337,12 +1338,20 @@ bool MainWindow::saveAction(const QPixmap &pix) if (!screenShotPix.save(m_saveFileName, QFileInfo(m_saveFileName).suffix().toLocal8Bit())) return false; } else if (saveOption != QStandardPaths::TempLocation && m_saveFileName.isEmpty()) { + QString savePath = QStandardPaths::writableLocation(saveOption); + QDir saveDir(savePath); + if (!saveDir.exists()) { + bool mkdirSucc = saveDir.mkpath("."); + if (!mkdirSucc) { + qCritical() << "Save path not exist and cannot be created:" << savePath; + qCritical() << "Fall back to temp location!"; + savePath = QDir::tempPath(); + } + } if (m_selectAreaName.isEmpty()) { - m_saveFileName = QString("%1/%2_%3.png").arg(QStandardPaths::writableLocation( - saveOption)).arg(tr("DeepinScreenshot")).arg(currentTime); + m_saveFileName = QString("%1/%2_%3.png").arg(savePath, tr("DeepinScreenshot"), currentTime); } else { - m_saveFileName = QString("%1/%2_%3_%4.png").arg(QStandardPaths::writableLocation( - saveOption)).arg(tr("DeepinScreenshot")).arg(m_selectAreaName).arg(currentTime); + m_saveFileName = QString("%1/%2_%3_%4.png").arg(savePath, tr("DeepinScreenshot"), m_selectAreaName, currentTime); } if (!screenShotPix.save(m_saveFileName, "PNG")) return false;