Skip to content
GitLab
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
0395dd96
Commit
0395dd96
authored
Aug 09, 2022
by
Pablo Rauzy
Committed by
Christoph Cullmann
Aug 14, 2022
Browse files
locking macros storage JSON file for reading and writing
parent
a3fc28ee
Changes
2
Hide whitespace changes
Inline
Side-by-side
addons/keyboardmacros/keyboardmacrosplugin.cpp
View file @
0395dd96
...
...
@@ -21,6 +21,7 @@
#include
<QKeySequence>
#include
<QLineEdit>
#include
<QList>
#include
<QLockFile>
#include
<QLoggingCategory>
#include
<QMessageBox>
#include
<QObject>
...
...
@@ -60,11 +61,13 @@ KeyboardMacrosPlugin::KeyboardMacrosPlugin(QObject *parent, const QList<QVariant
m_commands
=
new
KeyboardMacrosPluginCommands
(
this
);
m_storage
=
QStandardPaths
::
writableLocation
(
QStandardPaths
::
GenericDataLocation
)
+
QStringLiteral
(
"/kate/keyboardmacros.json"
);
loadNamedMacros
();
m_storageLock
=
new
QLockFile
(
m_storage
+
QStringLiteral
(
".lock"
));
}
KeyboardMacrosPlugin
::~
KeyboardMacrosPlugin
()
{
saveNamedMacros
();
delete
m_storageLock
;
delete
m_commands
;
}
...
...
@@ -75,8 +78,12 @@ QObject *KeyboardMacrosPlugin::createView(KTextEditor::MainWindow *mainWindow)
return
m_pluginView
;
}
void
KeyboardMacrosPlugin
::
loadNamedMacros
()
void
KeyboardMacrosPlugin
::
loadNamedMacros
(
bool
locked
)
{
if
(
!
locked
&&
!
m_storageLock
->
tryLock
())
{
sendMessage
(
i18n
(
"Could not acquire macros storage lock; abort loading macros."
),
true
);
return
;
}
QFile
storage
(
m_storage
);
if
(
!
storage
.
open
(
QIODevice
::
ReadOnly
|
QIODevice
::
Text
))
{
sendMessage
(
i18n
(
"Could not open file '%1'."
,
m_storage
),
false
);
...
...
@@ -95,15 +102,22 @@ void KeyboardMacrosPlugin::loadNamedMacros()
}
}
storage
.
close
();
if
(
!
locked
)
{
m_storageLock
->
unlock
();
}
}
void
KeyboardMacrosPlugin
::
saveNamedMacros
()
{
if
(
!
m_storageLock
->
tryLock
())
{
sendMessage
(
i18n
(
"Could not acquire macros storage lock; abort saving macros."
),
true
);
return
;
}
// first keep a copy of the named macros of our instance
QMap
<
QString
,
Macro
>
ourNamedMacros
;
ourNamedMacros
.
swap
(
m_namedMacros
);
// then reload from storage in case another instance saved macros since we first loaded ours from storage
loadNamedMacros
();
loadNamedMacros
(
true
);
// then insert all of our macros, prioritizing ours in case of name conflict since we are the most recent save
m_namedMacros
.
insert
(
ourNamedMacros
);
// and now save named macros
...
...
@@ -118,6 +132,7 @@ void KeyboardMacrosPlugin::saveNamedMacros()
}
storage
.
write
(
QJsonDocument
(
json
).
toJson
(
QJsonDocument
::
Compact
));
storage
.
close
();
m_storageLock
->
unlock
();
}
// END
...
...
addons/keyboardmacros/keyboardmacrosplugin.h
View file @
0395dd96
...
...
@@ -8,6 +8,7 @@
#include
<QEvent>
#include
<QList>
#include
<QLockFile>
#include
<QMap>
#include
<QObject>
#include
<QPointer>
...
...
@@ -51,6 +52,7 @@ class KeyboardMacrosPlugin : public KTextEditor::Plugin
Macro
m_tape
;
Macro
m_macro
;
QString
m_storage
;
QLockFile
*
m_storageLock
;
QMap
<
QString
,
Macro
>
m_namedMacros
;
QSet
<
QString
>
m_wipedMacros
;
...
...
@@ -61,7 +63,7 @@ public:
QObject
*
createView
(
KTextEditor
::
MainWindow
*
mainWindow
)
override
;
private:
void
loadNamedMacros
();
void
loadNamedMacros
(
bool
locked
=
false
);
void
saveNamedMacros
();
// GUI feedback helpers
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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