Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/ApplicationSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ CREATE_SETTING(Gui, ExitOnLastTabClosed, exitOnLastTabClosed, bool, false)

CREATE_SETTING(Gui, CombineSearchResults, combineSearchResults, bool, false)

CREATE_SETTING(App, RestorePreviousSession, restorePreviousSession, bool, false)
CREATE_SETTING(App, RestoreUnsavedFiles, restoreUnsavedFiles, bool, false)
CREATE_SETTING(App, RestoreTempFiles, restoreTempFiles, bool, false)
CREATE_SETTING(App, EnableSessionSnapshot, enableSessionSnapshot, bool, true)
CREATE_SETTING(App, RestorePreviousSession, restorePreviousSession, bool, true)
CREATE_SETTING(App, RestoreUnsavedFiles, restoreUnsavedFiles, bool, true)
CREATE_SETTING(App, RestoreTempFiles, restoreTempFiles, bool, true)

CREATE_SETTING(App, DefaultDirectoryBehavior, defaultDirectoryBehavior, ApplicationSettings::DefaultDirectoryBehaviorEnum, ApplicationSettings::FollowCurrentDocument)
CREATE_SETTING(App, DefaultDirectory, defaultDirectory, QString, QString())
Expand Down
1 change: 1 addition & 0 deletions src/ApplicationSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class ApplicationSettings : public QSettings

DEFINE_SETTING(CombineSearchResults, combineSearchResults, bool)

DEFINE_SETTING(EnableSessionSnapshot, enableSessionSnapshot, bool)
DEFINE_SETTING(RestorePreviousSession, restorePreviousSession, bool)
DEFINE_SETTING(RestoreUnsavedFiles, restoreUnsavedFiles, bool)
DEFINE_SETTING(RestoreTempFiles, restoreTempFiles, bool)
Expand Down
24 changes: 15 additions & 9 deletions src/NotepadNextApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,10 @@ bool NotepadNextApplication::init()
}
});

