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. | |||||
130 | { | 129 | { | ||
131 | const QString baseDir = saveLocation(); | 130 | const QString baseDir = saveLocation(); | ||
132 | const QDir baseDirPath(baseDir); | 131 | const QDir baseDirPath(baseDir); | ||
133 | const QString filename = makeAutosaveFilename(); | 132 | const QUrl fileNameUrl = autoIncrementFilename(baseDirPath, makeAutosaveFilename(), | ||
134 | const QString fullpath = autoIncrementFilename(baseDirPath.filePath(filename), | | |||
135 | SpectacleConfig::instance()->saveImageFormat(), | 133 | SpectacleConfig::instance()->saveImageFormat(), | ||
136 | &ExportManager::isFileExists); | 134 | &ExportManager::isFileExists); | ||
137 | | ||||
138 | const QUrl fileNameUrl = QUrl::fromUserInput(fullpath); | | |||
139 | if (fileNameUrl.isValid()) { | 135 | if (fileNameUrl.isValid()) { | ||
140 | return fileNameUrl; | 136 | return fileNameUrl; | ||
141 | } else { | 137 | } else { | ||
Context not available. | |||||
160 | .replace(QLatin1String("%S"), timestamp.toString(QStringLiteral("ss"))); | 156 | .replace(QLatin1String("%S"), timestamp.toString(QStringLiteral("ss"))); | ||
161 | } | 157 | } | ||
162 | 158 | | |||
163 | QString ExportManager::autoIncrementFilename(const QString &baseName, const QString &extension, | 159 | QUrl ExportManager::autoIncrementFilename(const QDir &baseDir, const QString &fileName, | ||
164 | FileNameAlreadyUsedCheck isFileNameUsed) | 160 | const QString &extension, | ||
161 | FileNameAlreadyUsedCheck isFileNameUsed) | ||||
165 | { | 162 | { | ||
166 | if (!((this->*isFileNameUsed)(QUrl::fromUserInput(baseName + QLatin1Char('.') + extension)))) { | 163 | QString resultFileName = fileName.isEmpty() ? QStringLiteral("1") : fileName; | ||
167 | return baseName + QLatin1Char('.') + extension; | 164 | QString resultFilePath = baseDir.filePath(resultFileName) + QLatin1Char('.') + extension; | ||
165 | | ||||
166 | QUrl filePath = QUrl::fromUserInput(resultFilePath); | ||||
167 | if (!((this->*isFileNameUsed)(filePath))) { | ||||
168 | return filePath; | ||||
168 | } | 169 | } | ||
169 | 170 | | |||
170 | QString fileNameFmt(baseName + QStringLiteral("-%1.") + extension); | 171 | QString fileNameFmt = fileName.isEmpty() ? QStringLiteral("%1") : fileName + QStringLiteral("-%1"); | ||
172 | QString filePathFmt = baseDir.filePath(fileNameFmt) + QLatin1Char('.') + extension; | ||||
173 | | ||||
171 | for (quint64 i = 1; i < std::numeric_limits<quint64>::max(); i++) { | 174 | for (quint64 i = 1; i < std::numeric_limits<quint64>::max(); i++) { | ||
172 | if (!((this->*isFileNameUsed)(QUrl::fromUserInput(fileNameFmt.arg(i))))) { | 175 | QUrl incrementedNumberFilePath = QUrl::fromUserInput(filePathFmt.arg(i)); | ||
173 | return fileNameFmt.arg(i); | 176 | if (!((this->*isFileNameUsed)(incrementedNumberFilePath))) { | ||
177 | return incrementedNumberFilePath; | ||||
174 | } | 178 | } | ||
175 | } | 179 | } | ||
176 | 180 | | |||
177 | // unlikely this will ever happen, but just in case we've run | 181 | // unlikely this will ever happen, but just in case we've run | ||
178 | // out of numbers | 182 | // out of numbers | ||
179 | 183 | QString overflowNumberFilePath = filePathFmt.arg(QStringLiteral("OVERFLOW-") + QString::number(qrand() % 10000)); | |||
180 | return fileNameFmt.arg(QStringLiteral("OVERFLOW-") + QString::number(qrand() % 10000)); | 184 | return QUrl::fromUserInput(overflowNumberFilePath); | ||
181 | } | 185 | } | ||
182 | 186 | | |||
183 | QString ExportManager::makeSaveMimetype(const QUrl &url) | 187 | QString ExportManager::makeSaveMimetype(const QUrl &url) | ||
Context not available. | |||||
254 | // supports the use-case of creating multiple screenshots in a row | 258 | // supports the use-case of creating multiple screenshots in a row | ||
255 | // and exporting them to the same destination e.g. via clipboard, | 259 | // and exporting them to the same destination e.g. via clipboard, | ||
256 | // where the temp file name is used as filename suggestion | 260 | // where the temp file name is used as filename suggestion | ||
257 | const QString baseFileName = mTempDir->path() + QDir::separator() + makeAutosaveFilename(); | 261 | const QUrl filePath = autoIncrementFilename(mTempDir->path(), makeAutosaveFilename(), mimetype, | ||
258 | const QString fileName = autoIncrementFilename(baseFileName, mimetype, | 262 | &ExportManager::isTempFileAlreadyUsed); | ||
259 | &ExportManager::isTempFileAlreadyUsed); | 263 | QFile tmpFile(filePath.toLocalFile()); | ||
260 | QFile tmpFile(fileName); | | |||
261 | if (tmpFile.open(QFile::WriteOnly)) { | 264 | if (tmpFile.open(QFile::WriteOnly)) { | ||
262 | if(writeImage(&tmpFile, mimetype.toLatin1())) { | 265 | if(writeImage(&tmpFile, mimetype.toLatin1())) { | ||
263 | mTempFile = QUrl::fromLocalFile(tmpFile.fileName()); | 266 | mTempFile = QUrl::fromLocalFile(tmpFile.fileName()); | ||
Context not available. | |||||
341 | QFileDialog dialog(parentWindow); | 344 | QFileDialog dialog(parentWindow); | ||
342 | dialog.setAcceptMode(QFileDialog::AcceptSave); | 345 | dialog.setAcceptMode(QFileDialog::AcceptSave); | ||
343 | dialog.setFileMode(QFileDialog::AnyFile); | 346 | dialog.setFileMode(QFileDialog::AnyFile); | ||
344 | dialog.setDirectoryUrl(config->lastSaveAsLocation()); | 347 | const QUrl saveAsLocation = config->lastSaveAsLocation(); | ||
345 | dialog.selectFile(makeAutosaveFilename() + QStringLiteral(".png")); | 348 | dialog.setDirectoryUrl(saveAsLocation); | ||
349 | const QDir baseDirPath(saveAsLocation.toString()); | ||||
350 | const QUrl fullPath = autoIncrementFilename(baseDirPath, makeAutosaveFilename(), | ||||
351 | QStringLiteral("png"), | ||||
352 | &ExportManager::isFileExists); | ||||
353 | dialog.selectFile(fullPath.fileName()); | ||||
346 | dialog.setDefaultSuffix(QStringLiteral(".png")); | 354 | dialog.setDefaultSuffix(QStringLiteral(".png")); | ||
347 | dialog.setMimeTypeFilters(supportedFilters); | 355 | dialog.setMimeTypeFilters(supportedFilters); | ||
348 | dialog.selectMimeTypeFilter(QStringLiteral("image/png")); | 356 | dialog.selectMimeTypeFilter(QStringLiteral("image/png")); | ||
Context not available. |