Skip to content

Commit 9184f1d

Browse files
SimTheVoidSimLV
authored andcommitted
+ Save sessions on exit
+ References instead of pointer to QSettings
1 parent 8e10ea5 commit 9184f1d

4 files changed

Lines changed: 47 additions & 38 deletions

File tree

doc/TODO.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## TODO
22

33
- Sessions
4-
- [ ] Save/Load sessions
5-
- [ ] Remove tabs on session load
6-
- [ ] Store Bookmarks
7-
- [ ] Store Tab settings
4+
- [v] Save/Load sessions
5+
- [ ] Remove old tabs on session load
6+
- [v] Store Bookmarks
7+
- [ ] Store per session settings
88

99
- Editor
1010
- [ ] Auto Tabs/Spaces

src/NotepadNext/NotepadNextApplication.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,7 @@ MainWindow *NotepadNextApplication::createNewWindow()
523523
}
524524
}
525525

526-
getSessionManager()->saveDefaultSession(window);
526+
getSessionManager()->saveCurrentSession(window);
527527
});
528528

529529
return window;

src/NotepadNext/SessionManager.cpp

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,40 @@ bool SessionManager::saveSessionTo(MainWindow *window, const QString &path)
157157
return false;
158158
}
159159

160-
QSettings *settings = new QSettings(sessionDirectory().absolutePath() + QDir::separator() + SETTINGS_FILE, QSettings::IniFormat);
160+
QSettings settings(sessionDirectory().absolutePath() + QDir::separator() + SETTINGS_FILE, QSettings::IniFormat);
161161

162-
settings->beginGroup("Session");
162+
settings.beginGroup("Session");
163163

164164
setSessionFileTypes(tmp | SavedFile);
165165
saveSession(window, settings);
166166
setSessionFileTypes(tmp);
167167

168-
settings->endGroup();
169-
settings->sync();
168+
settings.endGroup();
169+
settings.sync();
170170

171-
delete settings;
172171
return true;
173172
}
174173

174+
void SessionManager::saveCurrentSession(MainWindow *window)
175+
{
176+
if (useCustomSessionFolder) {
177+
saveSessionTo(window, customSessionPath);
178+
}
179+
else {
180+
saveDefaultSession(window);
181+
}
182+
}
183+
175184
void SessionManager::saveDefaultSession(MainWindow *window)
176185
{
177186
useCustomSessionFolder = false;
178187
ApplicationSettings appSettings;
179188
appSettings.beginGroup("CurrentSession");
180-
saveSession(window, &appSettings);
189+
saveSession(window, appSettings);
181190
appSettings.endGroup();
182191
}
183192

