Skip to content

Commit 9cc6dee

Browse files
committed
#3575 fix: note folder switching with pending note saves
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 384263a commit 9cc6dee

4 files changed

Lines changed: 41 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 26.4.19
44

5+
- Fixed note folder switching so all modified notes are written to disk before
6+
another note folder becomes active, preventing edited notes from being saved
7+
into the newly selected folder instead of their original one (for
8+
[#3575](https://github.com/pbek/QOwnNotes/issues/3575))
59
- Fixed the **Navigation panel** heading parser so lines inside fenced code
610
blocks are no longer interpreted as Markdown headings, preventing entries
711
like `# ls -l` or `## test` from appearing in the heading tree (for

src/entities/note.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,6 +2810,19 @@ int Note::storeDirtyNotesToDisk(Note &currentNote, bool *currentNoteChanged, boo
28102810
return count;
28112811
}
28122812

2813+
bool Note::hasDirtyNotes() {
2814+
const QSqlDatabase db = QSqlDatabase::database(QStringLiteral("memory"));
2815+
QSqlQuery query(db);
2816+
2817+
query.prepare(QStringLiteral("SELECT COUNT(*) AS cnt FROM note WHERE has_dirty_data = 1"));
2818+
if (!query.exec()) {
2819+
qWarning() << __func__ << ": " << query.lastError();
2820+
return true;
2821+
}
2822+
2823+
return !query.first() || (query.value(QStringLiteral("cnt")).toInt() > 0);
2824+
}
2825+
28132826
/**
28142827
* Strips trailing whitespaces off the note text
28152828
*

src/entities/note.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ class Note {
138138
bool *noteWasRenamed = nullptr,
139139
bool *currentNoteTextChanged = nullptr);
140140

141+
static bool hasDirtyNotes();
142+
141143
bool updateNoteTextFromDisk();
142144

143145
friend QDebug operator<<(QDebug dbg, const Note &note);

src/mainwindow.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,30 +2100,42 @@ bool MainWindow::changeNoteFolder(const int noteFolderId, const bool forceChange
21002100

21012101
QString folderName = noteFolder.getLocalPath();
21022102
const QString oldPath = this->notesPath;
2103-
2104-
// Switch the active note folder first so all subsequent refresh steps
2105-
// (including combo/menu rebuilding) use the correct current folder.
2106-
noteFolder.setAsCurrent();
2107-
21082103
const bool notesPathChanged = (oldPath != folderName);
21092104

21102105
// reload notes if notes folder was changed
21112106
if (notesPathChanged) {
2107+
// Store everything before changing folder
2108+
storeUpdatedNotesToDisk();
2109+
2110+
if (Note::hasDirtyNotes()) {
2111+
loadNoteFolderListMenu();
2112+
QMessageBox::warning(
2113+
this, tr("Could not switch note folder"),
2114+
tr("Modified notes could not be written to disk. Please resolve the problem and "
2115+
"try switching note folders again."));
2116+
return false;
2117+
}
2118+
21122119
const QSignalBlocker blocker2(this->ui->searchLineEdit);
21132120
{
21142121
Q_UNUSED(blocker2)
21152122
ui->searchLineEdit->clear();
21162123
}
21172124

2118-
// store everything before changing folder
2119-
storeUpdatedNotesToDisk();
2120-
21212125
// commit the changes in the current note folder to git
21222126
gitCommitCurrentNoteFolder();
21232127

21242128
// update the recent note folder list
21252129
storeRecentNoteFolder(this->notesPath, folderName);
21262130

2131+
// Switch the active note folder only after all pending writes for the
2132+
// current folder were flushed to disk.
2133+
noteFolder.setAsCurrent();
2134+
2135+
// Rebuild the selector and menu after switching so the newly active
2136+
// note folder stays selected in the UI.
2137+
loadNoteFolderListMenu();
2138+
21272139
// change notes path
21282140
this->notesPath = folderName;
21292141

@@ -2139,6 +2151,8 @@ bool MainWindow::changeNoteFolder(const int noteFolderId, const bool forceChange
21392151
// switching to another note folder
21402152
unsetCurrentNote();
21412153
} else {
2154+
noteFolder.setAsCurrent();
2155+
21422156
// Keep selector and Note -> Note folders menu in sync when switching
21432157
// between folders that share the same path.
21442158
loadNoteFolderListMenu();

0 commit comments

Comments
 (0)