File indexing completed on 2021-12-21 13:27:52

0001 /**
0002  * Copyright (C) 2004, 2008 Michael Pyne <mpyne@kde.org>
0003  *
0004  * This program is free software; you can redistribute it and/or modify it under
0005  * the terms of the GNU General Public License as published by the Free Software
0006  * Foundation; either version 2 of the License, or (at your option) any later
0007  * version.
0008  *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0012  *
0013  * You should have received a copy of the GNU General Public License along with
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.
0015  */
0016 
0017 #include "deletedialog.h"
0018 #include "ui_deletedialogbase.h"
0019 
0020 #include <KStandardGuiItem>
0021 #include <KLocalizedString>
0022 #include <KIconLoader>
0023 #include <KConfig>
0024 #include <KConfigGroup>
0025 #include <KSharedConfig>
0026 
0027 #include <QDialogButtonBox>
0028 #include <QStringList>
0029 #include <QCheckBox>
0030 #include <QVBoxLayout>
0031 
0032 #include "iconsupport.h"
0033 
0034 //////////////////////////////////////////////////////////////////////////////
0035 // DeleteWidget implementation
0036 //////////////////////////////////////////////////////////////////////////////
0037 
0038 DeleteWidget::DeleteWidget(QWidget *parent)
0039   : QWidget(parent)
0040   , m_ui(new Ui::DeleteDialogBase)
0041 {
0042     m_ui->setupUi(this);
0043 
0044     setObjectName(QLatin1String("delete_dialog_widget"));
0045 
0046     KConfigGroup messageGroup(KSharedConfig::openConfig(), "FileRemover");
0047 
0048     bool deleteInstead = messageGroup.readEntry("deleteInsteadOfTrash", false);
0049     slotShouldDelete(deleteInstead);
0050     m_ui->ddShouldDelete->setChecked(deleteInstead);
0051 
0052     // Forward on signals
0053     connect(m_ui->ddShouldDelete, SIGNAL(toggled(bool)), SIGNAL(signalShouldDelete(bool)));
0054     connect(m_ui->ddButtonBox,    SIGNAL(accepted()),    SIGNAL(accepted()));
0055     connect(m_ui->ddButtonBox,    SIGNAL(rejected()),    SIGNAL(rejected()));
0056 }
0057 
0058 void DeleteWidget::setFiles(const QStringList &files)
0059 {
0060     m_ui->ddFileList->clear();
0061     m_ui->ddFileList->insertItems(0, files);
0062     m_ui->ddNumFiles->setText(i18np("<b>1</b> file selected.", "<b>%1</b> files selected.", files.count()));
0063 }
0064 
0065 bool DeleteWidget::shouldDelete() const
0066 {
0067     return m_ui->ddShouldDelete->isChecked();
0068 }
0069 
0070 void DeleteWidget::slotShouldDelete(bool shouldDelete)
0071 {
0072     using namespace IconSupport;
0073 
0074     if(shouldDelete) {
0075         m_ui->ddDeleteText->setText(i18n("<qt>These items will be <b>permanently "
0076             "deleted</b> from your hard disk.</qt>"));
0077         m_ui->ddWarningIcon->setPixmap(
0078                 ("dialog-warning"_icon).pixmap(KIconLoader::SizeLarge));
0079     }
0080     else {
0081         m_ui->ddDeleteText->setText(i18n("<qt>These items will be moved to the Trash Bin.</qt>"));
0082         m_ui->ddWarningIcon->setPixmap(
0083                 ("user-trash-full"_icon).pixmap(KIconLoader::SizeLarge));
0084     }
0085 }
0086 
0087 //////////////////////////////////////////////////////////////////////////////
0088 // DeleteDialog implementation
0089 //////////////////////////////////////////////////////////////////////////////
0090 
0091 DeleteDialog::DeleteDialog(QWidget *parent) :
0092     QDialog(parent),
0093     m_trashGuiItem(i18n("&Send to Trash"), "user-trash-full")
0094 {
0095     setObjectName(QLatin1String("juk_delete_dialog"));
0096     setModal(true);
0097     setWindowTitle(i18n("About to delete selected files"));
0098 
0099     auto layout = new QVBoxLayout(this);
0100 
0101     m_widget = new DeleteWidget(this);
0102     m_widget->setMinimumSize(400, 300);
0103     layout->addWidget(m_widget);
0104 
0105     // Trying to adjust for Qt bug with rich text where the layout is ignored
0106     // (something about not being able to get height-for-width on X11?)
0107     setMinimumSize(410, 326);
0108     adjustSize();
0109 
0110     slotShouldDelete(shouldDelete());
0111 
0112     connect(m_widget, SIGNAL(accepted()), SLOT(accept()));
0113     connect(m_widget, SIGNAL(rejected()), SLOT(reject()));
0114     connect(m_widget, SIGNAL(signalShouldDelete(bool)), SLOT(slotShouldDelete(bool)));
0115 }
0116 
0117 bool DeleteDialog::confirmDeleteList(const QStringList &condemnedFiles)
0118 {
0119     m_widget->setFiles(condemnedFiles);
0120 
0121     return exec() == QDialog::Accepted;
0122 }
0123 
0124 void DeleteDialog::setFiles(const QStringList &files)
0125 {
0126     m_widget->setFiles(files);
0127 }
0128 
0129 void DeleteDialog::accept()
0130 {
0131     KConfigGroup messageGroup(KSharedConfig::openConfig(), "FileRemover");
0132 
0133     // Save user's preference
0134 
0135     messageGroup.writeEntry("deleteInsteadOfTrash", shouldDelete());
0136     messageGroup.sync();
0137 
0138     QDialog::accept();
0139 }
0140 
0141 void DeleteDialog::slotShouldDelete(bool shouldDelete)
0142 {
0143     KGuiItem::assign(m_widget->m_ui->ddButtonBox->button(QDialogButtonBox::Ok),
0144                      shouldDelete ? KStandardGuiItem::del() : m_trashGuiItem);
0145 }
0146 
0147 // vim: set et sw=4 tw=0 sta: