]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
be9e34c4a138ce2d8db31008f949b019df981bfb
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>
28 #include <QHBoxLayout>
30 #include <QVBoxLayout>
31 #include <QMimeDatabase>
32 #include <QDialogButtonBox>
33 #include <QPushButton>
38 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
40 m_renameOneItem(false),
44 m_allExtensionsDifferent(true),
47 const QSize minSize
= minimumSize();
48 setMinimumSize(QSize(320, minSize
.height()));
50 const int itemCount
= items
.count();
51 Q_ASSERT(itemCount
>= 1);
52 m_renameOneItem
= (itemCount
== 1);
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
, SIGNAL(accepted()), this, SLOT(slotAccepted()));
64 connect(buttonBox
, SIGNAL(rejected()), this, SLOT(reject()));
65 m_okButton
->setDefault(true);
67 KGuiItem::assign(m_okButton
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
69 QWidget
* page
= new QWidget(this);
70 mainLayout
->addWidget(page
);
71 mainLayout
->addWidget(buttonBox
);
73 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
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
),
80 editLabel
->setTextFormat(Qt::PlainText
);
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
),
89 m_lineEdit
= new QLineEdit(page
);
90 mainLayout
->addWidget(m_lineEdit
);
91 connect(m_lineEdit
, &QLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
93 int selectionLength
= m_newName
.length();
94 if (m_renameOneItem
) {
95 const QString fileName
= items
.first().url().toDisplayString();
97 const QString extension
= db
.suffixForFileName(fileName
.toLower());
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;
105 // Don't select the # character
109 m_lineEdit
->setText(m_newName
);
110 m_lineEdit
->setSelection(0, selectionLength
);
111 m_lineEdit
->setFocus();
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::renameItem(const KFileItem
&item
, const QString
& newName
)
154 const QUrl oldUrl
= item
.url();
155 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
156 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
158 QWidget
* widget
= parentWidget();
163 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
164 KJobWidgets::setWindow(job
, widget
);
165 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, {oldUrl
}, newUrl
, job
);
166 job
->ui()->setAutoErrorHandlingEnabled(true);
169 void RenameDialog::slotAccepted()
171 m_newName
= m_lineEdit
->text();
173 if (m_renameOneItem
) {
174 Q_ASSERT(m_items
.count() == 1);
175 renameItem(m_items
.first(), m_newName
);
182 void RenameDialog::slotTextChanged(const QString
& newName
)
184 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
185 if (enable
&& !m_renameOneItem
) {
186 const int count
= newName
.count(QLatin1Char('#'));
188 // Renaming multiple files without '#' will only work if all extensions are different.
189 enable
= m_allExtensionsDifferent
;
191 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
192 const int first
= newName
.indexOf(QLatin1Char('#'));
193 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
194 enable
= (last
- first
+ 1 == count
);
197 m_okButton
->setEnabled(enable
);
200 void RenameDialog::renameItems()
202 // Iterate through all items and rename them...
203 int index
= m_spinBox
->value();
204 foreach (const KFileItem
& item
, m_items
) {
205 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
208 const QUrl oldUrl
= item
.url();
210 const QString extension
= db
.suffixForFileName(oldUrl
.path().toLower());
211 if (!extension
.isEmpty()) {
212 newName
.append(QLatin1Char('.'));
213 newName
.append(extension
);
216 if (oldUrl
.fileName() != newName
) {
217 renameItem(item
, newName
);
222 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
224 QString newName
= name
;
226 QString indexString
= QString::number(index
);
228 // Insert leading zeros if necessary
229 const int minIndexLength
= name
.count(indexPlaceHolder
);
230 while (indexString
.length() < minIndexLength
) {
231 indexString
.prepend(QLatin1Char('0'));
234 // Replace the index placeholders by the indexString
235 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
236 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);