Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Utilities
Kate
Commits
248b0611
Commit
248b0611
authored
Feb 07, 2021
by
Méven Car
Committed by
Christoph Cullmann
Mar 05, 2021
Browse files
Allow to stash changes on Close
BUG: 353654
parent
ef3df8d1
Changes
4
Hide whitespace changes
Inline
Side-by-side
kate/CMakeLists.txt
View file @
248b0611
...
...
@@ -95,6 +95,7 @@ target_sources(
commandmodel.cpp
kateoutputview.cpp
katestashmanager.cpp
)
# Executable only adds the main definition.
...
...
kate/katemainwindow.cpp
View file @
248b0611
...
...
@@ -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
);
}
...
...
kate/katestashmanager.cpp
0 → 100644
View file @
248b0611
/* 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
();
}
kate/katestashmanager.h
0 → 100644
View file @
248b0611
/* 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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment