]> cloud.milkyroute.net Git - dolphin.git/blob - src/renamedialog.cpp
Allow renaming of items (note that currently the "rename multiple files" dialog is...
[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 <QLabel>
26 #include <QVBoxLayout>
27
28 RenameDialog::RenameDialog(const KUrl::List& items) :
29 KDialog()
30 {
31 setCaption(i18n("Rename Items"));
32 setButtons(Ok|Cancel);
33 setDefaultButton(Ok);
34
35 setButtonGuiItem(Ok, KGuiItem(i18n("Rename"), "dialog-apply"));
36
37 QWidget* page = new QWidget(this);
38 setMainWidget(page);
39
40 QVBoxLayout* topLayout = new QVBoxLayout(page);
41 topLayout->setMargin(KDialog::marginHint());
42
43 const int itemCount = items.count();
44 QLabel* editLabel = new QLabel(i18n("Rename the %1 selected items to:", itemCount),
45 page);
46
47 m_lineEdit = new KLineEdit(page);
48 m_newName = i18n("New name #");
49
50 // TODO: reactivate assertion as soon as KFileItemDelegate supports renaming of
51 // single items
52 //Q_ASSERT(itemCount > 1);
53
54 QString postfix(items[0].prettyUrl().section('.',1));
55 if (postfix.length() > 0) {
56 // The first item seems to have a postfix (e. g. 'jpg' or 'txt'). Now
57 // check whether all other items have the same postfix. If this is the
58 // case, add this postfix to the name suggestion.
59 postfix.insert(0, '.');
60 for (int i = 1; i < itemCount; ++i) {
61 if (!items[i].prettyUrl().contains(postfix)) {
62 // at least one item does not have the same postfix
63 postfix.truncate(0);
64 break;
65 }
66 }
67 }
68
69 const int selectionLength = m_newName.length();
70 if (postfix.length() > 0) {
71 m_newName.append(postfix);
72 }
73 m_lineEdit->setText(m_newName);
74 m_lineEdit->setSelection(0, selectionLength - 1);
75 m_lineEdit->setFocus();
76
77 QLabel* infoLabel = new QLabel(i18n("(# will be replaced by ascending numbers)"), page);
78
79 topLayout->addWidget(editLabel);
80 topLayout->addWidget(m_lineEdit);
81 topLayout->addWidget(infoLabel);
82 }
83
84 RenameDialog::~RenameDialog()
85 {
86 }
87
88 void RenameDialog::slotButtonClicked(int button)
89 {
90 if (button==Ok) {
91 m_newName = m_lineEdit->text();
92 if (m_newName.isEmpty()) {
93 m_errorString = i18n("The new name is empty. A name with at least one character must be entered.");
94 }
95 else if (m_newName.contains('#') != 1) {
96 m_newName.truncate(0);
97 m_errorString = i18n("The name must contain exactly one # character.");
98 }
99 }
100
101 KDialog::slotButtonClicked(button);
102 }
103
104 #include "renamedialog.moc"