]> cloud.milkyroute.net Git - dolphin.git/blob - src/renamedialog.cpp
Factorize all the view-related action handling to DolphinViewActionHandler, to remove...
[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 <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 topLayout->setMargin(KDialog::marginHint());
53
54 QLabel* editLabel = 0;
55 if (m_renameOneItem) {
56 m_newName = items.first().name();
57 editLabel = new QLabel(i18nc("@label:textbox", "Rename the item <filename>%1</filename> to:", m_newName),
58 page);
59 } else {
60 m_newName = i18nc("@info:status", "New name #");
61 editLabel = new QLabel(i18ncp("@label:textbox",
62 "Rename the %1 selected item to:",
63 "Rename the %1 selected items to:", itemCount),
64 page);
65 }
66
67 m_lineEdit = new KLineEdit(page);
68 QString extension = extensionString(items[0].url().prettyUrl());
69 if (extension.length() > 0) {
70 // The first item seems to have a extension (e. g. '.jpg' or '.txt'). Now
71 // check whether all other URLs have the same extension. If this is the
72 // case, add this extension to the name suggestion.
73 for (int i = 1; i < itemCount; ++i) {
74 if (!items[i].url().prettyUrl().contains(extension)) {
75 // at least one item does not have the same extension
76 extension.truncate(0);
77 break;
78 }
79 }
80 }
81
82 int selectionLength = m_newName.length();
83 if (!m_renameOneItem) {
84 --selectionLength; // don't select the # character
85 }
86
87 const int extensionLength = extension.length();
88 if (extensionLength > 0) {
89 if (m_renameOneItem) {
90 selectionLength -= extensionLength;
91 } else {
92 m_newName.append(extension);
93 }
94 }
95
96 m_lineEdit->setText(m_newName);
97 m_lineEdit->setSelection(0, selectionLength);
98 m_lineEdit->setFocus();
99
100 topLayout->addWidget(editLabel);
101 topLayout->addWidget(m_lineEdit);
102
103 if (!m_renameOneItem) {
104 QLabel* infoLabel = new QLabel(i18nc("@info", "(# will be replaced by ascending numbers)"), page);
105 topLayout->addWidget(infoLabel);
106 }
107 }
108
109 RenameDialog::~RenameDialog()
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 = i18nc("@info:status",
118 "The new name is empty. A name with at least one character must be entered.");
119 } else if (!m_renameOneItem && m_newName.count('#') != 1) {
120 m_newName.truncate(0);
121 m_errorString = i18nc("@info:status", "The name must contain exactly one # character.");
122 }
123 }
124
125 KDialog::slotButtonClicked(button);
126 }
127
128 QString RenameDialog::extensionString(const QString& name)
129 {
130 QString extension;
131 bool foundExtension = false; // true if at least one valid file extension
132 // like "gif", "txt", ... has been found
133
134 QStringList strings = name.split('.');
135 const int size = strings.size();
136 for (int i = 1; i < size; ++i) {
137 const QString& str = strings.at(i);
138 if (!foundExtension) {
139 // Sub strings like "9", "12", "99", ... which contain only
140 // numbers don't count as extension. Usually they are version
141 // numbers like in "cmake-2.4.5".
142 bool ok = false;
143 str.toInt(&ok);
144 foundExtension = !ok;
145 }
146 if (foundExtension) {
147 extension += '.' + str;
148 }
149 }
150
151 return extension;
152 }
153
154 #include "renamedialog.moc"