X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/49396318f0f37f913a91bf02484a16e34fec6bf4..87e8d0ba5f:/src/views/renamedialog.cpp diff --git a/src/views/renamedialog.cpp b/src/views/renamedialog.cpp index 6309bfbdf..2e1fa8634 100644 --- a/src/views/renamedialog.cpp +++ b/src/views/renamedialog.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -39,10 +40,10 @@ RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) : QDialog(parent), m_renameOneItem(false), m_newName(), - m_lineEdit(0), + m_lineEdit(nullptr), m_items(items), m_allExtensionsDifferent(true), - m_spinBox(0) + m_spinBox(nullptr) { const QSize minSize = minimumSize(); setMinimumSize(QSize(320, minSize.height())); @@ -72,7 +73,7 @@ RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) : QVBoxLayout* topLayout = new QVBoxLayout(page); - QLabel* editLabel = 0; + QLabel* editLabel = nullptr; if (m_renameOneItem) { m_newName = items.first().name(); editLabel = new QLabel(xi18nc("@label:textbox", "Rename the item %1 to:", m_newName), @@ -148,38 +149,32 @@ 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); + KIO::FileUndoManager::CommandType cmdType; + if (m_renameOneItem) { + Q_ASSERT(m_items.count() == 1); + cmdType = KIO::FileUndoManager::Rename; + } else { + cmdType = KIO::FileUndoManager::BatchRename; + } + + const QList srcList = m_items.urlList(); + KIO::BatchRenameJob* job = KIO::batchRename(srcList, m_lineEdit->text(), m_spinBox->value(), QLatin1Char('#')); KJobWidgets::setWindow(job, widget); - KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job); + const QUrl parentUrl = srcList.first().adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash); + KIO::FileUndoManager::self()->recordJob(cmdType, srcList, parentUrl, job); - if (!job->error()) { - m_renamedItems << newUrl; - } + connect(job, &KIO::BatchRenameJob::fileRenamed, this, &RenameDialog::slotFileRenamed); + connect(job, &KIO::BatchRenameJob::result, this, &RenameDialog::slotResult); job->uiDelegate()->setAutoErrorHandlingEnabled(true); -} -void RenameDialog::slotAccepted() -{ - m_newName = m_lineEdit->text(); - - if (m_renameOneItem) { - Q_ASSERT(m_items.count() == 1); - renameItem(m_items.first(), m_newName); - } else { - renameItems(); - } accept(); } @@ -201,55 +196,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); } -