|
@@ -5,7 +5,6 @@
|
|
|
#include "src/utils/confighandler.h"
|
|
|
#include "src/utils/strfparse.h"
|
|
|
#include <QDir>
|
|
|
-#include <QStandardPaths>
|
|
|
#include <ctime>
|
|
|
#include <exception>
|
|
|
#include <locale>
|
|
@@ -52,65 +51,75 @@ QString FileNameHandler::parseFilename(const QString& name)
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
-QString FileNameHandler::generateAbsolutePath(const QString& path)
|
|
|
+/**
|
|
|
+ * @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)
|
|
|
{
|
|
|
- QString directory = path;
|
|
|
- QString filename = parsedPattern();
|
|
|
- fixPath(directory, filename);
|
|
|
- return directory + filename;
|
|
|
-}
|
|
|
-// path a images si no existe, add numeration
|
|
|
-void FileNameHandler::setPattern(const QString& pattern)
|
|
|
-{
|
|
|
- ConfigHandler().setFilenamePattern(pattern);
|
|
|
-}
|
|
|
+ QFileInfo info(path);
|
|
|
+ QString suffix = info.suffix();
|
|
|
|
|
|
-QString FileNameHandler::absoluteSavePath(QString& directory, QString& filename)
|
|
|
-{
|
|
|
- ConfigHandler config;
|
|
|
- directory = config.savePath();
|
|
|
- if (directory.isEmpty() || !QDir(directory).exists() ||
|
|
|
- !QFileInfo(directory).isWritable()) {
|
|
|
- directory =
|
|
|
- QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
|
|
|
+ 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();
|
|
|
}
|
|
|
- filename = parsedPattern();
|
|
|
- fixPath(directory, filename);
|
|
|
- return directory + filename;
|
|
|
-}
|
|
|
-
|
|
|
-QString FileNameHandler::absoluteSavePath()
|
|
|
-{
|
|
|
- QString dir, file;
|
|
|
- return absoluteSavePath(dir, file);
|
|
|
-}
|
|
|
|
|
|
-QString FileNameHandler::charArrToQString(const char* c)
|
|
|
-{
|
|
|
- return QString::fromLocal8Bit(c, MAX_CHARACTERS);
|
|
|
-}
|
|
|
+ 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";
|
|
|
+ }
|
|
|
|
|
|
-char* FileNameHandler::QStringToCharArr(const QString& s)
|
|
|
-{
|
|
|
- QByteArray ba = s.toLocal8Bit();
|
|
|
- return const_cast<char*>(strdup(ba.constData()));
|
|
|
+ if (!QFileInfo::exists(path)) {
|
|
|
+ return path;
|
|
|
+ } else {
|
|
|
+ return autoNumerateDuplicate(path);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
-void FileNameHandler::fixPath(QString& directory, QString& filename)
|
|
|
+QString FileNameHandler::autoNumerateDuplicate(QString path)
|
|
|
{
|
|
|
- // add '/' at the end of the directory
|
|
|
- if (!directory.endsWith(QLatin1String("/"))) {
|
|
|
- directory += QLatin1String("/");
|
|
|
- }
|
|
|
// add numeration in case of repeated filename in the directory
|
|
|
// find unused name adding _n where n is a number
|
|
|
- QFileInfo checkFile(directory + filename + ".png");
|
|
|
+ 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) +
|
|
|
- ".png");
|
|
|
+ checkFile.setFile(directory + "/" + filename + QString::number(i) +
|
|
|
+ suffix);
|
|
|
if (!checkFile.exists()) {
|
|
|
filename += QString::number(i);
|
|
|
break;
|
|
@@ -118,4 +127,5 @@ void FileNameHandler::fixPath(QString& directory, QString& filename)
|
|
|
++i;
|
|
|
}
|
|
|
}
|
|
|
+ return checkFile.filePath();
|
|
|
}
|