]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz (peter.penz@gmx.at) *
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. *
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. *
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 ***************************************************************************/
20 #include "renamedialog.h"
22 #include <KLocalizedString>
23 #include <KJobWidgets>
24 #include <KIO/CopyJob>
25 #include <KIO/FileUndoManager>
26 #include <KJobUiDelegate>
27 #include <KIO/BatchRenameJob>
29 #include <QHBoxLayout>
31 #include <QVBoxLayout>
32 #include <QMimeDatabase>
33 #include <QDialogButtonBox>
34 #include <QPushButton>
39 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
41 m_renameOneItem(false),
45 m_allExtensionsDifferent(true),
48 const QSize minSize
= minimumSize();
49 setMinimumSize(QSize(320, minSize
.height()));
51 const int itemCount
= items
.count();
52 Q_ASSERT(itemCount
>= 1);
53 m_renameOneItem
= (itemCount
== 1);
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
, &QDialogButtonBox::accepted
, this, &RenameDialog::slotAccepted
);
65 connect(buttonBox
, &QDialogButtonBox::rejected
, this, &RenameDialog::reject
);
66 m_okButton
->setDefault(true);
68 KGuiItem::assign(m_okButton
, KGuiItem(i18nc("@action:button", "&Rename"), QStringLiteral("dialog-ok-apply")));
70 QWidget
* page
= new QWidget(this);
71 mainLayout
->addWidget(page
);
72 mainLayout
->addWidget(buttonBox
);
74 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
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
),
81 editLabel
->setTextFormat(Qt::PlainText
);
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
),
90 m_lineEdit
= new QLineEdit(page
);
91 mainLayout
->addWidget(m_lineEdit
);
92 connect(m_lineEdit
, &QLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
94 int selectionLength
= m_newName
.length();
95 if (m_renameOneItem
) {
96 const QString fileName
= items
.first().url().toDisplayString();
98 const QString extension
= db
.suffixForFileName(fileName
.toLower());
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;
106 // Don't select the # character
110 m_lineEdit
->setText(m_newName
);
111 m_lineEdit
->setSelection(0, selectionLength
);
113 topLayout
->addWidget(editLabel
);
114 topLayout
->addWidget(m_lineEdit
);
116 if (!m_renameOneItem
) {
117 QSet
<QString
> extensions
;
118 foreach (const KFileItem
& item
, m_items
) {
120 const QString extension
= db
.suffixForFileName(item
.url().toDisplayString().toLower());
122 if (extensions
.contains(extension
)) {
123 m_allExtensionsDifferent
= false;
127 extensions
.insert(extension
);
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);
139 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
140 horizontalLayout
->setMargin(0);
141 horizontalLayout
->addWidget(infoLabel
);
142 horizontalLayout
->addWidget(m_spinBox
);
144 topLayout
->addLayout(horizontalLayout
);
148 RenameDialog::~RenameDialog()
152 void RenameDialog::slotAccepted()
154 QWidget
* widget
= parentWidget();
159 KIO::FileUndoManager::CommandType cmdType
;
160 if (m_renameOneItem
) {
161 Q_ASSERT(m_items
.count() == 1);
162 cmdType
= KIO::FileUndoManager::Rename
;
164 cmdType
= KIO::FileUndoManager::BatchRename
;
167 const QList
<QUrl
> srcList
= m_items
.urlList();
168 KIO::BatchRenameJob
* job
= KIO::batchRename(srcList
, m_lineEdit
->text(), m_spinBox
->value(), QLatin1Char('#'));
169 KJobWidgets::setWindow(job
, widget
);
170 const QUrl parentUrl
= srcList
.first().adjusted(QUrl::RemoveFilename
| QUrl::StripTrailingSlash
);
171 KIO::FileUndoManager::self()->recordJob(cmdType
, srcList
, parentUrl
, job
);
173 connect(job
, &KIO::BatchRenameJob::fileRenamed
, this, &RenameDialog::slotFileRenamed
);
174 connect(job
, &KIO::BatchRenameJob::result
, this, &RenameDialog::slotResult
);
176 job
->uiDelegate()->setAutoErrorHandlingEnabled(true);
181 void RenameDialog::slotTextChanged(const QString
& newName
)
183 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
184 if (enable
&& !m_renameOneItem
) {
185 const int count
= newName
.count(QLatin1Char('#'));
187 // Renaming multiple files without '#' will only work if all extensions are different.
188 enable
= m_allExtensionsDifferent
;
190 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
191 const int first
= newName
.indexOf(QLatin1Char('#'));
192 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
193 enable
= (last
- first
+ 1 == count
);
196 m_okButton
->setEnabled(enable
);
199 void RenameDialog::slotFileRenamed(const QUrl
&oldUrl
, const QUrl
&newUrl
)
202 m_renamedItems
<< newUrl
;
205 void RenameDialog::slotResult(KJob
*job
)
208 emit
renamingFinished(m_renamedItems
);
212 void RenameDialog::showEvent(QShowEvent
* event
)
214 m_lineEdit
->setFocus();
216 QDialog::showEvent(event
);