+void RenameDialog::slotTextChanged(const QString& newName)
+{
+ bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
+ 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);
+ }
+ }
+ enableButtonOk(enable);
+}
+
+void RenameDialog::renameItems()
+{
+ // 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 KUrl oldUrl = item.url();
+ const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
+ if (!extension.isEmpty()) {
+ newName.append(QLatin1Char('.'));
+ newName.append(extension);
+ }
+
+ if (oldUrl.fileName() != newName) {
+ renameItem(item, newName);
+ }
+ }
+}
+
+QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
+{
+ 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);
+
+ return newName;
+}
+