File indexing completed on 2024-05-19 04:55:49
0001 /** 0002 * \file abstractcli.h 0003 * Abstract base class for command line interface. 0004 * 0005 * \b Project: Kid3 0006 * \author Urs Fleisch 0007 * \date 10 Aug 2013 0008 * 0009 * Copyright (C) 2013-2024 Urs Fleisch 0010 * 0011 * This file is part of Kid3. 0012 * 0013 * Kid3 is free software; you can redistribute it and/or modify 0014 * it under the terms of the GNU General Public License as published by 0015 * the Free Software Foundation; either version 2 of the License, or 0016 * (at your option) any later version. 0017 * 0018 * Kid3 is distributed in the hope that it will be useful, 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0021 * GNU General Public License for more details. 0022 * 0023 * You should have received a copy of the GNU General Public License 0024 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0025 */ 0026 0027 #pragma once 0028 0029 #include <QObject> 0030 0031 /** 0032 * Abstract base class for command line I/O handler. 0033 */ 0034 class AbstractCliIO : public QObject { 0035 Q_OBJECT 0036 public: 0037 /** 0038 * Destructor. 0039 */ 0040 ~AbstractCliIO() override; 0041 0042 /** 0043 * Can be reimplemented for cleanup, e.g. restoring the terminal. 0044 * Is called from the destructor. 0045 */ 0046 virtual void cleanup(); 0047 0048 /** 0049 * Write a line to standard output. 0050 * @param line line to write 0051 */ 0052 virtual void writeLine(const QString& line) = 0; 0053 0054 /** 0055 * Write a line to standard error. 0056 * @param line line to write 0057 */ 0058 virtual void writeErrorLine(const QString& line) = 0; 0059 0060 /** 0061 * Flush the standard output. 0062 */ 0063 virtual void flushStandardOutput() = 0; 0064 0065 /** 0066 * Read the next line. 0067 * When the line is ready, lineReady() is emitted. 0068 */ 0069 virtual void readLine() = 0; 0070 0071 public slots: 0072 /** 0073 * Start processing. 0074 * lineReady() is emitted when the first line is ready. To request 0075 * subsequent lines, readLine() has to be called. 0076 */ 0077 virtual void start() = 0; 0078 0079 /** 0080 * Stop processing. 0081 * Implementations must finally call deleteLater() to delete this object. 0082 */ 0083 virtual void stop() = 0; 0084 0085 signals: 0086 /** 0087 * Emitted when a line from standard input is ready. 0088 * @param line line read from standard input 0089 */ 0090 void lineReady(const QString& line); 0091 }; 0092 0093 0094 /** 0095 * Abstract base class for command line interface. 0096 */ 0097 class AbstractCli : public QObject { 0098 Q_OBJECT 0099 public: 0100 /** 0101 * Constructor. 0102 * @param io I/O handler 0103 * @param parent parent object 0104 */ 0105 explicit AbstractCli(AbstractCliIO* io, QObject* parent = nullptr); 0106 0107 /** 0108 * Destructor. 0109 */ 0110 ~AbstractCli() override = default; 0111 0112 /** 0113 * Write a line to standard output. 0114 * @param line line to write 0115 */ 0116 void writeLine(const QString& line); 0117 0118 /** 0119 * Write a line to standard error. 0120 * @param line line to write 0121 */ 0122 virtual void writeErrorLine(const QString& line); 0123 0124 /** 0125 * Flush the standard output. 0126 */ 0127 void flushStandardOutput(); 0128 0129 /** 0130 * Prompt next line from standard input. 0131 * Has to be called when the processing in readLine() is finished and 0132 * the user shall be prompted for the next line. 0133 */ 0134 void promptNextLine(); 0135 0136 /** 0137 * Set return code of application. 0138 * @param code return code, 0 means success 0139 */ 0140 void setReturnCode(int code); 0141 0142 public slots: 0143 /** 0144 * Execute process. 0145 */ 0146 virtual void execute(); 0147 0148 /** 0149 * Terminate command line processor. 0150 */ 0151 virtual void terminate(); 0152 0153 protected slots: 0154 /** 0155 * Process command line. 0156 * Has to be implemented by concrete derived class. 0157 * @param line command line 0158 */ 0159 virtual void readLine(const QString& line) = 0; 0160 0161 private slots: 0162 /** 0163 * Exit application with return code. 0164 */ 0165 void quitApplicationWithReturnCode(); 0166 0167 private: 0168 AbstractCliIO* m_io; 0169 int m_returnCode; 0170 bool m_terminating; 0171 };