]>
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>
37 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
39 m_renameOneItem(false),
43 m_allExtensionsDifferent(true),
46 const QSize minSize
= minimumSize();
47 setMinimumSize(QSize(320, minSize
.height()));
49 const int itemCount
= items
.count();
50 Q_ASSERT(itemCount
>= 1);
51 m_renameOneItem
= (itemCount
== 1);
53 setCaption(m_renameOneItem
?
54 i18nc("@title:window", "Rename Item") :
55 i18nc("@title:window", "Rename Items"));
56 setButtons(Ok
| Cancel
);
59 setButtonGuiItem(Ok
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
61 QWidget
* page
= new QWidget(this);
64 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
66 QLabel
* editLabel
= 0;
67 if (m_renameOneItem
) {
68 m_newName
= items
.first().name();
69 editLabel
= new QLabel(xi18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
71 editLabel
->setTextFormat(Qt::PlainText
);
73 m_newName
= i18nc("@info:status", "New name #");
74 editLabel
= new QLabel(i18ncp("@label:textbox",
75 "Rename the %1 selected item to:",
76 "Rename the %1 selected items to:", itemCount
),
80 m_lineEdit
= new KLineEdit(page
);
81 connect(m_lineEdit
, &KLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
83 int selectionLength
= m_newName
.length();
84 if (m_renameOneItem
) {
85 const QString fileName
= items
.first().url().toDisplayString();
86 const QString extension
= KMimeType::extractKnownExtension(fileName
.toLower());
88 // If the current item is a directory, select the whole file name.
89 if ((extension
.length() > 0) && !items
.first().isDir()) {
90 // Don't select the extension
91 selectionLength
-= extension
.length() + 1;
94 // Don't select the # character
98 m_lineEdit
->setText(m_newName
);
99 m_lineEdit
->setSelection(0, selectionLength
);
100 m_lineEdit
->setFocus();
102 topLayout
->addWidget(editLabel
);
103 topLayout
->addWidget(m_lineEdit
);
105 if (!m_renameOneItem
) {
106 QSet
<QString
> extensions
;
107 foreach (const KFileItem
& item
, m_items
) {
108 const QString extension
= KMimeType::extractKnownExtension(item
.url().toDisplayString().toLower());
110 if (extensions
.contains(extension
)) {
111 m_allExtensionsDifferent
= false;
115 extensions
.insert(extension
);
118 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
119 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
121 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
122 horizontalLayout
->setMargin(0);
123 horizontalLayout
->addWidget(infoLabel
);
124 horizontalLayout
->addWidget(m_spinBox
);
126 topLayout
->addLayout(horizontalLayout
);
130 RenameDialog::~RenameDialog()
134 void RenameDialog::renameItem(const KFileItem
&item
, const QString
& newName
)
136 const QUrl oldUrl
= item
.url();
137 QUrl newUrl
= oldUrl
.adjusted(QUrl::RemoveFilename
);
138 newUrl
.setPath(newUrl
.path() + KIO::encodeFileName(newName
));
140 QWidget
* widget
= parentWidget();
145 KIO::Job
* job
= KIO::moveAs(oldUrl
, newUrl
);
146 KJobWidgets::setWindow(job
, widget
);
147 KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Rename
, QList
<QUrl
>() << oldUrl
, newUrl
, job
);
148 job
->ui()->setAutoErrorHandlingEnabled(true);
151 void RenameDialog::slotButtonClicked(int button
)
153 if (button
== KDialog::Ok
) {
154 m_newName
= m_lineEdit
->text();
156 if (m_renameOneItem
) {
157 Q_ASSERT(m_items
.count() == 1);
158 renameItem(m_items
.first(), m_newName
);
164 KDialog::slotButtonClicked(button
);
167 void RenameDialog::slotTextChanged(const QString
& newName
)
169 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
170 if (enable
&& !m_renameOneItem
) {
171 const int count
= newName
.count(QLatin1Char('#'));
173 // Renaming multiple files without '#' will only work if all extensions are different.
174 enable
= m_allExtensionsDifferent
;
176 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
177 const int first
= newName
.indexOf(QLatin1Char('#'));
178 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
179 enable
= (last
- first
+ 1 == count
);
182 enableButtonOk(enable
);
185 void RenameDialog::renameItems()
187 // Iterate through all items and rename them...
188 int index
= m_spinBox
->value();
189 foreach (const KFileItem
& item
, m_items
) {
190 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
193 const QUrl oldUrl
= item
.url();
194 const QString extension
= KMimeType::extractKnownExtension(oldUrl
.path().toLower());
195 if (!extension
.isEmpty()) {
196 newName
.append(QLatin1Char('.'));
197 newName
.append(extension
);
200 if (oldUrl
.fileName() != newName
) {
201 renameItem(item
, newName
);
206 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
208 QString newName
= name
;
210 QString indexString
= QString::number(index
);
212 // Insert leading zeros if necessary
213 const int minIndexLength
= name
.count(indexPlaceHolder
);
214 while (indexString
.length() < minIndexLength
) {
215 indexString
.prepend(QLatin1Char('0'));
218 // Replace the index placeholders by the indexString
219 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
220 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);