File indexing completed on 2024-12-01 07:38:56

0001 /***************************************************************************
0002  *   Copyright (C) 2009 Matthias Fuchs <mat69@gmx.net>                     *
0003  *                                                                         *
0004  *   This program is free software; you can redistribute it and/or modify  *
0005  *   it under the terms of the GNU General Public License as published by  *
0006  *   the Free Software Foundation; either version 2 of the License, or     *
0007  *   (at your option) any later version.                                   *
0008  *                                                                         *
0009  *   This program is distributed in the hope that it will be useful,       *
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0012  *   GNU General Public License for more details.                          *
0013  *                                                                         *
0014  *   You should have received a copy of the GNU General Public License     *
0015  *   along with this program; if not, write to the                         *
0016  *   Free Software Foundation, Inc.,                                       *
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
0018  ***************************************************************************/
0019 
0020 #include "renamefile.h"
0021 #include "core/filemodel.h"
0022 
0023 #include <QFile>
0024 #include <QPushButton>
0025 
0026 #include <KLocalizedString>
0027 
0028 RenameFile::RenameFile(FileModel *model, const QModelIndex &index, QWidget *parent, Qt::WindowFlags flags)
0029     : QDialog(parent, flags)
0030     , m_model(model)
0031     , m_index(index)
0032 {
0033     setWindowTitle(i18n("Rename File"));
0034     ui.setupUi(this);
0035 
0036     const QString originalName = m_model->data(m_index, Qt::DisplayRole).toString();
0037     m_dest = m_model->getUrl(m_index).adjusted(QUrl::RemoveFilename);
0038 
0039     ui.label->setText(i18n("Rename %1 to:", originalName));
0040     ui.name->setText(originalName);
0041 
0042     ui.buttonBox->button(QDialogButtonBox::Ok)->setText(i18n("&Rename"));
0043     ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0044     // ui.buttonBox->button(QDialogButtonBox::Ok)->setShortcut(Qt::CTRL | Qt::Key_Return);
0045 
0046     connect(ui.name, &KLineEdit::textEdited, this, &RenameFile::updateButton);
0047     connect(ui.buttonBox, &QDialogButtonBox::accepted, this, &RenameFile::accept);
0048     connect(ui.buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0049     connect(this, &QDialog::accepted, this, &RenameFile::rename);
0050 }
0051 
0052 void RenameFile::updateButton()
0053 {
0054     const QString newName = ui.name->text();
0055     QUrl dest = m_dest;
0056     dest.setPath(m_dest.path() + newName);
0057 
0058     const bool enabled = !newName.isEmpty() && !QFile::exists(dest.toString());
0059     ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
0060 }
0061 
0062 void RenameFile::rename()
0063 {
0064     const QString newName = ui.name->text();
0065     m_model->rename(m_index, newName);
0066 }
0067 
0068 #include "moc_renamefile.cpp"