]>
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>
34 * Helper function for sorting items with qSort() in
35 * DolphinView::renameSelectedItems().
37 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
39 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
42 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
44 m_renameOneItem(false),
50 const QSize minSize
= minimumSize();
51 setMinimumSize(QSize(320, minSize
.height()));
53 const int itemCount
= items
.count();
54 Q_ASSERT(itemCount
>= 1);
55 m_renameOneItem
= (itemCount
== 1);
57 setCaption(m_renameOneItem
?
58 i18nc("@title:window", "Rename Item") :
59 i18nc("@title:window", "Rename Items"));
60 setButtons(Ok
| Cancel
);
63 setButtonGuiItem(Ok
, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
65 QWidget
* page
= new QWidget(this);
68 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
70 QLabel
* editLabel
= 0;
71 if (m_renameOneItem
) {
72 m_newName
= items
.first().name();
73 editLabel
= new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName
),
75 editLabel
->setTextFormat(Qt::PlainText
);
77 m_newName
= i18nc("@info:status", "New name #");
78 editLabel
= new QLabel(i18ncp("@label:textbox",
79 "Rename the %1 selected item to:",
80 "Rename the %1 selected items to:", itemCount
),
84 m_lineEdit
= new KLineEdit(page
);
85 connect(m_lineEdit
, SIGNAL(textChanged(QString
)), this, SLOT(slotTextChanged(QString
)));
87 int selectionLength
= m_newName
.length();
88 if (m_renameOneItem
) {
89 const QString fileName
= items
.first().url().prettyUrl();
90 const QString extension
= KMimeType::extractKnownExtension(fileName
.toLower());
92 // If the current item is a directory, select the whole file name.
93 if ((extension
.length() > 0) && !items
.first().isDir()) {
94 // Don't select the extension
95 selectionLength
-= extension
.length() + 1;
98 // Don't select the # character
102 m_lineEdit
->setText(m_newName
);
103 m_lineEdit
->setSelection(0, selectionLength
);
104 m_lineEdit
->setFocus();
106 topLayout
->addWidget(editLabel
);
107 topLayout
->addWidget(m_lineEdit
);
109 if (!m_renameOneItem
) {
110 QLabel
* infoLabel
= new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page
);
111 m_spinBox
= new KIntSpinBox(0, 10000, 1, 1, page
, 10);
113 QHBoxLayout
* horizontalLayout
= new QHBoxLayout(page
);
114 horizontalLayout
->setMargin(0);
115 horizontalLayout
->addWidget(infoLabel
);
116 horizontalLayout
->addWidget(m_spinBox
);
118 topLayout
->addLayout(horizontalLayout
);
122 RenameDialog::~RenameDialog()
126 void RenameDialog::slotButtonClicked(int button
)
128 if (button
== KDialog::Ok
) {
129 m_newName
= m_lineEdit
->text();
131 if (m_renameOneItem
) {
132 Q_ASSERT(m_items
.count() == 1);
133 const KUrl oldUrl
= m_items
.first().url();
134 KUrl newUrl
= oldUrl
;
135 newUrl
.setFileName(KIO::encodeFileName(m_newName
));
136 KonqOperations::rename(this, oldUrl
, newUrl
);
142 KDialog::slotButtonClicked(button
);
145 void RenameDialog::slotTextChanged(const QString
& newName
)
147 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
148 if (enable
&& !m_renameOneItem
) {
149 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
150 const int count
= newName
.count(QLatin1Char('#'));
151 const int first
= newName
.indexOf(QLatin1Char('#'));
152 const int last
= newName
.lastIndexOf(QLatin1Char('#'));
153 enable
= (last
- first
+ 1 == count
);
155 enableButtonOk(enable
);
158 void RenameDialog::renameItems()
160 // Iterate through all items and rename them...
161 int index
= m_spinBox
->value();
162 foreach (const KFileItem
& item
, m_items
) {
163 QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
166 const KUrl oldUrl
= item
.url();
167 const QString extension
= KMimeType::extractKnownExtension(oldUrl
.prettyUrl().toLower());
168 if (!extension
.isEmpty()) {
169 newName
.append(QLatin1Char('.'));
170 newName
.append(extension
);
173 if (oldUrl
.fileName() != newName
) {
174 KUrl newUrl
= oldUrl
;
175 newUrl
.setFileName(KIO::encodeFileName(newName
));
176 KonqOperations::rename(this, oldUrl
, newUrl
);
181 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
183 QString newName
= name
;
185 QString indexString
= QString::number(index
);
187 // Insert leading zeros if necessary
188 const int minIndexLength
= name
.count(indexPlaceHolder
);
189 while (indexString
.length() < minIndexLength
) {
190 indexString
.prepend(QLatin1Char('0'));
193 // Replace the index placeholders by the indexString
194 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
195 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);
200 #include "renamedialog.moc"