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: 75.0 % 8 6

            Line data    Source code
       1              : // SPDX-FileCopyrightText: 2022-2024 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          594 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
      21          484 : {
      22              : 
      23          902 : }
      24              : 
      25          494 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
      26          572 : {
      27         2418 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      28          572 :         QLatin1String("new-name"),
      29         1807 :     };
      30          858 : }
      31              : 
      32          228 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
      33          264 : {
      34          492 :     return DeviceCommand::supportedOptions(parser);
      35          264 : }
      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          190 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
      44          220 : {
      45          410 :     QStringList errors = DeviceCommand::processOptions(parser);
      46          410 :     if (!errors.isEmpty()) {
      47           44 :         return errors;
      48           44 :     }
      49              : 
      50          388 :     newName = parser.value(QLatin1String("new-name"));
      51          328 :     if (newName.isEmpty()) {
      52           82 :         errors.append(tr("New name cannot be empty."));
      53          246 :     } else if (newName.length() > 11) {
      54           82 :         errors.append(tr("New name cannot exceed 11 characters."));
      55           44 :     }
      56          176 :     return errors;
      57          220 : }
      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          114 : void SetNameCommand::deviceNameWritten()
      93          132 : {
      94          246 :     switch (format) {
      95           82 :     case OutputFormat::Csv:
      96           82 :         std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
      97           82 :         break;
      98           44 :     case OutputFormat::Json:
      99           93 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     100           82 :         break;
     101           82 :     case OutputFormat::Text:
     102           82 :         std::cout << qUtf8Printable(tr("Done.\n"));
     103           82 :         break;
     104          132 :     }
     105          246 :     if (device) disconnect(); // Will exit the application once disconnected.
     106          246 : }
        

Generated by: LCOV version 2.0-1