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