diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -15,6 +15,7 @@ knotifyconfig.cpp knotificationplugin.cpp + knotificationjobuidelegate.cpp notifybyexecute.cpp notifybylogfile.cpp notifybytaskbar.cpp @@ -170,6 +171,7 @@ KNotification KPassivePopup KStatusNotifierItem + KNotificationJobUiDelegate KNotificationRestrictions KNotificationPlugin KNotifyConfig diff --git a/src/knotificationjobuidelegate.h b/src/knotificationjobuidelegate.h new file mode 100644 --- /dev/null +++ b/src/knotificationjobuidelegate.h @@ -0,0 +1,61 @@ +/* This file is part of the KDE Frameworks + Copyright (C) 2020 Kai Uwe Broulik + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#ifndef KNOTIFICATIONJOBUIDELEGATE_H +#define KNOTIFICATIONJOBUIDELEGATE_H + +#include + +#include + +/** + * @class KNotificationJobUiDelegate knotificationjobuidelegate.h KNotificationJobUiDelegate + * + * A UI delegate using KNotification for interaction (showing errors and warnings). + */ +class KNOTIFICATIONS_EXPORT KNotificationJobUiDelegate : public KJobUiDelegate +{ + Q_OBJECT + +public: + /** + * Constructs a new KNotificationJobUiDelegate. + */ + KNotificationJobUiDelegate(); + + /** + * Destroys the KNotificationJobUiDelegate. + */ + ~KNotificationJobUiDelegate() override; + +public: + /** + * Display a notification to inform the user of the error given by + * this job. + */ + void showErrorMessage() override; + +protected Q_SLOTS: + void slotWarning(KJob *job, const QString &plain, const QString &rich) override; + +private: + class Private; + Private *const d; +}; + +#endif // KNOTIFICATIONJOBUIDELEGATE_H diff --git a/src/knotificationjobuidelegate.cpp b/src/knotificationjobuidelegate.cpp new file mode 100644 --- /dev/null +++ b/src/knotificationjobuidelegate.cpp @@ -0,0 +1,70 @@ +/* This file is part of the KDE Frameworks + Copyright (C) 2020 Kai Uwe Broulik + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License version 2 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "knotificationjobuidelegate.h" + +#include +#include + +class Q_DECL_HIDDEN KNotificationJobUiDelegate::Private +{ +public: + Private(); + + void showNotification(KNotification::StandardEvent standardEvent, const QString &text); +}; + +KNotificationJobUiDelegate::Private::Private() +{ +} + +void KNotificationJobUiDelegate::Private::showNotification(KNotification::StandardEvent standardEvent, const QString &text) +{ + KNotification::event(standardEvent, QString(), text); +} + +KNotificationJobUiDelegate::KNotificationJobUiDelegate() + : d(new KNotificationJobUiDelegate::Private) +{ +} + +KNotificationJobUiDelegate::~KNotificationJobUiDelegate() +{ + delete d; +} + +void KNotificationJobUiDelegate::showErrorMessage() +{ + if (job()->error() == KJob::KilledJobError) { + return; + } + + d->showNotification(KNotification::Error, job()->errorString()); +} + +void KNotificationJobUiDelegate::slotWarning(KJob *job, const QString &plain, const QString &rich) +{ + Q_UNUSED(job); + Q_UNUSED(rich); + + if (isAutoErrorHandlingEnabled()) { + d->showNotification(KNotification::Notification, plain); + } +} + +#include "knotificationjobuidelegate.moc"