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 1496 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
24 792 : {
25 :
26 1672 : }
27 :
28 1040 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
29 936 : {
30 4654 : return DeviceCommand::requiredOptions(parser) + QStringList{
31 936 : u"new-name"_s,
32 3679 : };
33 936 : }
34 :
35 480 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
36 432 : {
37 912 : return DeviceCommand::supportedOptions(parser);
38 432 : }
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 360 : {
48 760 : QStringList errors = DeviceCommand::processOptions(parser);
49 760 : if (!errors.isEmpty()) {
50 72 : return errors;
51 72 : }
52 :
53 764 : newName = parser.value(u"new-name"_s);
54 608 : if (newName.isEmpty()) {
55 152 : errors.append(tr("New name cannot be empty."));
56 456 : } else if (newName.length() > 11) {
57 152 : errors.append(tr("New name cannot exceed 11 characters."));
58 72 : }
59 288 : return errors;
60 360 : }
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 216 : {
97 456 : switch (format) {
98 152 : case OutputFormat::Csv:
99 164 : std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
100 152 : break;
101 152 : case OutputFormat::Json:
102 164 : std::cout << qUtf8Printable(u"true\n"_s);
103 152 : break;
104 152 : case OutputFormat::Text:
105 164 : std::cout << qUtf8Printable(tr("Done.\n"));
106 152 : break;
107 216 : }
108 456 : if (device) disconnect(); // Will exit the application once disconnected.
109 456 : }
|