Files
flameshot/src/utils/screenshotsaver.cpp
Haris Gušić 233c765b1f Reduce dependence on D-Bus (#2003)
* Handle captures without sigslots

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Set {app,organization}Name and version consistently

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Make 'full' dbus-free

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Move CaptureRequest::exportCapture to Controller

We need to wait until the upload widget (or similar widgets) have
finished before exiting. This must be done using a signal. The problem
is that CaptureRequest can't be guaranteed to survive until the widget
has finished what it's doing.

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Use QApplication with the 'full' subcommand

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Do unto 'screen' as we did to 'full'

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Add FlameshotDaemon singleton class

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Support clipboard hosting for both pixmaps and text

* Fix upload handling

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Do not show tray icon if not daemon

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Clean up handling of pin task

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Remove annoying Qt warning messages

The messages were caused by the color wheel.

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix small bug in Controller::exportCapture

* Fix --raw output

* Make 'gui' dbus-independent

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix accept on select bug

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix compile error on Windows

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Make it work on Windows

* Remove obsolete function in main.cpp

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Make 'launcher' work without dbus

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* clang-format, sigh

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Enable CLI parsing on MacOS

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Make 'config' work without dbus

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Small refactor of capture request handling

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Remove obsolete DBusUtils

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Remove unused D-Bus sigslots

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Remove D-Bus methods openConfig, autostartEnabled and trayIconEnabled

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Remove D-Bus method requestCapture

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Remove CaptureRequest id mechanism

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix 'launcher' crash

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Handle clipboard notifications properly

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Add 'autoCloseIdleDaemon' option

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Document FlameshotDaemon class

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Make 'flameshot gui' run in single-application mode

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Add `allowmultipleGuiInstances` config option

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix endless loop with multiple GUI instances

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Move upload confirmation dialog where it belongs

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Add the new config options to the GUI as well

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix failing build on Windows

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Handle persistence on MacOS

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* fixed notifications on macos

* Fixed display on macos

* Reformat tests/action_options.sh

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

* Fix infinite recursion in tests/action_options.sh

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>

Co-authored-by: Dearsh Oberoi <59907159+deo002@users.noreply.github.com>
Co-authored-by: Jeremy Borgman <borgman.jeremy@pm.me>
2021-12-08 22:18:39 +01:00

210 lines
6.7 KiB
C++

// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
#include "screenshotsaver.h"
#include "src/core/controller.h"
#include "src/core/flameshotdaemon.h"
#include "src/utils/confighandler.h"
#include "src/utils/filenamehandler.h"
#include "src/utils/globalvalues.h"
#include "src/utils/systemnotification.h"
#include "utils/desktopinfo.h"
#include <QApplication>
#include <QBuffer>
#include <QClipboard>
#include <QFileDialog>
#include <QImageWriter>
#include <QMessageBox>
#include <QMimeData>
#include <QStandardPaths>
#include <qimagewriter.h>
#include <qmimedatabase.h>
#if defined(Q_OS_MACOS)
#include "src/widgets/capture/capturewidget.h"
#endif
ScreenshotSaver::ScreenshotSaver() {}
void ScreenshotSaver::saveToClipboardMime(const QPixmap& capture,
const QString& imageType)
{
QByteArray array;
QBuffer buffer{ &array };
QImageWriter imageWriter{ &buffer, imageType.toUpper().toUtf8() };
imageWriter.write(capture.toImage());
QPixmap pngPixmap;
bool isLoaded =
pngPixmap.loadFromData(reinterpret_cast<uchar*>(array.data()),
array.size(),
imageType.toUpper().toUtf8());
if (isLoaded) {
QMimeData* mimeData = new QMimeData;
mimeData->setData("image/" + imageType, array);
QApplication::clipboard()->setMimeData(mimeData);
} else {
SystemNotification().sendMessage(
QObject::tr("Error while saving to clipboard"));
}
}
// TODO: If data is saved to the clipboard before the notification is sent via
// dbus, the application freezes.
void ScreenshotSaver::saveToClipboard(const QPixmap& capture)
{
// If we are able to properly save the file, save the file and copy to
// clipboard.
if ((ConfigHandler().saveAfterCopy()) &&
(!ConfigHandler().savePath().isEmpty())) {
saveToFilesystem(capture,
ConfigHandler().savePath(),
QObject::tr("Capture saved to clipboard."));
} else {
SystemNotification().sendMessage(
QObject::tr("Capture saved to clipboard."));
}
if (ConfigHandler().useJpgForClipboard()) {
// FIXME - it doesn't work on MacOS
saveToClipboardMime(capture, "jpeg");
} else {
// Need to send message before copying to clipboard
#if defined(Q_OS_LINUX) || defined(Q_OS_UNIX)
if (DesktopInfo().waylandDetected()) {
saveToClipboardMime(capture, "png");
} else {
QApplication::clipboard()->setPixmap(capture);
}
#else
QApplication::clipboard()->setPixmap(capture);
#endif
}
}
bool ScreenshotSaver::saveToFilesystem(const QPixmap& capture,
const QString& path,
const QString& messagePrefix)
{
QString completePath = FileNameHandler().properScreenshotPath(
path, ConfigHandler().setSaveAsFileExtension());
QFile file{ completePath };
file.open(QIODevice::WriteOnly);
bool ok = capture.save(&file);
QString saveMessage = messagePrefix;
QString notificationPath = completePath;
if (!saveMessage.isEmpty()) {
saveMessage += " ";
}
if (ok) {
saveMessage += QObject::tr("Capture saved as ") + completePath;
} else {
saveMessage += QObject::tr("Error trying to save as ") + completePath;
if (file.error() != QFile::NoError) {
saveMessage += ": " + file.errorString();
}
notificationPath = "";
}
SystemNotification().sendMessage(saveMessage, notificationPath);
return ok;
}
QString ScreenshotSaver::ShowSaveFileDialog(QWidget* parent,
const QString& title,
const QString& directory)
{
QFileDialog dialog(parent, title, directory);
if (parent) {
dialog.setWindowModality(Qt::WindowModal);
}
dialog.setAcceptMode(QFileDialog::AcceptSave);
// Build string list of supported image formats
QStringList mimeTypeList;
foreach (auto mimeType, QImageWriter::supportedMimeTypes())
mimeTypeList.append(mimeType);
dialog.setMimeTypeFilters(mimeTypeList);
QString suffix = ConfigHandler().setSaveAsFileExtension();
if (suffix.isEmpty()) {
suffix = "png";
}
QString defaultMimeType =
QMimeDatabase().mimeTypeForFile("image." + suffix).name();
dialog.selectMimeTypeFilter(defaultMimeType);
dialog.setDefaultSuffix(suffix);
if (dialog.exec() == QDialog::Accepted) {
return dialog.selectedFiles().first();
} else {
return QString();
}
}
bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture)
{
bool ok = false;
ConfigHandler config;
QString defaultSavePath = ConfigHandler().savePath();
if (defaultSavePath.isEmpty() || !QDir(defaultSavePath).exists() ||
!QFileInfo(defaultSavePath).isWritable()) {
defaultSavePath =
QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
}
QString savePath = FileNameHandler().properScreenshotPath(
defaultSavePath, ConfigHandler().setSaveAsFileExtension());
#if defined(Q_OS_MACOS)
for (QWidget* widget : qApp->topLevelWidgets()) {
QString className(widget->metaObject()->className());
if (0 ==
className.compare(CaptureWidget::staticMetaObject.className())) {
widget->showNormal();
widget->hide();
break;
}
}
#endif
if (!config.savePathFixed()) {
// auto imageFormats = QImageWriter::supportedImageFormats();
savePath =
ShowSaveFileDialog(nullptr, QObject::tr("Save screenshot"), savePath);
}
if (savePath == "") {
return ok;
}
QFile file{ savePath };
file.open(QIODevice::WriteOnly);
ok = capture.save(&file);
if (ok) {
QString pathNoFile =
savePath.left(savePath.lastIndexOf(QLatin1String("/")));
ConfigHandler().setSavePath(pathNoFile);
QString msg = QObject::tr("Capture saved as ") + savePath;
SystemNotification().sendMessage(msg, savePath);
if (config.copyPathAfterSave()) {
FlameshotDaemon::copyToClipboard(
savePath, QObject::tr("Path copied to clipboard as ") + savePath);
}
} else {
QString msg = QObject::tr("Error trying to save as ") + savePath;
if (file.error() != QFile::NoError) {
msg += ": " + file.errorString();
}
QMessageBox saveErrBox(
QMessageBox::Warning, QObject::tr("Save Error"), msg);
saveErrBox.setWindowIcon(QIcon(GlobalValues::iconPath()));
saveErrBox.exec();
}
return ok;
}