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 : #include "../stringliterals_p.h"
6 :
7 : #include <qtpokit/pokitdevice.h>
8 : #include <qtpokit/statusservice.h>
9 :
10 : #include <iostream>
11 :
12 : DOKIT_USE_STRINGLITERALS
13 :
14 : /*!
15 : * \class SetNameCommand
16 : *
17 : * The SetNameCommand class implements the `set-name` CLI command.
18 : */
19 :
20 : /*!
21 : * Construct a new SetNameCommand object with \a parent.
22 : */
23 1320 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
24 616 : {
25 :
26 1496 : }
27 :
28 1040 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
29 728 : {
30 4446 : return DeviceCommand::requiredOptions(parser) + QStringList{
31 728 : u"new-name"_s,
32 3471 : };
33 728 : }
34 :
35 480 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
36 336 : {
37 816 : return DeviceCommand::supportedOptions(parser);
38 336 : }
39 :
40 : /*!
41 : * \copybrief DeviceCommand::processOptions
42 : *
43 : * This implementation extends DeviceCommand::processOptions to process additional CLI options
44 : * supported (or required) by this command.
45 : */
46 400 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
47 280 : {
48 680 : QStringList errors = DeviceCommand::processOptions(parser);
49 680 : if (!errors.isEmpty()) {
50 56 : return errors;
51 56 : }
52 :
53 700 : newName = parser.value(u"new-name"_s);
54 544 : if (newName.isEmpty()) {
55 136 : errors.append(tr("New name cannot be empty."));
56 408 : } else if (newName.length() > 11) {
57 136 : errors.append(tr("New name cannot exceed 11 characters."));
58 56 : }
59 224 : return errors;
60 280 : }
61 :
62 : /*!
63 : * \copybrief DeviceCommand::getService
64 : *
65 : * This override returns a pointer to a StatusService object.
66 : */
67 0 : AbstractPokitService * SetNameCommand::getService()
68 0 : {
69 0 : Q_ASSERT(device);
70 0 : if (!service) {
71 0 : service = device->status();
72 0 : Q_ASSERT(service);
73 0 : connect(service, &StatusService::deviceNameWritten,
74 0 : this, &SetNameCommand::deviceNameWritten);
75 0 : }
76 0 : return service;
77 0 : }
78 :
79 : /*!
80 : * \copybrief DeviceCommand::serviceDetailsDiscovered
81 : *
82 : * This override sets the device's name, via the Pokit Status service.
83 : */
84 0 : void SetNameCommand::serviceDetailsDiscovered()
85 0 : {
86 0 : qCInfo(lc).noquote() << tr("Setting device name to: %1").arg(newName);
87 0 : if (!service->setDeviceName(newName)) {
88 0 : QCoreApplication::exit(EXIT_FAILURE);
89 0 : }
90 0 : }
91 :
92 : /*!
93 : * Handles StatusService::deviceNameWritten events, by outputting the result and exiting.
94 : */
95 240 : void SetNameCommand::deviceNameWritten()
96 168 : {
97 408 : switch (format) {
98 136 : case OutputFormat::Csv:
99 148 : std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
100 136 : break;
101 136 : case OutputFormat::Json:
102 148 : std::cout << qUtf8Printable(u"true\n"_s);
103 136 : break;
104 136 : case OutputFormat::Text:
105 148 : std::cout << qUtf8Printable(tr("Done.\n"));
106 136 : break;
107 168 : }
108 408 : if (device) disconnect(); // Will exit the application once disconnected.
109 408 : }
|