184-
void SessionManager::saveSession(MainWindow *window, QSettings* settings)
193+
void SessionManager::saveSession(MainWindow *window, QSettings &settings)
185194
{
186195
qInfo(Q_FUNC_INFO);
187196

@@ -196,23 +205,23 @@ void SessionManager::saveSession(MainWindow *window, QSettings* settings)
196205
int currentEditorIndex = 0;
197206
ApplicationSettings appSettings;
198207

199-
settings->beginWriteArray("OpenedFiles");
208+
settings.beginWriteArray("OpenedFiles");
200209

201210
int index = 0;
202211
for (const auto &editor : window->editors()) {
203212
SessionFileType editorType = determineType(editor);
204213

205214
if (fileTypes.testFlag(editorType)) {
206-
settings->setArrayIndex(index);
215+
settings.setArrayIndex(index);
207216

208217
if (editorType == SessionManager::SavedFile) {
209-
storeFileDetails(editor, *settings);
218+
storeFileDetails(editor, settings);
210219
}
211220
else if (editorType == SessionManager::UnsavedFile) {
212-
storeUnsavedFileDetails(editor, *settings);
221+
storeUnsavedFileDetails(editor, settings);
213222
}
214223
else if (editorType == SessionManager::TempFile) {
215-
storeTempFile(editor, *settings);
224+
storeTempFile(editor, settings);
216225
}
217226
else {
218227
qWarning("Unknown SessionFileType %d", editorType);
@@ -226,17 +235,17 @@ void SessionManager::saveSession(MainWindow *window, QSettings* settings)
226235
}
227236
}
228237

229-
settings->endArray();
238+
settings.endArray();
230239

231-
settings->setValue("CurrentEditorIndex", currentEditorIndex);
240+
settings.setValue("CurrentEditorIndex", currentEditorIndex);
232241
}
233242

234243
void SessionManager::loadDefaultSession(MainWindow *window)
235244
{
236245
ApplicationSettings settings;
237246

238247
settings.beginGroup("CurrentSession");
239-
ScintillaNext *currentEditor = loadSession(window, &settings);
248+
ScintillaNext *currentEditor = loadSession(window, settings);
240249
settings.endGroup();
241250

242251
if (currentEditor) {
@@ -258,47 +267,46 @@ bool SessionManager::loadSessionFrom(MainWindow *window, const QString &path)
258267
{
259268
return false;
260269
}
261-
QSettings *settings = new QSettings(sessionDirectory().absolutePath() + QDir::separator() + SETTINGS_FILE, QSettings::IniFormat);
270+
QSettings settings(sessionDirectory().absolutePath() + QDir::separator() + SETTINGS_FILE, QSettings::IniFormat);
262271

263-
settings->beginGroup("Session");
272+
settings.beginGroup("Session");
264273
ScintillaNext *currentEditor = loadSession(window, settings);
265-
settings->endGroup();
266-
delete settings;
274+
settings.endGroup();
267275

268276
if (currentEditor) {
269277
window->switchToEditor(currentEditor);
270278
}
271279
return true;
272280
}
273281

274-
ScintillaNext *SessionManager::loadSession(MainWindow *window, QSettings *settings)
282+
ScintillaNext *SessionManager::loadSession(MainWindow *window, QSettings &settings)
275283
{
276284
qInfo(Q_FUNC_INFO);
277285

278286
ScintillaNext *currentEditor = Q_NULLPTR;
279-
const int currentEditorIndex = settings->value("CurrentEditorIndex").toInt();
280-
const int size = settings->beginReadArray("OpenedFiles");
287+
const int currentEditorIndex = settings.value("CurrentEditorIndex").toInt();
288+
const int size = settings.beginReadArray("OpenedFiles");
281289

282290
// NOTE: In theory the fileTypes should determine what is loaded, however if the session fileTypes
283291
// change from the last time it was saved then it means the settings were manually altered outside of the app,
284292
// which is non-standard behavior, so just load anything in the file
285293

286294
for (int index = 0; index < size; ++index) {
287-
settings->setArrayIndex(index);
295+
settings.setArrayIndex(index);
288296

289297
ScintillaNext *editor = Q_NULLPTR;
290298

291-
if (settings->contains("Type")) {
292-
const QString type = settings->value("Type").toString();
299+
if (settings.contains("Type")) {
300+
const QString type = settings.value("Type").toString();
293301

294302
if (type == QStringLiteral("File")) {
295-
editor = loadFileDetails(*settings);
303+
editor = loadFileDetails(settings);
296304
}
297305
else if (type == QStringLiteral("UnsavedFile")) {
298-
editor = loadUnsavedFileDetails(*settings);
306+
editor = loadUnsavedFileDetails(settings);
299307
}
300308
else if (type == QStringLiteral("Temp")) {
301-
editor = loadTempFile(*settings);
309+
editor = loadTempFile(settings);
302310
}
303311
else {
304312
qDebug("Unknown session entry type: %s", qUtf8Printable(type));
@@ -315,8 +323,8 @@ ScintillaNext *SessionManager::loadSession(MainWindow *window, QSettings *settin
315323
}
316324
}
317325

318-
settings->endArray();
319-
return currentEditor;
326+
settings.endArray();
327+
return currentEditor;
320328
}
321329

322330
bool SessionManager::willFileGetStoredInSession(ScintillaNext *editor) const

src/NotepadNext/SessionManager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SessionManager
4848
void clear() const;
4949

5050
bool saveSessionTo(MainWindow *window, const QString &path);
51-
void saveDefaultSession(MainWindow *window);
51+
void saveCurrentSession(MainWindow *window);
5252
void loadDefaultSession(MainWindow *window);
5353
bool loadSessionFrom(MainWindow *window, const QString &path);
5454

@@ -58,8 +58,9 @@ class SessionManager
5858
bool isCustomSessionFolder() const { return useCustomSessionFolder; }
5959

6060
private:
61-
void saveSession(MainWindow *window, QSettings *settings);
62-
ScintillaNext *loadSession(MainWindow *window, QSettings *settings);
61+
void saveSession(MainWindow *window, QSettings &settings);
62+
void saveDefaultSession(MainWindow *window);
63+
ScintillaNext *loadSession(MainWindow *window, QSettings &settings);
6364

6465
void saveIntoSessionDirectory(ScintillaNext *editor, const QString &sessionFileName) const;
6566

0 commit comments

Comments
 (0)