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

0001 /**
0002  * \file abstractcliformatter.h
0003  * Abstract base class for CLI formatter.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 28 Jul 2019
0008  *
0009  * Copyright (C) 2019  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 #include <QStringList>
0031 #include <QVariantMap>
0032 
0033 class AbstractCliIO;
0034 enum class CliError : int;
0035 
0036 /**
0037  * Abstract base class for CLI formatter.
0038  */
0039 class AbstractCliFormatter : public QObject {
0040 public:
0041   /**
0042    * Constructor.
0043    * @param io I/O handler
0044    */
0045   explicit AbstractCliFormatter(AbstractCliIO* io);
0046 
0047   /**
0048    * Destructor.
0049    */
0050   virtual ~AbstractCliFormatter();
0051 
0052   /**
0053    * Clear parser state.
0054    */
0055   virtual void clear() = 0;
0056 
0057   /**
0058    * Get command and parameters from input line.
0059    * @param line input line
0060    * @return list of command and arguments, empty if not found or incomplete.
0061    */
0062   virtual QStringList parseArguments(const QString& line) = 0;
0063 
0064   /**
0065    * Get error which occurred in previous method call.
0066    * @return error message, null if no error.
0067    */
0068   virtual QString getErrorMessage() const = 0;
0069 
0070   /**
0071    * Check if format was recognized and parsed, but input is will be continued
0072    * in subsequent lines.
0073    * @return true if input is incomplete.
0074    */
0075   virtual bool isIncomplete() const = 0;
0076 
0077   /**
0078    * Check if format was recognized and parsed.
0079    * @return true if format was recognized.
0080    */
0081   virtual bool isFormatRecognized() const = 0;
0082 
0083   /**
0084    * Write error message.
0085    * @param errorCode error code
0086    */
0087   virtual void writeError(CliError errorCode) = 0;
0088 
0089   /**
0090    * Write error message.
0091    * @param msg error message
0092    */
0093   virtual void writeError(const QString& msg) = 0;
0094 
0095   /**
0096    * Write error message.
0097    * @param msg error message
0098    * @param errorCode error code
0099    */
0100   virtual void writeError(const QString& msg, CliError errorCode) = 0;
0101 
0102   /**
0103    * Write result message.
0104    * @param str result as string
0105    */
0106   virtual void writeResult(const QString& str) = 0;
0107 
0108   /**
0109    * Write result message.
0110    * @param strs result as string list
0111    */
0112   virtual void writeResult(const QStringList& strs) = 0;
0113 
0114   /**
0115    * Write result message.
0116    * @param map result as map
0117    */
0118   virtual void writeResult(const QVariantMap& map) = 0;
0119 
0120   /**
0121    * Write result message.
0122    * @param result result as boolean
0123    */
0124   virtual void writeResult(bool result) = 0;
0125 
0126   /**
0127    * Called when a command is finished.
0128    */
0129   virtual void finishWriting() = 0;
0130 
0131 protected:
0132   /**
0133    * Access to CLI I/O.
0134    * @return CLI I/O.
0135    */
0136   AbstractCliIO* io() const { return m_io; }
0137 
0138 private:
0139   AbstractCliIO* const m_io;
0140 };