File indexing completed on 2024-07-14 14:34:08

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kio/skipdialog.h"
0009 
0010 #include <assert.h>
0011 #include <stdio.h>
0012 
0013 #include <QDialogButtonBox>
0014 #include <QLabel>
0015 #include <QPushButton>
0016 #include <QVBoxLayout>
0017 #include <QWidget>
0018 
0019 #include <KGuiItem>
0020 #include <KLocalizedString>
0021 #include <KStandardGuiItem>
0022 
0023 using namespace KIO;
0024 
0025 SkipDialog::SkipDialog(QWidget *parent, KIO::SkipDialog_Options options, const QString &_error_text)
0026     : QDialog(parent)
0027     , d(nullptr)
0028 {
0029     setWindowTitle(i18n("Information"));
0030 
0031     QVBoxLayout *layout = new QVBoxLayout(this);
0032 
0033     auto *label = new QLabel(_error_text, this);
0034     label->setWordWrap(true);
0035     layout->addWidget(label);
0036 
0037     QDialogButtonBox *buttonBox = new QDialogButtonBox(this);
0038     layout->addWidget(buttonBox);
0039 
0040     const bool isMultiple = options & SkipDialog_MultipleItems;
0041     const bool isInvalidChars = options & SkipDialog_Replace_Invalid_Chars;
0042     const bool hideRetry = options & SkipDialog_Hide_Retry;
0043 
0044     // Retrying to e.g. copy a file with "*" in the name to a fat32
0045     // partition will always fail
0046     if (isInvalidChars) {
0047         QPushButton *replaceCharButton = new QPushButton(i18n("Replace"));
0048         connect(replaceCharButton, &QAbstractButton::clicked, this, [this]() {
0049             done(KIO::Result_ReplaceInvalidChars);
0050         });
0051         buttonBox->addButton(replaceCharButton, QDialogButtonBox::ActionRole);
0052     } else if (!hideRetry) {
0053         QPushButton *retryButton = new QPushButton(i18n("Retry"));
0054         connect(retryButton, &QAbstractButton::clicked, this, &SkipDialog::retryPressed);
0055         buttonBox->addButton(retryButton, QDialogButtonBox::ActionRole);
0056     }
0057 
0058     if (isMultiple) {
0059         if (isInvalidChars) {
0060             QPushButton *autoReplaceButton = new QPushButton(i18n("Replace All"));
0061             connect(autoReplaceButton, &QAbstractButton::clicked, this, [this]() {
0062                 done(KIO::Result_ReplaceAllInvalidChars);
0063             });
0064             buttonBox->addButton(autoReplaceButton, QDialogButtonBox::ActionRole);
0065         }
0066 
0067         QPushButton *skipButton = new QPushButton(i18n("Skip"));
0068         connect(skipButton, &QAbstractButton::clicked, this, &SkipDialog::skipPressed);
0069         buttonBox->addButton(skipButton, QDialogButtonBox::ActionRole);
0070 
0071         QPushButton *autoSkipButton = new QPushButton(i18n("Skip All"));
0072         connect(autoSkipButton, &QAbstractButton::clicked, this, &SkipDialog::autoSkipPressed);
0073         buttonBox->addButton(autoSkipButton, QDialogButtonBox::ActionRole);
0074     }
0075 
0076     auto *cancelBtn = buttonBox->addButton(QDialogButtonBox::Cancel);
0077     // If it's one item and the Retry button is hidden, replace the Cancel
0078     // button text with OK
0079     if (hideRetry && !isMultiple) {
0080         KGuiItem::assign(cancelBtn, KStandardGuiItem::ok());
0081     }
0082     connect(buttonBox, &QDialogButtonBox::rejected, this, &SkipDialog::cancelPressed);
0083 
0084     resize(sizeHint());
0085 }
0086 
0087 SkipDialog::~SkipDialog()
0088 {
0089 }
0090 
0091 void SkipDialog::cancelPressed()
0092 {
0093     done(KIO::Result_Cancel);
0094 }
0095 
0096 void SkipDialog::skipPressed()
0097 {
0098     done(KIO::Result_Skip);
0099 }
0100 
0101 void SkipDialog::autoSkipPressed()
0102 {
0103     done(KIO::Result_AutoSkip);
0104 }
0105 
0106 void SkipDialog::retryPressed()
0107 {
0108     done(KIO::Result_Retry);
0109 }
0110 
0111 #include "moc_skipdialog.cpp"