LCOV - code coverage report
Current view: top level - src/cli - setnamecommand.cpp (source / functions) Hit Total Coverage
Project: Dokit Lines: 27 38 71.1 %
Version: Functions: 6 15 40.0 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2022-2023 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         209 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent)
      21             : {
      22             : 
      23         209 : }
      24             : 
      25         247 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
      26             : {
      27         819 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      28             :         QLatin1String("new-name"),
      29         715 :     };
      30             : }
      31             : 
      32         114 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
      33             : {
      34         114 :     return DeviceCommand::supportedOptions(parser);
      35             : }
      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          95 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
      44             : {
      45          95 :     QStringList errors = DeviceCommand::processOptions(parser);
      46          95 :     if (!errors.isEmpty()) {
      47             :         return errors;
      48             :     }
      49             : 
      50          96 :     newName = parser.value(QLatin1String("new-name"));
      51          76 :     if (newName.isEmpty()) {
      52          19 :         errors.append(tr("New name cannot be empty."));
      53          57 :     } else if (newName.length() > 11) {
      54          19 :         errors.append(tr("New name cannot exceed 11 characters."));
      55             :     }
      56             :     return errors;
      57             : }
      58             : 
      59             : /*!
      60             :  * \copybrief DeviceCommand::getService
      61             :  *
      62             :  * This override returns a pointer to a StatusService object.
      63             :  */
      64           0 : AbstractPokitService * SetNameCommand::getService()
      65             : {
      66             :     Q_ASSERT(device);
      67           0 :     if (!service) {
      68           0 :         service = device->status();
      69             :         Q_ASSERT(service);
      70           0 :         connect(service, &StatusService::deviceNameWritten,
      71           0 :                 this, &SetNameCommand::deviceNameWritten);
      72             :     }
      73           0 :     return service;
      74             : }
      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             : {
      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             :     }
      87           0 : }
      88             : 
      89             : /*!
      90             :  * Handles StatusService::deviceNameWritten events, by outputting the result and exiting.
      91             :  */
      92          57 : void SetNameCommand::deviceNameWritten()
      93             : {
      94          57 :     switch (format) {
      95          19 :     case OutputFormat::Csv:
      96          24 :         std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
      97          19 :         break;
      98             :     case OutputFormat::Json:
      99          29 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     100          19 :         break;
     101          19 :     case OutputFormat::Text:
     102          24 :         std::cout << qUtf8Printable(tr("Done.\n"));
     103          19 :         break;
     104             :     }
     105          57 :     if (device) disconnect(); // Will exit the application once disconnected.
     106          57 : }

Generated by: LCOV version 1.14