if (settings->restorePreviousSession()) {
// Initialize session manager file types from settings before loading session
getSessionManager();

if (settings->enableSessionSnapshot()) {
qInfo("Restoring previous session");

sessionManager->loadSession(window);
Expand Down Expand Up @@ -229,16 +232,14 @@ SessionManager *NotepadNextApplication::getSessionManager() const
{
SessionManager::SessionFileTypes fileTypes;

if (settings->restorePreviousSession()) {
fileTypes |= SessionManager::SavedFile;
}

if (settings->restoreUnsavedFiles()) {
if (settings->enableSessionSnapshot()) {
// Safety net: always persist unsaved and temp file content, regardless of restore settings
fileTypes |= SessionManager::UnsavedFile;
fileTypes |= SessionManager::TempFile;
}

if (settings->restoreTempFiles()) {
fileTypes |= SessionManager::TempFile;
if (settings->restorePreviousSession()) {
fileTypes |= SessionManager::SavedFile;
}

// Update the file types supported in case something has changed in the settings
Expand Down Expand Up @@ -487,7 +488,12 @@ MainWindow *NotepadNextApplication::createNewWindow()

// Timer to autosave the session
connect(&autoSaveTimer, &QTimer::timeout, this, &NotepadNextApplication::saveSession);
autoSaveTimer.start(60 * 1000);
autoSaveTimer.start(7 * 1000);

// Backup session when switching between editors
connect(window, &MainWindow::editorActivated, this, [=](ScintillaNext *) {
saveSession();
});

return window;
}
Expand Down
27 changes: 20 additions & 7 deletions src/SessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,12 @@ void SessionManager::saveSession(MainWindow *window)
for (const auto &editor : window->editors()) {
SessionFileType editorType = determineType(editor);

if (fileTypes.testFlag(editorType)) {
// UnsavedFile and TempFile are always saved as a safety net;
// SavedFile is only saved when the corresponding flag is set
bool shouldSave = (editorType == SessionManager::UnsavedFile || editorType == SessionManager::TempFile)
|| fileTypes.testFlag(editorType);

if (shouldSave) {
settings.setArrayIndex(index);

if (editorType == SessionManager::SavedFile) {
Expand Down Expand Up @@ -189,9 +194,9 @@ void SessionManager::loadSession(MainWindow *window)
const int currentEditorIndex = settings.value("CurrentEditorIndex").toInt();
const int size = settings.beginReadArray("OpenedFiles");

// NOTE: In theory the fileTypes should determine what is loaded, however if the session fileTypes
// change from the last time it was saved then it means the settings were manually altered outside of the app,
// which is non-standard behavior, so just load anything in the file
// NOTE: fileTypes determines what is loaded based on user settings.
// UnsavedFile and TempFile restore is gated by restoreUnsavedFiles/restoreTempFiles,
// but they are always saved so content is never lost.

for (int index = 0; index < size; ++index) {
settings.setArrayIndex(index);
Expand All @@ -202,13 +207,16 @@ void SessionManager::loadSession(MainWindow *window)
const QString type = settings.value("Type").toString();

if (type == QStringLiteral("File")) {
editor = loadFileDetails(settings);
if (fileTypes.testFlag(SessionManager::SavedFile))
editor = loadFileDetails(settings);
}
else if (type == QStringLiteral("UnsavedFile")) {
editor = loadUnsavedFileDetails(settings);
if (fileTypes.testFlag(SessionManager::UnsavedFile))
editor = loadUnsavedFileDetails(settings);
}
else if (type == QStringLiteral("Temp")) {
editor = loadTempFile(settings);
if (fileTypes.testFlag(SessionManager::TempFile))
editor = loadTempFile(settings);
}
else {
qDebug("Unknown session entry type: %s", qUtf8Printable(type));
Expand Down Expand Up @@ -238,6 +246,11 @@ bool SessionManager::willFileGetStoredInSession(ScintillaNext *editor) const
{
SessionFileType editorType = determineType(editor);

// UnsavedFile and TempFile are always persisted as a safety net
if (editorType == SessionManager::UnsavedFile || editorType == SessionManager::TempFile) {
return true;
}

// See if the editor type is in the currently supported file types
return fileTypes.testFlag(editorType);
}
Expand Down
14 changes: 8 additions & 6 deletions src/dialogs/PreferencesDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include <QButtonGroup>
#include <QFileDialog>
#include <QMessageBox>


PreferencesDialog::PreferencesDialog(ApplicationSettings *settings, QWidget *parent) :
Expand All @@ -46,17 +45,20 @@ PreferencesDialog::PreferencesDialog(ApplicationSettings *settings, QWidget *par
MapSettingToCheckBox(ui->checkBoxStatusBar, &ApplicationSettings::showStatusBar, &ApplicationSettings::setShowStatusBar, &ApplicationSettings::showStatusBarChanged);
MapSettingToCheckBox(ui->checkBoxRecenterSearchDialog, &ApplicationSettings::centerSearchDialog, &ApplicationSettings::setCenterSearchDialog, &ApplicationSettings::centerSearchDialogChanged);

MapSettingToGroupBox(ui->gbxRestorePreviousSession, &ApplicationSettings::restorePreviousSession, &ApplicationSettings::setRestorePreviousSession, &ApplicationSettings::restorePreviousSessionChanged);
connect(ui->gbxRestorePreviousSession, &QGroupBox::toggled, this, [=](bool checked) {
MapSettingToCheckBox(ui->checkBoxSessionSnapshot, &ApplicationSettings::enableSessionSnapshot, &ApplicationSettings::setEnableSessionSnapshot, &ApplicationSettings::enableSessionSnapshotChanged);
connect(ui->checkBoxSessionSnapshot, &QCheckBox::toggled, this, [=](bool checked) {
ui->gbxRestorePreviousSession->setEnabled(checked);
if (!checked) {
ui->checkBoxRestorePreviousSession->setChecked(false);
ui->checkBoxUnsavedFiles->setChecked(false);
ui->checkBoxRestoreTempFiles->setChecked(false);
}
else {
QMessageBox::warning(this, tr("Warning"), tr("This feature is experimental and it should not be considered safe for critically important work. It may lead to possible data loss. Use at your own risk."));
}
});

// Initialize the restore group box enabled state
ui->gbxRestorePreviousSession->setEnabled(settings->enableSessionSnapshot());

MapSettingToCheckBox(ui->checkBoxRestorePreviousSession, &ApplicationSettings::restorePreviousSession, &ApplicationSettings::setRestorePreviousSession, &ApplicationSettings::restorePreviousSessionChanged);
MapSettingToCheckBox(ui->checkBoxUnsavedFiles, &ApplicationSettings::restoreUnsavedFiles, &ApplicationSettings::setRestoreUnsavedFiles, &ApplicationSettings::restoreUnsavedFilesChanged);
MapSettingToCheckBox(ui->checkBoxRestoreTempFiles, &ApplicationSettings::restoreTempFiles, &ApplicationSettings::setRestoreTempFiles, &ApplicationSettings::restoreTempFilesChanged);

Expand Down
23 changes: 17 additions & 6 deletions src/dialogs/PreferencesDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,28 @@
</widget>
</item>
<item>
<widget class="QGroupBox" name="gbxRestorePreviousSession">
<property name="title">
<string>Restore previous session</string>
<widget class="QCheckBox" name="checkBoxSessionSnapshot">
<property name="text">
<string>Enable Session Snapshot</string>
</property>
<property name="checkable">
<property name="checked">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</widget>
</item>
<item>
<widget class="QGroupBox" name="gbxRestorePreviousSession">
<property name="title">
<string>Restore previous session</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<widget class="QCheckBox" name="checkBoxRestorePreviousSession">
<property name="text">
<string>Previously open files</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBoxUnsavedFiles">
<property name="text">
Expand Down
5 changes: 3 additions & 2 deletions thirdparty/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ CPMAddPackage(
"QSIMPLE_UPDATER_BUILD_TESTS OFF"
)

set(QT_DEFAULT_MAJOR_VERSION 6 CACHE STRING "" FORCE)
set(QAPPLICATION_CLASS QApplication CACHE STRING "Inheritance class for SingleApplication" FORCE)
CPMAddPackage(
NAME SingleApplication
GITHUB_REPOSITORY itay-grudev/SingleApplication
GIT_TAG 494772e98cef0aa88124f154feb575cc60b08b38
GITHUB_REPOSITORY chen3feng/SingleApplication
GIT_TAG 1bb301a46c5727fac959ef35a6bc6d05ef3115be
)

CPMAddPackage(
Expand Down
Loading