]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Merge branch 'KDE/4.13'
[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 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
34 KDialog(parent),
35 m_renameOneItem(false),
36 m_newName(),
37 m_lineEdit(0),
38 m_items(items),
39 m_allExtensionsDifferent(true),
40 m_spinBox(0)
41 {
42 const QSize minSize = minimumSize();
43 setMinimumSize(QSize(320, minSize.height()));
44
45 const int itemCount = items.count();
46 Q_ASSERT(itemCount >= 1);
47 m_renameOneItem = (itemCount == 1);
48
49 setCaption(m_renameOneItem ?
50 i18nc("@title:window", "Rename Item") :
51 i18nc("@title:window", "Rename Items"));
52 setButtons(Ok | Cancel);
53 setDefaultButton(Ok);
54
55 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
56
57 QWidget* page = new QWidget(this);
58 setMainWidget(page);
59
60 QVBoxLayout* topLayout = new QVBoxLayout(page);
61
62 QLabel* editLabel = 0;
63 if (m_renameOneItem) {
64 m_newName = items.first().name();
65 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
66 page);
67 editLabel->setTextFormat(Qt::PlainText);
68 } else {
69 m_newName = i18nc("@info:status", "New name #");
70 editLabel = new QLabel(i18ncp("@label:textbox",
71 "Rename the %1 selected item to:",
72 "Rename the %1 selected items to:", itemCount),
73 page);
74 }
75
76 m_lineEdit = new KLineEdit(page);
77 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
78
79 int selectionLength = m_newName.length();
80 if (m_renameOneItem) {
81 const QString fileName = items.first().url().prettyUrl();
82 const QString extension = KMimeType::extractKnownExtension(fileName.toLower());
83
84 // If the current item is a directory, select the whole file name.
85 if ((extension.length() > 0) && !items.first().isDir()) {
86 // Don't select the extension
87 selectionLength -= extension.length() + 1;
88 }
89 } else {
90 // Don't select the # character
91 --selectionLength;
92 }
93
94 m_lineEdit->setText(m_newName);
95 m_lineEdit->setSelection(0, selectionLength);
96 m_lineEdit->setFocus();
97
98 topLayout->addWidget(editLabel);
99 topLayout->addWidget(m_lineEdit);
100
101 if (!m_renameOneItem) {
102 QSet<QString> extensions;
103 foreach (const KFileItem& item, m_items) {
104 const QString extension = KMimeType::extractKnownExtension(item.url().prettyUrl().toLower());
105
106 if (extensions.contains(extension)) {
107 m_allExtensionsDifferent = false;
108 break;
109 }
110
111 extensions.insert(extension);
112 }
113
114 QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page);
115 m_spinBox = new KIntSpinBox(0, 10000, 1, 1, page, 10);
116
117 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
118 horizontalLayout->setMargin(0);
119 horizontalLayout->addWidget(infoLabel);
120 horizontalLayout->addWidget(m_spinBox);
121
122 topLayout->addLayout(horizontalLayout);
123 }
124 }
125
126 RenameDialog::~RenameDialog()
127 {
128 }
129
130 void RenameDialog::slotButtonClicked(int button)
131 {
132 if (button == KDialog::Ok) {
133 m_newName = m_lineEdit->text();
134
135 if (m_renameOneItem) {
136 Q_ASSERT(m_items.count() == 1);
137 const KUrl oldUrl = m_items.first().url();
138 KUrl newUrl = oldUrl;
139 newUrl.setFileName(KIO::encodeFileName(m_newName));
140
141 QWidget* widget = parentWidget();
142 if (!widget) {
143 widget = this;
144 }
145
146 KonqOperations::rename(widget, oldUrl, newUrl);
147 } else {
148 renameItems();
149 }
150 }
151
152 KDialog::slotButtonClicked(button);
153 }
154
155 void RenameDialog::slotTextChanged(const QString& newName)
156 {
157 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
158 if (enable && !m_renameOneItem) {
159 const int count = newName.count(QLatin1Char('#'));
160 if (count == 0) {
161 // Renaming multiple files without '#' will only work if all extensions are different.
162 enable = m_allExtensionsDifferent;
163 } else {
164 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
165 const int first = newName.indexOf(QLatin1Char('#'));
166 const int last = newName.lastIndexOf(QLatin1Char('#'));
167 enable = (last - first + 1 == count);
168 }
169 }
170 enableButtonOk(enable);
171 }
172
173 void RenameDialog::renameItems()
174 {
175 // Iterate through all items and rename them...
176 int index = m_spinBox->value();
177 foreach (const KFileItem& item, m_items) {
178 QString newName = indexedName(m_newName, index, QLatin1Char('#'));
179 ++index;
180
181 const KUrl oldUrl = item.url();
182 const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
183 if (!extension.isEmpty()) {
184 newName.append(QLatin1Char('.'));
185 newName.append(extension);
186 }
187
188 if (oldUrl.fileName() != newName) {
189 KUrl newUrl = oldUrl;
190 newUrl.setFileName(KIO::encodeFileName(newName));
191
192 QWidget* widget = parentWidget();
193 if (!widget) {
194 widget = this;
195 }
196
197 KonqOperations::rename(widget, oldUrl, newUrl);
198 }
199 }
200 }
201
202 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
203 {
204 QString newName = name;
205
206 QString indexString = QString::number(index);
207
208 // Insert leading zeros if necessary
209 const int minIndexLength = name.count(indexPlaceHolder);
210 while (indexString.length() < minIndexLength) {
211 indexString.prepend(QLatin1Char('0'));
212 }
213
214 // Replace the index placeholders by the indexString
215 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
216 newName.replace(placeHolderStart, minIndexLength, indexString);
217
218 return newName;
219 }
220
221 #include "renamedialog.moc"