]>
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 <konq_operations.h>
25 #include <KStringHandler>
26 #include <knuminput.h>
27 #include <kmimetype.h>
29 #include <QHBoxLayout>
31 #include <QVBoxLayout>
33 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
35 m_renameOneItem(false),
39 m_allExtensionsDifferent(true),
42 const QSize minSize
= minimumSize();
43 setMinimumSize(QSize(320, minSize
.height()));
45 const int itemCount
= items
.count();
46 Q_ASSERT(itemCount
>= 1);
47 m_renameOneItem
= (itemCount
== 1);
49 setCaption(m_renameOneItem
?
50 i18nc("@title:window", "Rename Item") :
51 i18nc("@title:window", "Rename Items"));
52 setButtons(Ok
| Cancel
);
55 setButtonGuiItem(Ok
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
57 QWidget
* page
= new QWidget(this);
60 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
62 QLabel
* editLabel
= 0;
63 if (m_renameOneItem
) {
64 m_newName
= items
.first().name();
65 editLabel
= new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
67 editLabel
->setTextFormat(Qt::PlainText
);
69 m_newName
= i18nc("@info:status", "New name #");
70 editLabel
= new QLabel(i18ncp("@label:textbox",
71 "Rename the %1 selected item to:",
72 "Rename the %1 selected items to:", itemCount
),
76 m_lineEdit
= new KLineEdit(page
);
77 connect(m_lineEdit
, SIGNAL(textChanged(QString
)), this, SLOT(slotTextChanged(QString
)));
79 int selectionLength
= m_newName
.length();
80 if (m_renameOneItem
) {
81 const QString fileName
= items
.first().url().prettyUrl();
82 const QString extension
= KMimeType::extractKnownExtension(fileName
.toLower());
84 // If the current item is a directory, select the whole file name.
85 if ((extension
.length() > 0) && !items
.first().isDir()) {
86 // Don't select the extension
87 selectionLength
-= extension
.length() + 1;
90 // Don't select the # character
94 m_lineEdit
->setText(m_newName
);
95 m_lineEdit
->setSelection(0, selectionLength
);
96 m_lineEdit
->setFocus();
98 topLayout
->addWidget(editLabel
);
99 topLayout
->addWidget(m_lineEdit
);
101 if (!m_renameOneItem
) {
102 QSet
<QString
> extensions
;
103 foreach (const KFileItem
& item
, m_items
) {
104 const QString extension
= KMimeType::extractKnownExtension(item
.url().prettyUrl().toLower());
106 if (extensions
.contains(extension
)) {
107 m_allExtensionsDifferent
= false;
111 extensions
.insert(extension
);
114 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
115 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
117 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
118 horizontalLayout
->setMargin(0);
119 horizontalLayout
->addWidget(infoLabel
);
120 horizontalLayout
->addWidget(m_spinBox
);
122 topLayout
->addLayout(horizontalLayout
);
126 RenameDialog::~RenameDialog()
130 void RenameDialog::slotButtonClicked(int button
)
132 if (button
== KDialog::Ok
) {
133 m_newName
= m_lineEdit
->text();
135 if (m_renameOneItem
) {
136 Q_ASSERT(m_items
.count() == 1);
137 const KUrl oldUrl
= m_items
.first().url();
138 KUrl newUrl
= oldUrl
;
139 newUrl
.setFileName(KIO::encodeFileName(m_newName
));
141 QWidget
* widget
= parentWidget();
146 KonqOperations::rename(widget
, oldUrl
, newUrl
);
152 KDialog::slotButtonClicked(button
);
155 void RenameDialog::slotTextChanged(const QString
& newName
)
157 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
158 if (enable
&& !m_renameOneItem
) {
159 const int count
= newName
.count(QLatin1Char('#'));
161 // Renaming multiple files without '#' will only work if all extensions are different.
162 enable
= m_allExtensionsDifferent
;
164 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
165 const int first
= newName
.indexOf(QLatin1Char('#'));
166 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
167 enable
= (last
- first
+ 1 == count
);
170 enableButtonOk(enable
);
173 void RenameDialog::renameItems()
175 // Iterate through all items and rename them...
176 int index
= m_spinBox
->value();
177 foreach (const KFileItem
& item
, m_items
) {
178 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
181 const KUrl oldUrl
= item
.url();
182 const QString extension
= KMimeType::extractKnownExtension(oldUrl
.prettyUrl().toLower());
183 if (!extension
.isEmpty()) {
184 newName
.append(QLatin1Char('.'));
185 newName
.append(extension
);
188 if (oldUrl
.fileName() != newName
) {
189 KUrl newUrl
= oldUrl
;
190 newUrl
.setFileName(KIO::encodeFileName(newName
));
192 QWidget
* widget
= parentWidget();
197 KonqOperations::rename(widget
, oldUrl
, newUrl
);
202 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
204 QString newName
= name
;
206 QString indexString
= QString::number(index
);
208 // Insert leading zeros if necessary
209 const int minIndexLength
= name
.count(indexPlaceHolder
);
210 while (indexString
.length() < minIndexLength
) {
211 indexString
.prepend(QLatin1Char('0'));
214 // Replace the index placeholders by the indexString
215 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
216 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);
221 #include "renamedialog.moc"