Added qml plugin exposing QProcess functionality.
[quick-widgets] / plugins / qmlprocess / ProcessObject.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** Commercial Usage
10 **
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.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
25 ** If you are unsure which license is appropriate for your use, please
26 ** contact the sales department at http://qt.nokia.com/contact.
27 **
28 **************************************************************************/
29
30 #include <signal.h>
31 #include <sys/types.h>
32
33 #include <QtCore/QTime>
34 #include <QtDeclarative/qdeclarative.h>
35 #include <QtCore/QDebug>
36
37 #include "ProcessObject.h"
38
39 ProcessObject::ProcessObject(QObject *parent):
40         QObject(parent),
41         m_suspend(false), m_started(false)
42 {
43     m_process = new QProcess(this);
44     connect(m_process, SIGNAL(finished(int, QProcess::ExitStatus)),
45             this,
46             SLOT(processFinished(int, QProcess::ExitStatus)));
47     connect(m_process, SIGNAL(error(QProcess::ProcessError)),this,
48             SLOT(processErrored(QProcess::ProcessError)));
49 }
50
51 ProcessObject::~ProcessObject()
52 {
53     if (m_started)
54     {
55         terminate();
56     }
57     delete m_process;
58 }
59
60 void ProcessObject::processErrored(QProcess::ProcessError error) {
61     QString err(m_process->readAllStandardError());
62     qDebug() << "failed with exitCode" << m_process->exitCode();
63     emit failed(m_process->exitCode(), err, error);
64     m_started = false;
65 }
66
67 void ProcessObject::processFinished(int exitCode,
68                                     QProcess::ExitStatus exitStatus)
69 {
70     if (exitStatus == QProcess::NormalExit)
71     {
72         if (exitCode == 0)
73         {
74             QString out(m_process->readAllStandardOutput());
75             qDebug() << "normal";
76             emit completed(out);
77         }
78         else
79         {
80             QString err(m_process->readAllStandardError());
81             qDebug() << "failed";
82             emit failed(exitCode, err, m_process->error());
83         }
84     }
85     else
86     {
87         QString err(m_process->readAllStandardError());
88         qDebug() << "failed";
89         emit failed(exitCode, err, m_process->error());
90     }
91     m_started = false;
92 }
93
94 QString ProcessObject::command() const
95 {
96     return m_command;
97 }
98
99 void ProcessObject::setCommand(const QString &command)
100 {
101     if (m_command != command) {
102         m_command = command;
103     }
104 }
105
106 void ProcessObject::run()
107 {
108     if (m_started)
109     {
110         return;
111         qDebug() << "already started";
112     }
113     qDebug() << m_command;
114     m_process->start(m_command);
115     qDebug() << "started";
116     m_started = true;
117 }
118
119 bool ProcessObject::isSuspended() const
120 {
121     return m_suspend;
122 }
123
124 void ProcessObject::setSuspend(bool suspend)
125 {
126     if (!m_started || m_process->pid() == 0)
127     {
128         qDebug() << "No process running";
129         return;
130     }
131     if (m_suspend != suspend)
132     {
133         if (m_suspend)
134         {
135             ::kill(m_process->pid(), SIGCONT);
136             qDebug() << "Process resumed";
137
138         } else {
139             ::kill(m_process->pid(), SIGSTOP);
140             qDebug() << "Process suspended";
141
142         }
143         emit suspendChanged(suspend);
144         m_suspend = suspend;
145     }
146 }
147
148 void ProcessObject::terminate()
149 {
150     if (m_started && m_process->pid() != 0)
151     {
152         m_process->kill();
153     }
154 }
155
156
157 QML_DECLARE_TYPE(ProcessObject);