Commit 9bcc98e8 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

ExternalToolsPlugin: delay saving the changes to the apply() slot

In case the user clicks Cancel.

If a tool is renamed, remove the old config file.
parent d35cb784
......@@ -154,13 +154,19 @@ void KateExternalToolsPlugin::removeTools(const std::vector<KateExternalTool *>
m_tools.erase(it, m_tools.end());
}
void KateExternalToolsPlugin::save(KateExternalTool *tool) const
void KateExternalToolsPlugin::save(KateExternalTool *tool, const QString &oldName) const
{
const QString name = KateExternalTool::configFileName(tool->name);
KConfig config(toolsConfigDir() + name);
KConfigGroup cg = config.group("General");
tool->save(cg);
config.sync();
// The tool was renamed, remove the old config file
if (!oldName.isEmpty()) {
const QString oldFile = toolsConfigDir() + KateExternalTool::configFileName(oldName);
QFile::remove(oldFile);
}
}
void KateExternalToolsPlugin::reload()
......
......@@ -126,9 +126,11 @@ public:
void removeTools(const std::vector<KateExternalTool *> &toRemove);
/**
* Saves the configuration of @p tool to that tool's config file.
* Saves @p tool config. If @p oldName is not empty, then the tool's name
* was changed, and after saving the config (to a new file based on the
* new name), the old config file is removed.
*/
void save(KateExternalTool *tool) const;
void save(KateExternalTool *tool, const QString &oldName) const;
private:
void migrateConfig();
......
......@@ -307,6 +307,11 @@ void KateExternalToolsConfigWidget::apply()
m_plugin->removeTools(m_toolsToRemove);
m_toolsToRemove.clear();
for (auto &[tool, oldName] : m_changedTools) {
m_plugin->save(tool, oldName);
}
m_changedTools.clear();
// So that KateExternalToolsPluginView::rebuildMenu() is called,
// needed to update the menu actions
Q_EMIT m_plugin->externalToolsChanged();
......@@ -333,6 +338,7 @@ bool KateExternalToolsConfigWidget::editTool(KateExternalTool *tool)
editor.resize(editorGroup.readEntry("Size", QSize()));
if (editor.exec() == QDialog::Accepted) {
const QString oldName = tool->name;
tool->name = editor.ui.edtName->text().trimmed();
tool->icon = editor.ui.btnIcon->icon();
tool->arguments = editor.ui.edtArgs->text();
......@@ -354,7 +360,9 @@ bool KateExternalToolsConfigWidget::editTool(KateExternalTool *tool)
makeToolUnique(tool, m_plugin->tools());
m_plugin->save(tool);
const bool renamed = !oldName.isEmpty() && oldName != tool->name;
// Delay saving to apply()
m_changedTools.push_back({tool, renamed ? oldName : QString{}});
changed = true;
}
......@@ -415,6 +423,7 @@ void KateExternalToolsConfigWidget::addNewTool(KateExternalTool *tool)
lbTools->setCurrentIndex(item->index());
m_plugin->addNewTool(tool);
m_changedTools.push_back({tool, {}});
Q_EMIT changed();
m_changed = true;
......@@ -519,6 +528,7 @@ void KateExternalToolsConfigWidget::slotEdit()
m_changed = true;
}
}
// END KateExternalToolsConfigWidget
// kate: space-indent on; indent-width 4; replace-tabs on;
......@@ -79,6 +79,12 @@ private:
std::vector<KateExternalTool *> m_toolsToRemove;
QStandardItemModel m_toolsModel;
QStandardItem *m_noCategory = nullptr;
struct ChangedToolInfo {
KateExternalTool *tool = nullptr;
QString oldName;
};
std::vector<ChangedToolInfo> m_changedTools;
};
/**
......
Supports Markdown
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