diff --git a/src/core/flameshot.cpp b/src/core/flameshot.cpp index 3eb1432668..c6660518b7 100644 --- a/src/core/flameshot.cpp +++ b/src/core/flameshot.cpp @@ -382,11 +382,10 @@ void Flameshot::exportCapture(const QPixmap& capture, QByteArray byteArray; QBuffer buffer(&byteArray); capture.save(&buffer, "PNG"); - QFile file; - file.open(stdout, QIODevice::WriteOnly); - - file.write(byteArray); - file.close(); + if (QFile file; file.open(stdout, QIODevice::WriteOnly)) { + file.write(byteArray); + file.close(); + } } if (tasks & CR::SAVE) { diff --git a/src/utils/confighandler.cpp b/src/utils/confighandler.cpp index d6fd8f3667..c9f97bc33b 100644 --- a/src/utils/confighandler.cpp +++ b/src/utils/confighandler.cpp @@ -749,8 +749,9 @@ void ConfigHandler::ensureFileWatched() const { QFile file(m_settings.fileName()); if (!file.exists()) { - file.open(QFileDevice::WriteOnly); - file.close(); + if (file.open(QFileDevice::WriteOnly)) { + file.close(); + } } if (m_configWatcher != nullptr && m_configWatcher->files().isEmpty() && qApp != nullptr // ensures that the organization name can be accessed diff --git a/src/utils/history.cpp b/src/utils/history.cpp index 877e6195fa..073f58a9f6 100644 --- a/src/utils/history.cpp +++ b/src/utils/history.cpp @@ -44,8 +44,9 @@ void History::save(const QPixmap& pixmap, const QString& fileName) // save preview QFile file(path() + fileName); - file.open(QIODevice::WriteOnly); - pixmapScaled.save(&file, "PNG"); + if (file.open(QIODevice::WriteOnly)) { + pixmapScaled.save(&file, "PNG"); + } history(); } diff --git a/src/utils/screenshotsaver.cpp b/src/utils/screenshotsaver.cpp index 8a0bb90fa4..965e5378e0 100644 --- a/src/utils/screenshotsaver.cpp +++ b/src/utils/screenshotsaver.cpp @@ -44,37 +44,38 @@ bool saveToFilesystem(const QPixmap& capture, QString completePath = FileNameHandler().properScreenshotPath( path, ConfigHandler().saveAsFileExtension()); QFile file{ completePath }; - file.open(QIODevice::WriteOnly); + bool okay = false; - bool okay; - QString saveExtension; - saveExtension = QFileInfo(completePath).suffix().toLower(); - if (saveExtension == "jpg" || saveExtension == "jpeg") { - okay = capture.save(&file, nullptr, ConfigHandler().jpegQuality()); - } else { - okay = capture.save(&file); - } + if (file.open(QIODevice::WriteOnly)) { + QString saveExtension; + saveExtension = QFileInfo(completePath).suffix().toLower(); + if (saveExtension == "jpg" || saveExtension == "jpeg") { + okay = capture.save(&file, nullptr, ConfigHandler().jpegQuality()); + } else { + okay = capture.save(&file); + } - QString saveMessage = messagePrefix; - QString notificationPath = completePath; - if (!saveMessage.isEmpty()) { - saveMessage += " "; - } + QString saveMessage = messagePrefix; + QString notificationPath = completePath; + if (!saveMessage.isEmpty()) { + saveMessage += " "; + } - if (okay) { - saveMessage += QObject::tr("Capture saved as ") + completePath; - AbstractLogger::info().attachNotificationPath(notificationPath) - << saveMessage; - } else { - saveMessage += QObject::tr("Error trying to save as ") + completePath; - if (file.error() != QFile::NoError) { - saveMessage += ": " + file.errorString(); + if (okay) { + saveMessage += QObject::tr("Capture saved as ") + completePath; + AbstractLogger::info().attachNotificationPath(notificationPath) + << saveMessage; + } else { + saveMessage += + QObject::tr("Error trying to save as ") + completePath; + if (file.error() != QFile::NoError) { + saveMessage += ": " + file.errorString(); + } + notificationPath = ""; + AbstractLogger::error().attachNotificationPath(notificationPath) + << saveMessage; } - notificationPath = ""; - AbstractLogger::error().attachNotificationPath(notificationPath) - << saveMessage; } - return okay; } @@ -325,45 +326,47 @@ bool saveToFilesystemGUI(const QPixmap& capture) } QFile file{ savePath }; - file.open(QIODevice::WriteOnly); - - QString saveExtension; - saveExtension = QFileInfo(savePath).suffix().toLower(); - if (saveExtension == "jpg" || saveExtension == "jpeg") { - okay = capture.save(&file, nullptr, ConfigHandler().jpegQuality()); - } else { - okay = capture.save(&file); - } + if (file.open(QIODevice::WriteOnly)) { + QString saveExtension; + saveExtension = QFileInfo(savePath).suffix().toLower(); + if (saveExtension == "jpg" || saveExtension == "jpeg") { + okay = capture.save(&file, nullptr, ConfigHandler().jpegQuality()); + } else { + okay = capture.save(&file); + } - if (okay) { - // Don't use QDir::separator() here, as Qt internally always uses '/' - QString pathNoFile = savePath.left(savePath.lastIndexOf('/')); + if (okay) { + // Don't use QDir::separator() here, as Qt internally always uses + // '/' + QString pathNoFile = savePath.left(savePath.lastIndexOf('/')); - ConfigHandler().setSavePath(pathNoFile); + ConfigHandler().setSavePath(pathNoFile); - QString msg = QObject::tr("Capture saved as ") + savePath; - AbstractLogger().attachNotificationPath(savePath) << msg; + QString msg = QObject::tr("Capture saved as ") + savePath; + AbstractLogger().attachNotificationPath(savePath) << msg; - if (config.copyPathAfterSave()) { + if (config.copyPathAfterSave()) { #ifdef Q_OS_WIN - savePath.replace('/', '\\'); + savePath.replace('/', '\\'); #endif - FlameshotDaemon::copyToClipboard( - savePath, QObject::tr("Path copied to clipboard as ") + savePath); - } + FlameshotDaemon::copyToClipboard( + savePath, + QObject::tr("Path copied to clipboard as ") + savePath); + } - } else { - QString msg = QObject::tr("Error trying to save as ") + savePath; + } else { + QString msg = QObject::tr("Error trying to save as ") + savePath; - if (file.error() != QFile::NoError) { - msg += ": " + file.errorString(); - } + if (file.error() != QFile::NoError) { + msg += ": " + file.errorString(); + } - QMessageBox saveErrBox( - QMessageBox::Warning, QObject::tr("Save Error"), msg); - saveErrBox.setWindowIcon(QIcon(GlobalValues::iconPath())); - saveErrBox.exec(); + QMessageBox saveErrBox( + QMessageBox::Warning, QObject::tr("Save Error"), msg); + saveErrBox.setWindowIcon(QIcon(GlobalValues::iconPath())); + saveErrBox.exec(); + } } return okay; -} +} \ No newline at end of file diff --git a/src/widgets/capture/capturewidget.cpp b/src/widgets/capture/capturewidget.cpp index a2bbfd0bf2..7db109a94e 100644 --- a/src/widgets/capture/capturewidget.cpp +++ b/src/widgets/capture/capturewidget.cpp @@ -918,7 +918,9 @@ void CaptureWidget::mouseDoubleClickEvent(QMouseEvent* event) drawToolsData(); updateLayersPanel(); handleToolSignal(CaptureTool::REQ_ADD_CHILD_WIDGET); - m_panel->setToolWidget(m_activeTool->configurationWidget()); + if (!m_activeTool.isNull()) { + m_panel->setToolWidget(m_activeTool->configurationWidget()); + } } } else if (m_selection->geometry().contains(event->pos())) { if ((event->button() == Qt::LeftButton) &&