#include "renamedialog.h"
-#include <KLocalizedString>
-#include <KJobWidgets>
+#include <KGuiItem>
+#include <KIO/BatchRenameJob>
#include <KIO/CopyJob>
#include <KIO/FileUndoManager>
#include <KJobUiDelegate>
+#include <KJobWidgets>
+#include <KLocalizedString>
+#include <QDialogButtonBox>
#include <QHBoxLayout>
#include <QLabel>
-#include <QVBoxLayout>
+#include <QLineEdit>
#include <QMimeDatabase>
-#include <QDialogButtonBox>
#include <QPushButton>
-#include <QLineEdit>
#include <QSpinBox>
-#include <KGuiItem>
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()));
setLayout(mainLayout);
m_okButton = buttonBox->button(QDialogButtonBox::Ok);
m_okButton->setDefault(true);
- m_okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
- connect(buttonBox, SIGNAL(accepted()), this, SLOT(slotAccepted()));
- connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
+ m_okButton->setShortcut(Qt::CTRL + Qt::Key_Return);
+ connect(buttonBox, &QDialogButtonBox::accepted, this, &RenameDialog::slotAccepted);
+ connect(buttonBox, &QDialogButtonBox::rejected, this, &RenameDialog::reject);
+ connect(buttonBox, &QDialogButtonBox::rejected, this, &QObject::deleteLater);
m_okButton->setDefault(true);
- KGuiItem::assign(m_okButton, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
+ KGuiItem::assign(m_okButton, KGuiItem(i18nc("@action:button", "&Rename"), QStringLiteral("dialog-ok-apply")));
QWidget* page = new QWidget(this);
mainLayout->addWidget(page);
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 <filename>%1</filename> to:", m_newName),
m_lineEdit->setText(m_newName);
m_lineEdit->setSelection(0, selectionLength);
- m_lineEdit->setFocus();
topLayout->addWidget(editLabel);
topLayout->addWidget(m_lineEdit);
m_spinBox->setDisplayIntegerBase(10);
QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
- horizontalLayout->setMargin(0);
+ horizontalLayout->setContentsMargins(0, 0, 0, 0);
horizontalLayout->addWidget(infoLabel);
horizontalLayout->addWidget(m_spinBox);
{
}
-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);
- KJobWidgets::setWindow(job, widget);
- KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename, {oldUrl}, newUrl, job);
- job->ui()->setAutoErrorHandlingEnabled(true);
-}
-
-void RenameDialog::slotAccepted()
-{
- m_newName = m_lineEdit->text();
-
+ 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);
- renameItem(m_items.first(), m_newName);
+ 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 {
- renameItems();
+ 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();
}
m_okButton->setEnabled(enable);
}
-void RenameDialog::renameItems()
+void RenameDialog::slotFileRenamed(const QUrl &oldUrl, const QUrl &newUrl)
{
- // 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);
- }
- }
+ Q_UNUSED(oldUrl)
+ m_renamedItems << newUrl;
}
-QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
+void RenameDialog::slotResult(KJob *job)
{
- 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'));
+ if (!job->error()) {
+ emit renamingFinished(m_renamedItems);
}
+}
- // Replace the index placeholders by the indexString
- const int placeHolderStart = newName.indexOf(indexPlaceHolder);
- newName.replace(placeHolderStart, minIndexLength, indexString);
+void RenameDialog::showEvent(QShowEvent* event)
+{
+ m_lineEdit->setFocus();
- return newName;
+ QDialog::showEvent(event);
}
-