Changeset View
Changeset View
Standalone View
Standalone View
src/ExportManager.cpp
Context not available. | |||||
19 | 19 | | |||
---|---|---|---|---|---|
20 | #include "ExportManager.h" | 20 | #include "ExportManager.h" | ||
21 | 21 | | |||
22 | #include <QDir> | | |||
23 | #include <QMimeDatabase> | 22 | #include <QMimeDatabase> | ||
24 | #include <QImageWriter> | 23 | #include <QImageWriter> | ||
25 | #include <QTemporaryDir> | 24 | #include <QTemporaryDir> | ||
Context not available. | |||||
149 | { | 148 | { | ||
150 | const QString baseDir = saveLocation(); | 149 | const QString baseDir = saveLocation(); | ||
151 | const QDir baseDirPath(baseDir); | 150 | const QDir baseDirPath(baseDir); | ||
152 | const QString filename = makeAutosaveFilename(); | 151 | const QString fileName = makeAutosaveFilename(); | ||
153 | const QString fullpath = autoIncrementFilename(baseDirPath.filePath(filename), | 152 | const QUrl fileNameUrl = autoIncrementFilename(baseDirPath, | ||
153 | fileName, | ||||
154 | SpectacleConfig::instance()->saveImageFormat(), | 154 | SpectacleConfig::instance()->saveImageFormat(), | ||
155 | &ExportManager::isFileExists); | 155 | &ExportManager::isFileExists); | ||
156 | | ||||
157 | const QUrl fileNameUrl = QUrl::fromUserInput(fullpath); | | |||
158 | if (fileNameUrl.isValid()) { | 156 | if (fileNameUrl.isValid()) { | ||
159 | return fileNameUrl; | 157 | return fileNameUrl; | ||
160 | } else { | 158 | } else { | ||
Context not available. | |||||
215 | return truncatedFilename(result); | 213 | return truncatedFilename(result); | ||
216 | } | 214 | } | ||
217 | 215 | | |||
218 | QString ExportManager::autoIncrementFilename(const QString &baseName, const QString &extension, | 216 | QUrl ExportManager::autoIncrementFilename(const QDir &baseDir, | ||
219 | FileNameAlreadyUsedCheck isFileNameUsed) | 217 | const QString &fileName, | ||
218 | const QString &extension, | ||||
219 | FileNameAlreadyUsedCheck isFileNameUsed) | ||||
220 | { | 220 | { | ||
221 | QString result = truncatedFilename(baseName) + QLatin1Literal(".") + extension; | 221 | const QString resultFileName = fileName.isEmpty() ? QStringLiteral("1") : fileName; | ||
222 | if (!((this->*isFileNameUsed)(QUrl::fromUserInput(result)))) { | 222 | const QString resultFilePath = baseDir.filePath(resultFileName) + QLatin1Char('.') + extension; | ||
223 | return result; | 223 | | ||
224 | const QUrl filePath = QUrl::fromUserInput(resultFilePath); | ||||
225 | if (!((this->*isFileNameUsed)(filePath))) { | ||||
226 | return filePath; | ||||
224 | } | 227 | } | ||
225 | 228 | | |||
226 | QString fileNameFmt = truncatedFilename(baseName) + QStringLiteral("-%1."); | 229 | const QString fileNameFmt = fileName.isEmpty() ? QStringLiteral("%1") : fileName + QStringLiteral("-%1"); | ||
230 | const QString filePathFmt = baseDir.filePath(fileNameFmt) + QLatin1Char('.') + extension; | ||||
231 | | ||||
227 | for (quint64 i = 1; i < std::numeric_limits<quint64>::max(); i++) { | 232 | for (quint64 i = 1; i < std::numeric_limits<quint64>::max(); i++) { | ||
228 | result = fileNameFmt.arg(i) + extension; | 233 | const QUrl incrementedNumberFilePath = QUrl::fromUserInput(filePathFmt.arg(i)); | ||
229 | if (!((this->*isFileNameUsed)(QUrl::fromUserInput(result)))) { | 234 | if (!((this->*isFileNameUsed)(incrementedNumberFilePath))) { | ||
230 | return result; | 235 | return incrementedNumberFilePath; | ||
231 | } | 236 | } | ||
232 | } | 237 | } | ||
233 | 238 | | |||
234 | // unlikely this will ever happen, but just in case we've run | 239 | // unlikely this will ever happen, but just in case we've run out of numbers | ||
235 | // out of numbers | 240 | const QString overflowNumberFilePath = filePathFmt.arg(QStringLiteral("OVERFLOW-") + QString::number(qrand() % 10000)); | ||
236 | 241 | return QUrl::fromUserInput(overflowNumberFilePath); | |||
237 | result = fileNameFmt.arg(QStringLiteral("OVERFLOW-") + QString::number(qrand() % 10000)); | | |||
238 | return truncatedFilename(result) + extension; | | |||
239 | } | 242 | } | ||
240 | 243 | | |||
241 | QString ExportManager::makeSaveMimetype(const QUrl &url) | 244 | QString ExportManager::makeSaveMimetype(const QUrl &url) | ||
Context not available. | |||||
344 | // supports the use-case of creating multiple screenshots in a row | 347 | // supports the use-case of creating multiple screenshots in a row | ||
345 | // and exporting them to the same destination e.g. via clipboard, | 348 | // and exporting them to the same destination e.g. via clipboard, | ||
346 | // where the temp file name is used as filename suggestion | 349 | // where the temp file name is used as filename suggestion | ||
347 | const QString baseFileName = mTempDir->path() + QDir::separator() + makeAutosaveFilename(); | 350 | const QUrl filePath = autoIncrementFilename(mTempDir->path(), makeAutosaveFilename(), mimetype, | ||
348 | const QString fileName = autoIncrementFilename(baseFileName, mimetype, | 351 | &ExportManager::isTempFileAlreadyUsed); | ||
349 | &ExportManager::isTempFileAlreadyUsed); | 352 | QFile tmpFile(filePath.toLocalFile()); | ||
350 | QFile tmpFile(fileName); | | |||
351 | if (tmpFile.open(QFile::WriteOnly)) { | 353 | if (tmpFile.open(QFile::WriteOnly)) { | ||
352 | if(writeImage(&tmpFile, mimetype.toLatin1())) { | 354 | if(writeImage(&tmpFile, mimetype.toLatin1())) { | ||
353 | mTempFile = QUrl::fromLocalFile(tmpFile.fileName()); | 355 | mTempFile = QUrl::fromLocalFile(tmpFile.fileName()); | ||
Context not available. | |||||
430 | QFileDialog dialog(parentWindow); | 432 | QFileDialog dialog(parentWindow); | ||
431 | dialog.setAcceptMode(QFileDialog::AcceptSave); | 433 | dialog.setAcceptMode(QFileDialog::AcceptSave); | ||
432 | dialog.setFileMode(QFileDialog::AnyFile); | 434 | dialog.setFileMode(QFileDialog::AnyFile); | ||
433 | dialog.setDirectoryUrl(config->lastSaveAsLocation()); | 435 | const QUrl saveAsLocation = config->lastSaveAsLocation(); | ||
434 | dialog.selectFile(makeAutosaveFilename() + QStringLiteral(".png")); | 436 | dialog.setDirectoryUrl(saveAsLocation); | ||
437 | const QDir baseDirPath(saveAsLocation.toString()); | ||||
438 | const QUrl fullPath = autoIncrementFilename(baseDirPath, | ||||
439 | makeAutosaveFilename(), | ||||
440 | QStringLiteral("png"), | ||||
441 | &ExportManager::isFileExists); | ||||
442 | dialog.selectFile(fullPath.fileName()); | ||||
435 | dialog.setDefaultSuffix(QStringLiteral(".png")); | 443 | dialog.setDefaultSuffix(QStringLiteral(".png")); | ||
436 | dialog.setMimeTypeFilters(supportedFilters); | 444 | dialog.setMimeTypeFilters(supportedFilters); | ||
437 | dialog.selectMimeTypeFilter(QStringLiteral("image/png")); | 445 | dialog.selectMimeTypeFilter(QStringLiteral("image/png")); | ||
Context not available. |