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: 73.3 % 15 11

            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          627 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
      21          517 : {
      22              : 
      23          957 : }
      24              : 
      25          520 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
      26          611 : {
      27         2561 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      28          611 :         QLatin1String("new-name"),
      29         1898 :     };
      30          897 : }
      31              : 
      32          240 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
      33          282 : {
      34          522 :     return DeviceCommand::supportedOptions(parser);
      35          282 : }
      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          200 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
      44          235 : {
      45          435 :     QStringList errors = DeviceCommand::processOptions(parser);
      46          435 :     if (!errors.isEmpty()) {
      47           47 :         return errors;
      48           47 :     }
      49              : 
      50          416 :     newName = parser.value(QLatin1String("new-name"));
      51          348 :     if (newName.isEmpty()) {
      52           87 :         errors.append(tr("New name cannot be empty."));
      53          261 :     } else if (newName.length() > 11) {
      54           87 :         errors.append(tr("New name cannot exceed 11 characters."));
      55           47 :     }
      56          188 :     return errors;
      57          235 : }
      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          120 : void SetNameCommand::deviceNameWritten()
      93          141 : {
      94          261 :     switch (format) {
      95           87 :     case OutputFormat::Csv:
      96           87 :         std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
      97           87 :         break;
      98           47 :     case OutputFormat::Json:
      99           98 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     100           87 :         break;
     101           87 :     case OutputFormat::Text:
     102           87 :         std::cout << qUtf8Printable(tr("Done.\n"));
     103           87 :         break;
     104          141 :     }
     105          261 :     if (device) disconnect(); // Will exit the application once disconnected.
     106          261 : }
        

Generated by: LCOV version 2.2-1