]> cloud.milkyroute.net Git - dolphin.git/blob - src/renamedialog.cpp
KUIT adaptions
[dolphin.git] / src / renamedialog.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 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.h>
23 #include <klocale.h>
24
25 #include <QtGui/QLabel>
26 #include <QtGui/QBoxLayout>
27
28 RenameDialog::RenameDialog(const KUrl::List& items) :
29 KDialog(),
30 m_renameOneItem(false)
31 {
32 const QSize minSize = minimumSize();
33 setMinimumSize(QSize(320, minSize.height()));
34
35 const int itemCount = items.count();
36 Q_ASSERT(itemCount >= 1);
37 m_renameOneItem = (itemCount == 1);
38
39 setCaption(m_renameOneItem ?
40 i18nc("@title:window", "Rename Item") :
41 i18nc("@title:window", "Rename Items"));
42 setButtons(Ok | Cancel);
43 setDefaultButton(Ok);
44
45 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "Rename"), "dialog-apply"));
46
47 QWidget* page = new QWidget(this);
48 setMainWidget(page);
49
50 QVBoxLayout* topLayout = new QVBoxLayout(page);
51 topLayout->setMargin(KDialog::marginHint());
52
53 QLabel* editLabel = 0;
54 if (m_renameOneItem) {
55 const KUrl& url = items.first();
56 m_newName = url.fileName();
57 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
58 page);
59 } else {
60 m_newName = i18nc("@info:status", "New name #");
61 editLabel = new QLabel(i18np("Rename the %1 selected item to:",
62 "Rename the %1 selected items to:", itemCount),
63 page);
64 }
65
66 m_lineEdit = new KLineEdit(page);
67 QString extension = extensionString(items[0].prettyUrl());
68 if (extension.length() > 0) {
69 // The first item seems to have a extension (e. g. '.jpg' or '.txt'). Now
70 // check whether all other items have the same extension. If this is the
71 // case, add this extension to the name suggestion.
72 for (int i = 1; i < itemCount; ++i) {
73 if (!items[i].prettyUrl().contains(extension)) {
74 // at least one item does not have the same extension
75 extension.truncate(0);
76 break;
77 }
78 }
79 }
80
81 int selectionLength = m_newName.length();
82 if (!m_renameOneItem) {
83 --selectionLength; // don't select the # character
84 }
85
86 const int extensionLength = extension.length();
87 if (extensionLength > 0) {
88 if (m_renameOneItem) {
89 selectionLength -= extensionLength;
90 } else {
91 m_newName.append(extension);
92 }
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 QLabel* infoLabel = new QLabel(i18nc("@info", "(# will be replaced by ascending numbers)"), page);
104 topLayout->addWidget(infoLabel);
105 }
106 }
107
108 RenameDialog::~RenameDialog()
109 {}
110
111 void RenameDialog::slotButtonClicked(int button)
112 {
113 if (button == Ok) {
114 m_newName = m_lineEdit->text();
115 if (m_newName.isEmpty()) {
116 m_errorString = i18nc("@info:status",
117 "The new name is empty. A name with at least one character must be entered.");
118 } else if (!m_renameOneItem && m_newName.contains('#') != 1) {
119 m_newName.truncate(0);
120 m_errorString = i18nc("@info:status", "The name must contain exactly one # character.");
121 }
122 }
123
124 KDialog::slotButtonClicked(button);
125 }
126
127 QString RenameDialog::extensionString(const QString& name)
128 {
129 QString extension;
130 bool foundExtension = false; // true if at least one valid file extension
131 // like "gif", "txt", ... has been found
132
133 QStringList strings = name.split('.');
134 const int size = strings.size();
135 for (int i = 1; i < size; ++i) {
136 const QString& str = strings.at(i);
137 if (!foundExtension) {
138 // Sub strings like "9", "12", "99", ... which contain only
139 // numbers don't count as extension. Usually they are version
140 // numbers like in "cmake-2.4.5".
141 bool ok = false;
142 str.toInt(&ok);
143 foundExtension = !ok;
144 }
145 if (foundExtension) {
146 extension += '.' + str;
147 }
148 }
149
150 return extension;
151 }
152
153 #include "renamedialog.moc"