]>
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"
23 #include <KLocalizedString>
24 #include <KJobWidgets>
25 #include <KJobUiDelegate>
26 #include <KIO/CopyJob>
27 #include <KIO/FileUndoManager>
28 #include <KStringHandler>
29 #include <kstringhandler_deprecated.h> //TODO port to QCollator
30 #include <knuminput.h>
31 #include <kmimetype.h>
33 #include <QHBoxLayout>
35 #include <QVBoxLayout>
36 #include <QMimeDatabase>
37 #include <KConfigGroup>
38 #include <QDialogButtonBox>
39 #include <QPushButton>
42 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
44 m_renameOneItem(false),
48 m_allExtensionsDifferent(true),
51 const QSize minSize
= minimumSize();
52 setMinimumSize(QSize(320, minSize
.height()));
54 const int itemCount
= items
.count();
55 Q_ASSERT(itemCount
>= 1);
56 m_renameOneItem
= (itemCount
== 1);
58 setWindowTitle(m_renameOneItem
?
59 i18nc("@title:window", "Rename Item") :
60 i18nc("@title:window", "Rename Items"));
61 QDialogButtonBox
*buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
|QDialogButtonBox::Cancel
);
62 QVBoxLayout
*mainLayout
= new QVBoxLayout
;
63 setLayout(mainLayout
);
64 m_okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
65 m_okButton
->setDefault(true);
66 m_okButton
->setShortcut(Qt::CTRL
| Qt::Key_Return
);
67 connect(buttonBox
, SIGNAL(accepted()), this, SLOT(slotAccepted()));
68 connect(buttonBox
, SIGNAL(rejected()), this, SLOT(reject()));
69 m_okButton
->setDefault(true);
71 KGuiItem::assign(m_okButton
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
73 QWidget
* page
= new QWidget(this);
74 mainLayout
->addWidget(page
);
75 mainLayout
->addWidget(buttonBox
);
77 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
79 QLabel
* editLabel
= 0;
80 if (m_renameOneItem
) {
81 m_newName
= items
.first().name();
82 editLabel
= new QLabel(xi18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
84 editLabel
->setTextFormat(Qt::PlainText
);
86 m_newName
= i18nc("@info:status", "New name #");
87 editLabel
= new QLabel(i18ncp("@label:textbox",
88 "Rename the %1 selected item to:",
89 "Rename the %1 selected items to:", itemCount
),
93 m_lineEdit
= new KLineEdit(page
);
94 mainLayout
->addWidget(m_lineEdit
);
95 connect(m_lineEdit
, &KLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
97 int selectionLength
= m_newName
.length();
98 if (m_renameOneItem
) {
99 const QString fileName
= items
.first().url().toDisplayString();
101 const QString extension
= db
.suffixForFileName(fileName
.toLower());
103 // If the current item is a directory, select the whole file name.
104 if ((extension
.length() > 0) && !items
.first().isDir()) {
105 // Don't select the extension
106 selectionLength
-= extension
.length() + 1;
109 // Don't select the # character
113 m_lineEdit
->setText(m_newName
);
114 m_lineEdit
->setSelection(0, selectionLength
);
115 m_lineEdit
->setFocus();
117 topLayout
->addWidget(editLabel
);
118 topLayout
->addWidget(m_lineEdit
);
120 if (!m_renameOneItem
) {
121 QSet
<QString
> extensions
;
122 foreach (const KFileItem
& item
, m_items
) {
124 const QString extension
= db
.suffixForFileName(item
.url().toDisplayString().toLower());
126 if (extensions
.contains(extension
)) {
127 m_allExtensionsDifferent
= false;
131 extensions
.insert(extension
);
134 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
135 mainLayout
->addWidget(infoLabel
);
136 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
138 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
139 horizontalLayout
->setMargin(0);
140 horizontalLayout
->addWidget(infoLabel
);
141 horizontalLayout
->addWidget(m_spinBox
);
143 topLayout
->addLayout(horizontalLayout
);
147 RenameDialog::~RenameDialog()
151 void RenameDialog::renameItem(const KFileItem
&item
, const QString
& newName
)
153 const QUrl oldUrl
= item
.url();
154 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
155 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
157 QWidget
* widget
= parentWidget();
162 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
163 KJobWidgets::setWindow(job
, widget
);
164 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, QList
<QUrl
>() << oldUrl
, newUrl
, job
);
165 job
->ui()->setAutoErrorHandlingEnabled(true);
168 void RenameDialog::slotAccepted()
170 m_newName
= m_lineEdit
->text();
172 if (m_renameOneItem
) {
173 Q_ASSERT(m_items
.count() == 1);
174 renameItem(m_items
.first(), m_newName
);
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::renameItems()
201 // Iterate through all items and rename them...
202 int index
= m_spinBox
->value();
203 foreach (const KFileItem
& item
, m_items
) {
204 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
207 const QUrl oldUrl
= item
.url();
209 const QString extension
= db
.suffixForFileName(oldUrl
.path().toLower());
210 if (!extension
.isEmpty()) {
211 newName
.append(QLatin1Char('.'));
212 newName
.append(extension
);
215 if (oldUrl
.fileName() != newName
) {
216 renameItem(item
, newName
);
221 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
223 QString newName
= name
;
225 QString indexString
= QString::number(index
);
227 // Insert leading zeros if necessary
228 const int minIndexLength
= name
.count(indexPlaceHolder
);
229 while (indexString
.length() < minIndexLength
) {
230 indexString
.prepend(QLatin1Char('0'));
233 // Replace the index placeholders by the indexString
234 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
235 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);