1 | /* |
2 | SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-or-later |
5 | */ |
6 | |
7 | #ifndef KSIGNALHANDLER_H |
8 | #define KSIGNALHANDLER_H |
9 | |
10 | #include <QObject> |
11 | #include <kcoreaddons_export.h> |
12 | |
13 | class KSignalHandlerPrivate; |
14 | |
15 | /*! |
16 | * \class KSignalHandler |
17 | * \inmodule KCoreAddons |
18 | * |
19 | * \brief Allows getting ANSI C signals and forward them onto the Qt eventloop. |
20 | * |
21 | * It's a singleton as it relies on static data getting defined. |
22 | * |
23 | * \code |
24 | * { |
25 | * KSignalHandler::self()->watchSignal(SIGTERM); |
26 | * connect(KSignalHandler::self(), &KSignalHandler::signalReceived, |
27 | * this, &SomeClass::handleSignal); |
28 | * job->start(); |
29 | * } |
30 | * \endcode |
31 | * |
32 | * \since 5.92 |
33 | */ |
34 | class KCOREADDONS_EXPORT KSignalHandler : public QObject |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | ~KSignalHandler() override; |
39 | |
40 | /*! |
41 | * Adds \a signal to be watched for. Once the process is notified about this signal, @m signalReceived will be emitted with the same \a signal as an |
42 | * argument. |
43 | * |
44 | * \sa signalReceived |
45 | */ |
46 | void watchSignal(int signal); |
47 | |
48 | /*! |
49 | * Fetches an instance we can use to register our signals. |
50 | */ |
51 | static KSignalHandler *self(); |
52 | |
53 | Q_SIGNALS: |
54 | /*! |
55 | * Notifies that \a signal is emitted. |
56 | * |
57 | * To catch a signal, we need to make sure it's registered using @m watchSignal. |
58 | * |
59 | * \sa watchSignal |
60 | */ |
61 | void signalReceived(int signal); |
62 | |
63 | private: |
64 | KCOREADDONS_NO_EXPORT KSignalHandler(); |
65 | |
66 | QScopedPointer<KSignalHandlerPrivate> d; |
67 | }; |
68 | |
69 | #endif |
70 | |