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