Warning, file /multimedia/kid3/src/app/cli/kid3cli.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /**
0002  * \file kid3cli.h
0003  * Command line interface for Kid3.
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 "frame.h"
0031 #include "cliconfig.h"
0032 #ifdef HAVE_READLINE
0033 #include <QScopedPointer>
0034 #endif
0035 
0036 class QTimer;
0037 class Kid3Application;
0038 class FileProxyModel;
0039 class CliCommand;
0040 class AbstractCliFormatter;
0041 enum class CliError : int;
0042 
0043 #ifdef HAVE_READLINE
0044 class Kid3CliCompleter;
0045 #endif
0046 
0047 /**
0048  * Command line interface for Kid3.
0049  */
0050 class Kid3Cli : public AbstractCli {
0051   Q_OBJECT
0052 public:
0053   /**
0054    * Constructor.
0055    * @param app application context
0056    * @param io I/O handler
0057    * @param args command line arguments
0058    * @param parent parent object
0059    */
0060   explicit Kid3Cli(Kid3Application* app,
0061                    AbstractCliIO* io, const QStringList& args,
0062                    QObject* parent = nullptr);
0063 
0064   /**
0065    * Destructor.
0066    */
0067   ~Kid3Cli() override;
0068 
0069   /**
0070    * Execute process.
0071    */
0072   void execute() override;
0073 
0074   /**
0075    * Write a line to standard error.
0076    * @param line line to write
0077    */
0078   void writeErrorLine(const QString& line) override;
0079 
0080   /**
0081    * Write result of command.
0082    * @param str result as string
0083    */
0084   void writeResult(const QString& str);
0085 
0086   /**
0087    * Write result of command.
0088    * @param strs result as string list
0089    */
0090   void writeResult(const QStringList& strs);
0091 
0092   /**
0093    * Write result of command.
0094    * @param map result as map
0095    */
0096   void writeResult(const QVariantMap& map);
0097 
0098   /**
0099    * Write result of command.
0100    * @param map result as boolean
0101    */
0102   void writeResult(bool result);
0103 
0104   /**
0105    * Called when a command is finished.
0106    */
0107   void finishWriting();
0108 
0109   /**
0110    * Access to application.
0111    * @return application.
0112    */
0113   Kid3Application* app() const { return m_app; }
0114 
0115   /**
0116    * Open directory.
0117    * @param paths directory or file paths
0118    * @return true if ok.
0119    */
0120   bool openDirectory(const QStringList& paths);
0121 
0122   /**
0123    * Select files in the current directory.
0124    * @param paths file names
0125    * @return true if files found and selected.
0126    */
0127   bool selectFile(const QStringList &paths);
0128 
0129   /**
0130    * Display help about available commands.
0131    * @param cmdName command name, for all commands if empty
0132    * @param usageMessage true if this is a usage error message
0133    */
0134   void writeHelp(const QString& cmdName = QString(),
0135                  bool usageMessage = false);
0136 
0137   /**
0138    * Display information about selected files.
0139    * @param tagMask tag bits (1 for tag 1, 2 for tag 2)
0140    */
0141   void writeFileInformation(int tagMask);
0142 
0143   /**
0144    * Write currently active tag mask.
0145    */
0146   void writeTagMask();
0147 
0148   /**
0149    * List files.
0150    */
0151   void writeFileList();
0152 
0153   /**
0154    * Respond with an error message.
0155    * @param errorCode error code
0156    */
0157   void writeErrorCode(CliError errorCode);
0158 
0159   /**
0160    * Respond with an error message.
0161    * @param msg error message
0162    * @param errorCode error code
0163    */
0164   void writeError(const QString& msg, CliError errorCode);
0165 
0166   /**
0167    * Get currently active tag mask.
0168    * @return tag bits.
0169    */
0170   Frame::TagVersion tagMask() const { return m_tagMask; }
0171 
0172   /**
0173    * Set currently active tag mask.
0174    *
0175    * @param tagMask tag bits
0176    */
0177   void setTagMask(Frame::TagVersion tagMask);
0178 
0179   /**
0180    * Get timeout value.
0181    * @return timeout in ms, 0 to use defaults, -1 to switch off.
0182    */
0183   int getTimeout() const { return m_timeoutMs; }
0184 
0185   /**
0186    * Set timeout value.
0187    * @param timeout timeout in ms, 0 to use defaults, -1 to switch off
0188    */
0189   void setTimeout(int timeout) { m_timeoutMs = timeout; }
0190 
0191   /**
0192    * Expand wildcards in path list.
0193    * @param paths paths to expand
0194    * @return expanded paths.
0195    */
0196   static QStringList expandWildcards(const QStringList& paths);
0197 
0198 public slots:
0199   /**
0200    * Update the currently selected files from the frame tables.
0201    */
0202   void updateSelectedFiles();
0203 
0204   /**
0205    * Has to be called when the selection changes to update the frame tables
0206    * and the information about the selected files.
0207    */
0208   void updateSelection();
0209 
0210   /**
0211    * Called when a command is finished.
0212    */
0213   void onCommandFinished();
0214 
0215 protected:
0216   /**
0217    * Process command line.
0218    * @param line command line
0219    */
0220   void readLine(const QString& line) override;
0221 
0222 private slots:
0223   /**
0224    * Select files passed as command line arguments after the initial directory has
0225    * been opened. Start execution of commands if existing.
0226    */
0227   void onInitialDirectoryOpened();
0228 
0229   /**
0230    * Called when an argument command is finished.
0231    */
0232   void onArgCommandFinished();
0233 
0234 private:
0235   /**
0236    * Get command for a command line.
0237    * @param line command line
0238    * @return command, 0 if no command found.
0239    */
0240   CliCommand* commandForArgs(const QString& line);
0241 
0242   QVariantList listFiles(const FileProxyModel* model,
0243                            const QModelIndex& parent);
0244   bool parseOptions();
0245   void executeNextArgCommand();
0246 
0247   Kid3Application* m_app;
0248   QStringList m_args;
0249 #ifdef HAVE_READLINE
0250   QScopedPointer<Kid3CliCompleter> m_completer;
0251 #endif
0252   AbstractCliFormatter* m_formatter;
0253   QList<AbstractCliFormatter*> m_formatters;
0254   QList<CliCommand*> m_cmds;
0255   QStringList m_argCommands;
0256   QString m_detailInfo;
0257   QString m_filename;
0258   QString m_tagFormat[Frame::Tag_NumValues];
0259   Frame::TagVersion m_tagMask;
0260   /** Overwrites command timeout, -1 to switch off, 0 for defaults, else ms. */
0261   int m_timeoutMs;
0262   bool m_fileNameChanged;
0263 };