File indexing completed on 2024-05-12 05:44:25

0001 /***************************************************************************
0002  *   Copyright (C) 2005-2009 by Rajko Albrecht  ral@alwins-world.de        *
0003  *   https://kde.org/applications/development/org.kde.kdesvn               *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; if not, write to the                         *
0017  *   Free Software Foundation, Inc.,                                       *
0018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
0019  ***************************************************************************/
0020 #include "watchedprocess.h"
0021 
0022 #include <QDir>
0023 #include <QStringList>
0024 
0025 class ProcessData
0026 {
0027 public:
0028     ProcessData() = default;
0029     ~ProcessData()
0030     {
0031         QStringList::iterator it2;
0032         for (const QString &fn : qAsConst(_tempFiles)) {
0033             QFile::remove(fn);
0034         }
0035         for (const QString &dir : qAsConst(_tempDirs)) {
0036             QDir(dir).removeRecursively();
0037         }
0038     }
0039 
0040     QStringList _tempFiles;
0041     QStringList _tempDirs;
0042     bool _autoDelete = false;
0043 };
0044 
0045 WatchedProcess::WatchedProcess(QObject *parent)
0046     : KProcess(parent)
0047     , m_Data(new ProcessData)
0048 {
0049     connect(this, &QProcess::errorOccurred, this, &WatchedProcess::slotError);
0050     connect(this, QOverload<int, QProcess::ExitStatus>::of(&KProcess::finished), this, &WatchedProcess::slotFinished);
0051     connect(this, &QProcess::readyReadStandardError, this, &WatchedProcess::slotReadyReadStandardError);
0052     connect(this, &QProcess::readyReadStandardOutput, this, &WatchedProcess::slotReadyReadStandardOutput);
0053     connect(this, &KProcess::started, this, &WatchedProcess::slotStarted);
0054     connect(this, &KProcess::stateChanged, this, &WatchedProcess::slotStateChanged);
0055 }
0056 
0057 WatchedProcess::~WatchedProcess()
0058 {
0059     if (state() == QProcess::NotRunning) {
0060         terminate();
0061         if (!waitForFinished(1000)) {
0062             kill();
0063         }
0064     }
0065     delete m_Data;
0066 }
0067 
0068 void WatchedProcess::setAutoDelete(bool autodel)
0069 {
0070     m_Data->_autoDelete = autodel;
0071 }
0072 
0073 bool WatchedProcess::autoDelete() const
0074 {
0075     return m_Data->_autoDelete;
0076 }
0077 
0078 void WatchedProcess::slotError(QProcess::ProcessError error_code)
0079 {
0080     emit error(error_code, this);
0081 }
0082 
0083 void WatchedProcess::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
0084 {
0085     emit finished(exitCode, exitStatus, this);
0086     if (m_Data->_autoDelete) {
0087         m_Data->_autoDelete = false;
0088         deleteLater();
0089     }
0090 }
0091 
0092 void WatchedProcess::WatchedProcess::slotReadyReadStandardError()
0093 {
0094     emit dataStderrRead(readAllStandardError(), this);
0095 }
0096 
0097 void WatchedProcess::slotReadyReadStandardOutput()
0098 {
0099     emit dataStdoutRead(readAllStandardOutput(), this);
0100 }
0101 
0102 void WatchedProcess::slotStarted()
0103 {
0104     emit started(this);
0105 }
0106 
0107 void WatchedProcess::slotStateChanged(QProcess::ProcessState state)
0108 {
0109     emit stateChanged(state, this);
0110 }
0111 
0112 void WatchedProcess::appendTempFile(const QString &aFile)
0113 {
0114     m_Data->_tempFiles.append(aFile);
0115 }
0116 
0117 void WatchedProcess::appendTempDir(const QString &aDir)
0118 {
0119     m_Data->_tempDirs.append(aDir);
0120 }
0121 
0122 #include "moc_watchedprocess.cpp"