Dokit
Internal development documentation
Loading...
Searching...
No Matches
setnamecommand.cpp
1// SPDX-FileCopyrightText: 2022-2023 Paul Colby <git@colby.id.au>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
4#include "setnamecommand.h"
5
8
9#include <iostream>
10
11/*!
12 * \class SetNameCommand
13 *
14 * The SetNameCommand class implements the `set-name` CLI command.
15 */
16
17/*!
18 * Construct a new SetNameCommand object with \a parent.
19 */
21{
22
23}
24
26{
28 QLatin1String("new-name"),
29 };
30}
31
36
37/*!
38 * \copybrief DeviceCommand::processOptions
39 *
40 * This implementation extends DeviceCommand::processOptions to process additional CLI options
41 * supported (or required) by this command.
42 */
44{
46 if (!errors.isEmpty()) {
47 return errors;
48 }
49
50 newName = parser.value(QLatin1String("new-name"));
51 if (newName.isEmpty()) {
52 errors.append(tr("New name cannot be empty."));
53 } else if (newName.length() > 11) {
54 errors.append(tr("New name cannot exceed 11 characters."));
55 }
56 return errors;
57}
58
59/*!
60 * \copybrief DeviceCommand::getService
61 *
62 * This override returns a pointer to a StatusService object.
63 */
75
76/*!
77 * \copybrief DeviceCommand::serviceDetailsDiscovered
78 *
79 * This override sets the device's name, via the Pokit Status service.
80 */
82{
83 qCInfo(lc).noquote() << tr("Setting device name to: %1").arg(newName);
85 QCoreApplication::exit(EXIT_FAILURE);
86 }
87}
88
89/*!
90 * Handles StatusService::deviceNameWritten events, by outputting the result and exiting.
91 */
93{
94 switch (format) {
96 std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
97 break;
99 std::cout << qUtf8Printable(QLatin1String("true\n"));
100 break;
102 std::cout << qUtf8Printable(tr("Done.\n"));
103 break;
104 }
105 if (device) disconnect(); // Will exit the application once disconnected.
106}
virtual QStringList supportedOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names supported by this command.
OutputFormat format
Selected output format.
@ Text
Plain unstructured text.
@ Csv
RFC 4180 compliant CSV text.
@ Json
RFC 8259 compliant JSON text.
virtual QStringList processOptions(const QCommandLineParser &parser)
Processes the relevant options from the command line parser.
virtual QStringList requiredOptions(const QCommandLineParser &parser) const
Returns a list of CLI option names required by this command.
The AbstractPokitService class provides a common base for Pokit services classes.
The AbstractCommand class extends AbstractCommand to add a PokitDevice instance.
PokitDevice * device
Pokit Bluetooth device (if any) this command inerracts with.
void disconnect(int exitCode=EXIT_SUCCESS)
Disconnects the underlying Pokit device, and sets exitCode to be return to the OS once the disconnect...
StatusService * status()
Returns a pointer to StatusService instance that uses this device's controller for access.
void serviceDetailsDiscovered() override
Handles service detail discovery events.
void deviceNameWritten()
Handles StatusService::deviceNameWritten events, by outputting the result and exiting.
QString newName
New name to set on the Pokit device.
QStringList processOptions(const QCommandLineParser &parser) override
Processes the relevant options from the command line parser.
QStringList supportedOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names supported by this command.
QStringList requiredOptions(const QCommandLineParser &parser) const override
Returns a list of CLI option names required by this command.
AbstractPokitService * getService() override
Returns a Pokit service object for the derived command class.
SetNameCommand(QObject *const parent=nullptr)
Construct a new SetNameCommand object with parent.
StatusService * service
Bluetooth service this command interracts with.
void deviceNameWritten()
This signal is emitted when the Device Name characteristic has been written successfully.
bool setDeviceName(const QString &name)
Set's the Pokit device's name to name.
Declares the PokitDevice class.
QString value(const QString &optionName) const const
void exit(int returnCode)
void append(const T &value)
bool isEmpty() const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString tr(const char *sourceText, const char *disambiguation, int n)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
bool isEmpty() const const
int length() const const
Declares the StatusService class.