diff --git a/src/ExportManager.h b/src/ExportManager.h --- a/src/ExportManager.h +++ b/src/ExportManager.h @@ -53,14 +53,11 @@ public: - Q_PROPERTY(QString saveLocation READ saveLocation WRITE setSaveLocation NOTIFY saveLocationChanged) Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap NOTIFY pixmapChanged) Q_PROPERTY(QString windowTitle READ windowTitle WRITE setWindowTitle) Q_PROPERTY(ImageGrabber::GrabMode grabMode READ grabMode WRITE setGrabMode) - void setSaveLocation(const QString &location); - QString saveLocation() const; - QUrl lastSavePath() const; + QString defaultSaveLocation() const; bool isFileExists(const QUrl &url) const; void setPixmap(const QPixmap &pixmap); QPixmap pixmap() const; @@ -73,7 +70,6 @@ Q_SIGNALS: void errorMessage(const QString &str); - void saveLocationChanged(const QString &location); void pixmapChanged(const QPixmap &pixmap); void imageSaved(const QUrl &savedAt); void forceNotify(const QUrl &savedAt); @@ -104,7 +100,6 @@ QPixmap mSavePixmap; QDateTime mPixmapTimestamp; - QUrl mLastSavePath; QUrl mTempFile; QTemporaryDir *mTempDir; QList mUsedTempFileNames; diff --git a/src/ExportManager.cpp b/src/ExportManager.cpp --- a/src/ExportManager.cpp +++ b/src/ExportManager.cpp @@ -44,12 +44,11 @@ ExportManager::ExportManager(QObject *parent) : QObject(parent), mSavePixmap(QPixmap()), - mLastSavePath(QUrl()), mTempFile(QUrl()), mTempDir(nullptr) { connect(this, &ExportManager::imageSaved, [this](const QUrl savedAt) { - mLastSavePath = savedAt; + SpectacleConfig::instance()->setLastSaveFile(savedAt); }); } @@ -111,43 +110,26 @@ // native file save helpers -QString ExportManager::saveLocation() const +QString ExportManager::defaultSaveLocation() const { - KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("spectaclerc")); - KConfigGroup generalConfig = KConfigGroup(config, "General"); - - QString savePath = generalConfig.readPathEntry( - "default-save-location", QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)); + QString savePath = SpectacleConfig::instance()->defaultSaveLocation(); if (savePath.isEmpty() || savePath.isNull()) { savePath = QDir::homePath(); } savePath = QDir::cleanPath(savePath); QDir savePathDir(savePath); if (!(savePathDir.exists())) { savePathDir.mkpath(QStringLiteral(".")); - generalConfig.writePathEntry("last-saved-to", savePath); + SpectacleConfig::instance()->setDefaultSaveLocation(savePath); } return savePath; } -QUrl ExportManager::lastSavePath() const -{ - return isFileExists(mLastSavePath) ? mLastSavePath : QUrl(); -} - -void ExportManager::setSaveLocation(const QString &savePath) -{ - KSharedConfigPtr config = KSharedConfig::openConfig(QStringLiteral("spectaclerc")); - KConfigGroup generalConfig = KConfigGroup(config, "General"); - - generalConfig.writePathEntry("last-saved-to", savePath); -} - QUrl ExportManager::getAutosaveFilename() { - const QString baseDir = saveLocation(); + const QString baseDir = defaultSaveLocation(); const QDir baseDirPath(baseDir); const QString filename = makeAutosaveFilename(); const QString fullpath = autoIncrementFilename(baseDirPath.filePath(filename), @@ -294,7 +276,7 @@ if (listJob->error() != KJob::NoError) { // Create remote save directory - KIO::MkpathJob *mkpathJob = KIO::mkpath(dirPath, QUrl(saveLocation())); + KIO::MkpathJob *mkpathJob = KIO::mkpath(dirPath, QUrl(defaultSaveLocation())); mkpathJob->exec(); if (mkpathJob->error() != KJob::NoError) { @@ -407,7 +389,7 @@ if (save(savePath)) { QDir dir(savePath.path()); dir.cdUp(); - setSaveLocation(dir.absolutePath()); + SpectacleConfig::instance()->setLastSaveFile(savePath); emit imageSaved(savePath); if (notify) { @@ -442,7 +424,7 @@ if (saveUrl.isValid()) { if (save(saveUrl)) { emit imageSaved(saveUrl); - config->setLastSaveAsLocation(saveUrl.adjusted(QUrl::RemoveFilename)); + config->setLastSaveAsFile(saveUrl); if (notify) { emit forceNotify(saveUrl); diff --git a/src/Gui/KSMainWindow.cpp b/src/Gui/KSMainWindow.cpp --- a/src/Gui/KSMainWindow.cpp +++ b/src/Gui/KSMainWindow.cpp @@ -297,23 +297,30 @@ void KSMainWindow::openScreenshotsFolder() { - // Highlight last screenshot in file manager if user saved at least once, - // or open default directory as determined by save button - QUrl location = ExportManager::instance()->lastSavePath(); - if (!ExportManager::instance()->isFileExists(location)) { - switch(SpectacleConfig::instance()->lastUsedSaveMode()) { - case SaveMode::Save: - location = QUrl::fromLocalFile(ExportManager::instance()->saveLocation() + QStringLiteral("/")); - break; - case SaveMode::SaveAs: - default: - location = SpectacleConfig::instance()->lastSaveAsLocation(); // already has a "/" at the end - break; + // Highlight last screenshot in file manager if user saved at least once ever + // (since last save and saveas file names are stored in spectaclerc), otherwise, + // if in save mode, open default save location from configure > save > location + // if in save as mode, open last save as files location + // failsafe for either option is default save location from configure > save > location + SpectacleConfig *cfgManager = SpectacleConfig::instance(); + ExportManager *exportManager = ExportManager::instance(); + QUrl location; + + switch(cfgManager->lastUsedSaveMode()) { + case SaveMode::Save: + location = cfgManager->lastSaveFile(); + if (!exportManager->isFileExists(location)) { + location = QUrl(cfgManager->defaultSaveLocation()); } - if (!ExportManager::instance()->isFileExists(location)) { - location = QUrl(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + QStringLiteral("/")); + break; + case SaveMode::SaveAs: + location = cfgManager->lastSaveAsFile(); // already has a "/" at the end + if (!exportManager->isFileExists(location)) { + location = cfgManager->lastSaveAsLocation(); } + break; } + KIO::highlightInFileManager({location}); } diff --git a/src/Gui/SettingsDialog/SaveOptionsPage.cpp b/src/Gui/SettingsDialog/SaveOptionsPage.cpp --- a/src/Gui/SettingsDialog/SaveOptionsPage.cpp +++ b/src/Gui/SettingsDialog/SaveOptionsPage.cpp @@ -136,7 +136,7 @@ // save the data - cfgManager->setAutoSaveLocation(mUrlRequester->url().toDisplayString(QUrl::PreferLocalFile)); + cfgManager->setDefaultSaveLocation(mUrlRequester->url().toDisplayString(QUrl::PreferLocalFile)); cfgManager->setAutoSaveFilenameFormat(mSaveNameFormat->text()); cfgManager->setSaveImageFormat(mSaveImageFormat->currentText().toLower()); cfgManager->setCopySaveLocationToClipboard(mCopyPathToClipboard->checkState() == Qt::Checked); @@ -155,7 +155,7 @@ // read in the data mSaveNameFormat->setText(cfgManager->autoSaveFilenameFormat()); - mUrlRequester->setUrl(QUrl::fromUserInput(cfgManager->autoSaveLocation())); + mUrlRequester->setUrl(QUrl::fromUserInput(cfgManager->defaultSaveLocation())); mCopyPathToClipboard->setChecked(cfgManager->copySaveLocationToClipboard()); // read in the save image format and calculate its index diff --git a/src/SpectacleConfig.h b/src/SpectacleConfig.h --- a/src/SpectacleConfig.h +++ b/src/SpectacleConfig.h @@ -44,7 +44,10 @@ QString defaultFilename() const; QString defaultTimestampTemplate() const; - + + QUrl lastSaveAsLocation() const; + QUrl lastSaveLocation() const; + private: explicit SpectacleConfig(QObject *parent = nullptr); @@ -57,8 +60,11 @@ public Q_SLOTS: - QUrl lastSaveAsLocation() const; - void setLastSaveAsLocation(const QUrl &location); + QUrl lastSaveAsFile() const; + void setLastSaveAsFile(const QUrl &location); + + QUrl lastSaveFile() const; + void setLastSaveFile(const QUrl &location); QRect cropRegion() const; void setCropRegion(const QRect ®ion); @@ -99,8 +105,8 @@ QString autoSaveFilenameFormat() const; void setAutoSaveFilenameFormat(const QString &format); - QString autoSaveLocation() const; - void setAutoSaveLocation(const QString &location); + QString defaultSaveLocation() const; + void setDefaultSaveLocation(const QString &location); bool copySaveLocationToClipboard() const; void setCopySaveLocationToClipboard(bool enabled); diff --git a/src/SpectacleConfig.cpp b/src/SpectacleConfig.cpp --- a/src/SpectacleConfig.cpp +++ b/src/SpectacleConfig.cpp @@ -49,18 +49,42 @@ // lastSaveAsLocation +QUrl SpectacleConfig::lastSaveAsFile() const +{ + return mGeneralConfig.readEntry(QStringLiteral("lastSaveAsFile"), + QUrl(this->defaultSaveLocation())); +} + +void SpectacleConfig::setLastSaveAsFile(const QUrl &location) +{ + mGeneralConfig.writeEntry(QStringLiteral("lastSaveAsFile"), location); + mGeneralConfig.sync(); +} + QUrl SpectacleConfig::lastSaveAsLocation() const { - return mGeneralConfig.readEntry(QStringLiteral("lastSaveAsLocation"), - QUrl::fromUserInput(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) + QStringLiteral("/"))); + return this->lastSaveAsFile().adjusted(QUrl::RemoveFilename); } -void SpectacleConfig::setLastSaveAsLocation(const QUrl &location) +// lastSaveLocation + +QUrl SpectacleConfig::lastSaveFile() const +{ + return mGeneralConfig.readEntry(QStringLiteral("lastSaveFile"), + QUrl(this->defaultSaveLocation())); +} + +void SpectacleConfig::setLastSaveFile(const QUrl &location) { - mGeneralConfig.writeEntry(QStringLiteral("lastSaveAsLocation"), location); + mGeneralConfig.writeEntry(QStringLiteral("lastSaveFile"), location); mGeneralConfig.sync(); } +QUrl SpectacleConfig::lastSaveLocation() const +{ + return this->lastSaveFile().adjusted(QUrl::RemoveFilename); +} + // cropRegion QRect SpectacleConfig::cropRegion() const @@ -239,13 +263,13 @@ // autosave location -QString SpectacleConfig::autoSaveLocation() const +QString SpectacleConfig::defaultSaveLocation() const { return mGeneralConfig.readPathEntry(QStringLiteral("default-save-location"), QStandardPaths::writableLocation(QStandardPaths::PicturesLocation)); } -void SpectacleConfig::setAutoSaveLocation(const QString &location) +void SpectacleConfig::setDefaultSaveLocation(const QString &location) { mGeneralConfig.writePathEntry(QStringLiteral("default-save-location"), location); mGeneralConfig.sync();