File indexing completed on 2024-11-24 04:44:36

0001 /*
0002     SPDX-FileCopyrightText: 2008 Bertjan Broeksema <b.broeksema@kdemail.org>
0003     SPDX-FileCopyrightText: 2008 Volker Krause <vkrause@kde.org>
0004     SPDX-FileCopyrightText: 2010, 2011 David Jarvie <djarvie@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "singlefileresourceconfigwidgetbase.h"
0010 
0011 #include <KIO/Job>
0012 #include <KIO/StatJob>
0013 #include <QTimer>
0014 
0015 #include <KConfigDialogManager>
0016 #include <KLocalizedString>
0017 #include <KSharedConfig>
0018 
0019 #include <QFontDatabase>
0020 #include <QPushButton>
0021 #include <QTabBar>
0022 #include <QVBoxLayout>
0023 
0024 using namespace Akonadi;
0025 
0026 SingleFileResourceConfigWidgetBase::SingleFileResourceConfigWidgetBase(QWidget *parent)
0027     : QWidget(parent)
0028 {
0029     auto mainWidget = new QWidget(this);
0030     auto mainLayout = new QVBoxLayout(this);
0031     mainLayout->addWidget(mainWidget);
0032     mainLayout->setContentsMargins({});
0033     ui.setupUi(mainWidget);
0034     ui.kcfg_Path->setMode(KFile::File);
0035     ui.statusLabel->setVisible(false);
0036 
0037     ui.tabWidget->tabBar()->hide();
0038 
0039     connect(ui.kcfg_Path, &KUrlRequester::textChanged, this, &SingleFileResourceConfigWidgetBase::validate);
0040     connect(ui.kcfg_MonitorFile, &QCheckBox::toggled, this, &SingleFileResourceConfigWidgetBase::validate);
0041     ui.kcfg_Path->setFocus();
0042     QTimer::singleShot(0, this, &SingleFileResourceConfigWidgetBase::validate);
0043 
0044     const auto smallFont = QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont);
0045     ui.readOnlyLabel->setFont(smallFont);
0046     ui.monitoringLabel->setFont(smallFont);
0047     ui.pathLabel->setFont(smallFont);
0048     ui.periodicUpdateLabel->setFont(smallFont);
0049 }
0050 
0051 SingleFileResourceConfigWidgetBase::~SingleFileResourceConfigWidgetBase() = default;
0052 
0053 void SingleFileResourceConfigWidgetBase::addPage(const QString &title, QWidget *page)
0054 {
0055     ui.tabWidget->tabBar()->show();
0056     ui.tabWidget->addTab(page, title);
0057     mManager->addWidget(page);
0058     mManager->updateWidgets();
0059 }
0060 
0061 void SingleFileResourceConfigWidgetBase::setFilter(const QString &filter)
0062 {
0063     ui.kcfg_Path->setNameFilter(filter);
0064 }
0065 
0066 void SingleFileResourceConfigWidgetBase::setMonitorEnabled(bool enable)
0067 {
0068     mMonitorEnabled = enable;
0069     ui.kcfg_MonitorFile->setEnabled(mMonitorEnabled);
0070 }
0071 
0072 void SingleFileResourceConfigWidgetBase::setUrl(const QUrl &url)
0073 {
0074     ui.kcfg_Path->setUrl(url);
0075 }
0076 
0077 QUrl SingleFileResourceConfigWidgetBase::url() const
0078 {
0079     return ui.kcfg_Path->url();
0080 }
0081 
0082 void SingleFileResourceConfigWidgetBase::setLocalFileOnly(bool local)
0083 {
0084     mLocalFileOnly = local;
0085     ui.kcfg_Path->setMode(mLocalFileOnly ? KFile::File | KFile::LocalOnly : KFile::File);
0086 }
0087 
0088 void SingleFileResourceConfigWidgetBase::appendWidget(SingleFileValidatingWidget *widget)
0089 {
0090     widget->setParent(static_cast<QWidget *>(ui.tab));
0091     ui.tabLayout->addWidget(widget);
0092     connect(widget, &SingleFileValidatingWidget::changed, this, &SingleFileResourceConfigWidgetBase::validate);
0093     mAppendedWidget = widget;
0094 }
0095 
0096 void SingleFileResourceConfigWidgetBase::validate()
0097 {
0098     if (mAppendedWidget && !mAppendedWidget->validate()) {
0099         Q_EMIT okEnabled(false);
0100         return;
0101     }
0102 
0103     const QUrl currentUrl = ui.kcfg_Path->url();
0104     if (ui.kcfg_Path->text().trimmed().isEmpty() || currentUrl.isEmpty()) {
0105         Q_EMIT okEnabled(false);
0106         return;
0107     }
0108 
0109     if (currentUrl.isLocalFile()) {
0110         if (mMonitorEnabled) {
0111             ui.kcfg_MonitorFile->setEnabled(true);
0112         }
0113         ui.statusLabel->setVisible(false);
0114 
0115         // The read-only checkbox used to be disabled if the file is read-only,
0116         // but it is then impossible to know at a later date if the file
0117         // permissions change, whether the user actually wanted the resource to be
0118         // read-only or not. So just leave the read-only checkbox untouched.
0119         Q_EMIT okEnabled(true);
0120     } else {
0121         // Not a local file.
0122         if (mLocalFileOnly) {
0123             Q_EMIT okEnabled(false);
0124             return;
0125         }
0126 
0127         ui.kcfg_MonitorFile->setChecked(false);
0128         ui.kcfg_MonitorFile->setEnabled(false);
0129         ui.statusLabel->setText(i18nc("@info:status", "Checking file information..."));
0130         ui.statusLabel->setVisible(true);
0131 
0132         if (mStatJob) {
0133             mStatJob->kill();
0134         }
0135         mStatJob = KIO::stat(currentUrl, KIO::StatJob::SourceSide, KIO::StatDetail::StatDefaultDetails, KIO::DefaultFlags | KIO::HideProgressInfo);
0136 
0137         connect(mStatJob, &KIO::StatJob::result, this, &SingleFileResourceConfigWidgetBase::slotStatJobResult);
0138 
0139         // Allow the OK button to be disabled until the MetaJob is finished.
0140         Q_EMIT okEnabled(false);
0141     }
0142 }
0143 
0144 void SingleFileResourceConfigWidgetBase::slotStatJobResult(KJob *job)
0145 {
0146     if (job->error() == KIO::ERR_DOES_NOT_EXIST && !mDirUrlChecked) {
0147         // The file did not exist, so let's see if the directory the file should
0148         // reside in supports writing.
0149 
0150         QUrl dirUrl(ui.kcfg_Path->url());
0151         dirUrl = KIO::upUrl(dirUrl);
0152 
0153         mStatJob = KIO::stat(dirUrl, KIO::StatJob::SourceSide, KIO::StatDetail::StatDefaultDetails, KIO::DefaultFlags | KIO::HideProgressInfo);
0154 
0155         connect(mStatJob, &KIO::StatJob::result, this, &SingleFileResourceConfigWidgetBase::slotStatJobResult);
0156 
0157         // Make sure we don't check the whole path upwards.
0158         mDirUrlChecked = true;
0159         return;
0160     } else if (job->error()) {
0161         // It doesn't seem possible to read nor write from the location so leave the
0162         // ok button disabled
0163         ui.statusLabel->setVisible(false);
0164         Q_EMIT okEnabled(false);
0165         mDirUrlChecked = false;
0166         mStatJob = nullptr;
0167         return;
0168     }
0169 
0170     ui.statusLabel->setVisible(false);
0171     Q_EMIT okEnabled(true);
0172 
0173     mDirUrlChecked = false;
0174     mStatJob = nullptr;
0175 }
0176 
0177 SingleFileValidatingWidget::SingleFileValidatingWidget(QWidget *parent)
0178     : QWidget(parent)
0179 {
0180 }
0181 
0182 #include "moc_singlefileresourceconfigwidgetbase.cpp"