+void RenameDialog::slotTextChanged(const QString& newName)
+{
+ bool enable = !newName.isEmpty();
+ if (enable) {
+ if (m_renameOneItem) {
+ enable = enable && (newName != m_newName);
+ } else {
+ // 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 = 1;
+ foreach (const KFileItem& item, m_items) {
+ const QString newName = indexedName(m_newName, index, QLatin1Char('#'));
+ ++index;
+
+ const KUrl oldUrl = item.url();
+ if (oldUrl.fileName() != newName) {
+ KUrl newUrl = oldUrl;
+ newUrl.setFileName(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;
+}
+