Commit 248b0611 authored by Méven Car's avatar Méven Car Committed by Christoph Cullmann
Browse files

Allow to stash changes on Close

BUG: 353654
parent ef3df8d1
......@@ -95,6 +95,7 @@ target_sources(
commandmodel.cpp
kateoutputview.cpp
katestashmanager.cpp
)
# Executable only adds the main definition.
......
......@@ -25,6 +25,7 @@
#include "katesavemodifieddialog.h"
#include "katesessionmanager.h"
#include "katesessionsaction.h"
#include "katestashmanager.h"
#include "kateupdatedisabler.h"
#include "kateviewspace.h"
......@@ -158,6 +159,9 @@ KateMainWindow::KateMainWindow(KConfig *sconfig, const QString &sgroup)
connect(KateApp::self()->sessionManager(), SIGNAL(sessionChanged()), this, SLOT(updateCaption()));
// unstash
KateStashManager().popStash(m_viewManager);
connect(this, &KateMDI::MainWindow::sigShowPluginConfigPage, this, &KateMainWindow::showPluginConfigPage);
// prior to this there was (possibly) no view, therefore not context menu.
......@@ -538,6 +542,9 @@ bool KateMainWindow::queryClose_internal(KTextEditor::Document *doc)
modifiedDocuments.removeAll(doc);
bool shutdown = (modifiedDocuments.count() == 0);
if (!shutdown) {
shutdown = KateStashManager().stash(modifiedDocuments);
}
if (!shutdown) {
shutdown = KateSaveModifiedDialog::queryClose(this, modifiedDocuments);
}
......
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2021 Méven Car <meven.car@kdemail.net>
SPDX-License-Identifier: LGPL-2.0-only
*/
#include "katestashmanager.h"
#include "katedebug.h"
#include "katedocmanager.h"
#include "kateviewmanager.h"
#include "kconfiggroup.h"
#include "ksharedconfig.h"
#include <QDir>
#include <QFile>
#include <QSaveFile>
#include <QUrl>
KateStashManager::KateStashManager()
: m_stashGroup(KSharedConfig::openConfig()->group("stash"))
{
}
bool KateStashManager::stash(const QList<KTextEditor::Document *> &modifiedDocuments)
{
qCDebug(LOG_KATE) << "stashing" << modifiedDocuments.length() << "files";
const QString appDataPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).constFirst();
int nFile = 0;
bool success = true;
QDir dir(appDataPath);
dir.mkdir(QStringLiteral("stash"));
for (auto &docu : modifiedDocuments) {
// Stash changes
QString stashfileName;
if (docu->url().isValid()) {
stashfileName = docu->url().fileName() + QStringLiteral("-") + QString::number(nFile++);
} else {
stashfileName = QStringLiteral("Unsaved-") + QString::number(nFile++);
}
QString stashedFile = appDataPath + QStringLiteral("/stash/") + stashfileName;
// save the current document changes to stash
QSaveFile saveFile(stashedFile);
saveFile.open(QIODevice::WriteOnly);
saveFile.write(docu->text().toUtf8());
if (!saveFile.commit()) {
qCWarning(LOG_KATE()) << "Could not write to stash file" << stashedFile;
success = false;
continue;
}
// write stash metadata to config
auto fileGroup = m_stashGroup.group(stashfileName);
fileGroup.writeEntry("stashedFile", stashedFile);
const auto url = docu->url();
if (url.isValid()) {
fileGroup.writeEntry("Url", docu->url());
}
}
m_stashGroup.sync();
return success;
}
void KateStashManager::popStash(KateViewManager *viewManager)
{
qCDebug(LOG_KATE) << "popping stash";
for (auto &stashedFileName : m_stashGroup.groupList()) {
// read metadata
auto group = m_stashGroup.group(stashedFileName);
auto stashedFile = group.readEntry(QStringLiteral("stashedFile"));
auto url = QUrl(group.readEntry(QStringLiteral("Url")));
// open file with stashed content
KateDocumentInfo docInfo;
auto doc = viewManager->openUrl(url, QString(), false, true, docInfo);
QFile file(stashedFile);
file.open(QIODevice::ReadOnly);
QTextStream out(&file);
doc->setText(out.readAll());
// clean stashed file
file.remove();
}
// clean metadata
m_stashGroup.deleteGroup();
m_stashGroup.sync();
}
/* This file is part of the KDE project
SPDX-FileCopyrightText: 2021 Méven Car <meven.car@kdemail.net>
SPDX-License-Identifier: LGPL-2.0-only
*/
#ifndef KATESTASHMANAGER_H
#define KATESTASHMANAGER_H
#include "kconfiggroup.h"
namespace KTextEditor
{
class Document;
}
class KateViewManager;
class KateStashManager
{
public:
KateStashManager();
bool stash(const QList<KTextEditor::Document *> &modifieddocuments);
void popStash(KateViewManager *viewManager);
private:
KConfigGroup m_stashGroup;
};
#endif // KATESTASHMANAGER_H
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment