]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Show the file name as plain text in the rename dialog
[dolphin.git] / src / views / renamedialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz (peter.penz@gmx.at) *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "renamedialog.h"
21
22 #include <KLineEdit>
23 #include <KLocale>
24 #include <konq_operations.h>
25 #include <KStringHandler>
26 #include <knuminput.h>
27 #include <kmimetype.h>
28
29 #include <QHBoxLayout>
30 #include <QLabel>
31 #include <QVBoxLayout>
32
33 /**
34 * Helper function for sorting items with qSort() in
35 * DolphinView::renameSelectedItems().
36 */
37 bool lessThan(const KFileItem& item1, const KFileItem& item2)
38 {
39 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
40 }
41
42 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
43 KDialog(parent),
44 m_renameOneItem(false),
45 m_newName(),
46 m_lineEdit(0),
47 m_items(items),
48 m_spinBox(0)
49 {
50 const QSize minSize = minimumSize();
51 setMinimumSize(QSize(320, minSize.height()));
52
53 const int itemCount = items.count();
54 Q_ASSERT(itemCount >= 1);
55 m_renameOneItem = (itemCount == 1);
56
57 setCaption(m_renameOneItem ?
58 i18nc("@title:window", "Rename Item") :
59 i18nc("@title:window", "Rename Items"));
60 setButtons(Ok | Cancel);
61 setDefaultButton(Ok);
62
63 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
64
65 QWidget* page = new QWidget(this);
66 setMainWidget(page);
67
68 QVBoxLayout* topLayout = new QVBoxLayout(page);
69
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),
74 page);
75 editLabel->setTextFormat(Qt::PlainText);
76 } else {
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),
81 page);
82 }
83
84 m_lineEdit = new KLineEdit(page);
85 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
86
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());
91
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;
96 }
97 } else {
98 // Don't select the # character
99 --selectionLength;
100 }
101
102 m_lineEdit->setText(m_newName);
103 m_lineEdit->setSelection(0, selectionLength);
104 m_lineEdit->setFocus();
105
106 topLayout->addWidget(editLabel);
107 topLayout->addWidget(m_lineEdit);
108
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);
112
113 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
114 horizontalLayout->setMargin(0);
115 horizontalLayout->addWidget(infoLabel);
116 horizontalLayout->addWidget(m_spinBox);
117
118 topLayout->addLayout(horizontalLayout);
119 }
120 }
121
122 RenameDialog::~RenameDialog()
123 {
124 }
125
126 void RenameDialog::slotButtonClicked(int button)
127 {
128 if (button == KDialog::Ok) {
129 m_newName = m_lineEdit->text();
130
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);
137 } else {
138 renameItems();
139 }
140 }
141
142 KDialog::slotButtonClicked(button);
143 }
144
145 void RenameDialog::slotTextChanged(const QString& newName)
146 {
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 minSplitCount = 1;
151 int maxSplitCount = 2;
152 if (newName.startsWith(QLatin1Char('#'))) {
153 --maxSplitCount;
154 }
155 if (newName.endsWith(QLatin1Char('#'))) {
156 --maxSplitCount;
157 }
158 const int splitCount = newName.split(QLatin1Char('#'), QString::SkipEmptyParts).count();
159 enable = enable && (splitCount >= minSplitCount) && (splitCount <= maxSplitCount);
160 }
161 enableButtonOk(enable);
162 }
163
164 void RenameDialog::renameItems()
165 {
166 // Iterate through all items and rename them...
167 int index = m_spinBox->value();
168 foreach (const KFileItem& item, m_items) {
169 QString newName = indexedName(m_newName, index, QLatin1Char('#'));
170 ++index;
171
172 const KUrl oldUrl = item.url();
173 const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
174 if (!extension.isEmpty()) {
175 newName.append(QLatin1Char('.'));
176 newName.append(extension);
177 }
178
179 if (oldUrl.fileName() != newName) {
180 KUrl newUrl = oldUrl;
181 newUrl.setFileName(KIO::encodeFileName(newName));
182 KonqOperations::rename(this, oldUrl, newUrl);
183 }
184 }
185 }
186
187 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
188 {
189 QString newName = name;
190
191 QString indexString = QString::number(index);
192
193 // Insert leading zeros if necessary
194 const int minIndexLength = name.count(indexPlaceHolder);
195 while (indexString.length() < minIndexLength) {
196 indexString.prepend(QLatin1Char('0'));
197 }
198
199 // Replace the index placeholders by the indexString
200 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
201 newName.replace(placeHolderStart, minIndexLength, indexString);
202
203 return newName;
204 }
205
206 #include "renamedialog.moc"