Commit 15085b8a authored by Méven Car's avatar Méven Car Committed by Christoph Cullmann
Browse files

Allow to restore using saved encoding, only stash file when closing kate

parent ef18e004
......@@ -414,30 +414,12 @@ void KateDocManager::saveDocumentList(KConfig *config)
openDocGroup.writeEntry("Count", m_docList.count());
// prepare stash directory
const QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(appDataPath);
dir.mkdir(QStringLiteral("stash"));
dir.cd(QStringLiteral("stash"));
const auto session = KateApp::self()->sessionManager()->activeSession();
if (session) {
const QString sessionName = session->name();
dir.mkdir(sessionName);
dir.cd(sessionName);
}
int i = 0;
for (KTextEditor::Document *doc : qAsConst(m_docList)) {
const QString entryName = QStringLiteral("Document %1").arg(i);
KConfigGroup cg(config, entryName);
doc->writeSessionConfig(cg);
// stash the file content
if (doc->isModified()) {
KateApp::self()->stashManager()->stashDocument(doc, entryName, cg, dir.path());
}
i++;
}
}
......
......@@ -586,6 +586,8 @@ bool KateMainWindow::queryClose()
// and save docs if we really close down !
if (queryClose_internal()) {
KateApp::self()->sessionManager()->saveActiveSession(true);
KateApp::self()->stashManager()->stashDocuments(KateApp::self()->sessionManager()->activeSession()->config(),
KateApp::self()->documentManager()->documentList());
return true;
}
......
......@@ -18,6 +18,7 @@
#include <QDir>
#include <QFile>
#include <QSaveFile>
#include <QTextCodec>
#include <QUrl>
KateStashManager::KateStashManager(QObject *parent)
......@@ -25,6 +26,35 @@ KateStashManager::KateStashManager(QObject *parent)
{
}
void KateStashManager::stashDocuments(KConfig *config, const QList<KTextEditor::Document *> &documents)
{
// prepare stash directory
const QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(appDataPath);
dir.mkdir(QStringLiteral("stash"));
dir.cd(QStringLiteral("stash"));
const auto session = KateApp::self()->sessionManager()->activeSession();
if (session) {
const QString sessionName = session->name();
dir.mkdir(sessionName);
dir.cd(sessionName);
}
int i = 0;
for (KTextEditor::Document *doc : qAsConst(documents)) {
const QString entryName = QStringLiteral("Document %1").arg(i);
KConfigGroup cg(config, entryName);
// stash the file content
if (doc->isModified()) {
stashDocument(doc, entryName, cg, dir.path());
}
i++;
}
}
bool KateStashManager::willStashDoc(KTextEditor::Document *doc)
{
if (m_stashUnsaveChanges == 0) {
......@@ -85,7 +115,7 @@ bool KateStashManager::popDocument(KTextEditor::Document *doc, const KConfigGrou
bool checksumOk = true;
if (url.isValid()) {
auto sum = kconfig.readEntry(QStringLiteral("checksum")).toLatin1().constData();
const auto sum = kconfig.readEntry(QStringLiteral("checksum")).toLatin1().constData();
checksumOk = sum != doc->checksum();
}
......@@ -94,10 +124,17 @@ bool KateStashManager::popDocument(KTextEditor::Document *doc, const KConfigGrou
QFile file(stashedFile);
file.open(QIODevice::ReadOnly);
QTextStream out(&file);
const auto codec = QTextCodec::codecForName(kconfig.readEntry("Encoding").toLocal8Bit());
if (codec != 0) {
out.setCodec(codec);
}
doc->setText(out.readAll());
// clean stashed file
file.remove();
if (!file.remove()) {
qCWarning(LOG_KATE) << "Could not remove stash file" << stashedFile;
}
return true;
} else {
......
......@@ -32,6 +32,8 @@ public:
m_stashUnsaveChanges = stashUnsaveChanges;
}
void stashDocuments(KConfig *cfg, const QList<KTextEditor::Document *> &documents);
bool willStashDoc(KTextEditor::Document *doc);
void stashDocument(KTextEditor::Document *doc, const QString &stashfileName, KConfigGroup &kconfig, const QString &path);
......
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