]>
cloud.milkyroute.net Git - dolphin.git/blob - src/renamedialog.cpp
def141e047a104bee6d8dc40ee78b761ff9d337c
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
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. *
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. *
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 ***************************************************************************/
20 #include "renamedialog.h"
22 #include <klineedit.h>
25 #include <QtGui/QLabel>
26 #include <QtGui/QBoxLayout>
28 RenameDialog::RenameDialog(const KUrl::List
& items
) :
30 m_renameOneItem(false)
32 const QSize minSize
= minimumSize();
33 setMinimumSize(QSize(320, minSize
.height()));
35 const int itemCount
= items
.count();
36 Q_ASSERT(itemCount
>= 1);
37 m_renameOneItem
= (itemCount
== 1);
39 setCaption(m_renameOneItem
? i18n("Rename Item") : i18n("Rename Items"));
40 setButtons(Ok
| Cancel
);
43 setButtonGuiItem(Ok
, KGuiItem(i18n("Rename"), "dialog-apply"));
45 QWidget
* page
= new QWidget(this);
48 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
49 topLayout
->setMargin(KDialog::marginHint());
51 QLabel
* editLabel
= 0;
52 if (m_renameOneItem
) {
53 const KUrl
& url
= items
.first();
54 m_newName
= url
.fileName();
55 editLabel
= new QLabel(i18n("Rename the item '%1' to:", m_newName
),
58 m_newName
= i18n("New name #");
59 editLabel
= new QLabel(i18n("Rename the %1 selected items to:", itemCount
),
63 m_lineEdit
= new KLineEdit(page
);
64 QString extension
= extensionString(items
[0].prettyUrl());
65 if (extension
.length() > 0) {
66 // The first item seems to have a extension (e. g. '.jpg' or '.txt'). Now
67 // check whether all other items have the same extension. If this is the
68 // case, add this extension to the name suggestion.
69 for (int i
= 1; i
< itemCount
; ++i
) {
70 if (!items
[i
].prettyUrl().contains(extension
)) {
71 // at least one item does not have the same extension
72 extension
.truncate(0);
78 int selectionLength
= m_newName
.length();
79 if (!m_renameOneItem
) {
80 --selectionLength
; // don't select the # character
83 const int extensionLength
= extension
.length();
84 if (extensionLength
> 0) {
85 if (m_renameOneItem
) {
86 selectionLength
-= extensionLength
;
88 m_newName
.append(extension
);
92 m_lineEdit
->setText(m_newName
);
93 m_lineEdit
->setSelection(0, selectionLength
);
94 m_lineEdit
->setFocus();
96 topLayout
->addWidget(editLabel
);
97 topLayout
->addWidget(m_lineEdit
);
99 if (!m_renameOneItem
) {
100 QLabel
* infoLabel
= new QLabel(i18n("(# will be replaced by ascending numbers)"), page
);
101 topLayout
->addWidget(infoLabel
);
105 RenameDialog::~RenameDialog()
108 void RenameDialog::slotButtonClicked(int button
)
111 m_newName
= m_lineEdit
->text();
112 if (m_newName
.isEmpty()) {
113 m_errorString
= i18n("The new name is empty. A name with at least one character must be entered.");
114 } else if (!m_renameOneItem
&& m_newName
.contains('#') != 1) {
115 m_newName
.truncate(0);
116 m_errorString
= i18n("The name must contain exactly one # character.");
120 KDialog::slotButtonClicked(button
);
123 QString
RenameDialog::extensionString(const QString
& name
) const
126 bool foundExtension
= false; // true if at least one valid file extension
127 // like "gif", "txt", ... has been found
129 QStringList strings
= name
.split('.');
130 const int size
= strings
.size();
131 for (int i
= 1; i
< size
; ++i
) {
132 const QString
& str
= strings
.at(i
);
133 if (!foundExtension
) {
134 // Sub strings like "9", "12", "99", ... which contain only
135 // numbers don't count as extension. Usually they are version
136 // numbers like in "cmake-2.4.5".
139 foundExtension
= !ok
;
141 if (foundExtension
) {
142 extension
+= '.' + str
;
149 #include "renamedialog.moc"