Commit 32d07734 authored by Ahmad Samir's avatar Ahmad Samir
Browse files

KateFileTree: fix renaming files if dest already exists

The old code was using a blocking exec() call, which meant that if you try
to rename a file and get the "file already exists" dialog from KIO, the
dialog and Kate are both blocked.

This was fixed some time ago in KateFileActions, and now that it's part of
a kateprivate lib, we can use it in the addons.
parent 899339cf
Pipeline #251020 passed with stage
in 8 minutes and 6 seconds
......@@ -12,6 +12,8 @@
#include "katefiletreemodel.h"
#include "katefiletreeproxymodel.h"
#include "katefileactions.h"
#include <ktexteditor/application.h>
#include <ktexteditor/document.h>
#include <ktexteditor/editor.h>
......@@ -613,49 +615,7 @@ void KateFileTree::slotCopyFilename()
void KateFileTree::slotRenameFile()
{
KTextEditor::Document *doc = m_proxyModel->docFromIndex(m_indexContextMenu);
// TODO: the following code was improved in kate/katefileactions.cpp and should be reused here
if (!doc) {
return;
}
const QUrl oldFileUrl = doc->url();
const QString oldFileName = doc->url().fileName();
bool ok;
QString newFileName = QInputDialog::getText(this, i18n("Rename file"), i18n("New file name"), QLineEdit::Normal, oldFileName, &ok);
if (!ok) {
return;
}
QUrl newFileUrl = oldFileUrl.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
newFileUrl.setPath(newFileUrl.path() + QLatin1Char('/') + newFileName);
if (!newFileUrl.isValid()) {
return;
}
if (!doc->closeUrl()) {
return;
}
doc->waitSaveComplete();
KIO::CopyJob *job = KIO::move(oldFileUrl, newFileUrl);
QSharedPointer<QMetaObject::Connection> sc(new QMetaObject::Connection());
auto success = [doc, sc](KIO::Job *, const QUrl &, const QUrl &realNewFileUrl, const QDateTime &, bool, bool) {
doc->openUrl(realNewFileUrl);
doc->documentSavedOrUploaded(doc, true);
QObject::disconnect(*sc);
};
*sc = connect(job, &KIO::CopyJob::copyingDone, doc, success);
if (!job->exec()) {
KMessageBox::error(this, i18n("File \"%1\" could not be moved to \"%2\"", oldFileUrl.toDisplayString(), newFileUrl.toDisplayString()));
doc->openUrl(oldFileUrl);
}
KateFileActions::renameDocumentFile(this, m_proxyModel->docFromIndex(m_indexContextMenu));
}
void KateFileTree::slotDocumentFirst()
......
......@@ -48,9 +48,6 @@ void KateFileActions::openFilePropertiesDialog(QWidget *parent, KTextEditor::Doc
void KateFileActions::renameDocumentFile(QWidget *parent, KTextEditor::Document *doc)
{
// TODO: code was copied and adapted from ../addons/filetree/katefiletree.cpp
// (-> DUPLICATE CODE, the new code here should be also used there!)
if (!doc) {
return;
}
......
......@@ -8,6 +8,8 @@
#ifndef KATE_FILEACTIONS_H
#define KATE_FILEACTIONS_H
#include "kateprivate_export.h"
#include <utility>
#include <vector>
......@@ -38,7 +40,7 @@ void openContainingFolder(KTextEditor::Document *document);
*
* Nothing is done if the document is nullptr or has no associated file.
*/
void renameDocumentFile(QWidget *parent, KTextEditor::Document *document);
KATE_PRIVATE_EXPORT void renameDocumentFile(QWidget *parent, KTextEditor::Document *document);
void openFilePropertiesDialog(QWidget *parent, KTextEditor::Document *document);
......
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