Commit 761f1dca authored by Méven Car's avatar Méven Car Committed by Christoph Cullmann
Browse files

Allow to stash modifications in sessions

parent 248b0611
......@@ -53,6 +53,7 @@ KateApp::KateApp(const QCommandLineParser &args)
, m_adaptor(this)
, m_pluginManager(this)
, m_sessionManager(this)
, m_stashManager(this)
{
/**
* re-route some signals to application wrapper
......@@ -308,6 +309,11 @@ KateSessionManager *KateApp::sessionManager()
return &m_sessionManager;
}
KateStashManager *KateApp::stashManager()
{
return &m_stashManager;
}
bool KateApp::openUrl(const QUrl &url, const QString &encoding, bool isTempFile)
{
return openDocUrl(url, encoding, isTempFile);
......
......@@ -20,6 +20,7 @@
#include "katemainwindow.h"
#include "katepluginmanager.h"
#include "katesessionmanager.h"
#include "katestashmanager.h"
#include "katetests_export.h"
#include <KConfig>
......@@ -134,6 +135,11 @@ public:
*/
KateSessionManager *sessionManager();
/**
*
*/
KateStashManager *stashManager();
/**
* window management
*/
......@@ -391,6 +397,8 @@ private:
*/
KateSessionManager m_sessionManager;
KateStashManager m_stashManager;
#ifdef WITH_KUSERFEEDBACK
/**
* user feedback provider
......
......@@ -416,8 +416,19 @@ void KateDocManager::saveDocumentList(KConfig *config)
int i = 0;
for (KTextEditor::Document *doc : qAsConst(m_docList)) {
KConfigGroup cg(config, QStringLiteral("Document %1").arg(i));
const QString entryName = QStringLiteral("Document %1").arg(i);
KConfigGroup cg(config, entryName);
doc->writeSessionConfig(cg);
// stash the file content
if (doc->isModified()) {
const QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(appDataPath);
dir.mkdir(QStringLiteral("stash"));
KateApp::self()->stashManager()->stashDocument(doc, entryName, cg, appDataPath + QStringLiteral("/stash/"));
}
i++;
}
}
......@@ -453,6 +464,8 @@ void KateDocManager::restoreDocumentList(KConfig *config)
doc->readSessionConfig(cg);
KateApp::self()->stashManager()->popDocument(doc, cg);
progress.setValue(i);
}
}
......
......@@ -151,6 +151,9 @@ KateMainWindow::KateMainWindow(KConfig *sconfig, const QString &sgroup)
m_viewManager->restoreViewConfiguration(KConfigGroup(sconfig, sgroup));
}
// unstash
// KateStashManager().popStash(m_viewManager);
finishRestore();
m_fileOpenRecent->loadEntries(KConfigGroup(sconfig, "Recent Files"));
......@@ -159,9 +162,6 @@ 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.
......@@ -540,11 +540,17 @@ bool KateMainWindow::queryClose_internal(KTextEditor::Document *doc)
QList<KTextEditor::Document *> modifiedDocuments = KateApp::self()->documentManager()->modifiedDocumentList();
modifiedDocuments.removeAll(doc);
bool shutdown = (modifiedDocuments.count() == 0);
bool shutdown = (modifiedDocuments.count() == 0) || KateApp::self()->sessionManager()->activeSession();
if (!shutdown) {
shutdown = KateStashManager().stash(modifiedDocuments);
/*
if (!KateApp::self()->sessionManager()->activeSession()) {
shutdown = KateApp::self()->stashManager()->stash(modifiedDocuments);
} else {
KConfigGroup cfg(KSharedConfig::openConfig(), "MainWindow");
KateApp::self()->documentManager()->saveDocumentList(cfg);
shutdown = true;
}
*/
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
SPDX-License-Identifier: LGPL-2.0-or-later
*/
#include "katestashmanager.h"
#include "katedebug.h"
#include "kateapp.h"
#include "katedocmanager.h"
#include "kateviewmanager.h"
......@@ -19,16 +20,17 @@
#include <QSaveFile>
#include <QUrl>
KateStashManager::KateStashManager()
: m_stashGroup(KSharedConfig::openConfig()->group("stash"))
KateStashManager::KateStashManager(QObject *parent)
: QObject(parent)
{
}
bool KateStashManager::stash(const QList<KTextEditor::Document *> &modifiedDocuments)
{
qCDebug(LOG_KATE) << "stashing" << modifiedDocuments.length() << "files";
const QString appDataPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).constFirst();
const QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
KConfigGroup stashGroup = KSharedConfig::openConfig()->group("stash");
int nFile = 0;
bool success = true;
......@@ -43,29 +45,10 @@ bool KateStashManager::stash(const QList<KTextEditor::Document *> &modifiedDocum
} 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());
}
stashDocument(docu, stashfileName, stashGroup, appDataPath);
}
m_stashGroup.sync();
stashGroup.sync();
return success;
}
......@@ -73,16 +56,67 @@ bool KateStashManager::stash(const QList<KTextEditor::Document *> &modifiedDocum
void KateStashManager::popStash(KateViewManager *viewManager)
{
qCDebug(LOG_KATE) << "popping stash";
KConfigGroup stashGroup = KSharedConfig::openConfig()->group("stash");
for (auto &stashedFileName : m_stashGroup.groupList()) {
for (auto &stashedFileName : stashGroup.groupList()) {
// read metadata
auto group = m_stashGroup.group(stashedFileName);
auto stashedFile = group.readEntry(QStringLiteral("stashedFile"));
auto url = QUrl(group.readEntry(QStringLiteral("Url")));
auto doc = KateApp::self()->documentManager()->createDoc();
popDocument(doc, stashGroup.group(stashedFileName));
}
// clean metadata
stashGroup.deleteGroup();
stashGroup.sync();
}
bool KateStashManager::stashDocument(KTextEditor::Document *doc, const QString &stashfileName, KConfigGroup &kconfig, const QString &path)
{
if (doc->text().isEmpty() || !kconfig.hasKey("URL")) {
// No need to stash empty documents or /tmp files
return true;
}
qCDebug(LOG_KATE) << "stashing document" << stashfileName << doc->url();
// Stash changes
QString stashedFile = path + QStringLiteral("/") + stashfileName;
// save the current document changes to stash
QSaveFile saveFile(stashedFile);
saveFile.open(QIODevice::WriteOnly);
saveFile.write(doc->text().toUtf8());
if (!saveFile.commit()) {
qCWarning(LOG_KATE()) << "Could not write to stash file" << stashedFile;
return false;
}
// write stash metadata to config
kconfig.writeEntry("stashedFile", stashedFile);
if (doc->url().isValid()) {
kconfig.writeEntry("checksum", doc->checksum());
}
kconfig.sync();
return true;
}
bool KateStashManager::popDocument(KTextEditor::Document *doc, const KConfigGroup &kconfig)
{
if (!(kconfig.hasKey("stashedFile"))) {
return false;
}
qCDebug(LOG_KATE) << "popping stashed document" << doc->url();
// read metadata
auto stashedFile = kconfig.readEntry("stashedFile");
auto url = QUrl(kconfig.readEntry("URL"));
bool checksumOk = true;
if (url.isValid()) {
auto sum = kconfig.readEntry(QStringLiteral("checksum")).toLatin1().constData();
checksumOk = sum != doc->checksum();
}
if (checksumOk) {
// 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);
......@@ -90,8 +124,9 @@ void KateStashManager::popStash(KateViewManager *viewManager)
// clean stashed file
file.remove();
return true;
} else {
return false;
}
// clean metadata
m_stashGroup.deleteGroup();
m_stashGroup.sync();
}
......@@ -16,15 +16,16 @@ class Document;
class KateViewManager;
class KateStashManager
class KateStashManager : QObject
{
Q_OBJECT
public:
KateStashManager();
KateStashManager(QObject *parent = nullptr);
bool stash(const QList<KTextEditor::Document *> &modifieddocuments);
void popStash(KateViewManager *viewManager);
private:
KConfigGroup m_stashGroup;
bool stashDocument(KTextEditor::Document *doc, const QString &stashfileName, KConfigGroup &kconfig, const QString &path);
bool popDocument(KTextEditor::Document *doc, const KConfigGroup &kconfig);
};
#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