]>
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>
28 #include <QVBoxLayout>
31 * Helper function for sorting items with qSort() in
32 * DolphinView::renameSelectedItems().
34 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
36 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
39 RenameDialog::RenameDialog(QWidget
*parent
, const KFileItemList
& items
) :
41 m_renameOneItem(false),
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(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:",
70 KStringHandler::csqueeze(m_newName
)), page
);
71 if (m_newName
.size() > 40) {
72 editLabel
->setToolTip(m_newName
); // Set the filename as a the tool tip...
75 m_newName
= i18nc("@info:status", "New name #");
76 editLabel
= new QLabel(i18ncp("@label:textbox",
77 "Rename the %1 selected item to:",
78 "Rename the %1 selected items to:", itemCount
),
82 m_lineEdit
= new KLineEdit(page
);
83 connect(m_lineEdit
, SIGNAL(textChanged(QString
)), this, SLOT(slotTextChanged(QString
)));
85 QString fileName
= items
[0].url().prettyUrl();
86 QString extension
= KMimeType::extractKnownExtension(fileName
.toLower());
87 if (!extension
.isEmpty()) {
88 extension
.insert(0, '.');
89 // The first item seems to have a extension (e. g. '.jpg' or '.txt'). Now
90 // check whether all other URLs have the same extension. If this is the
91 // case, add this extension to the name suggestion.
92 for (int i
= 1; i
< itemCount
; ++i
) {
93 fileName
= items
[i
].url().prettyUrl().toLower();
94 if (!fileName
.endsWith(extension
)) {
95 // at least one item does not have the same extension
96 extension
.truncate(0);
102 int selectionLength
= m_newName
.length();
103 if (!m_renameOneItem
) {
104 --selectionLength
; // don't select the # character
107 const int extensionLength
= extension
.length();
108 if (extensionLength
> 0) {
109 if (m_renameOneItem
) {
110 selectionLength
-= extensionLength
;
112 m_newName
.append(extension
);
116 m_lineEdit
->setText(m_newName
);
117 m_lineEdit
->setSelection(0, selectionLength
);
118 m_lineEdit
->setFocus();
120 topLayout
->addWidget(editLabel
);
121 topLayout
->addWidget(m_lineEdit
);
123 if (!m_renameOneItem
) {
124 QLabel
* infoLabel
= new QLabel(i18nc("@info", "(# will be replaced by ascending numbers)"), page
);
125 topLayout
->addWidget(infoLabel
);
129 RenameDialog::~RenameDialog()
133 void RenameDialog::slotButtonClicked(int button
)
135 if (button
== KDialog::Ok
) {
136 m_newName
= m_lineEdit
->text();
138 if (m_renameOneItem
) {
139 Q_ASSERT(m_items
.count() == 1);
140 const KUrl oldUrl
= m_items
.first().url();
141 KUrl newUrl
= oldUrl
;
142 newUrl
.setFileName(KIO::encodeFileName(m_newName
));
143 KonqOperations::rename(this, oldUrl
, newUrl
);
149 KDialog::slotButtonClicked(button
);
152 void RenameDialog::slotTextChanged(const QString
& newName
)
154 bool enable
= !newName
.isEmpty() && (newName
!= QLatin1String("..")) && (newName
!= QLatin1String("."));
156 if (m_renameOneItem
) {
157 enable
= enable
&& (newName
!= m_newName
);
159 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
160 const int minSplitCount
= 1;
161 int maxSplitCount
= 2;
162 if (newName
.startsWith(QLatin1Char('#'))) {
165 if (newName
.endsWith(QLatin1Char('#'))) {
168 const int splitCount
= newName
.split(QLatin1Char('#'), QString::SkipEmptyParts
).count();
169 enable
= enable
&& (splitCount
>= minSplitCount
) && (splitCount
<= maxSplitCount
);
172 enableButtonOk(enable
);
175 void RenameDialog::renameItems()
177 // Currently the items are sorted by the selection order, resort
178 // them by the filename. This assures that the new sort order is similar to
179 // the current filename sort order.
180 qSort(m_items
.begin(), m_items
.end(), lessThan
);
182 // Iterate through all items and rename them...
184 foreach (const KFileItem
& item
, m_items
) {
185 const QString newName
= indexedName(m_newName
, index
, QLatin1Char('#'));
188 const KUrl oldUrl
= item
.url();
189 if (oldUrl
.fileName() != newName
) {
190 KUrl newUrl
= oldUrl
;
191 newUrl
.setFileName(KIO::encodeFileName(newName
));
192 KonqOperations::rename(this, oldUrl
, newUrl
);
197 QString
RenameDialog::indexedName(const QString
& name
, int index
, const QChar
& indexPlaceHolder
)
199 QString newName
= name
;
201 QString indexString
= QString::number(index
);
203 // Insert leading zeros if necessary
204 const int minIndexLength
= name
.count(indexPlaceHolder
);
205 while (indexString
.length() < minIndexLength
) {
206 indexString
.prepend(QLatin1Char('0'));
209 // Replace the index placeholders by the indexString
210 const int placeHolderStart
= newName
.indexOf(indexPlaceHolder
);
211 newName
.replace(placeHolderStart
, minIndexLength
, indexString
);
216 #include "renamedialog.moc"