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