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