]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Merge branch 'release/19.12'
[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 <KGuiItem>
23 #include <KIO/BatchRenameJob>
24 #include <KIO/CopyJob>
25 #include <KIO/FileUndoManager>
26 #include <KJobUiDelegate>
27 #include <KJobWidgets>
28 #include <KLocalizedString>
29
30 #include <QDialogButtonBox>
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QLineEdit>
34 #include <QMimeDatabase>
35 #include <QPushButton>
36 #include <QSpinBox>
37
38 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
39 QDialog(parent),
40 m_renameOneItem(false),
41 m_newName(),
42 m_lineEdit(nullptr),
43 m_items(items),
44 m_allExtensionsDifferent(true),
45 m_spinBox(nullptr)
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 connect(buttonBox, &QDialogButtonBox::rejected, this, &QObject::deleteLater);
66 m_okButton->setDefault(true);
67
68 KGuiItem::assign(m_okButton, KGuiItem(i18nc("@action:button", "&Rename"), QStringLiteral("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 = nullptr;
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
113 topLayout->addWidget(editLabel);
114 topLayout->addWidget(m_lineEdit);
115
116 if (!m_renameOneItem) {
117 QSet<QString> extensions;
118 foreach (const KFileItem& item, m_items) {
119 QMimeDatabase db;
120 const QString extension = db.suffixForFileName(item.url().toDisplayString().toLower());
121
122 if (extensions.contains(extension)) {
123 m_allExtensionsDifferent = false;
124 break;
125 }
126
127 extensions.insert(extension);
128 }
129
130 QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page);
131 mainLayout->addWidget(infoLabel);
132 m_spinBox = new QSpinBox(page);
133 m_spinBox->setMaximum(10000);
134 m_spinBox->setMinimum(0);
135 m_spinBox->setSingleStep(1);
136 m_spinBox->setValue(1);
137 m_spinBox->setDisplayIntegerBase(10);
138
139 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
140 horizontalLayout->setContentsMargins(0, 0, 0, 0);
141 horizontalLayout->addWidget(infoLabel);
142 horizontalLayout->addWidget(m_spinBox);
143
144 topLayout->addLayout(horizontalLayout);
145 }
146 }
147
148 RenameDialog::~RenameDialog()
149 {
150 }
151
152 void RenameDialog::slotAccepted()
153 {
154 QWidget* widget = parentWidget();
155 if (!widget) {
156 widget = this;
157 }
158
159 const QList<QUrl> srcList = m_items.urlList();
160 const QString newName = m_lineEdit->text();
161 KIO::FileUndoManager::CommandType cmdType;
162 KIO::Job *job = nullptr;
163 if (m_renameOneItem) {
164 Q_ASSERT(m_items.count() == 1);
165 cmdType = KIO::FileUndoManager::Rename;
166 const QUrl oldUrl = m_items.constFirst().url();
167 QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename);
168 newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName));
169 m_renamedItems << newUrl;
170 job = KIO::moveAs(oldUrl, newUrl, KIO::HideProgressInfo);
171 } else {
172 cmdType = KIO::FileUndoManager::BatchRename;
173 job = KIO::batchRename(srcList, newName, m_spinBox->value(), QLatin1Char('#'));
174 connect(qobject_cast<KIO::BatchRenameJob*>(job), &KIO::BatchRenameJob::fileRenamed, this, &RenameDialog::slotFileRenamed);
175 }
176
177 KJobWidgets::setWindow(job, widget);
178 const QUrl parentUrl = srcList.first().adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash);
179 KIO::FileUndoManager::self()->recordJob(cmdType, srcList, parentUrl, job);
180
181 connect(job, &KJob::result, this, &RenameDialog::slotResult);
182 connect(job, &KJob::result, this, &QObject::deleteLater);
183
184 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
185
186 accept();
187 }
188
189 void RenameDialog::slotTextChanged(const QString& newName)
190 {
191 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1Char('.'));
192 if (enable && !m_renameOneItem) {
193 const int count = newName.count(QLatin1Char('#'));
194 if (count == 0) {
195 // Renaming multiple files without '#' will only work if all extensions are different.
196 enable = m_allExtensionsDifferent;
197 } else {
198 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
199 const int first = newName.indexOf(QLatin1Char('#'));
200 const int last = newName.lastIndexOf(QLatin1Char('#'));
201 enable = (last - first + 1 == count);
202 }
203 }
204 m_okButton->setEnabled(enable);
205 }
206
207 void RenameDialog::slotFileRenamed(const QUrl &oldUrl, const QUrl &newUrl)
208 {
209 Q_UNUSED(oldUrl)
210 m_renamedItems << newUrl;
211 }
212
213 void RenameDialog::slotResult(KJob *job)
214 {
215 if (!job->error()) {
216 emit renamingFinished(m_renamedItems);
217 }
218 }
219
220 void RenameDialog::showEvent(QShowEvent* event)
221 {
222 m_lineEdit->setFocus();
223
224 QDialog::showEvent(event);
225 }