Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2025 Paul Colby <git@colby.id.au>
2 : // SPDX-License-Identifier: LGPL-3.0-or-later
3 :
4 : #ifndef DOKIT_ABSTRACTCOMMAND_H
5 : #define DOKIT_ABSTRACTCOMMAND_H
6 :
7 : #include <qtpokit/qtpokit_global.h>
8 :
9 : #include <QBluetoothDeviceInfo>
10 : #include <QCommandLineParser>
11 : #include <QLoggingCategory>
12 : #include <QObject>
13 :
14 : class PokitDiscoveryAgent;
15 :
16 17918 : class AbstractCommand : public QObject
17 : {
18 1225 : Q_OBJECT
19 :
20 : public:
21 : /// Supported output formats.
22 : enum class OutputFormat {
23 : Csv, ///< RFC 4180 compliant CSV text.
24 : Json, ///< RFC 8259 compliant JSON text.
25 : Text, ///< Plain unstructured text.
26 : };
27 :
28 : explicit AbstractCommand(QObject * const parent = nullptr);
29 :
30 : virtual QStringList requiredOptions(const QCommandLineParser &parser) const;
31 : virtual QStringList supportedOptions(const QCommandLineParser &parser) const;
32 :
33 : static QString escapeCsvField(const QString &field);
34 :
35 : template<typename R>
36 : static quint32 parseNumber(const QString &value, const QString &unit, const quint32 sensibleMinimum = 0);
37 :
38 : public slots:
39 : virtual QStringList processOptions(const QCommandLineParser &parser);
40 : virtual bool start() = 0;
41 :
42 : protected:
43 : QString deviceToScanFor; ///< Device (if any) that were passed to processOptions().
44 : PokitDiscoveryAgent * discoveryAgent; ///< Agent for Pokit device descovery.
45 : OutputFormat format { OutputFormat::Text }; ///< Selected output format.
46 61108 : static Q_LOGGING_CATEGORY(lc, "dokit.cli.command", QtInfoMsg); ///< Logging category for UI commands.
47 :
48 : protected slots:
49 : virtual void deviceDiscovered(const QBluetoothDeviceInfo &info) = 0;
50 : virtual void deviceDiscoveryFinished() = 0;
51 :
52 : QTPOKIT_BEFRIEND_TEST(AbstractCommand)
53 : };
54 :
55 : #endif // DOKIT_ABSTRACTCOMMAND_H
|