]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
RenameDialog: Don't disable the OK-button if nothing has been changed
[dolphin.git] / src / views / renamedialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz (peter.penz@gmx.at) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "renamedialog.h"
21
22 #include <KLineEdit>
23 #include <KLocale>
24 #include <konq_operations.h>
25 #include <KStringHandler>
26 #include <knuminput.h>
27
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QVBoxLayout>
31
32 /**
33 * Helper function for sorting items with qSort() in
34 * DolphinView::renameSelectedItems().
35 */
36 bool lessThan(const KFileItem& item1, const KFileItem& item2)
37 {
38 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
39 }
40
41 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
42 KDialog(parent),
43 m_renameOneItem(false),
44 m_newName(),
45 m_lineEdit(0),
46 m_items(items),
47 m_spinBox(0)
48 {
49 const QSize minSize = minimumSize();
50 setMinimumSize(QSize(320, minSize.height()));
51
52 const int itemCount = items.count();
53 Q_ASSERT(itemCount >= 1);
54 m_renameOneItem = (itemCount == 1);
55
56 setCaption(m_renameOneItem ?
57 i18nc("@title:window", "Rename Item") :
58 i18nc("@title:window", "Rename Items"));
59 setButtons(Ok | Cancel);
60 setDefaultButton(Ok);
61
62 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
63
64 QWidget* page = new QWidget(this);
65 setMainWidget(page);
66
67 QVBoxLayout* topLayout = new QVBoxLayout(page);
68
69 QLabel* editLabel = 0;
70 if (m_renameOneItem) {
71 m_newName = items.first().name();
72 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
73 page);
74 } else {
75 m_newName = i18nc("@info:status", "New name #");
76 editLabel = new QLabel(i18ncp("@label:textbox",
77 "Rename the %1 selected item to:",
78 "Rename the %1 selected items to:", itemCount),
79 page);
80 }
81
82 m_lineEdit = new KLineEdit(page);
83 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
84
85 int selectionLength = m_newName.length();
86 if (m_renameOneItem) {
87 const QString fileName = items.first().url().prettyUrl();
88 const QString extension = KMimeType::extractKnownExtension(fileName.toLower());
89 if (extension.length() > 0) {
90 // Don't select the extension
91 selectionLength -= extension.length() + 1;
92 }
93 } else {
94 // Don't select the # character
95 --selectionLength;
96 }
97
98 m_lineEdit->setText(m_newName);
99 m_lineEdit->setSelection(0, selectionLength);
100 m_lineEdit->setFocus();
101
102 topLayout->addWidget(editLabel);
103 topLayout->addWidget(m_lineEdit);
104
105 if (!m_renameOneItem) {
106 QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page);
107 m_spinBox = new KIntSpinBox(0, 10000, 1, 1, page, 10);
108
109 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
110 horizontalLayout->setMargin(0);
111 horizontalLayout->addWidget(infoLabel);
112 horizontalLayout->addWidget(m_spinBox);
113
114 topLayout->addLayout(horizontalLayout);
115 }
116 }
117
118 RenameDialog::~RenameDialog()
119 {
120 }
121
122 void RenameDialog::slotButtonClicked(int button)
123 {
124 if (button == KDialog::Ok) {
125 m_newName = m_lineEdit->text();
126
127 if (m_renameOneItem) {
128 Q_ASSERT(m_items.count() == 1);
129 const KUrl oldUrl = m_items.first().url();
130 KUrl newUrl = oldUrl;
131 newUrl.setFileName(KIO::encodeFileName(m_newName));
132 KonqOperations::rename(this, oldUrl, newUrl);
133 } else {
134 renameItems();
135 }
136 }
137
138 KDialog::slotButtonClicked(button);
139 }
140
141 void RenameDialog::slotTextChanged(const QString& newName)
142 {
143 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
144 if (enable && !m_renameOneItem) {
145 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
146 const int minSplitCount = 1;
147 int maxSplitCount = 2;
148 if (newName.startsWith(QLatin1Char('#'))) {
149 --maxSplitCount;
150 }
151 if (newName.endsWith(QLatin1Char('#'))) {
152 --maxSplitCount;
153 }
154 const int splitCount = newName.split(QLatin1Char('#'), QString::SkipEmptyParts).count();
155 enable = enable && (splitCount >= minSplitCount) && (splitCount <= maxSplitCount);
156 }
157 enableButtonOk(enable);
158 }
159
160 void RenameDialog::renameItems()
161 {
162 // Currently the items are sorted by the selection order, resort
163 // them by the filename. This assures that the new sort order is similar to
164 // the current filename sort order.
165 qSort(m_items.begin(), m_items.end(), lessThan);
166
167 // Iterate through all items and rename them...
168 int index = m_spinBox->value();
169 foreach (const KFileItem& item, m_items) {
170 QString newName = indexedName(m_newName, index, QLatin1Char('#'));
171 ++index;
172
173 const KUrl oldUrl = item.url();
174 const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
175 if (!extension.isEmpty()) {
176 newName.append(QLatin1Char('.'));
177 newName.append(extension);
178 }
179
180 if (oldUrl.fileName() != newName) {
181 KUrl newUrl = oldUrl;
182 newUrl.setFileName(KIO::encodeFileName(newName));
183 KonqOperations::rename(this, oldUrl, newUrl);
184 }
185 }
186 }
187
188 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
189 {
190 QString newName = name;
191
192 QString indexString = QString::number(index);
193
194 // Insert leading zeros if necessary
195 const int minIndexLength = name.count(indexPlaceHolder);
196 while (indexString.length() < minIndexLength) {
197 indexString.prepend(QLatin1Char('0'));
198 }
199
200 // Replace the index placeholders by the indexString
201 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
202 newName.replace(placeHolderStart, minIndexLength, indexString);
203
204 return newName;
205 }
206
207 #include "renamedialog.moc"