]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Renamed some signals for consistency with KItemViews classes
[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 <knuminput.h>
27
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include <QVBoxLayout>
31
32 /**
33 * Helper function for sorting items with qSort() in
34 * DolphinView::renameSelectedItems().
35 */
36 bool lessThan(const KFileItem& item1, const KFileItem& item2)
37 {
38 return KStringHandler::naturalCompare(item1.name(), item2.name()) < 0;
39 }
40
41 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
42 KDialog(parent),
43 m_renameOneItem(false),
44 m_newName(),
45 m_lineEdit(0),
46 m_items(items),
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 setCaption(m_renameOneItem ?
57 i18nc("@title:window", "Rename Item") :
58 i18nc("@title:window", "Rename Items"));
59 setButtons(Ok | Cancel);
60 setDefaultButton(Ok);
61
62 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
63
64 QWidget* page = new QWidget(this);
65 setMainWidget(page);
66
67 QVBoxLayout* topLayout = new QVBoxLayout(page);
68
69 QLabel* editLabel = 0;
70 if (m_renameOneItem) {
71 m_newName = items.first().name();
72 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
73 page);
74 } else {
75 m_newName = i18nc("@info:status", "New name #");
76 editLabel = new QLabel(i18ncp("@label:textbox",
77 "Rename the %1 selected item to:",
78 "Rename the %1 selected items to:", itemCount),
79 page);
80 }
81
82 m_lineEdit = new KLineEdit(page);
83 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
84
85 QString fileName = items[0].url().prettyUrl();
86 QString extension = KMimeType::extractKnownExtension(fileName.toLower());
87 if (!extension.isEmpty()) {
88 extension.insert(0, '.');
89 // The first item seems to have a extension (e. g. '.jpg' or '.txt'). Now
90 // check whether all other URLs have the same extension. If this is the
91 // case, add this extension to the name suggestion.
92 for (int i = 1; i < itemCount; ++i) {
93 fileName = items[i].url().prettyUrl().toLower();
94 if (!fileName.endsWith(extension)) {
95 // at least one item does not have the same extension
96 extension.truncate(0);
97 break;
98 }
99 }
100 }
101
102 int selectionLength = m_newName.length();
103 if (!m_renameOneItem) {
104 --selectionLength; // don't select the # character
105 }
106
107 const int extensionLength = extension.length();
108 if (extensionLength > 0) {
109 if (m_renameOneItem) {
110 selectionLength -= extensionLength;
111 } else {
112 m_newName.append(extension);
113 }
114 }
115
116 m_lineEdit->setText(m_newName);
117 m_lineEdit->setSelection(0, selectionLength);
118 m_lineEdit->setFocus();
119
120 topLayout->addWidget(editLabel);
121 topLayout->addWidget(m_lineEdit);
122
123 if (!m_renameOneItem) {
124 QLabel* infoLabel = new QLabel(i18nc("@info", "# will be replaced by ascending numbers starting with:"), page);
125 m_spinBox = new KIntSpinBox(0, 10000, 1, 1, page, 10);
126
127 QHBoxLayout* horizontalLayout = new QHBoxLayout(page);
128 horizontalLayout->setMargin(0);
129 horizontalLayout->addWidget(infoLabel);
130 horizontalLayout->addWidget(m_spinBox);
131
132 topLayout->addLayout(horizontalLayout);
133 }
134 }
135
136 RenameDialog::~RenameDialog()
137 {
138 }
139
140 void RenameDialog::slotButtonClicked(int button)
141 {
142 if (button == KDialog::Ok) {
143 m_newName = m_lineEdit->text();
144
145 if (m_renameOneItem) {
146 Q_ASSERT(m_items.count() == 1);
147 const KUrl oldUrl = m_items.first().url();
148 KUrl newUrl = oldUrl;
149 newUrl.setFileName(KIO::encodeFileName(m_newName));
150 KonqOperations::rename(this, oldUrl, newUrl);
151 } else {
152 renameItems();
153 }
154 }
155
156 KDialog::slotButtonClicked(button);
157 }
158
159 void RenameDialog::slotTextChanged(const QString& newName)
160 {
161 bool enable = !newName.isEmpty() && (newName != QLatin1String("..")) && (newName != QLatin1String("."));
162 if (enable) {
163 if (m_renameOneItem) {
164 enable = enable && (newName != m_newName);
165 } else {
166 // Assure that the new name contains exactly one # (or a connected sequence of #'s)
167 const int minSplitCount = 1;
168 int maxSplitCount = 2;
169 if (newName.startsWith(QLatin1Char('#'))) {
170 --maxSplitCount;
171 }
172 if (newName.endsWith(QLatin1Char('#'))) {
173 --maxSplitCount;
174 }
175 const int splitCount = newName.split(QLatin1Char('#'), QString::SkipEmptyParts).count();
176 enable = enable && (splitCount >= minSplitCount) && (splitCount <= maxSplitCount);
177 }
178 }
179 enableButtonOk(enable);
180 }
181
182 void RenameDialog::renameItems()
183 {
184 // Currently the items are sorted by the selection order, resort
185 // them by the filename. This assures that the new sort order is similar to
186 // the current filename sort order.
187 qSort(m_items.begin(), m_items.end(), lessThan);
188
189 // Iterate through all items and rename them...
190 int index = m_spinBox->value();
191 foreach (const KFileItem& item, m_items) {
192 const QString newName = indexedName(m_newName, index, QLatin1Char('#'));
193 ++index;
194
195 const KUrl oldUrl = item.url();
196 if (oldUrl.fileName() != newName) {
197 KUrl newUrl = oldUrl;
198 newUrl.setFileName(KIO::encodeFileName(newName));
199 KonqOperations::rename(this, oldUrl, newUrl);
200 }
201 }
202 }
203
204 QString RenameDialog::indexedName(const QString& name, int index, const QChar& indexPlaceHolder)
205 {
206 QString newName = name;
207
208 QString indexString = QString::number(index);
209
210 // Insert leading zeros if necessary
211 const int minIndexLength = name.count(indexPlaceHolder);
212 while (indexString.length() < minIndexLength) {
213 indexString.prepend(QLatin1Char('0'));
214 }
215
216 // Replace the index placeholders by the indexString
217 const int placeHolderStart = newName.indexOf(indexPlaceHolder);
218 newName.replace(placeHolderStart, minIndexLength, indexString);
219
220 return newName;
221 }
222
223 #include "renamedialog.moc"