]>
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"
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>
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 setCaption(m_renameOneItem
?
55 i18nc("@title:window", "Rename Item") :
56 i18nc("@title:window", "Rename Items"));
57 setButtons(Ok
| Cancel
);
60 setButtonGuiItem(Ok
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
62 QWidget
* page
= new QWidget(this);
65 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
67 QLabel
* editLabel
= 0;
68 if (m_renameOneItem
) {
69 m_newName
= items
.first().name();
70 editLabel
= new QLabel(xi18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
72 editLabel
->setTextFormat(Qt::PlainText
);
74 m_newName
= i18nc("@info:status", "New name #");
75 editLabel
= new QLabel(i18ncp("@label:textbox",
76 "Rename the %1 selected item to:",
77 "Rename the %1 selected items to:", itemCount
),
81 m_lineEdit
= new KLineEdit(page
);
82 connect(m_lineEdit
, &KLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
84 int selectionLength
= m_newName
.length();
85 if (m_renameOneItem
) {
86 const QString fileName
= items
.first().url().toDisplayString();
88 const QString extension
= db
.suffixForFileName(fileName
.toLower());
90 // If the current item is a directory, select the whole file name.
91 if ((extension
.length() > 0) && !items
.first().isDir()) {
92 // Don't select the extension
93 selectionLength
-= extension
.length() + 1;
96 // Don't select the # character
100 m_lineEdit
->setText(m_newName
);
101 m_lineEdit
->setSelection(0, selectionLength
);
102 m_lineEdit
->setFocus();
104 topLayout
->addWidget(editLabel
);
105 topLayout
->addWidget(m_lineEdit
);
107 if (!m_renameOneItem
) {
108 QSet
<QString
> extensions
;
109 foreach (const KFileItem
& item
, m_items
) {
111 const QString extension
= db
.suffixForFileName(item
.url().toDisplayString().toLower());
113 if (extensions
.contains(extension
)) {
114 m_allExtensionsDifferent
= false;
118 extensions
.insert(extension
);
121 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
122 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
124 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
125 horizontalLayout
->setMargin(0);
126 horizontalLayout
->addWidget(infoLabel
);
127 horizontalLayout
->addWidget(m_spinBox
);
129 topLayout
->addLayout(horizontalLayout
);
133 RenameDialog::~RenameDialog()
137 void RenameDialog::renameItem(const KFileItem
&item
, const QString
& newName
)
139 const QUrl oldUrl
= item
.url();
140 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
141 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
143 QWidget
* widget
= parentWidget();
148 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
149 KJobWidgets::setWindow(job
, widget
);
150 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, QList
<QUrl
>() << oldUrl
, newUrl
, job
);
151 job
->ui()->setAutoErrorHandlingEnabled(true);
154 void RenameDialog::slotButtonClicked(int button
)
156 if (button
== KDialog::Ok
) {
157 m_newName
= m_lineEdit
->text();
159 if (m_renameOneItem
) {
160 Q_ASSERT(m_items
.count() == 1);
161 renameItem(m_items
.first(), m_newName
);
167 KDialog::slotButtonClicked(button
);
170 void RenameDialog::slotTextChanged(const QString
& newName
)
172 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
173 if (enable
&& !m_renameOneItem
) {
174 const int count
= newName
.count(QLatin1Char('#'));
176 // Renaming multiple files without '#' will only work if all extensions are different.
177 enable
= m_allExtensionsDifferent
;
179 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
180 const int first
= newName
.indexOf(QLatin1Char('#'));
181 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
182 enable
= (last
- first
+ 1 == count
);
185 enableButtonOk(enable
);
188 void RenameDialog::renameItems()
190 // Iterate through all items and rename them...
191 int index
= m_spinBox
->value();
192 foreach (const KFileItem
& item
, m_items
) {
193 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
196 const QUrl oldUrl
= item
.url();
198 const QString extension
= db
.suffixForFileName(oldUrl
.path().toLower());
199 if (!extension
.isEmpty()) {
200 newName
.append(QLatin1Char('.'));
201 newName
.append(extension
);
204 if (oldUrl
.fileName() != newName
) {
205 renameItem(item
, newName
);
210 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
212 QString newName
= name
;
214 QString indexString
= QString::number(index
);
216 // Insert leading zeros if necessary
217 const int minIndexLength
= name
.count(indexPlaceHolder
);
218 while (indexString
.length() < minIndexLength
) {
219 indexString
.prepend(QLatin1Char('0'));
222 // Replace the index placeholders by the indexString
223 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
224 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);