]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/renamedialog.cpp
Does not use bitwise operators on booleans. Thanks to dfaure for pointing it out.
[dolphin.git] / src / views / 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 <kfileitem.h>
23 #include <klineedit.h>
24 #include <klocale.h>
25
26 #include <QtGui/QLabel>
27 #include <QtGui/QBoxLayout>
28
29 RenameDialog::RenameDialog(QWidget *parent, const KFileItemList& items) :
30 KDialog(parent),
31 m_renameOneItem(false)
32 {
33 const QSize minSize = minimumSize();
34 setMinimumSize(QSize(320, minSize.height()));
35
36 const int itemCount = items.count();
37 Q_ASSERT(itemCount >= 1);
38 m_renameOneItem = (itemCount == 1);
39
40 setCaption(m_renameOneItem ?
41 i18nc("@title:window", "Rename Item") :
42 i18nc("@title:window", "Rename Items"));
43 setButtons(Ok | Cancel);
44 setDefaultButton(Ok);
45
46 setButtonGuiItem(Ok, KGuiItem(i18nc("@action:button", "&Rename"), "dialog-ok-apply"));
47
48 QWidget* page = new QWidget(this);
49 setMainWidget(page);
50
51 QVBoxLayout* topLayout = new QVBoxLayout(page);
52
53 QLabel* editLabel = 0;
54 if (m_renameOneItem) {
55 m_newName = items.first().name();
56 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
57 page);
58 } else {
59 m_newName = i18nc("@info:status", "New name #");
60 editLabel = new QLabel(i18ncp("@label:textbox",
61 "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 connect(m_lineEdit, SIGNAL(textChanged(QString)), this, SLOT(slotTextChanged(QString)));
68
69 QString fileName = items[0].url().prettyUrl();
70 QString extension = KMimeType::extractKnownExtension(fileName.toLower());
71 if (!extension.isEmpty()) {
72 extension.insert(0, '.');
73 // The first item seems to have a extension (e. g. '.jpg' or '.txt'). Now
74 // check whether all other URLs have the same extension. If this is the
75 // case, add this extension to the name suggestion.
76 for (int i = 1; i < itemCount; ++i) {
77 fileName = items[i].url().prettyUrl().toLower();
78 if (!fileName.endsWith(extension)) {
79 // at least one item does not have the same extension
80 extension.truncate(0);
81 break;
82 }
83 }
84 }
85
86 int selectionLength = m_newName.length();
87 if (!m_renameOneItem) {
88 --selectionLength; // don't select the # character
89 }
90
91 const int extensionLength = extension.length();
92 if (extensionLength > 0) {
93 if (m_renameOneItem) {
94 selectionLength -= extensionLength;
95 } else {
96 m_newName.append(extension);
97 }
98 }
99
100 m_lineEdit->setText(m_newName);
101 m_lineEdit->setSelection(0, selectionLength);
102 m_lineEdit->setFocus();
103
104 topLayout->addWidget(editLabel);
105 topLayout->addWidget(m_lineEdit);
106
107 if (!m_renameOneItem) {
108 QLabel* infoLabel = new QLabel(i18nc("@info", "(# will be replaced by ascending numbers)"), page);
109 topLayout->addWidget(infoLabel);
110 }
111 }
112
113 RenameDialog::~RenameDialog()
114 {
115 }
116
117 void RenameDialog::slotButtonClicked(int button)
118 {
119 if (button == Ok) {
120 m_newName = m_lineEdit->text();
121 if (m_newName.isEmpty()) {
122 m_errorString = i18nc("@info:status",
123 "The new name is empty. A name with at least one character must be entered.");
124 } else if (!m_renameOneItem && (m_newName.count('#') == 0)) {
125 m_newName.truncate(0);
126 m_errorString = i18nc("@info:status", "The name must contain at least one # character.");
127 }
128 }
129
130 KDialog::slotButtonClicked(button);
131 }
132
133 void RenameDialog::slotTextChanged(const QString &newName)
134 {
135 const bool enable = !newName.isEmpty() && (m_renameOneItem ? (newName != m_newName) : newName.contains('#'));
136 enableButtonOk(enable);
137 }
138
139
140 #include "renamedialog.moc"