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