File indexing completed on 2024-05-19 04:56:01

0001 /**
0002  * \file externalprocess.h
0003  * Handler for external process.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 22 Feb 2007
0008  *
0009  * Copyright (C) 2007-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 #include <QStringList>
0031 #include "kid3api.h"
0032 
0033 class QProcess;
0034 class Kid3Application;
0035 
0036 /**
0037  * Handler for external process.
0038  */
0039 class KID3_CORE_EXPORT ExternalProcess : public QObject {
0040   Q_OBJECT
0041 public:
0042   /**
0043    * Interface for viewer to show output from external process.
0044    */
0045   class KID3_CORE_EXPORT IOutputViewer {
0046   public:
0047     /**
0048      * Destructor.
0049      */
0050     virtual ~IOutputViewer() = 0;
0051 
0052     /**
0053      * Set caption.
0054      * @param title caption
0055      */
0056     virtual void setCaption(const QString& title) = 0;
0057 
0058     /**
0059      * Append text.
0060      */
0061     virtual void append(const QString& text) = 0;
0062 
0063     /**
0064      * Scroll text to bottom.
0065      */
0066     virtual void scrollToBottom() = 0;
0067   };
0068 
0069 
0070   /**
0071    * Constructor.
0072    *
0073    * @param app application context
0074    * @param parent parent object
0075    */
0076   explicit ExternalProcess(Kid3Application* app, QObject* parent = nullptr);
0077 
0078   /**
0079    * Destructor.
0080    */
0081   ~ExternalProcess() override;
0082 
0083   /**
0084    * Get output viewer.
0085    * @return output viewer, default is null.
0086    */
0087   IOutputViewer* outputViewer() const { return m_outputViewer; }
0088 
0089   /**
0090    * Set output viewer.
0091    * @param viewer output viewer to be used
0092    */
0093   void setOutputViewer(IOutputViewer* viewer) { m_outputViewer = viewer; }
0094 
0095   /**
0096    * Launch a command.
0097    *
0098    * @param name       display name
0099    * @param args       command and arguments
0100    * @param showOutput true to show output of process
0101    * @return false if process could not be executed.
0102    */
0103   bool launchCommand(const QString& name, const QStringList& args,
0104                      bool showOutput = false);
0105 
0106 signals:
0107   /**
0108    * Emitted when the process finishes.
0109    * @param exitCode exit code of process
0110    */
0111   void finished(int exitCode);
0112 
0113 private slots:
0114   /**
0115    * Read data from standard output and display it in the output viewer.
0116    */
0117   void readFromStdout();
0118 
0119   /**
0120    * Show a line in the output viewer.
0121    * @param msg message to be displayed
0122    */
0123   void showOutputLine(const QString& msg);
0124 
0125 private:
0126   Kid3Application* m_app;
0127   QProcess* m_process;
0128   IOutputViewer* m_outputViewer;
0129 };