]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
d8dbd7749dc3eb7fd91b29a3dd68fb2a90f2d9cd
[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 #include <kmimetype.h>
28
29 #include <QHBoxLayout>
30 #include <QLabel>
31 #include <QVBoxLayout>
32
33 /**
34 * Helper function for sorting items with qSort() in
35 * DolphinView::renameSelectedItems().
36 */
37 bool lessThan(const KFileItem& item1, const KFileItem& item2)
38 {
39 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
40 }
41
42 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
43 KDialog(parent),
44 m_renameOneItem(false),
45 m_newName(),
46 m_lineEdit(0),
47 m_items(items),
48 m_allExtensionsDifferent(true),
49 m_spinBox(0)
50 {
51 const QSize minSize = minimumSize();
52 setMinimumSize(QSize(320, minSize.height()));
53
54 const int itemCount = items.count();
55 Q_ASSERT(itemCount >= 1);
56 m_renameOneItem = (itemCount == 1);
57
58 setCaption(m_renameOneItem ?
59 i18nc("@title:window", "Rename Item") :
60 i18nc("@title:window", "Rename Items"));
61 setButtons(Ok | Cancel);
62 setDefaultButton(Ok);
63
64 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
65
66 QWidget* page = new QWidget(this);
67 setMainWidget(page);
68
69 QVBoxLayout* topLayout = new QVBoxLayout(page);
70
71 QLabel* editLabel = 0;
72 if (m_renameOneItem) {
73 m_newName = items.first().name();
74 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
75 page);
76 editLabel->setTextFormat(Qt::PlainText);
77 } else {
78 m_newName = i18nc("@info:status", "New name #");
79 editLabel = new QLabel(i18ncp("@label:textbox",
80 "Rename the %1 selected item to:",
81 "Rename the %1 selected items to:", itemCount),
82 page);
83 }
84
85 m_lineEdit = new KLineEdit(page);
86 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
87
88 int selectionLength = m_newName.length();
89 if (m_renameOneItem) {
90 const QString fileName = items.first().url().prettyUrl();
91 const QString extension = KMimeType::extractKnownExtension(fileName.toLower());
92
93 // If the current item is a directory, select the whole file name.
94 if ((extension.length() > 0) && !items.first().isDir()) {
95 // Don't select the extension
96 selectionLength -= extension.length() + 1;
97 }
98 } else {
99 // Don't select the # character
100 --selectionLength;
101 }
102
103 m_lineEdit->setText(m_newName);
104 m_lineEdit->setSelection(0, selectionLength);
105 m_lineEdit->setFocus();
106
107 topLayout->addWidget(editLabel);
108 topLayout->addWidget(m_lineEdit);
109
110 if (!m_renameOneItem) {
111 QSet<QString> extensions;
112 foreach (const KFileItem& item, m_items) {
113 const QString extension = KMimeType::extractKnownExtension(item.url().prettyUrl().toLower());
114
115 if (extensions.contains(extension)) {
116 m_allExtensionsDifferent = false;
117 break;
118 }
119
120 extensions.insert(extension);
121 }
122
123 QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page);
124 m_spinBox = new KIntSpinBox(0, 10000, 1, 1, page, 10);
125
126 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
127 horizontalLayout->setMargin(0);
128 horizontalLayout->addWidget(infoLabel);
129 horizontalLayout->addWidget(m_spinBox);
130
131 topLayout->addLayout(horizontalLayout);
132 }
133 }
134
135 RenameDialog::~RenameDialog()
136 {
137 }
138
139 void RenameDialog::slotButtonClicked(int button)
140 {
141 if (button == KDialog::Ok) {
142 m_newName = m_lineEdit->text();
143
144 if (m_renameOneItem) {
145 Q_ASSERT(m_items.count() == 1);
146 const KUrl oldUrl = m_items.first().url();
147 KUrl newUrl = oldUrl;
148 newUrl.setFileName(KIO::encodeFileName(m_newName));
149
150 QWidget* widget = parentWidget();
151 if (!widget) {
152 widget = this;
153 }
154
155 KonqOperations::rename(widget, oldUrl, newUrl);
156 } else {
157 renameItems();
158 }
159 }
160
161 KDialog::slotButtonClicked(button);
162 }
163
164 void RenameDialog::slotTextChanged(const QString& newName)
165 {
166 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
167 if (enable && !m_renameOneItem) {
168 const int count = newName.count(QLatin1Char('#'));
169 if (count == 0) {
170 // Renaming multiple files without '#' will only work if all extensions are different.
171 enable = m_allExtensionsDifferent;
172 } else {
173 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
174 const int first = newName.indexOf(QLatin1Char('#'));
175 const int last = newName.lastIndexOf(QLatin1Char('#'));
176 enable = (last - first + 1 == count);
177 }
178 }
179 enableButtonOk(enable);
180 }
181
182 void RenameDialog::renameItems()
183 {
184 // Iterate through all items and rename them...
185 int index = m_spinBox->value();
186 foreach (const KFileItem& item, m_items) {
187 QString newName = indexedName(m_newName, index, QLatin1Char('#'));
188 ++index;
189
190 const KUrl oldUrl = item.url();
191 const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
192 if (!extension.isEmpty()) {
193 newName.append(QLatin1Char('.'));
194 newName.append(extension);
195 }
196
197 if (oldUrl.fileName() != newName) {
198 KUrl newUrl = oldUrl;
199 newUrl.setFileName(KIO::encodeFileName(newName));
200
201 QWidget* widget = parentWidget();
202 if (!widget) {
203 widget = this;
204 }
205
206 KonqOperations::rename(widget, oldUrl, newUrl);
207 }
208 }
209 }
210
211 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
212 {
213 QString newName = name;
214
215 QString indexString = QString::number(index);
216
217 // Insert leading zeros if necessary
218 const int minIndexLength = name.count(indexPlaceHolder);
219 while (indexString.length() < minIndexLength) {
220 indexString.prepend(QLatin1Char('0'));
221 }
222
223 // Replace the index placeholders by the indexString
224 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
225 newName.replace(placeHolderStart, minIndexLength, indexString);
226
227 return newName;
228 }
229
230 #include "renamedialog.moc"