Line data Source code
1 : // SPDX-FileCopyrightText: 2022-2026 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 : QTPOKIT_FORWARD_DECLARE_CLASS(PokitDiscoveryAgent)
15 :
16 32318 : class AbstractCommand : public QObject
17 : {
18 2310 : 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 appendSiPrefix(const double value, int precision = 6);
34 : static QString escapeCsvField(const QString &field);
35 :
36 : template<typename R, typename T = quint32>
37 : static T parseNumber(const QString &value, const QString &unit, const T sensibleMinimum = 0);
38 :
39 : public slots:
40 : virtual QStringList processOptions(const QCommandLineParser &parser);
41 : virtual bool start() = 0;
42 :
43 : protected:
44 : QString deviceToScanFor; ///< Device (if any) that were passed to processOptions().
45 : PokitDiscoveryAgent * discoveryAgent; ///< Agent for Pokit device discovery.
46 : OutputFormat format { OutputFormat::Text }; ///< Selected output format.
47 107240 : static Q_LOGGING_CATEGORY(lc, "dokit.cli.command", QtInfoMsg); ///< Logging category for UI commands.
48 :
49 : protected slots:
50 : virtual void deviceDiscovered(const QBluetoothDeviceInfo &info) = 0;
51 : virtual void deviceDiscoveryFinished() = 0;
52 :
53 : QTPOKIT_BEFRIEND_TEST(AbstractCommand)
54 : };
55 :
56 : #endif // DOKIT_ABSTRACTCOMMAND_H
|