]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Fix bug 303375 - Dots in directory names treated as file extension.
[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 } else {
76 m_newName = i18nc("@info:status", "New name #");
77 editLabel = new QLabel(i18ncp("@label:textbox",
78 "Rename the %1 selected item to:",
79 "Rename the %1 selected items to:", itemCount),
80 page);
81 }
82
83 m_lineEdit = new KLineEdit(page);
84 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
85
86 int selectionLength = m_newName.length();
87 if (m_renameOneItem) {
88 const QString fileName = items.first().url().prettyUrl();
89 const QString extension = KMimeType::extractKnownExtension(fileName.toLower());
90
91 // If the current item is a directory, select the whole file name.
92 if ((extension.length() > 0) && !items.first().isDir()) {
93 // Don't select the extension
94 selectionLength -= extension.length() + 1;
95 }
96 } else {
97 // Don't select the # character
98 --selectionLength;
99 }
100
101 m_lineEdit->setText(m_newName);
102 m_lineEdit->setSelection(0, selectionLength);
103 m_lineEdit->setFocus();
104
105 topLayout->addWidget(editLabel);
106 topLayout->addWidget(m_lineEdit);
107
108 if (!m_renameOneItem) {
109 QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page);
110 m_spinBox = new KIntSpinBox(0, 10000, 1, 1, page, 10);
111
112 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
113 horizontalLayout->setMargin(0);
114 horizontalLayout->addWidget(infoLabel);
115 horizontalLayout->addWidget(m_spinBox);
116
117 topLayout->addLayout(horizontalLayout);
118 }
119 }
120
121 RenameDialog::~RenameDialog()
122 {
123 }
124
125 void RenameDialog::slotButtonClicked(int button)
126 {
127 if (button == KDialog::Ok) {
128 m_newName = m_lineEdit->text();
129
130 if (m_renameOneItem) {
131 Q_ASSERT(m_items.count() == 1);
132 const KUrl oldUrl = m_items.first().url();
133 KUrl newUrl = oldUrl;
134 newUrl.setFileName(KIO::encodeFileName(m_newName));
135 KonqOperations::rename(this, oldUrl, newUrl);
136 } else {
137 renameItems();
138 }
139 }
140
141 KDialog::slotButtonClicked(button);
142 }
143
144 void RenameDialog::slotTextChanged(const QString& newName)
145 {
146 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
147 if (enable && !m_renameOneItem) {
148 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
149 const int minSplitCount = 1;
150 int maxSplitCount = 2;
151 if (newName.startsWith(QLatin1Char('#'))) {
152 --maxSplitCount;
153 }
154 if (newName.endsWith(QLatin1Char('#'))) {
155 --maxSplitCount;
156 }
157 const int splitCount = newName.split(QLatin1Char('#'), QString::SkipEmptyParts).count();
158 enable = enable && (splitCount >= minSplitCount) && (splitCount <= maxSplitCount);
159 }
160 enableButtonOk(enable);
161 }
162
163 void RenameDialog::renameItems()
164 {
165 // Currently the items are sorted by the selection order, resort
166 // them by the filename. This assures that the new sort order is similar to
167 // the current filename sort order.
168 qSort(m_items.begin(), m_items.end(), lessThan);
169
170 // Iterate through all items and rename them...
171 int index = m_spinBox->value();
172 foreach (const KFileItem& item, m_items) {
173 QString newName = indexedName(m_newName, index, QLatin1Char('#'));
174 ++index;
175
176 const KUrl oldUrl = item.url();
177 const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
178 if (!extension.isEmpty()) {
179 newName.append(QLatin1Char('.'));
180 newName.append(extension);
181 }
182
183 if (oldUrl.fileName() != newName) {
184 KUrl newUrl = oldUrl;
185 newUrl.setFileName(KIO::encodeFileName(newName));
186 KonqOperations::rename(this, oldUrl, newUrl);
187 }
188 }
189 }
190
191 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
192 {
193 QString newName = name;
194
195 QString indexString = QString::number(index);
196
197 // Insert leading zeros if necessary
198 const int minIndexLength = name.count(indexPlaceHolder);
199 while (indexString.length() < minIndexLength) {
200 indexString.prepend(QLatin1Char('0'));
201 }
202
203 // Replace the index placeholders by the indexString
204 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
205 newName.replace(placeHolderStart, minIndexLength, indexString);
206
207 return newName;
208 }
209
210 #include "renamedialog.moc"