Improve screenshot path handling (#1815)

* Make --path work correctly with relative paths

Relative paths are taken relative to the working directory of the calling
command, not relative to the daemon's working directory.

* Allow file paths in --path and refactor

* Remove some redundancy

These actions are already performed in the respective functions in
FlameshotDBusAdapter.

* Tweak --path error checker a bit more

* Rework FileNameHandler and update references

The class now has a much simpler interface.

- Screenshot paths are now universally determined by the function
  properScreenshotPath
- Some unreferenced methods have been removed
- The documentation of properScreenshotPath documents the changes well.

* Add crude tests for --path

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>

* Add a test for invalid path

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

* Make tests clearer

Thanks to @mmahmoudian for his review and contribution.

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

* Fix bug in properScreenshotPath

Auto-numeration did not work when the screenshot was automatically
saved when copied to clipboard.

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

* Fall back to default pictures location

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

* Revert "Remove some redundancy"

This was not redundancy. I had actually introduced a bug with this.

This reverts commit 011ef737564892e494518443e6b80ccf3d286ae1.

* Change default path only on interactive save

Previously, the default save path was changed every time a screenshot
was saved. Now, that only happens when it gets saved from the GUI.

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

* Change --path help text

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

* Allow other image formats

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>
This commit is contained in:
Haris Gušić
2021-08-20 20:10:12 +02:00
committed by GitHub
parent 584bcd7f6c
commit 41ca51bc10
14 changed files with 147 additions and 93 deletions

View File

@@ -14,6 +14,7 @@
#include <QImageWriter>
#include <QMessageBox>
#include <QMimeData>
#include <QStandardPaths>
#include <qimagewriter.h>
#include <qmimedatabase.h>
#if defined(Q_OS_MACOS)
@@ -87,8 +88,7 @@ bool ScreenshotSaver::saveToFilesystem(const QPixmap& capture,
const QString& path,
const QString& messagePrefix)
{
QString completePath = FileNameHandler().generateAbsolutePath(path);
completePath += QLatin1String(".png");
QString completePath = FileNameHandler().properScreenshotPath(path);
bool ok = capture.save(completePath);
QString saveMessage = messagePrefix;
QString notificationPath = completePath;
@@ -97,7 +97,6 @@ bool ScreenshotSaver::saveToFilesystem(const QPixmap& capture,
}
if (ok) {
ConfigHandler().setSavePath(path);
saveMessage += QObject::tr("Capture saved as ") + completePath;
Controller::getInstance()->sendCaptureSaved(
m_id, QFileInfo(completePath).canonicalFilePath());
@@ -143,7 +142,13 @@ bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture)
{
bool ok = false;
ConfigHandler config;
QString savePath = FileNameHandler().absoluteSavePath();
QString defaultSavePath = ConfigHandler().savePath();
if (defaultSavePath.isEmpty() || !QDir(defaultSavePath).exists() ||
!QFileInfo(defaultSavePath).isWritable()) {
defaultSavePath =
QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
}
QString savePath = FileNameHandler().properScreenshotPath(defaultSavePath);
#if defined(Q_OS_MACOS)
for (QWidget* widget : qApp->topLevelWidgets()) {
QString className(widget->metaObject()->className());
@@ -158,10 +163,7 @@ bool ScreenshotSaver::saveToFilesystemGUI(const QPixmap& capture)
if (!config.savePathFixed()) {
// auto imageFormats = QImageWriter::supportedImageFormats();
savePath =
ShowSaveFileDialog(nullptr,
QObject::tr("Save screenshot"),
FileNameHandler().absoluteSavePath() +
ConfigHandler().getSaveAsFileExtension());
ShowSaveFileDialog(nullptr, QObject::tr("Save screenshot"), savePath);
}
if (savePath == "") {
return ok;