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