LCOV - code coverage report
Current view: top level - src/app - setnamecommand.cpp (source / functions) Hit Total Coverage
Project: QtPokit Lines: 27 37 73.0 %
Version: Functions: 6 8 75.0 %

          Line data    Source code
       1             : // SPDX-FileCopyrightText: 2022 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         187 : SetNameCommand::SetNameCommand(QObject * const parent) : DeviceCommand(parent), service(nullptr)
      21             : {
      22             : 
      23         187 : }
      24             : 
      25         221 : QStringList SetNameCommand::requiredOptions(const QCommandLineParser &parser) const
      26             : {
      27         663 :     return DeviceCommand::requiredOptions(parser) + QStringList{
      28             :         QLatin1String("new-name"),
      29         624 :     };
      30             : }
      31             : 
      32         102 : QStringList SetNameCommand::supportedOptions(const QCommandLineParser &parser) const
      33             : {
      34         102 :     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          85 : QStringList SetNameCommand::processOptions(const QCommandLineParser &parser)
      44             : {
      45          85 :     QStringList errors = DeviceCommand::processOptions(parser);
      46          85 :     if (!errors.isEmpty()) {
      47             :         return errors;
      48             :     }
      49             : 
      50          80 :     newName = parser.value(QLatin1String("new-name"));
      51          68 :     if (newName.isEmpty()) {
      52          17 :         errors.append(tr("New name cannot be empty."));
      53          51 :     } else if (newName.length() > 11) {
      54          17 :         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             :                 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          51 : void SetNameCommand::deviceNameWritten()
      93             : {
      94          51 :     switch (format) {
      95           6 :     case OutputFormat::Csv:
      96          20 :         std::cout << qUtf8Printable(tr("set_name_result\nsuccess\n"));
      97          17 :         break;
      98             :     case OutputFormat::Json:
      99          23 :         std::cout << qUtf8Printable(QLatin1String("true\n"));
     100          17 :         break;
     101           6 :     case OutputFormat::Text:
     102          20 :         std::cout << qUtf8Printable(tr("Done.\n"));
     103          17 :         break;
     104             :     }
     105          51 :     if (device) disconnect(); // Will exit the application once disconnected.
     106          51 : }

Generated by: LCOV version 1.14