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 : #include "setnamecommand.h"
5 :
6 : #include <qtpokit/pokitdevice.h>
7 : #include <qtpokit/statusservice.h>
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 : */
20 737 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
21 572 : {
22 :
23 1067 : }
24 :
25 585 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
26 676 : {
27 2873 : return DeviceCommand::requiredOptions(parser) + QStringList{
28 676 : QLatin1String("new-name"),
29 2119 : };
30 988 : }
31 :
32 270 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
33 312 : {
34 582 : return DeviceCommand::supportedOptions(parser);
35 312 : }
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 : */
43 225 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
44 260 : {
45 485 : QStringList errors = DeviceCommand::processOptions(parser);
46 485 : if (!errors.isEmpty()) {
47 52 : return errors;
48 52 : }
49 :
50 472 : newName = parser.value(QLatin1String("new-name"));
51 388 : if (newName.isEmpty()) {
52 97 : errors.append(tr("New name cannot be empty."));
53 291 : } else if (newName.length() > 11) {
54 97 : errors.append(tr("New name cannot exceed 11 characters."));
55 52 : }
56 208 : return errors;
57 260 : }
58 :
59 : /*!
60 : * \copybrief DeviceCommand::getService
61 : *
62 : * This override returns a pointer to a StatusService object.
63 : */
64 0 : AbstractPokitService * SetNameCommand::getService()
65 0 : {
66 0 : Q_ASSERT(device);
67 0 : if (!service) {
68 0 : service = device->status();
69 0 : Q_ASSERT(service);
70 0 : connect(service, &StatusService::deviceNameWritten,
71 0 : this, &SetNameCommand::deviceNameWritten);
72 0 : }
73 0 : return service;
74 0 : }
75 :
76 : /*!
77 : * \copybrief DeviceCommand::serviceDetailsDiscovered
78 : *
79 : * This override sets the device's name, via the Pokit Status service.
80 : */
81 0 : void SetNameCommand::serviceDetailsDiscovered()
82 0 : {
83 0 : qCInfo(lc).noquote() << tr("Setting device name to: %1").arg(newName);
84 0 : if (!service->setDeviceName(newName)) {
85 0 : QCoreApplication::exit(EXIT_FAILURE);
86 0 : }
87 0 : }
88 :
89 : /*!
90 : * Handles StatusService::deviceNameWritten events, by outputting the result and exiting.
91 : */
92 135 : void SetNameCommand::deviceNameWritten()
93 156 : {
94 291 : switch (format) {
95 97 : case OutputFormat::Csv:
96 97 : std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
97 97 : break;
98 52 : case OutputFormat::Json:
99 109 : std::cout << qUtf8Printable(QLatin1String("true\n"));
100 97 : break;
101 97 : case OutputFormat::Text:
102 97 : std::cout << qUtf8Printable(tr("Done.\n"));
103 97 : break;
104 156 : }
105 291 : if (device) disconnect(); // Will exit the application once disconnected.
106 291 : }
|