]> cloud.milkyroute.net Git - dolphin.git/blob - src/renamedialog.cpp
SVN_SILENT made messages (.desktop file)
[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 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 ? i18n("Rename Item") : i18n("Rename Items"));
40 setButtons(Ok|Cancel);
41 setDefaultButton(Ok);
42
43 setButtonGuiItem(Ok, KGuiItem(i18n("Rename"), "dialog-apply"));
44
45 QWidget* page = new QWidget(this);
46 setMainWidget(page);
47
48 QVBoxLayout* topLayout = new QVBoxLayout(page);
49 topLayout->setMargin(KDialog::marginHint());
50
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),
56 page);
57 }
58 else {
59 m_newName = i18n("New name #");
60 editLabel = new QLabel(i18n("Rename the %1 selected items to:", itemCount),
61 page);
62 }
63
64 m_lineEdit = new KLineEdit(page);
65 QString postfix(items[0].prettyUrl().section('.',1));
66 if (postfix.length() > 0) {
67 // The first item seems to have a postfix (e. g. 'jpg' or 'txt'). Now
68 // check whether all other items have the same postfix. If this is the
69 // case, add this postfix to the name suggestion.
70 postfix.insert(0, '.');
71 for (int i = 1; i < itemCount; ++i) {
72 if (!items[i].prettyUrl().contains(postfix)) {
73 // at least one item does not have the same postfix
74 postfix.truncate(0);
75 break;
76 }
77 }
78 }
79
80 int selectionLength = m_newName.length();
81 if (!m_renameOneItem) {
82 --selectionLength; // don't select the # character
83 }
84
85 const int postfixLength = postfix.length();
86 if (postfixLength > 0) {
87 if (m_renameOneItem) {
88 selectionLength -= postfixLength;
89 }
90 else {
91 m_newName.append(postfix);
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(i18n("(# will be replaced by ascending numbers)"), page);
104 topLayout->addWidget(infoLabel);
105 }
106 }
107
108 RenameDialog::~RenameDialog()
109 {
110 }
111
112 void RenameDialog::slotButtonClicked(int button)
113 {
114 if (button == Ok) {
115 m_newName = m_lineEdit->text();
116 if (m_newName.isEmpty()) {
117 m_errorString = i18n("The new name is empty. A name with at least one character must be entered.");
118 }
119 else if (!m_renameOneItem && m_newName.contains('#') != 1) {
120 m_newName.truncate(0);
121 m_errorString = i18n("The name must contain exactly one # character.");
122 }
123 }
124
125 KDialog::slotButtonClicked(button);
126 }
127
128 #include "renamedialog.moc"