Hello,

This patch applies to dolphin/src/views/renamedialog.cpp and
dolphin/src/views/renamedialog.h. It is built on the current master
branch of kde-baseapps repository.

It replaces the current renaming functionality of Dolphin for multiple
files. The functionality gives user a little more control over how to
rename their files. It asks the user for the starting point of the
sequence of number instead of starting it always from 1. Renaming 3
files by doing "FILE##.txt" gives us FILE01.txt, FILE02.txt, FILE03.txt.

If we have to rename these 3 files to FILE04.txt, FILE05.txt,
FILE06.txt, we need 6 files for the operation. We cannot rename files
starting from a random number. Also, we cannot rename file with
different extensions in a sequence. Though this patch does not yet solve
this problem, what it does is to give the user a choice to start the
sequence from wherever he/she wants it to.

I have used parenthesis '(', ')' instead of '#' as placeholder. For the
above example, we have to rename the files by doing FILE(04).txt and it
will rename the files as required.

The patch currently does not handle enabling of button by validating
text in slotTextChanged(). It should also handle the issue of different
extensions. As this is my first patch, I wanted to confirm if I am going
in the right direction, then I can start working on the above issues
also? I do need help from the community. Suggestions and improvements in
the code, or on the idea are most welcome.

Thanks.

-- 
Regards
Chirag Anand

diff --git a/dolphin/src/views/renamedialog.cpp b/dolphin/src/views/renamedialog.cpp
index c0c6ad5..efdace0 100644
--- a/dolphin/src/views/renamedialog.cpp
+++ b/dolphin/src/views/renamedialog.cpp
@@ -69,7 +69,7 @@ RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
         editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
                                page);
     } else {
-        m_newName = i18nc("@info:status", "New name #");
+        m_newName = i18nc("@info:status", "New name (000)");
         editLabel = new QLabel(i18ncp("@label:textbox",
                                       "Rename the %1 selected item to:",
                                       "Rename the %1 selected items to:", itemCount),
@@ -118,7 +118,7 @@ RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
     topLayout->addWidget(m_lineEdit);
 
     if (!m_renameOneItem) {
-        QLabel* infoLabel = new QLabel(i18nc("@info", "(# will be replaced by ascending numbers)"), page);
+        QLabel* infoLabel = new QLabel(i18nc("@info", "(number inside the parenthesis will be incremented, default: 1)"), page);
         topLayout->addWidget(infoLabel);
     }
 }
@@ -178,8 +178,22 @@ void RenameDialog::renameItems()
 
     // Iterate through all items and rename them...
     int index = 1;
+    int indexStart;
+    int indexEnd;
+    bool ok;
+    QString indexInitialString;
+
+    indexStart = m_newName.indexOf(QLatin1Char('('));
+    indexEnd = m_newName.indexOf(QLatin1Char(')'));
+    indexInitialString = m_newName.mid(indexStart + 1, indexEnd - indexStart - 1);
+
+    index = indexInitialString.toInt(&ok, 10);
+    if (!ok) {
+        index = 1;
+    }
+
     foreach (const KFileItem& item, m_items) {
-        const QString newName = indexedName(m_newName, index, QLatin1Char('#'));
+        const QString newName = indexedName(m_newName, index, indexInitialString.size());
         ++index;
 
         const KUrl oldUrl = item.url();
@@ -191,21 +205,20 @@ void RenameDialog::renameItems()
     }
 }
 
-QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
+QString RenameDialog::indexedName(const QString& name, int index, int indexSize)
 {
     QString newName = name;
 
     QString indexString = QString::number(index);
 
     // Insert leading zeros if necessary
-    const int minIndexLength = name.count(indexPlaceHolder);
-    while (indexString.length() < minIndexLength) {
+    while (indexString.length() < indexSize) {
         indexString.prepend(QLatin1Char('0'));
     }
 
-    // Replace the index placeholders by the indexString
-    const int placeHolderStart = newName.indexOf(indexPlaceHolder);
-    newName.replace(placeHolderStart, minIndexLength, indexString);
+    // Replace the parenthesis by the indexString
+    const int indexStart = newName.indexOf(QLatin1Char('('));
+    newName.replace(indexStart, indexString.size() + 2, indexString);
 
     return newName;
 }
diff --git a/dolphin/src/views/renamedialog.h b/dolphin/src/views/renamedialog.h
index 8d8b73d..41ba96d 100644
--- a/dolphin/src/views/renamedialog.h
+++ b/dolphin/src/views/renamedialog.h
@@ -57,7 +57,7 @@ private:
      *         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);
+    static QString indexedName(const QString& name, int index, int indexSize);
 
 private:
     bool m_renameOneItem;
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

Reply via email to