Don't offer to restart a Running (=busy) agent, it won't work.
[kdepim.git] / messageviewer / renamefiledialog.cpp
blobd09c385ace00100485f0a169f89a36aa570e8922
1 /*
2 * Copyright (c) 2011 Montel Laurent <montel@kde.org>
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; version 2 of the License
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * In addition, as a special exception, the copyright holders give
18 * permission to link the code of this program with any edition of
19 * the Qt library by Trolltech AS, Norway (or with modified versions
20 * of Qt that use the same license as Qt), and distribute linked
21 * combinations including the two. You must obey the GNU General
22 * Public License in all respects for all of the code used other than
23 * Qt. If you modify this file, you may extend this exception to
24 * your version of the file, but you are not obligated to do so. If
25 * you do not wish to do so, delete this exception statement from
26 * your version.
28 #include "renamefiledialog.h"
30 #include <kseparator.h>
31 #include <KLineEdit>
32 #include <KLocale>
33 #include <kio/global.h>
35 #include <QPushButton>
36 #include <QHBoxLayout>
37 #include <QCheckBox>
38 #include <QLabel>
39 #include <QVBoxLayout>
40 #include <QFileInfo>
42 using namespace MessageViewer;
44 RenameFileDialog::RenameFileDialog(const KUrl& url, bool multiFiles, QWidget * parent)
45 :QDialog(parent),
46 mUrl(url),
47 mApplyAll(0)
49 setWindowTitle(i18n( "File Already Exists" ));
50 QVBoxLayout* pLayout = new QVBoxLayout(this);
52 QLabel *label = new QLabel(i18n( "A file named <filename>%1</filename> already exists. Do you want to overwrite it?",url.fileName()),this);
53 pLayout->addWidget(label);
55 QHBoxLayout* renameLayout = new QHBoxLayout();
56 pLayout->addLayout(renameLayout);
59 mNameEdit = new KLineEdit(this);
60 renameLayout->addWidget(mNameEdit);
61 mNameEdit->setText(url.fileName());
62 mSuggestNewName = new QPushButton(i18n("Suggest New &Name"), this);
63 renameLayout->addWidget(mSuggestNewName);
64 connect(mSuggestNewName, SIGNAL(clicked()), this, SLOT(slotSuggestNewNamePressed()));
67 mOverWrite = new QPushButton(i18n("&Overwrite"),this);
68 connect(mOverWrite,SIGNAL(clicked()),this,SLOT(slotOverwritePressed()));
70 mIgnore = new QPushButton(i18n("&Ignore"),this);
71 connect(mIgnore,SIGNAL(clicked()),this,SLOT(slotIgnorePressed()));
73 mRename = new QPushButton(i18n("&Rename"),this);
74 connect(mRename,SIGNAL(clicked()),this,SLOT(slotRenamePressed()));
76 KSeparator* separator = new KSeparator(this);
77 pLayout->addWidget(separator);
79 QHBoxLayout* layout = new QHBoxLayout();
80 pLayout->addLayout(layout);
82 if(multiFiles){
83 mApplyAll = new QCheckBox(i18n("Appl&y to All"), this);
84 connect(mApplyAll, SIGNAL(clicked()), this, SLOT(slotApplyAllPressed()));
85 layout->addWidget(mApplyAll);
86 slotApplyAllPressed();
88 layout->addWidget(mRename);
89 layout->addWidget(mOverWrite);
90 layout->addWidget(mIgnore);
93 RenameFileDialog::~RenameFileDialog()
97 void RenameFileDialog::slotOverwritePressed()
99 if(mApplyAll && mApplyAll->isChecked()) {
100 done(RENAMEFILE_OVERWRITEALL);
101 } else {
102 done(RENAMEFILE_OVERWRITE);
106 void RenameFileDialog::slotIgnorePressed()
108 if(mApplyAll && mApplyAll->isChecked()) {
109 done(RENAMEFILE_IGNOREALL);
110 } else {
111 done(RENAMEFILE_IGNORE);
115 void RenameFileDialog::slotRenamePressed()
117 if(mNameEdit->text().isEmpty())
118 return;
119 done(RENAMEFILE_RENAME);
122 void RenameFileDialog::slotApplyAllPressed()
124 const bool enabled(!mApplyAll->isChecked());
125 mNameEdit->setEnabled(enabled);
126 mSuggestNewName->setEnabled(enabled);
127 mRename->setEnabled(enabled);
130 void RenameFileDialog::slotSuggestNewNamePressed()
132 if (mNameEdit->text().isEmpty())
133 return;
135 KUrl destDirectory(mUrl);
137 destDirectory.setPath(destDirectory.directory());
138 mNameEdit->setText(suggestName(destDirectory, mNameEdit->text()));
141 KUrl RenameFileDialog::newName() const
143 KUrl newDest(mUrl);
144 QString fileName = mNameEdit->text();
146 newDest.setFileName(KIO::encodeFileName(fileName));
148 return newDest;
151 QString RenameFileDialog::suggestName(const KUrl& baseURL, const QString& oldName)
153 QString dotSuffix, suggestedName;
154 QString basename = oldName;
155 const QChar spacer(' ');
157 //ignore dots at the beginning, that way "..aFile.tar.gz" will become "..aFile 1.tar.gz" instead of " 1..aFile.tar.gz"
158 int index = basename.indexOf('.');
159 int continous = 0;
160 while (continous == index) {
161 index = basename.indexOf('.', index + 1);
162 ++continous;
165 if (index != -1) {
166 dotSuffix = basename.mid(index);
167 basename.truncate(index);
170 const int pos = basename.lastIndexOf(spacer);
172 if (pos != -1) {
173 const QString tmp = basename.mid(pos + 1);
174 bool ok;
175 const int number = tmp.toInt(&ok);
177 if (!ok) { // ok there is no number
178 suggestedName = basename + spacer + '1' + dotSuffix;
179 } else {
180 // yes there's already a number behind the spacer so increment it by one
181 basename.replace(pos + 1, tmp.length(), QString::number(number + 1));
182 suggestedName = basename + dotSuffix;
184 } else // no spacer yet
185 suggestedName = basename + spacer + '1' + dotSuffix ;
187 // Check if suggested name already exists
188 bool exists = false;
189 // TODO: network transparency. However, using NetAccess from a modal dialog
190 // could be a problem, no? (given that it uses a modal widget itself....)
191 if (baseURL.isLocalFile())
192 exists = QFileInfo(baseURL.toLocalFile(KUrl::AddTrailingSlash) + suggestedName).exists();
194 if (!exists)
195 return suggestedName;
196 else // already exists -> recurse
197 return suggestName(baseURL, suggestedName);
200 #include "renamefiledialog.moc"