X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/504545c59ab0cd99f782cbb2afd8c2a983c9adfc..ec12391a1b:/src/views/renamedialog.cpp diff --git a/src/views/renamedialog.cpp b/src/views/renamedialog.cpp index 79421a5ef..e49f56e2d 100644 --- a/src/views/renamedialog.cpp +++ b/src/views/renamedialog.cpp @@ -19,21 +19,21 @@ #include "renamedialog.h" -#include -#include +#include +#include #include #include #include +#include +#include +#include #include #include -#include +#include #include -#include #include -#include #include -#include RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) : QDialog(parent), @@ -59,7 +59,7 @@ RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) : setLayout(mainLayout); m_okButton = buttonBox->button(QDialogButtonBox::Ok); m_okButton->setDefault(true); - m_okButton->setShortcut(Qt::CTRL | Qt::Key_Return); + m_okButton->setShortcut(Qt::CTRL + Qt::Key_Return); connect(buttonBox, &QDialogButtonBox::accepted, this, &RenameDialog::slotAccepted); connect(buttonBox, &QDialogButtonBox::rejected, this, &RenameDialog::reject); m_okButton->setDefault(true); @@ -148,38 +148,39 @@ RenameDialog::~RenameDialog() { } -void RenameDialog::renameItem(const KFileItem &item, const QString& newName) +void RenameDialog::slotAccepted() { - const QUrl oldUrl = item.url(); - QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename); - newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName)); - QWidget* widget = parentWidget(); if (!widget) { widget = this; } - KIO::Job * job = KIO::moveAs(oldUrl, newUrl, KIO::HideProgressInfo); - KJobWidgets::setWindow(job, widget); - KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job); - - if (!job->error()) { + const QList srcList = m_items.urlList(); + const QString newName = m_lineEdit->text(); + KIO::FileUndoManager::CommandType cmdType; + KIO::Job *job = nullptr; + if (m_renameOneItem) { + Q_ASSERT(m_items.count() == 1); + cmdType = KIO::FileUndoManager::Rename; + const QUrl oldUrl = m_items.constFirst().url(); + QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename); + newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName)); m_renamedItems << newUrl; + job = KIO::moveAs(oldUrl, newUrl, KIO::HideProgressInfo); + } else { + cmdType = KIO::FileUndoManager::BatchRename; + job = KIO::batchRename(srcList, newName, m_spinBox->value(), QLatin1Char('#')); + connect(qobject_cast(job), &KIO::BatchRenameJob::fileRenamed, this, &RenameDialog::slotFileRenamed); } - job->uiDelegate()->setAutoErrorHandlingEnabled(true); -} + KJobWidgets::setWindow(job, widget); + const QUrl parentUrl = srcList.first().adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash); + KIO::FileUndoManager::self()->recordJob(cmdType, srcList, parentUrl, job); -void RenameDialog::slotAccepted() -{ - m_newName = m_lineEdit->text(); + connect(job, &KJob::result, this, &RenameDialog::slotResult); + + job->uiDelegate()->setAutoErrorHandlingEnabled(true); - if (m_renameOneItem) { - Q_ASSERT(m_items.count() == 1); - renameItem(m_items.first(), m_newName); - } else { - renameItems(); - } accept(); } @@ -201,55 +202,22 @@ void RenameDialog::slotTextChanged(const QString& newName) m_okButton->setEnabled(enable); } -void RenameDialog::showEvent(QShowEvent* event) +void RenameDialog::slotFileRenamed(const QUrl &oldUrl, const QUrl &newUrl) { - m_lineEdit->setFocus(); - - QDialog::showEvent(event); + Q_UNUSED(oldUrl) + m_renamedItems << newUrl; } -void RenameDialog::renameItems() +void RenameDialog::slotResult(KJob *job) { - // Iterate through all items and rename them... - int index = m_spinBox->value(); - foreach (const KFileItem& item, m_items) { - QString newName = indexedName(m_newName, index, QLatin1Char('#')); - ++index; - - const QUrl oldUrl = item.url(); - QMimeDatabase db; - const QString extension = db.suffixForFileName(oldUrl.path().toLower()); - if (!extension.isEmpty()) { - newName.append(QLatin1Char('.')); - newName.append(extension); - } - - if (oldUrl.fileName() != newName) { - renameItem(item, newName); - } - } - - if (!m_items.empty()) { + if (!job->error()) { emit renamingFinished(m_renamedItems); } } -QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder) +void RenameDialog::showEvent(QShowEvent* event) { - QString newName = name; - - QString indexString = QString::number(index); - - // Insert leading zeros if necessary - const int minIndexLength = name.count(indexPlaceHolder); - while (indexString.length() < minIndexLength) { - indexString.prepend(QLatin1Char('0')); - } - - // Replace the index placeholders by the indexString - const int placeHolderStart = newName.indexOf(indexPlaceHolder); - newName.replace(placeHolderStart, minIndexLength, indexString); + m_lineEdit->setFocus(); - return newName; + QDialog::showEvent(event); } -