Files
flameshot/src/utils/filenamehandler.cpp
Haris Gušić d1428889b9 Implement config checking (#1859)
* Add error handling functions to ConfigHandler

Refurbished functions setValue and value which were previously unused.
These functions now set/get a setting with error handling.
Currently recognizes only errors recognizable by QSettings.

* Make use of value and setValue in ConfigHandler

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

* Add checker for unrecognized general options

Extraneous config options in [General] will be reported as errors.
Added some placeholder functions to be implemented in future commits.

* Introduce keysFromGroup function

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

* Check shortcut names for duplicates

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

* Fix notification spam

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

* Implement shortcut conflict checking

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

* Fix reading of fallbacks on error

If there is a config error, some values would not be loaded correctly.
Using the newly implemented function ConfigHandler::contains instead of
QSettings::contains solves this issue.

These changes reveal u bug that causes a crash on startup.

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

* Fix crashes introduced in previous commit

Because ConfigHandler is a dependency of most other classes,
calling functions from those classes inside ConfigHandler caused
infinite recursions in some cases.

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

* Add config file watcher

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

* Add missing config options

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

* Fix bug in shortcut conflict detection

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

* Add error resolved notification

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

* Add GUI error message overlay

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

* Add indicator in config window

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

* Use ConfigHandler::fileChanged in ConfigWindow

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

* Fix watcher sometimes not firing

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

* Improve config file watching performance

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

* Add new way to handle config

This is only a fundamental implementation. Future commits will replace
everything with this new paradigm.

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

* Fix getButtons and related functions

Also refactored related code to use QList instead of QVector because
QSettings does not work well with QVector.

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

* Make good use of the new way

* Implement proper checking for basic types

Everything is covered, apart from KeySequence.

* Move fallback path to ExistingDir value handler

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

* Use consistent naming scheme in ConfigHandler

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

* Implement config getters/setters via macro

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

* Surround text with tr and clang-format

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

* Fix colors being saved obfuscated

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

* Add ValueHandler::represenation

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

* Move ValueHandler to separate files

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

* confighandler.cpp: rename macro CUSTOM to OPTION

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

* Fix bug with shortcut conflict checker

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

* Update docs and fix setAllTheButtons

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

* Handle filenamePattern properly

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

* Fix failing build due to wrong function name

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

* Fix QSet error due to Qt version mismatch

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

* Replace QSharedPointer::get with data for older Qt versions

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

* Fix failing build on MacOS and ubuntu 18.04

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

* Add column headers to recognizedGeneralOptions map

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

* Fix ubuntu 18.04 error

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

* Fix false positive when shortcuts empty

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

* Fix wrong shortcut group prefix

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

* Implement proper shortcut checking

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

* Add shortcut map in ConfigHandler

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

* Move ConfigShortcuts functions to ShortcutsWidget

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

* Fix minor bugs

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

* Add fallback scheme: Pictures, HOME, TMP

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

* Add config --check CLI option

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

* Add config error log to GUI

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

* Rename ValueHandler::description to expected

* Convert Qt's #AARRGGBB to #RRGGBBAA and vice versa

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

* Remove obsolete `saveAfterCopyPath`

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

* Fix errors in example config

Also added an additional ; in front of actual comments to differentiate
them from commented options.

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

* Allow special value 'picker' in userColors

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

* Allow only name, #RRGGBB, and #RRGGBBAA color formats

Signed-off-by: Haris Gušić <harisgusic.dev@gmail.com>
2021-09-15 11:56:01 -05:00

132 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors
#include "filenamehandler.h"
#include "src/utils/confighandler.h"
#include "src/utils/strfparse.h"
#include <QDir>
#include <ctime>
#include <exception>
#include <locale>
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include "spdlog/cfg/env.h"
#include "spdlog/spdlog.h"
FileNameHandler::FileNameHandler(QObject* parent)
: QObject(parent)
{
try {
std::locale::global(std::locale(""));
} catch (std::exception& e) {
spdlog::error("Locales on your system are not properly configured. "
"Falling back to defaults");
std::locale::global(std::locale("en_US.UTF-8"));
}
}
QString FileNameHandler::parsedPattern()
{
return parseFilename(ConfigHandler().filenamePattern());
}
QString FileNameHandler::parseFilename(const QString& name)
{
QString res = name;
if (name.isEmpty()) {
res = ConfigHandler().filenamePatternDefault();
}
// remove trailing characters '%' in the pattern
while (res.endsWith('%')) {
res.chop(1);
}
res =
QString::fromStdString(strfparse::format_time_string(name.toStdString()));
// add the parsed pattern in a correct format for the filesystem
res = res.replace(QLatin1String("/"), QStringLiteral(""))
.replace(QLatin1String(":"), QLatin1String("-"));
return res;
}
/**
* @brief Generate a valid destination path from the possibly incomplete `path`.
* The input `path` can be one of:
* - empty string
* - an existing directory
* - a file in an existing directory
* In each case, the output path will be an absolute path to a file with a
* suffix matching the specified `format`.
* @note
* - If `path` points to a directory, the file name will be generated from the
* formatted file name from the user configuration
* - If `path` points to a file, its suffix will be changed to match `format`
* - If `format` is not given, the suffix will remain untouched, unless `path`
* has no suffix, in which case it will be given the "png" suffix
* - If the path generated by the previous steps points to an existing file,
* "_NUM" will be appended to its base name, where NUM is the first
* available number that produces a non-existent path (starting from 1).
* @param path Possibly incomplete file name to transform
* @param format Desired output file suffix (excluding an initial '.' character)
*/
QString FileNameHandler::properScreenshotPath(QString path,
const QString& format)
{
QFileInfo info(path);
QString suffix = info.suffix();
if (info.isDir()) {
// path is a directory => generate filename from configured pattern
path = QDir(QDir(path).absolutePath() + "/" + parsedPattern()).path();
} else {
// path points to a file => strip it of its suffix for now
path = QDir(info.dir().absolutePath() + "/" + info.completeBaseName())
.path();
}
if (!format.isEmpty()) {
// Override suffix to match format
path += "." + format;
} else if (!suffix.isEmpty()) {
// Leave the suffix as it was
path += "." + suffix;
} else {
path += ".png";
}
if (!QFileInfo::exists(path)) {
return path;
} else {
return autoNumerateDuplicate(path);
}
}
QString FileNameHandler::autoNumerateDuplicate(QString path)
{
// add numeration in case of repeated filename in the directory
// find unused name adding _n where n is a number
QFileInfo checkFile(path);
QString directory = checkFile.dir().absolutePath(),
filename = checkFile.completeBaseName(),
suffix = checkFile.suffix();
if (!suffix.isEmpty()) {
suffix = QStringLiteral(".") + suffix;
}
if (checkFile.exists()) {
filename += QLatin1String("_");
int i = 1;
while (true) {
checkFile.setFile(directory + "/" + filename + QString::number(i) +
suffix);
if (!checkFile.exists()) {
filename += QString::number(i);
break;
}
++i;
}
}
return checkFile.filePath();
}