]>
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 <kstringhandler_deprecated.h> //TODO port to QCollator
27 #include <knuminput.h>
28 #include <kmimetype.h>
30 #include <QHBoxLayout>
32 #include <QVBoxLayout>
34 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
36 m_renameOneItem(false),
40 m_allExtensionsDifferent(true),
43 const QSize minSize
= minimumSize();
44 setMinimumSize(QSize(320, minSize
.height()));
46 const int itemCount
= items
.count();
47 Q_ASSERT(itemCount
>= 1);
48 m_renameOneItem
= (itemCount
== 1);
50 setCaption(m_renameOneItem
?
51 i18nc("@title:window", "Rename Item") :
52 i18nc("@title:window", "Rename Items"));
53 setButtons(Ok
| Cancel
);
56 setButtonGuiItem(Ok
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
58 QWidget
* page
= new QWidget(this);
61 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
63 QLabel
* editLabel
= 0;
64 if (m_renameOneItem
) {
65 m_newName
= items
.first().name();
66 editLabel
= new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
68 editLabel
->setTextFormat(Qt::PlainText
);
70 m_newName
= i18nc("@info:status", "New name #");
71 editLabel
= new QLabel(i18ncp("@label:textbox",
72 "Rename the %1 selected item to:",
73 "Rename the %1 selected items to:", itemCount
),
77 m_lineEdit
= new KLineEdit(page
);
78 connect(m_lineEdit
, &KLineEdit::textChanged
, this, &RenameDialog::slotTextChanged
);
80 int selectionLength
= m_newName
.length();
81 if (m_renameOneItem
) {
82 const QString fileName
= items
.first().url().toDisplayString();
83 const QString extension
= KMimeType::extractKnownExtension(fileName
.toLower());
85 // If the current item is a directory, select the whole file name.
86 if ((extension
.length() > 0) && !items
.first().isDir()) {
87 // Don't select the extension
88 selectionLength
-= extension
.length() + 1;
91 // Don't select the # character
95 m_lineEdit
->setText(m_newName
);
96 m_lineEdit
->setSelection(0, selectionLength
);
97 m_lineEdit
->setFocus();
99 topLayout
->addWidget(editLabel
);
100 topLayout
->addWidget(m_lineEdit
);
102 if (!m_renameOneItem
) {
103 QSet
<QString
> extensions
;
104 foreach (const KFileItem
& item
, m_items
) {
105 const QString extension
= KMimeType::extractKnownExtension(item
.url().toDisplayString().toLower());
107 if (extensions
.contains(extension
)) {
108 m_allExtensionsDifferent
= false;
112 extensions
.insert(extension
);
115 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
116 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
118 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
119 horizontalLayout
->setMargin(0);
120 horizontalLayout
->addWidget(infoLabel
);
121 horizontalLayout
->addWidget(m_spinBox
);
123 topLayout
->addLayout(horizontalLayout
);
127 RenameDialog::~RenameDialog()
131 void RenameDialog::slotButtonClicked(int button
)
133 if (button
== KDialog::Ok
) {
134 m_newName
= m_lineEdit
->text();
136 if (m_renameOneItem
) {
137 Q_ASSERT(m_items
.count() == 1);
138 const KUrl oldUrl
= m_items
.first().url();
139 KUrl newUrl
= oldUrl
;
140 newUrl
.setFileName(KIO::encodeFileName(m_newName
));
142 QWidget
* widget
= parentWidget();
147 KonqOperations::rename(widget
, oldUrl
, newUrl
);
153 KDialog::slotButtonClicked(button
);
156 void RenameDialog::slotTextChanged(const QString
& newName
)
158 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
159 if (enable
&& !m_renameOneItem
) {
160 const int count
= newName
.count(QLatin1Char('#'));
162 // Renaming multiple files without '#' will only work if all extensions are different.
163 enable
= m_allExtensionsDifferent
;
165 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
166 const int first
= newName
.indexOf(QLatin1Char('#'));
167 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
168 enable
= (last
- first
+ 1 == count
);
171 enableButtonOk(enable
);
174 void RenameDialog::renameItems()
176 // Iterate through all items and rename them...
177 int index
= m_spinBox
->value();
178 foreach (const KFileItem
& item
, m_items
) {
179 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
182 const KUrl oldUrl
= item
.url();
183 const QString extension
= KMimeType::extractKnownExtension(oldUrl
.prettyUrl().toLower());
184 if (!extension
.isEmpty()) {
185 newName
.append(QLatin1Char('.'));
186 newName
.append(extension
);
189 if (oldUrl
.fileName() != newName
) {
190 KUrl newUrl
= oldUrl
;
191 newUrl
.setFileName(KIO::encodeFileName(newName
));
193 QWidget
* widget
= parentWidget();
198 KonqOperations::rename(widget
, oldUrl
, newUrl
);
203 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
205 QString newName
= name
;
207 QString indexString
= QString::number(index
);
209 // Insert leading zeros if necessary
210 const int minIndexLength
= name
.count(indexPlaceHolder
);
211 while (indexString
.length() < minIndexLength
) {
212 indexString
.prepend(QLatin1Char('0'));
215 // Replace the index placeholders by the indexString
216 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
217 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);
222 #include "renamedialog.moc"