File indexing completed on 2024-05-19 04:55:50

0001 /**
0002  * \file standardiohandler.h
0003  * CLI I/O Handler for standard I/O.
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 "abstractcli.h"
0030 #include <QTextStream>
0031 
0032 /**
0033  * CLI I/O Handler for standard I/O.
0034  */
0035 class StandardIOHandler : public AbstractCliIO {
0036   Q_OBJECT
0037 public:
0038   /**
0039    * Constructor.
0040    * @param prompt command line prompt
0041    */
0042   explicit StandardIOHandler(const char* prompt = "");
0043 
0044   /**
0045    * Destructor.
0046    */
0047   ~StandardIOHandler() override = default;
0048 
0049   /**
0050    * Restore terminal on cleanup.
0051    */
0052   void cleanup() override;
0053 
0054   /**
0055    * Write a line to standard output.
0056    * @param line line to write
0057    */
0058   void writeLine(const QString& line) override;
0059 
0060   /**
0061    * Write a line to standard error.
0062    * @param line line to write
0063    */
0064   void writeErrorLine(const QString& line) override;
0065 
0066   /**
0067    * Flush the standard output.
0068    */
0069   void flushStandardOutput() override;
0070 
0071   /**
0072    * Read the next line.
0073    * This method will asynchronously invoke reading of a line from standard
0074    * input in the read thread.
0075    * When the line is ready, lineReady() is emitted.
0076    */
0077   void readLine() override;
0078 
0079 public slots:
0080   /**
0081    * Start processing.
0082    * This will start a read thread. lineReady() is emitted when the first
0083    * line is ready. To request subsequent lines, readLine() has to be called.
0084    */
0085   void start() override;
0086 
0087   /**
0088    * Stop processing.
0089    * This will stop the read thread and finally delete this object.
0090    */
0091   void stop() override;
0092 
0093 private slots:
0094   /**
0095    * Read the next line.
0096    * This method will block until a line is read from standard input.
0097    * When the line is ready, lineReady() is emitted.
0098    */
0099   void blockingReadLine();
0100 
0101 private:
0102   const char* m_prompt;
0103   QThread* m_conInThread;
0104   QTextStream m_cout;
0105   QTextStream m_cerr;
0106   bool m_consoleMode;
0107 };