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