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