- if (button == Ok) {
- m_newName = m_lineEdit->text();
- if (m_newName.isEmpty()) {
- m_errorString = i18nc("@info:status",
- "The new name is empty. A name with at least one character must be entered.");
- } else if (!m_renameOneItem && (m_newName.count('#') == 0)) {
- m_newName.truncate(0);
- m_errorString = i18nc("@info:status", "The name must contain at least one # character.");
+ QWidget* widget = parentWidget();
+ if (!widget) {
+ widget = this;
+ }
+
+ const QList<QUrl> 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<KIO::BatchRenameJob*>(job), &KIO::BatchRenameJob::fileRenamed, this, &RenameDialog::slotFileRenamed);
+ }
+
+ KJobWidgets::setWindow(job, widget);
+ const QUrl parentUrl = srcList.first().adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
+ KIO::FileUndoManager::self()->recordJob(cmdType, srcList, parentUrl, job);
+
+ connect(job, &KJob::result, this, &RenameDialog::slotResult);
+ connect(job, &KJob::result, this, &QObject::deleteLater);
+
+ job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+
+ accept();
+}
+
+void RenameDialog::slotTextChanged(const QString& newName)
+{
+ bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1Char('.'));
+ if (enable && !m_renameOneItem) {
+ const int count = newName.count(QLatin1Char('#'));
+ if (count == 0) {
+ // Renaming multiple files without '#' will only work if all extensions are different.
+ enable = m_allExtensionsDifferent;
+ } else {
+ // Assure that the new name contains exactly one # (or a connected sequence of #'s)
+ const int first = newName.indexOf(QLatin1Char('#'));
+ const int last = newName.lastIndexOf(QLatin1Char('#'));
+ enable = (last - first + 1 == count);