LCOV - code coverage report
Current view: top level - src/cli - setnamecommand.cpp (source / functions) Coverage Total Hit
Project: Dokit Lines: 70.0 % 60 42
Version: Functions: 71.4 % 7 5

            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          902 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
      24          572 : {
      25              : 
      26         1067 : }
      27              : 
      28          585 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
      29          676 : {
      30         2873 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      31          676 :         u"new-name"_s,
      32         2262 :     };
      33          676 : }
      34              : 
      35          270 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
      36          312 : {
      37          582 :     return DeviceCommand::supportedOptions(parser);
      38          312 : }
      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          225 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
      47          260 : {
      48          485 :     QStringList errors = DeviceCommand::processOptions(parser);
      49          485 :     if (!errors.isEmpty()) {
      50           52 :         return errors;
      51           52 :     }
      52              : 
      53          472 :     newName = parser.value(u"new-name"_s);
      54          388 :     if (newName.isEmpty()) {
      55           97 :         errors.append(tr("New name cannot be empty."));
      56          291 :     } else if (newName.length() > 11) {
      57           97 :         errors.append(tr("New name cannot exceed 11 characters."));
      58           52 :     }
      59          208 :     return errors;
      60          260 : }
      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          135 : void SetNameCommand::deviceNameWritten()
      96          156 : {
      97          291 :     switch (format) {
      98           97 :     case OutputFormat::Csv:
      99           97 :         std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
     100           97 :         break;
     101           97 :     case OutputFormat::Json:
     102           97 :         std::cout << qUtf8Printable(u"true\n"_s);
     103           97 :         break;
     104           97 :     case OutputFormat::Text:
     105           97 :         std::cout << qUtf8Printable(tr("Done.\n"));
     106           97 :         break;
     107          156 :     }
     108          291 :     if (device) disconnect(); // Will exit the application once disconnected.
     109          291 : }
        

Generated by: LCOV version 2.3.1-1