]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Merge branch '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(xi18nc("@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::renameItem(const KFileItem &item, const QString& newName)
132 {
133 const KUrl oldUrl = item.url();
134 KUrl newUrl = oldUrl;
135 newUrl.setFileName(KIO::encodeFileName(newName));
136
137 QWidget* widget = parentWidget();
138 if (!widget) {
139 widget = this;
140 }
141
142 KonqOperations::rename(widget, oldUrl, newUrl);
143 }
144
145 void RenameDialog::slotButtonClicked(int button)
146 {
147 if (button == KDialog::Ok) {
148 m_newName = m_lineEdit->text();
149
150 if (m_renameOneItem) {
151 Q_ASSERT(m_items.count() == 1);
152 renameItem(m_items.first(), m_newName);
153 } else {
154 renameItems();
155 }
156 }
157
158 KDialog::slotButtonClicked(button);
159 }
160
161 void RenameDialog::slotTextChanged(const QString& newName)
162 {
163 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
164 if (enable && !m_renameOneItem) {
165 const int count = newName.count(QLatin1Char('#'));
166 if (count == 0) {
167 // Renaming multiple files without '#' will only work if all extensions are different.
168 enable = m_allExtensionsDifferent;
169 } else {
170 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
171 const int first = newName.indexOf(QLatin1Char('#'));
172 const int last = newName.lastIndexOf(QLatin1Char('#'));
173 enable = (last - first + 1 == count);
174 }
175 }
176 enableButtonOk(enable);
177 }
178
179 void RenameDialog::renameItems()
180 {
181 // Iterate through all items and rename them...
182 int index = m_spinBox->value();
183 foreach (const KFileItem& item, m_items) {
184 QString newName = indexedName(m_newName, index, QLatin1Char('#'));
185 ++index;
186
187 const KUrl oldUrl = item.url();
188 const QString extension = KMimeType::extractKnownExtension(oldUrl.prettyUrl().toLower());
189 if (!extension.isEmpty()) {
190 newName.append(QLatin1Char('.'));
191 newName.append(extension);
192 }
193
194 if (oldUrl.fileName() != newName) {
195 renameItem(item, newName);
196 }
197 }
198 }
199
200 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
201 {
202 QString newName = name;
203
204 QString indexString = QString::number(index);
205
206 // Insert leading zeros if necessary
207 const int minIndexLength = name.count(indexPlaceHolder);
208 while (indexString.length() < minIndexLength) {
209 indexString.prepend(QLatin1Char('0'));
210 }
211
212 // Replace the index placeholders by the indexString
213 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
214 newName.replace(placeHolderStart, minIndexLength, indexString);
215
216 return newName;
217 }
218