Changeset View
Changeset View
Standalone View
Standalone View
renamefiledialog.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2006-2010 by Peter Penz (peter.penz@gmx.at) * | ||||
3 | * Copyright (C) 2019 by Méven Car (meven.car@kdemail.net) * | ||||
4 | * * | ||||
5 | * This program is free software; you can redistribute it and/or modify * | ||||
6 | * it under the terms of the GNU General Public License as published by * | ||||
7 | * the Free Software Foundation; either version 2 of the License, or * | ||||
8 | * (at your option) any later version. * | ||||
9 | * * | ||||
10 | * This program is distributed in the hope that it will be useful, * | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
13 | * GNU General Public License for more details. * | ||||
14 | * * | ||||
15 | * You should have received a copy of the GNU General Public License * | ||||
16 | * along with this program; if not, write to the * | ||||
17 | * Free Software Foundation, Inc., * | ||||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * | ||||
19 | ***************************************************************************/ | ||||
20 | | ||||
21 | #include "renamefiledialog.h" | ||||
22 | | ||||
23 | #include <KGuiItem> | ||||
24 | #include <KIO/BatchRenameJob> | ||||
25 | #include <KIO/CopyJob> | ||||
26 | #include <KIO/FileUndoManager> | ||||
27 | #include <KJobUiDelegate> | ||||
28 | #include <KJobWidgets> | ||||
29 | #include <KLocalizedString> | ||||
30 | | ||||
31 | #include <QDialogButtonBox> | ||||
32 | #include <QHBoxLayout> | ||||
33 | #include <QLabel> | ||||
34 | #include <QLineEdit> | ||||
35 | #include <QMimeDatabase> | ||||
36 | #include <QPushButton> | ||||
37 | #include <QSpinBox> | ||||
38 | | ||||
39 | namespace KIO | ||||
40 | { | ||||
41 | | ||||
42 | class Q_DECL_HIDDEN RenameFileDialog::RenameFileDialogPrivate | ||||
43 | { | ||||
44 | public: | ||||
45 | | ||||
46 | RenameFileDialogPrivate() : | ||||
47 | renameOneItem(false), | ||||
48 | lineEdit(nullptr), | ||||
49 | allExtensionsDifferent(true), | ||||
50 | spinBox(nullptr) | ||||
51 | { | ||||
52 | | ||||
53 | } | ||||
54 | | ||||
55 | bool renameOneItem; | ||||
56 | QList<QUrl> renamedItems; | ||||
57 | QLineEdit* lineEdit; | ||||
58 | KFileItemList items; | ||||
59 | bool allExtensionsDifferent; | ||||
60 | QSpinBox* spinBox; | ||||
61 | QPushButton* okButton; | ||||
62 | }; | ||||
63 | | ||||
64 | RenameFileDialog::RenameFileDialog(QWidget *parent, const KFileItemList& items) : | ||||
65 | QDialog(parent), | ||||
66 | d(new RenameFileDialogPrivate()) | ||||
67 | { | ||||
68 | const QSize minSize = minimumSize(); | ||||
69 | setMinimumSize(QSize(320, minSize.height())); | ||||
70 | | ||||
71 | const int itemCount = items.count(); | ||||
72 | Q_ASSERT(itemCount >= 1); | ||||
73 | d->renameOneItem = (itemCount == 1); | ||||
74 | | ||||
75 | setWindowTitle(d->renameOneItem ? | ||||
76 | i18nc("@title:window", "Rename Item") : | ||||
77 | i18nc("@title:window", "Rename Items")); | ||||
78 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); | ||||
79 | QVBoxLayout *mainLayout = new QVBoxLayout; | ||||
80 | setLayout(mainLayout); | ||||
81 | d->okButton = buttonBox->button(QDialogButtonBox::Ok); | ||||
82 | d->okButton->setDefault(true); | ||||
83 | d->okButton->setShortcut(Qt::CTRL + Qt::Key_Return); | ||||
84 | connect(buttonBox, &QDialogButtonBox::accepted, this, &RenameFileDialog::slotAccepted); | ||||
85 | connect(buttonBox, &QDialogButtonBox::rejected, this, &RenameFileDialog::reject); | ||||
86 | connect(buttonBox, &QDialogButtonBox::rejected, this, &QObject::deleteLater); | ||||
87 | d->okButton->setDefault(true); | ||||
88 | | ||||
89 | KGuiItem::assign(d->okButton, KGuiItem(i18nc("@action:button", "&Rename"), QStringLiteral("dialog-ok-apply"))); | ||||
90 | | ||||
91 | QWidget* page = new QWidget(this); | ||||
92 | mainLayout->addWidget(page); | ||||
93 | mainLayout->addWidget(buttonBox); | ||||
94 | | ||||
95 | QVBoxLayout* topLayout = new QVBoxLayout(page); | ||||
96 | | ||||
97 | QLabel* editLabel = nullptr; | ||||
98 | QString newName; | ||||
99 | if (d->renameOneItem) { | ||||
100 | newName = items.first().name(); | ||||
101 | editLabel = new QLabel(xi18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", newName), | ||||
102 | page); | ||||
103 | editLabel->setTextFormat(Qt::PlainText); | ||||
104 | } else { | ||||
105 | newName = i18nc("This a template for new filenames, # is replaced by a number later, must be the end charater", "New name #"); | ||||
106 | editLabel = new QLabel(i18ncp("@label:textbox", | ||||
107 | "Rename the %1 selected item to:", | ||||
108 | "Rename the %1 selected items to:", itemCount), | ||||
109 | page); | ||||
110 | } | ||||
111 | | ||||
112 | d->lineEdit = new QLineEdit(page); | ||||
113 | mainLayout->addWidget(d->lineEdit); | ||||
114 | connect(d->lineEdit, &QLineEdit::textChanged, this, &RenameFileDialog::slotTextChanged); | ||||
115 | | ||||
116 | int selectionLength = newName.length(); | ||||
117 | if (d->renameOneItem) { | ||||
118 | // If the current item is a directory, select the whole file name. | ||||
119 | if (!items.first().isDir()) { | ||||
120 | const QString fileName = items.first().url().toDisplayString(); | ||||
121 | QMimeDatabase db; | ||||
122 | const QString extension = db.suffixForFileName(fileName.toLower()); | ||||
123 | | ||||
124 | if (extension.length() > 0) { | ||||
125 | // Don't select the extension | ||||
126 | selectionLength -= extension.length() + 1; | ||||
127 | } | ||||
128 | } | ||||
129 | } else { | ||||
130 | // Don't select the # character | ||||
131 | --selectionLength; | ||||
132 | } | ||||
133 | | ||||
134 | d->lineEdit->setText(newName); | ||||
135 | d->lineEdit->setSelection(0, selectionLength); | ||||
136 | | ||||
137 | topLayout->addWidget(editLabel); | ||||
138 | topLayout->addWidget(d->lineEdit); | ||||
139 | | ||||
140 | if (!d->renameOneItem) { | ||||
141 | QMimeDatabase db; | ||||
142 | QSet<QString> extensions; | ||||
143 | for (const KFileItem& item : qAsConst(d->items)) { | ||||
144 | const QString extension = db.suffixForFileName(item.url().toDisplayString().toLower()); | ||||
145 | | ||||
146 | if (extensions.contains(extension)) { | ||||
147 | d->allExtensionsDifferent = false; | ||||
148 | break; | ||||
149 | } | ||||
150 | | ||||
151 | extensions.insert(extension); | ||||
152 | } | ||||
153 | | ||||
154 | QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page); | ||||
155 | mainLayout->addWidget(infoLabel); | ||||
156 | d->spinBox = new QSpinBox(page); | ||||
157 | d->spinBox->setMaximum(10000); | ||||
158 | d->spinBox->setMinimum(0); | ||||
159 | d->spinBox->setSingleStep(1); | ||||
160 | d->spinBox->setValue(1); | ||||
161 | d->spinBox->setDisplayIntegerBase(10); | ||||
162 | | ||||
163 | QHBoxLayout* horizontalLayout = new QHBoxLayout(page); | ||||
164 | horizontalLayout->setMargin(0); | ||||
165 | horizontalLayout->addWidget(infoLabel); | ||||
166 | horizontalLayout->addWidget(d->spinBox); | ||||
167 | | ||||
168 | topLayout->addLayout(horizontalLayout); | ||||
169 | } | ||||
170 | } | ||||
171 | | ||||
172 | RenameFileDialog::~RenameFileDialog() | ||||
173 | { | ||||
174 | } | ||||
175 | | ||||
176 | bool RenameFileDialog::event(QEvent* ev) | ||||
177 | { | ||||
178 | return QDialog::event(ev); | ||||
179 | } | ||||
180 | | ||||
181 | void RenameFileDialog::slotAccepted() | ||||
182 | { | ||||
183 | QWidget* widget = parentWidget(); | ||||
184 | if (!widget) { | ||||
185 | widget = this; | ||||
186 | } | ||||
187 | | ||||
188 | const QList<QUrl> srcList = d->items.urlList(); | ||||
189 | const QString newName = d->lineEdit->text(); | ||||
190 | KIO::FileUndoManager::CommandType cmdType; | ||||
191 | KIO::Job *job = nullptr; | ||||
192 | if (d->renameOneItem) { | ||||
193 | Q_ASSERT(d->items.count() == 1); | ||||
194 | cmdType = KIO::FileUndoManager::Rename; | ||||
195 | const QUrl oldUrl = d->items.constFirst().url(); | ||||
196 | QUrl newUrl = oldUrl.adjusted(QUrl::RemoveFilename); | ||||
197 | newUrl.setPath(newUrl.path() + KIO::encodeFileName(newName)); | ||||
198 | d->renamedItems << newUrl; | ||||
199 | job = KIO::moveAs(oldUrl, newUrl, KIO::HideProgressInfo); | ||||
200 | } else { | ||||
201 | d->renamedItems.reserve(d->items.count()); | ||||
202 | cmdType = KIO::FileUndoManager::BatchRename; | ||||
203 | job = KIO::batchRename(srcList, newName, d->spinBox->value(), QLatin1Char('#')); | ||||
204 | connect(qobject_cast<KIO::BatchRenameJob*>(job), &KIO::BatchRenameJob::fileRenamed, this, &RenameFileDialog::slotFileRenamed); | ||||
205 | } | ||||
206 | | ||||
207 | KJobWidgets::setWindow(job, widget); | ||||
208 | const QUrl parentUrl = srcList.first().adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash); | ||||
209 | KIO::FileUndoManager::self()->recordJob(cmdType, srcList, parentUrl, job); | ||||
210 | | ||||
211 | connect(job, &KJob::result, this, &RenameFileDialog::slotResult); | ||||
212 | connect(job, &KJob::result, this, &QObject::deleteLater); | ||||
213 | | ||||
214 | job->uiDelegate()->setAutoErrorHandlingEnabled(true); | ||||
215 | | ||||
216 | accept(); | ||||
217 | } | ||||
218 | | ||||
219 | void RenameFileDialog::slotTextChanged(const QString& newName) | ||||
220 | { | ||||
221 | bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String(".")); | ||||
222 | if (enable && !d->renameOneItem) { | ||||
223 | const int count = newName.count(QLatin1Char('#')); | ||||
224 | if (count == 0) { | ||||
225 | // Renaming multiple files without '#' will only work if all extensions are different. | ||||
226 | enable = d->allExtensionsDifferent; | ||||
227 | } else { | ||||
228 | // Assure that the new name contains exactly one # (or a connected sequence of #'s) | ||||
229 | const int first = newName.indexOf(QLatin1Char('#')); | ||||
230 | const int last = newName.lastIndexOf(QLatin1Char('#')); | ||||
231 | enable = (last - first + 1 == count); | ||||
232 | } | ||||
233 | } | ||||
234 | d->okButton->setEnabled(enable); | ||||
235 | } | ||||
236 | | ||||
237 | void RenameFileDialog::slotFileRenamed(const QUrl &oldUrl, const QUrl &newUrl) | ||||
238 | { | ||||
239 | Q_UNUSED(oldUrl) | ||||
240 | d->renamedItems << newUrl; | ||||
241 | } | ||||
242 | | ||||
243 | void RenameFileDialog::slotResult(KJob *job) | ||||
244 | { | ||||
245 | if (!job->error()) { | ||||
246 | emit renamingFinished(d->renamedItems); | ||||
247 | } | ||||
248 | } | ||||
249 | | ||||
250 | void RenameFileDialog::showEvent(QShowEvent* event) | ||||
251 | { | ||||
252 | d->lineEdit->setFocus(); | ||||
253 | | ||||
254 | QDialog::showEvent(event); | ||||
255 | } | ||||
256 | | ||||
257 | } // namespace KIO |