]>
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 <KIO/CopyJob>
26 #include <KIO/FileUndoManager>
27 #include <kstringhandler_deprecated.h> //TODO port to QCollator
28 #include <knuminput.h>
29 #include <kmimetype.h>
30 #include <KJobUiDelegate>
32 #include <QHBoxLayout>
34 #include <QVBoxLayout>
35 #include <QMimeDatabase>
36 #include <QDialogButtonBox>
37 #include <QPushButton>
40 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
42 m_renameOneItem(false),
46 m_allExtensionsDifferent(true),
49 const QSize minSize
= minimumSize();
50 setMinimumSize(QSize(320, minSize
.height()));
52 const int itemCount
= items
.count();
53 Q_ASSERT(itemCount
>= 1);
54 m_renameOneItem
= (itemCount
== 1);
56 setWindowTitle(m_renameOneItem
?
57 i18nc("@title:window", "Rename Item") :
58 i18nc("@title:window", "Rename Items"));
59 QDialogButtonBox
*buttonBox
= new QDialogButtonBox(QDialogButtonBox::Ok
|QDialogButtonBox::Cancel
);
60 QVBoxLayout
*mainLayout
= new QVBoxLayout
;
61 setLayout(mainLayout
);
62 m_okButton
= buttonBox
->button(QDialogButtonBox::Ok
);
63 m_okButton
->setDefault(true);
64 m_okButton
->setShortcut(Qt::CTRL
| Qt::Key_Return
);
65 connect(buttonBox
, SIGNAL(accepted()), this, SLOT(slotAccepted()));
66 connect(buttonBox
, SIGNAL(rejected()), this, SLOT(reject()));
67 m_okButton
->setDefault(true);
69 KGuiItem::assign(m_okButton
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
71 QWidget
* page
= new QWidget(this);
72 mainLayout
->addWidget(page
);
73 mainLayout
->addWidget(buttonBox
);
75 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
77 QLabel
* editLabel
= 0;
78 if (m_renameOneItem
) {
79 m_newName
= items
.first().name();
80 editLabel
= new QLabel(xi18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
82 editLabel
->setTextFormat(Qt::PlainText
);
84 m_newName
= i18nc("@info:status", "New name #");
85 editLabel
= new QLabel(i18ncp("@label:textbox",
86 "Rename the %1 selected item to:",
87 "Rename the %1 selected items to:", itemCount
),
91 m_lineEdit
= new KLineEdit(page
);
92 mainLayout
->addWidget(m_lineEdit
);
93 connect(m_lineEdit
, &KLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
95 int selectionLength
= m_newName
.length();
96 if (m_renameOneItem
) {
97 const QString fileName
= items
.first().url().toDisplayString();
99 const QString extension
= db
.suffixForFileName(fileName
.toLower());
101 // If the current item is a directory, select the whole file name.
102 if ((extension
.length() > 0) && !items
.first().isDir()) {
103 // Don't select the extension
104 selectionLength
-= extension
.length() + 1;
107 // Don't select the # character
111 m_lineEdit
->setText(m_newName
);
112 m_lineEdit
->setSelection(0, selectionLength
);
113 m_lineEdit
->setFocus();
115 topLayout
->addWidget(editLabel
);
116 topLayout
->addWidget(m_lineEdit
);
118 if (!m_renameOneItem
) {
119 QSet
<QString
> extensions
;
120 foreach (const KFileItem
& item
, m_items
) {
122 const QString extension
= db
.suffixForFileName(item
.url().toDisplayString().toLower());
124 if (extensions
.contains(extension
)) {
125 m_allExtensionsDifferent
= false;
129 extensions
.insert(extension
);
132 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
133 mainLayout
->addWidget(infoLabel
);
134 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
136 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
137 horizontalLayout
->setMargin(0);
138 horizontalLayout
->addWidget(infoLabel
);
139 horizontalLayout
->addWidget(m_spinBox
);
141 topLayout
->addLayout(horizontalLayout
);
145 RenameDialog::~RenameDialog()
149 void RenameDialog::renameItem(const KFileItem
&item
, const QString
& newName
)
151 const QUrl oldUrl
= item
.url();
152 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
153 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
155 QWidget
* widget
= parentWidget();
160 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
161 KJobWidgets::setWindow(job
, widget
);
162 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, QList
<QUrl
>() << oldUrl
, newUrl
, job
);
163 job
->ui()->setAutoErrorHandlingEnabled(true);
166 void RenameDialog::slotAccepted()
168 m_newName
= m_lineEdit
->text();
170 if (m_renameOneItem
) {
171 Q_ASSERT(m_items
.count() == 1);
172 renameItem(m_items
.first(), m_newName
);
179 void RenameDialog::slotTextChanged(const QString
& newName
)
181 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
182 if (enable
&& !m_renameOneItem
) {
183 const int count
= newName
.count(QLatin1Char('#'));
185 // Renaming multiple files without '#' will only work if all extensions are different.
186 enable
= m_allExtensionsDifferent
;
188 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
189 const int first
= newName
.indexOf(QLatin1Char('#'));
190 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
191 enable
= (last
- first
+ 1 == count
);
194 m_okButton
->setEnabled(enable
);
197 void RenameDialog::renameItems()
199 // Iterate through all items and rename them...
200 int index
= m_spinBox
->value();
201 foreach (const KFileItem
& item
, m_items
) {
202 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
205 const QUrl oldUrl
= item
.url();
207 const QString extension
= db
.suffixForFileName(oldUrl
.path().toLower());
208 if (!extension
.isEmpty()) {
209 newName
.append(QLatin1Char('.'));
210 newName
.append(extension
);
213 if (oldUrl
.fileName() != newName
) {
214 renameItem(item
, newName
);
219 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
221 QString newName
= name
;
223 QString indexString
= QString::number(index
);
225 // Insert leading zeros if necessary
226 const int minIndexLength
= name
.count(indexPlaceHolder
);
227 while (indexString
.length() < minIndexLength
) {
228 indexString
.prepend(QLatin1Char('0'));
231 // Replace the index placeholders by the indexString
232 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
233 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);