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