summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChinmoy Ranjan Pradhan <[email protected]>2018-01-27 17:19:39 +0530
committerChinmoy Ranjan Pradhan <[email protected]>2018-01-27 17:21:30 +0530
commitc5eb4e31161ccf422a2f2492fe998c5c9817bea4 (patch)
treea0f7ea4e44c1577d7caece22c55f92521c667b3c
parentadd821885d0e21509b15feed5826aadadb8364dd (diff)
Use BatchRenameJob to rename multiple files in Dolphin
Summary: Use KIO::BatchRenameJob in Dolphin::RenameDialog to rename multiple files. With this viewing progress of rename operation and undo is possible. See D9103 and D9107. Reviewers: #dolphin, elvisangelaccio Subscribers: elvisangelaccio, ngraham, broulik Differential Revision: https://phabricator.kde.org/D9836
-rw-r--r--src/views/renamedialog.cpp86
-rw-r--r--src/views/renamedialog.h17
2 files changed, 27 insertions, 76 deletions
diff --git a/src/views/renamedialog.cpp b/src/views/renamedialog.cpp
index 79421a5..2e1fa86 100644
--- a/src/views/renamedialog.cpp
+++ b/src/views/renamedialog.cpp
@@ -24,6 +24,7 @@
#include <KIO/CopyJob>
#include <KIO/FileUndoManager>
#include <KJobUiDelegate>
+#include <KIO/BatchRenameJob>
#include <QHBoxLayout>
#include <QLabel>
@@ -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<QUrl> 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);
}
-
diff --git a/src/views/renamedialog.h b/src/views/renamedialog.h
index 8a92ea1..8da7708 100644
--- a/src/views/renamedialog.h
+++ b/src/views/renamedialog.h
@@ -29,7 +29,7 @@
class QLineEdit;
class QSpinBox;
class QPushButton;
-
+class KJob;
/**
* @brief Dialog for renaming a variable number of files.
*/
@@ -47,24 +47,13 @@ signals:
private slots:
void slotAccepted();
void slotTextChanged(const QString& newName);
+ void slotFileRenamed(const QUrl& oldUrl, const QUrl& newUrl);
+ void slotResult(KJob* job);
protected:
void showEvent(QShowEvent* event) override;
private:
- void renameItems();
- void renameItem(const KFileItem &item, const QString& newName);
-
- /**
- * @return Returns the string \p name, where the characters represented by
- * \p indexPlaceHolder get replaced by the index \p index.
- * E. g. Calling indexedName("Test #.jpg", 12, '#') returns "Test 12.jpg".
- * A connected sequence of placeholders results in leading zeros:
- * indexedName("Test ####.jpg", 12, '#') returns "Test 0012.jpg".
- */
- static QString indexedName(const QString& name, int index, const QChar& indexPlaceHolder);
-
-private:
bool m_renameOneItem;
QList<QUrl> m_renamedItems;
QString m_newName;