1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** Licensees holding valid Qt Commercial licenses may use this file in
12 ** accordance with the Qt Commercial License Agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and Nokia.
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
28 **************************************************************************/
31 #include <sys/types.h>
33 #include <QtCore/QTime>
34 #include <QtDeclarative/qdeclarative.h>
35 #include <QtCore/QDebug>
36 #include <QtCore/QTextCodec>
38 #include "ProcessObject.h"
40 ProcessObject::ProcessObject(QObject *parent):
42 m_suspend(false), m_started(false)
44 m_process = new QProcess(this);
45 connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
47 SLOT(processFinished(int, QProcess::ExitStatus)));
48 connect(m_process, SIGNAL(error(QProcess::ProcessError)),this,
49 SLOT(processErrored(QProcess::ProcessError)));
52 ProcessObject::~ProcessObject()
61 void ProcessObject::processErrored(QProcess::ProcessError error) {
62 QString err(m_process->readAllStandardError());
63 qDebug() << "failed with exitCode" << m_process->exitCode();
64 emit failed(m_process->exitCode(), err, error);
68 void ProcessObject::processFinished(int exitCode,
69 QProcess::ExitStatus exitStatus)
71 QTextCodec* codec = QTextCodec::codecForName("utf-8");
72 if (exitStatus == QProcess::NormalExit)
76 QString out = codec->toUnicode(m_process->readAllStandardOutput());
78 emit completed(out.trimmed());
82 QString err = codec->toUnicode(m_process->readAllStandardError());
84 emit failed(exitCode, err.trimmed(), m_process->error());
89 QString err = codec->toUnicode(m_process->readAllStandardError());
91 emit failed(exitCode, err, m_process->error());
96 QString ProcessObject::command() const
101 void ProcessObject::setCommand(const QString &command)
103 if (m_command != command) {
108 void ProcessObject::run()
113 qDebug() << "already started";
115 qDebug() << m_command;
116 m_process->start(m_command);
117 qDebug() << "started";
121 bool ProcessObject::isSuspended() const
126 void ProcessObject::setSuspend(bool suspend)
128 if (!m_started || m_process->pid() == 0)
130 qDebug() << "No process running";
133 if (m_suspend != suspend)
137 ::kill(m_process->pid(), SIGCONT);
138 qDebug() << "Process resumed";
141 ::kill(m_process->pid(), SIGSTOP);
142 qDebug() << "Process suspended";
145 emit suspendChanged(suspend);
150 void ProcessObject::terminate()
152 if (m_started && m_process->pid() != 0)
159 QML_DECLARE_TYPE(ProcessObject);