Commit c97ff968 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

better fix of closing crash

close project one by one

this might ask the user more often for open files

but it will avoid any strange races between widget updates and Co.

we can't be sure what e.g. other plugins might do on the emitted
signals
parent 355241a9
......@@ -235,33 +235,22 @@ KateProject *KateProjectPlugin::projectForDir(QDir dir, bool userSpecified)
return nullptr;
}
void KateProjectPlugin::closeProjects(QList<KateProject *> projects)
void KateProjectPlugin::closeProject(KateProject *project)
{
if (projects.empty()) {
return;
}
// collect all documents we have mapped to the projects we want to close
// we can not delete projects that still have some mapping
QList<KTextEditor::Document *> projectDocuments;
for (const auto &it : m_document2Project) {
if (projects.contains(it.second)) {
if (it.second == project) {
projectDocuments.append(it.first);
printf("close %s\n", qPrintable(it.first->url().toString()));
}
}
// if we have some documents open for this project, ask if we want to close, else just do it
if (!projectDocuments.isEmpty()) {
// only tell the name if we close a singular project
QString title = i18n("Confirm closing of %1 projects", projects.size());
QString text = i18n("Do you want to close %1 projects and the related %2 open documents?", projects.size(), projectDocuments.size());
if (projects.size() == 1) {
title = i18n("Confirm project closing: %1", projects.first()->name());
text = i18n("Do you want to close the project %1 and the related %2 open documents?", projects.first()->name(), projectDocuments.size());
}
QWidget *window = KTextEditor::Editor::instance()->application()->activeMainWindow()->window();
const QString title = i18n("Confirm project closing: %1", project->name());
const QString text = i18n("Do you want to close the project %1 and the related %2 open documents?", project->name(), projectDocuments.size());
if (QMessageBox::Yes != QMessageBox::question(window, title, text, QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes)) {
return;
}
......@@ -270,17 +259,16 @@ void KateProjectPlugin::closeProjects(QList<KateProject *> projects)
KTextEditor::Editor::instance()->application()->closeDocuments(projectDocuments);
}
// now: close all projects we have no longer any open documents for, we just filter the list
// check: did all documents of the project go away? if not we shall not close it
for (const auto &it : m_document2Project) {
if (projects.contains(it.second)) {
projects.removeOne(it.second);
if (it.second == project) {
return;
}
}
for (auto project : projects) {
Q_EMIT pluginViewProjectClosing(project);
m_projects.removeOne(project);
delete project;
}
Q_EMIT pluginViewProjectClosing(project);
m_projects.removeOne(project);
delete project;
}
KateProject *KateProjectPlugin::projectForUrl(const QUrl &url)
......
......@@ -65,11 +65,11 @@ public:
KateProject *projectForDir(QDir dir, bool userSpecified = false);
/**
* Try to close given list of projects.
* Will ask if the files belonging to them shall be closed, if not this will just do nothing.
* @param projects projects that shall be closed
* Try to close the given project.
* Will ask if the files belonging to the project shall be closed, if not this will just do nothing.
* @param project project to close
*/
void closeProjects(QList<KateProject *> projects);
void closeProject(KateProject *project);
/**
* Search and open project that contains given url, if possible.
......
......@@ -644,13 +644,18 @@ void KateProjectPluginView::slotProjectReload()
void KateProjectPluginView::slotProjectAboutToClose()
{
if (QWidget *current = m_stackedProjectViews->currentWidget()) {
m_plugin->closeProjects({static_cast<KateProjectView *>(current)->project()});
m_plugin->closeProject(static_cast<KateProjectView *>(current)->project());
}
}
void KateProjectPluginView::slotAllProjectsAboutToClose()
{
m_plugin->closeProjects(m_plugin->projects());
// we must close project after project
// project closing might activate a different one and then would load the files of that project again
const auto copiedProjects = m_plugin->projects();
for (auto project : copiedProjects) {
m_plugin->closeProject(project);
}
}
void KateProjectPluginView::slotProjectClose(KateProject *project)
......
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