+void RenameDialog::slotTextChanged(const QString& newName)
+{
+ bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
+ if (enable && !m_renameOneItem) {
+ // Assure that the new name contains exactly one # (or a connected sequence of #'s)
+ const int minSplitCount = 1;
+ int maxSplitCount = 2;
+ if (newName.startsWith(QLatin1Char('#'))) {
+ --maxSplitCount;
+ }
+ if (newName.endsWith(QLatin1Char('#'))) {
+ --maxSplitCount;
+ }
+ const int splitCount = newName.split(QLatin1Char('#'), QString::SkipEmptyParts).count();
+ enable = enable && (splitCount >= minSplitCount) && (splitCount <= maxSplitCount);
+ }
+ enableButtonOk(enable);
+}
+
+void RenameDialog::renameItems()
+{
+ // Currently the items are sorted by the selection order, resort
+ // them by the filename. This assures that the new sort order is similar to
+ // the current filename sort order.
+ qSort(m_items.begin(), m_items.end(), lessThan);
+
+ // 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) {
+ KUrl newUrl = oldUrl;
+ newUrl.setFileName(KIO::encodeFileName(newName));
+ KonqOperations::rename(this, oldUrl, newUrl);
+ }
+ }
+}
+
+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;
+}
+