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