1
0
mirror of https://invent.kde.org/system/dolphin synced 2024-07-04 17:30:55 +00:00

DolphinMainWindow: autosave session

Currently, the session is saved only when the app quits normally. Save
the session after a fixed time interval from the last state change i.e.
anytime the url is changed, or a tab is opened or closed, or the active
view is changed.

BUG: 425627
This commit is contained in:
Amol Godbole 2023-11-09 11:05:48 -06:00 committed by Méven Car
parent f15c8d9238
commit c035e95e1d
5 changed files with 93 additions and 6 deletions

View File

@ -79,6 +79,7 @@
#include <QStandardPaths>
#include <QTimer>
#include <QToolButton>
#include <QtConcurrentRun>
#include <algorithm>
@ -114,6 +115,9 @@ DolphinMainWindow::DolphinMainWindow()
, m_tearDownFromPlacesRequested(false)
, m_backAction(nullptr)
, m_forwardAction(nullptr)
, m_sessionSaveTimer(nullptr)
, m_sessionSaveWatcher(nullptr)
, m_sessionSaveScheduled(false)
{
Q_INIT_RESOURCE(dolphin);
@ -676,12 +680,16 @@ void DolphinMainWindow::closeEvent(QCloseEvent *event)
}
}
if (GeneralSettings::rememberOpenedTabs()) {
KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
KConfig *config = KConfigGui::sessionConfig();
saveGlobalProperties(config);
savePropertiesInternal(config, 1);
config->sync();
if (m_sessionSaveTimer && (m_sessionSaveTimer->isActive() || m_sessionSaveWatcher->isRunning())) {
const bool sessionSaveTimerActive = m_sessionSaveTimer->isActive();
m_sessionSaveTimer->stop();
m_sessionSaveWatcher->disconnect();
m_sessionSaveWatcher->waitForFinished();
if (sessionSaveTimerActive || m_sessionSaveScheduled) {
slotSaveSession();
}
}
GeneralSettings::setVersion(CurrentDolphinVersion);
@ -690,6 +698,58 @@ void DolphinMainWindow::closeEvent(QCloseEvent *event)
KXmlGuiWindow::closeEvent(event);
}
void DolphinMainWindow::slotSaveSession()
{
m_sessionSaveScheduled = false;
if (m_sessionSaveWatcher->isRunning()) {
// The previous session is still being saved - schedule another save.
m_sessionSaveWatcher->disconnect();
connect(m_sessionSaveWatcher, &QFutureWatcher<void>::finished, this, &DolphinMainWindow::slotSaveSession, Qt::SingleShotConnection);
m_sessionSaveScheduled = true;
} else if (!m_sessionSaveTimer->isActive()) {
// No point in saving the session if the timer is running (since it will save the session again when it times out).
KConfigGui::setSessionConfig(QStringLiteral("dolphin"), QStringLiteral("dolphin"));
KConfig *config = KConfigGui::sessionConfig();
saveGlobalProperties(config);
savePropertiesInternal(config, 1);
auto future = QtConcurrent::run([config]() {
config->sync();
});
m_sessionSaveWatcher->setFuture(future);
}
}
void DolphinMainWindow::setSessionAutoSaveEnabled(bool enable)
{
if (enable) {
if (!m_sessionSaveTimer) {
m_sessionSaveTimer = new QTimer(this);
m_sessionSaveWatcher = new QFutureWatcher<void>(this);
m_sessionSaveTimer->setSingleShot(true);
m_sessionSaveTimer->setInterval(22000);
connect(m_sessionSaveTimer, &QTimer::timeout, this, &DolphinMainWindow::slotSaveSession);
}
connect(m_tabWidget, &DolphinTabWidget::urlChanged, m_sessionSaveTimer, qOverload<>(&QTimer::start), Qt::UniqueConnection);
connect(m_tabWidget, &DolphinTabWidget::tabCountChanged, m_sessionSaveTimer, qOverload<>(&QTimer::start), Qt::UniqueConnection);
connect(m_tabWidget, &DolphinTabWidget::activeViewChanged, m_sessionSaveTimer, qOverload<>(&QTimer::start), Qt::UniqueConnection);
} else if (m_sessionSaveTimer) {
m_sessionSaveTimer->stop();
m_sessionSaveWatcher->disconnect();
m_sessionSaveScheduled = false;
m_sessionSaveWatcher->waitForFinished();
m_sessionSaveTimer->deleteLater();
m_sessionSaveWatcher->deleteLater();
m_sessionSaveTimer = nullptr;
m_sessionSaveWatcher = nullptr;
}
}
void DolphinMainWindow::saveProperties(KConfigGroup &group)
{
m_tabWidget->saveProperties(group);

View File

@ -20,6 +20,7 @@
#include "panels/information/informationpanel.h"
#endif
#include <QFutureWatcher>
#include <QIcon>
#include <QList>
#include <QMenu>
@ -116,6 +117,14 @@ public:
*/
void setViewsToHomeIfMountPathOpen(const QString &mountPath);
/**
* Enables or disables the session autosaving feature.
*
* @param enable If true, saves the session automatically after a fixed
* time interval from the last state change.
*/
void setSessionAutoSaveEnabled(bool enable);
bool isFoldersPanelEnabled() const;
bool isInformationPanelEnabled() const;
bool isSplitViewEnabledInCurrentTab() const;
@ -619,6 +628,11 @@ private Q_SLOTS:
*/
void slotKeyBindings();
/**
* Saves the session.
*/
void slotSaveSession();
private:
/**
* Sets up the various menus and actions and connects them.
@ -718,6 +732,10 @@ private:
QMenu m_searchTools;
KFileItemActions m_fileItemActions;
QTimer *m_sessionSaveTimer;
QFutureWatcher<void> *m_sessionSaveWatcher;
bool m_sessionSaveScheduled;
friend class DolphinMainWindowTest;
};

View File

@ -426,6 +426,8 @@ void DolphinTabWidget::tabUrlChanged(const QUrl &url)
if (index == currentIndex()) {
Q_EMIT currentUrlChanged(url);
}
Q_EMIT urlChanged(url);
}
}

View File

@ -111,6 +111,11 @@ Q_SIGNALS:
*/
void currentUrlChanged(const QUrl &url);
/**
* Is emitted when the url of any tab has been changed (including the current tab).
*/
void urlChanged(const QUrl &url);
public Q_SLOTS:
/**
* Opens a new view with the current URL that is part of a tab and activates

View File

@ -245,6 +245,8 @@ int main(int argc, char **argv)
}
}
mainWindow->setSessionAutoSaveEnabled(GeneralSettings::rememberOpenedTabs());
#if HAVE_KUSERFEEDBACK
auto feedbackProvider = DolphinFeedbackProvider::instance();
Q_UNUSED(feedbackProvider)