]>
cloud.milkyroute.net Git - dolphin.git/blob - src/renamedialog.cpp
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
?
40 i18nc("@title:window", "Rename Item") :
41 i18nc("@title:window", "Rename Items"));
42 setButtons(Ok
| Cancel
);
45 setButtonGuiItem(Ok
, KGuiItem(i18nc("@action:button", "Rename"), "dialog-apply"));
47 QWidget
* page
= new QWidget(this);
50 QVBoxLayout
* topLayout
= new QVBoxLayout(page
);
51 topLayout
->setMargin(KDialog::marginHint());
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
),
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
),
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);
81 int selectionLength
= m_newName
.length();
82 if (!m_renameOneItem
) {
83 --selectionLength
; // don't select the # character
86 const int extensionLength
= extension
.length();
87 if (extensionLength
> 0) {
88 if (m_renameOneItem
) {
89 selectionLength
-= extensionLength
;
91 m_newName
.append(extension
);
95 m_lineEdit
->setText(m_newName
);
96 m_lineEdit
->setSelection(0, selectionLength
);
97 m_lineEdit
->setFocus();
99 topLayout
->addWidget(editLabel
);
100 topLayout
->addWidget(m_lineEdit
);
102 if (!m_renameOneItem
) {
103 QLabel
* infoLabel
= new QLabel(i18nc("@info", "(# will be replaced by ascending numbers)"), page
);
104 topLayout
->addWidget(infoLabel
);
108 RenameDialog::~RenameDialog()
111 void RenameDialog::slotButtonClicked(int button
)
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.");
124 KDialog::slotButtonClicked(button
);
127 QString
RenameDialog::extensionString(const QString
& name
)
130 bool foundExtension
= false; // true if at least one valid file extension
131 // like "gif", "txt", ... has been found
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".
143 foundExtension
= !ok
;
145 if (foundExtension
) {
146 extension
+= '.' + str
;
153 #include "renamedialog.moc"