File indexing completed on 2024-05-19 03:56:21

0001 /*
0002     This file is part of the KDE libraries
0003 
0004     SPDX-FileCopyrightText: 2000 Stephan Kulow <coolo@kde.org>
0005     SPDX-FileCopyrightText: 2000 David Faure <faure@kde.org>
0006     SPDX-FileCopyrightText: 2006 Kevin Ottens <ervin@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include "kjobuidelegate.h"
0012 #include "kcoreaddons_debug.h"
0013 #include "kjob.h"
0014 
0015 #include <QDebug>
0016 
0017 class KJobUiDelegatePrivate
0018 {
0019 public:
0020     KJobUiDelegatePrivate(KJobUiDelegate *delegate)
0021         : q(delegate)
0022         , autoErrorHandling(false)
0023         , autoWarningHandling(true)
0024     {
0025     }
0026 
0027     KJobUiDelegate *const q;
0028 
0029     KJob *job = nullptr;
0030     bool autoErrorHandling : 1;
0031     bool autoWarningHandling : 1;
0032 
0033     void connectJob(KJob *job);
0034     void _k_result();
0035 };
0036 
0037 KJobUiDelegate::KJobUiDelegate(Flags flags)
0038     : QObject()
0039     , d(new KJobUiDelegatePrivate(this))
0040 {
0041     if (flags & AutoErrorHandlingEnabled) {
0042         d->autoErrorHandling = true;
0043     }
0044     if (flags & AutoWarningHandlingEnabled) {
0045         d->autoWarningHandling = true;
0046     }
0047 }
0048 
0049 KJobUiDelegate::~KJobUiDelegate() = default;
0050 
0051 bool KJobUiDelegate::setJob(KJob *job)
0052 {
0053     if (d->job != nullptr) {
0054         qCWarning(KCOREADDONS_DEBUG) << "Trying to attach UI delegate:" << this << "to job" << job //
0055                                      << "but this delegate is already attached to a different job" << d->job;
0056         return false;
0057     }
0058 
0059     d->job = job;
0060     setParent(job);
0061 
0062     return true;
0063 }
0064 
0065 KJob *KJobUiDelegate::job() const
0066 {
0067     return d->job;
0068 }
0069 
0070 void KJobUiDelegate::showErrorMessage()
0071 {
0072     if (d->job->error() != KJob::KilledJobError) {
0073         qWarning() << d->job->errorString();
0074     }
0075 }
0076 
0077 void KJobUiDelegate::setAutoErrorHandlingEnabled(bool enable)
0078 {
0079     d->autoErrorHandling = enable;
0080 }
0081 
0082 bool KJobUiDelegate::isAutoErrorHandlingEnabled() const
0083 {
0084     return d->autoErrorHandling;
0085 }
0086 
0087 void KJobUiDelegate::setAutoWarningHandlingEnabled(bool enable)
0088 {
0089     d->autoWarningHandling = enable;
0090 }
0091 
0092 bool KJobUiDelegate::isAutoWarningHandlingEnabled() const
0093 {
0094     return d->autoWarningHandling;
0095 }
0096 
0097 void KJobUiDelegate::slotWarning(KJob *job, const QString &message)
0098 {
0099     Q_UNUSED(job)
0100     Q_UNUSED(message)
0101 }
0102 
0103 void KJobUiDelegate::connectJob(KJob *job)
0104 {
0105     connect(job, &KJob::result, this, [this]() {
0106         d->_k_result();
0107     });
0108     connect(job, &KJob::warning, this, &KJobUiDelegate::slotWarning);
0109 }
0110 
0111 void KJobUiDelegatePrivate::_k_result()
0112 {
0113     if (job->error() && autoErrorHandling) {
0114         q->showErrorMessage();
0115     }
0116 }
0117 
0118 #include "moc_kjobuidelegate.cpp"