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