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