File indexing completed on 2024-05-12 03:55:00

0001 /*
0002     SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #ifndef KSIGNALHANDLER_H
0008 #define KSIGNALHANDLER_H
0009 
0010 #include <QObject>
0011 #include <kcoreaddons_export.h>
0012 
0013 class KSignalHandlerPrivate;
0014 
0015 /**
0016  * Allows getting ANSI C signals and forward them onto the Qt eventloop.
0017  *
0018  * It's a singleton as it relies on static data getting defined.
0019  *
0020  * \code
0021  * {
0022  *   KSignalHandler::self()->watchSignal(SIGTERM);
0023  *   connect(KSignalHandler::self(), &KSignalHandler::signalReceived,
0024  *           this, &SomeClass::handleSignal);
0025  *   job->start();
0026  * }
0027  * \endcode
0028  *
0029  * @since 5.92
0030  */
0031 class KCOREADDONS_EXPORT KSignalHandler : public QObject
0032 {
0033     Q_OBJECT
0034 public:
0035     ~KSignalHandler() override;
0036 
0037     /**
0038      * Adds @p signal to be watched for. Once the process is notified about this signal, @m signalReceived will be emitted with the same @p signal as an
0039      * argument.
0040      *
0041      * @see signalReceived
0042      */
0043     void watchSignal(int signal);
0044 
0045     /**
0046      * Fetches an instance we can use to register our signals.
0047      */
0048     static KSignalHandler *self();
0049 
0050 Q_SIGNALS:
0051     /**
0052      * Notifies that @p signal is emitted.
0053      *
0054      * To catch a signal, we need to make sure it's registered using @m watchSignal.
0055      *
0056      * @see watchSignal
0057      */
0058     void signalReceived(int signal);
0059 
0060 private:
0061     KCOREADDONS_NO_EXPORT KSignalHandler();
0062 
0063     QScopedPointer<KSignalHandlerPrivate> d;
0064 };
0065 
0066 #endif